Search in sources :

Example 66 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class GlanceImageStore method uploadImage.

@Override
public String uploadImage(OpsTarget target, Tags tags, File imageFile, long rawImageFileSize) throws OpsException {
    OpenstackImageClient client = getOpenstackImageClient();
    String diskFormat = null;
    if (tags != null) {
        // This logic looks suspicious...
        assert false;
        for (Tag tag : tags.getTags()) {
            ImageFormat imageFormat = ImageFormat.fromTags(tags);
            diskFormat = mapToGlanceDiskFormat(imageFormat);
        }
    }
    String glanceBaseUrl;
    String tokenId;
    try {
        Access access = client.getSession().getAuthenticationToken();
        tokenId = access.getToken().getId();
        glanceBaseUrl = client.root().getBaseUrl();
    } catch (OpenstackException e) {
        throw new OpsException("Error getting glance url", e);
    }
    // Upload to glance
    String glanceUploadUrl = glanceBaseUrl;
    if (!glanceUploadUrl.endsWith("/")) {
        glanceUploadUrl += "/";
    }
    glanceUploadUrl += "images";
    String imageName = "image-" + System.currentTimeMillis();
    Command command = Command.build("curl");
    command.addLiteral("--fail");
    command.addLiteral("--upload-file").addFile(imageFile);
    command.addLiteral("-X").addLiteral("POST");
    command.addLiteral("-H").addQuoted("X-Auth-Token: " + tokenId);
    command.addLiteral("-H").addQuoted("Content-Type: application/octet-stream");
    command.addLiteral("-H").addQuoted("X-Image-Meta-Name: " + imageName);
    command.addLiteral("-H").addQuoted("X-Image-Meta-Is-Public: True");
    // } else {
    if (diskFormat != null) {
        command.addLiteral("-H").addQuoted("X-Image-Meta-Disk-Format: " + diskFormat);
    }
    command.addLiteral("-H").addQuoted("X-Image-Meta-Container-Format: bare");
    // }
    // command.addLiteral("-H").addQuoted("X-Image-Meta-Min-Disk: 0");
    // command.addLiteral("-H").addQuoted("X-Image-Meta-Min-Ram: 0");
    // command.addLiteral("-H").addQuoted("X-Image-Meta-Image-Size: " + rawImageFileSize);
    command.addLiteral("-H").addQuoted("X-Image-Meta-Size: " + rawImageFileSize);
    // image_meta = {'name': fields.pop('name'),
    // 'is_public': utils.bool_from_string(
    // fields.pop('is_public', False)),
    // 'disk_format': fields.pop('disk_format', 'raw'),
    // 'min_disk': fields.pop('min_disk', 0),
    // 'min_ram': fields.pop('min_ram', 0),
    // 'container_format': fields.pop('container_format', 'ovf')}
    // glance add name=DebianSqueeze is_public=True disk_format=raw container_format=bare
    // system_id="http://org.platformlayer/service/imagefactory/v1.0:bootstrap"
    // image_size="${RAW_SIZE}" < disk.raw.gz
    command.addQuoted(glanceUploadUrl);
    command.setTimeout(TimeSpan.FIFTEEN_MINUTES);
    ProcessExecution execution = target.executeCommand(command);
    String imageId;
    // String imageLocation;
    {
        // "is_public": true, "deleted_at": null, "min_ram": 0, "size": 925761536}}
        try {
            JSONObject json = new JSONObject(execution.getStdOut());
            JSONObject image = json.getJSONObject("image");
            // imageLocation = image.getString("location");
            imageId = image.getString("id");
        } catch (JSONException e) {
            log.warn("Image upload returned: " + execution.getStdOut());
            throw new OpsException("Error parsing return value from image upload", e);
        }
    }
    if (tags != null) {
        updateImageTags(imageId, tags);
    }
    return imageId;
}
Also used : OpsException(org.platformlayer.ops.OpsException) JSONObject(org.json.JSONObject) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) OpenstackImageClient(org.openstack.client.common.OpenstackImageClient) Access(org.openstack.model.identity.Access) JSONException(org.json.JSONException) Tag(org.platformlayer.core.model.Tag) OpenstackException(org.openstack.client.OpenstackException) ImageFormat(org.platformlayer.ops.images.ImageFormat)

Example 67 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class MkIsoFs method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    FilesystemInfo isoInfo = target.getFilesystemInfoFile(iso);
    boolean rebuild = true;
    if (isoInfo != null) {
        // TODO: Do timestamp based dependency checking?
        rebuild = false;
    }
    if (rebuild) {
        Command mkisoCommand = Command.build("genisoimage -input-charset utf-8 -R -o {0}", iso);
        if (volumeLabel != null) {
            mkisoCommand.addLiteral("-V").addQuoted(volumeLabel);
        }
        mkisoCommand.addFile(srcDir);
        target.executeCommand(mkisoCommand);
    }
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) Command(org.platformlayer.ops.Command) Handler(org.platformlayer.ops.Handler)

Example 68 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class LxcInstanceController method addChildren.

@Override
protected void addChildren() throws OpsException {
    final DirectInstance model = OpsContext.get().getInstance(DirectInstance.class);
    CloudInstanceMapper instance;
    {
        instance = injected(CloudInstanceMapper.class);
        instance.instance = OpsContext.get().getInstance(DirectInstance.class);
        addChild(instance);
    }
    instance.addChild(ManagedDirectory.build(getInstanceDir(), "700"));
    // TODO: If we're not going to assign an IPV4 redirect, we might not need this
    final Provider<AddressModel> address4;
    {
        NetworkAddressPoolAssignment provider = instance.addChild(NetworkAddressPoolAssignment.class);
        provider.holder = model.getKey();
        provider.poolProvider = DirectCloudUtils.getPrivateAddressPool4();
        address4 = provider;
    }
    final Provider<AddressModel> address6;
    {
        NetworkAddressPoolAssignment provider = instance.addChild(NetworkAddressPoolAssignment.class);
        provider.holder = model.getKey();
        provider.poolProvider = directCloudHelpers.getAddressPool6();
        address6 = provider;
    }
    // {
    // NetworkTunDevice tun = injected(NetworkTunDevice.class);
    // tun.interfaceName = getEthernetDeviceName();
    // tun.bridgeName = Providers.getProperty(assignNetworkAddress, "bridge");
    // instance.addChild(tun);
    // }
    {
        DownloadImage download = injected(DownloadImage.class);
        download.imageFile = new File(getInstanceDir(), "rootfs");
        download.recipeKey = model.recipeId;
        download.imageFormats = Collections.singletonList(ImageFormat.Tar);
        instance.addChild(download);
    }
    {
        LxcBootstrap bootstrap = injected(LxcBootstrap.class);
        bootstrap.address4 = address4;
        bootstrap.address6 = address6;
        bootstrap.lxcId = id;
        bootstrap.instanceDir = instanceDir;
        try {
            bootstrap.sshPublicKey = OpenSshUtils.readSshPublicKey(model.sshPublicKey);
        } catch (IOException e) {
            throw new OpsException("Error deserializing SSH key", e);
        }
        bootstrap.hostname = model.hostname;
        instance.addChild(bootstrap);
    }
    InstanceScript script;
    {
        script = instance.addChild(InstanceScript.class);
        script.filePath = new File(DirectHostController.LXC_INSTANCE_DIR, id);
        String key = "lxc-" + id;
        script.key = key;
        script.addresses.add(address4);
        script.addresses.add(address6);
        // script.hostPrimaryInterface = hostModel.publicInterface;
        Command command = Command.build("lxc-start");
        command.addLiteral("--name").addQuoted(id);
        script.launchInstanceCommand = command;
    }
    {
        // ManagedSupervisordInstance service = instance.addChild(ManagedSupervisordInstance.class);
        StandardService service = instance.addChild(StandardService.class);
        script.configure(model, service);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                TagChanges tagChanges = new TagChanges();
                tagChanges.addTags.add(Tag.INSTANCE_KEY.build(model.getKey()));
                AddressModel ipv4 = address4.get();
                AddressModel ipv6 = address6.get();
                if (ipv4 != null) {
                    tagChanges.addTags.add(Tag.NETWORK_ADDRESS.build(ipv4));
                }
                if (ipv6 != null) {
                    tagChanges.addTags.add(Tag.NETWORK_ADDRESS.build(ipv6));
                }
                return tagChanges;
            }
        };
        instance.addChild(Tagger.build(model, tagChanges));
    }
}
Also used : NetworkAddressPoolAssignment(org.platformlayer.ops.pool.NetworkAddressPoolAssignment) OpsException(org.platformlayer.ops.OpsException) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) InstanceScript(org.platformlayer.service.cloud.direct.ops.InstanceScript) StandardService(org.platformlayer.ops.supervisor.StandardService) IOException(java.io.IOException) TagChanges(org.platformlayer.core.model.TagChanges) CloudInstanceMapper(org.platformlayer.service.cloud.direct.ops.CloudInstanceMapper) OpsProvider(org.platformlayer.ops.OpsProvider) Command(org.platformlayer.ops.Command) AddressModel(org.platformlayer.core.model.AddressModel) File(java.io.File) DownloadImage(org.platformlayer.service.cloud.direct.ops.DownloadImage)

Example 69 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class PostgresqlServerBackup method listDatabases.

private List<String> listDatabases(OpsTarget target) throws OpsException {
    Command listDatabases = Command.build("su postgres -c \"psql -A -t -c 'select datname from pg_database'\"");
    ProcessExecution listDatabasesExecution = target.executeCommand(listDatabases);
    List<String> databases = Lists.newArrayList();
    for (String database : Splitter.on('\n').split(listDatabasesExecution.getStdOut())) {
        database = database.trim();
        if (database.isEmpty()) {
            continue;
        }
        databases.add(database);
    }
    return databases;
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 70 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class PostgresqlServerBackup method doBackup.

@Handler(BackupAction.class)
public void doBackup() throws OpsException, IOException {
    OpsContext opsContext = OpsContext.get();
    // Machine machine = opsContext.getInstance(Machine.class);
    OpsTarget target = opsContext.getInstance(OpsTarget.class);
    // We use pg_dump, not pg_dumpall:
    // 1) pg_dumpall doesn't support binary dumping (?)
    // 2) pg_dumpall wouldn't let us split the dump into different files (?)
    List<String> databases = listDatabases(target);
    BackupContext backupContext = backups.getContext();
    String baseName = UUID.randomUUID().toString();
    PostgresqlServer server = OpsContext.get().getInstance(PostgresqlServer.class);
    backupContext.add(new BackupItem(server.getKey(), FORMAT, baseName));
    {
        Command dumpAll = Command.build("su postgres -c \"pg_dumpall --globals-only\"");
        Backup request = new Backup();
        request.target = target;
        request.objectName = baseName + "/pgdump_meta";
        backupContext.uploadStream(request, dumpAll);
    }
    for (String database : databases) {
        // template0 cannot be backed up
        if (database.equals("template0")) {
            continue;
        }
        // template1 can be backed up, even though it isn't typically very useful
        String fileName = "pgdump_db_" + database;
        Backup request = new Backup();
        request.target = target;
        request.objectName = baseName + "/" + fileName;
        Command dumpDatabase = Command.build("su postgres -c \"pg_dump --oids -Fc --verbose {0}\"", database);
        backupContext.uploadStream(request, dumpDatabase);
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) BackupItem(org.platformlayer.ops.backups.BackupItem) Command(org.platformlayer.ops.Command) Backup(org.platformlayer.ops.backups.Backup) BackupContext(org.platformlayer.ops.backups.BackupContext) PostgresqlServer(org.platformlayer.service.postgresql.model.PostgresqlServer) OpsContext(org.platformlayer.ops.OpsContext) Handler(org.platformlayer.ops.Handler)

Aggregations

Command (org.platformlayer.ops.Command)72 File (java.io.File)21 Handler (org.platformlayer.ops.Handler)17 OpsException (org.platformlayer.ops.OpsException)16 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)16 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)11 OpsTarget (org.platformlayer.ops.OpsTarget)9 InetAddress (java.net.InetAddress)4 CurlRequest (org.platformlayer.ops.helpers.CurlRequest)4 Md5Hash (com.fathomdb.hash.Md5Hash)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Map (java.util.Map)3 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)3 ImageFormat (org.platformlayer.ops.images.ImageFormat)3 List (java.util.List)2 Properties (java.util.Properties)2 RequestBuilder (org.openstack.client.common.RequestBuilder)2 AddressModel (org.platformlayer.core.model.AddressModel)2 Tag (org.platformlayer.core.model.Tag)2