Search in sources :

Example 11 with Command

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

the class DownloadImage method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (target.getFilesystemInfoFile(imageFile) == null) {
        DirectCloud cloud = OpsContext.get().getInstance(DirectCloud.class);
        if (cloud == null) {
            throw new IllegalStateException("Cloud instance not found");
        }
        ImageStore imageStore = cloudHelpers.getImageStore(cloud);
        MachineProvider machineProvider = providers.toInterface(cloud, MachineProvider.class);
        CloudImage imageInfo = imageFactory.getOrCreateImageId(machineProvider, imageFormats, recipeKey);
        if (imageStore == null) {
            throw new OpsException("Image store not configured");
        }
        String fileName = imageInfo.getId() + ".image." + imageInfo.getFormat().name();
        // TODO: We don't need rawImage; delete or just request a read-only version
        File rawImage = new File(IMAGES_DIR, fileName);
        target.mkdir(IMAGES_DIR);
        // TODO: Caching / reuse ... need to check md5 though
        imageStore.bringToMachine(imageInfo.getId(), target, rawImage);
        ImageFormat imageFormat = imageInfo.getFormat();
        switch(imageFormat) {
            case Tar:
                {
                    target.mkdir(imageFile);
                    target.executeCommand(Command.build("cd {0}; tar jxf {1}", imageFile, rawImage).setTimeout(TimeSpan.FIVE_MINUTES));
                    break;
                }
            case DiskRaw:
                {
                    Command expand = Command.build("gunzip -c {0} | cp --sparse=always /proc/self/fd/0 {1}", rawImage, imageFile);
                    target.executeCommand(expand.setTimeout(TimeSpan.FIVE_MINUTES));
                    break;
                }
            case DiskQcow2:
                {
                    Command expand = Command.build("cp {0} {1}", rawImage, imageFile);
                    target.executeCommand(expand.setTimeout(TimeSpan.FIVE_MINUTES));
                    break;
                }
            default:
                throw new OpsException("Unknown image format: " + imageFormat);
        }
    }
}
Also used : DirectCloud(org.platformlayer.service.cloud.direct.model.DirectCloud) MachineProvider(org.platformlayer.ops.machines.MachineProvider) OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) CloudImage(org.platformlayer.ops.images.CloudImage) File(java.io.File) ImageStore(org.platformlayer.ops.images.ImageStore) ImageFormat(org.platformlayer.ops.images.ImageFormat) Handler(org.platformlayer.ops.Handler)

Example 12 with Command

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

the class InstanceScript method configure.

// public void configure(ManagedSupervisordInstance instance) {
// SupervisorProcessConfig sup = new SupervisorProcessConfig(key);
// Map<String, String> properties = sup.getProperties();
//
// Command command = Command.build(filePath.getAbsolutePath());
// properties.put("command", command.buildCommandString());
//
// instance.config = Providers.of(sup);
// }
public void configure(ItemBase owner, StandardService service) {
    Command command = Command.build(filePath.getAbsolutePath());
    service.command = OpsProvider.of(command);
    service.key = key;
    service.owner = owner.getKey();
// Map<String, String> env = template.getEnvironment();
// service.environment = Providers.of(env);
//
// service.instanceDir = instanceDir;
// service.key = template.getServiceKey();
//
// service.owner = template.getModel().getKey();
//
// service.matchExecutableName = template.getMatchExecutableName();
}
Also used : Command(org.platformlayer.ops.Command)

Example 13 with Command

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

the class ShellBackupClient method doBackup.

public void doBackup(DirectoryBackup request) throws OpsException {
    File tempDir = request.target.createTempDir();
    File excludeFile = new File(tempDir, "exclude.txt");
    if (request.objectName == null) {
        request.objectName = UUID.randomUUID().toString();
    }
    request.objectName += ".tgz";
    // TODO: Set content type?
    FileUpload.upload(request.target, excludeFile, Joiner.on("\n").join(request.exclude));
    Command tarCommand = Command.build("tar zcf - -X {0} {1}", excludeFile, request.rootDirectory);
    log.info("Backing up " + request.rootDirectory);
    uploadStream(request, tarCommand);
    request.target.rmdir(tempDir);
    log.info("Backup complete");
}
Also used : Command(org.platformlayer.ops.Command) File(java.io.File)

Example 14 with Command

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

the class DirectOpenstackDownload method download.

public void download(OpsTarget target, File targetPath, OpenstackCredentials credentials, String containerName, String objectPath) throws OpsException {
    RemoteCurlOpenstackSession session = new RemoteCurlOpenstackSession(target);
    session.authenticate(credentials, false);
    OpenstackStorageClient storageClient = session.getStorageClient();
    RequestBuilder request = storageClient.root().containers().id(containerName).objects().id(objectPath).buildDownloadRequest();
    CurlRequest curlRequest = session.toCurlRequest(request);
    curlRequest.bareRequest = true;
    Command curlCommand = curlRequest.toCommand();
    curlCommand.addLiteral(">");
    curlCommand.addFile(targetPath);
    ProcessExecution execution = target.executeCommand(curlCommand);
// CurlResult curlResult = curlRequest.parseResponse(execution);
//
// int httpResult = curlResult.getHttpResult();
// switch (httpResult) {
// case 200:
// break;
// case 201:
// break;
// default:
// throw new OpsException("Unexpected result code while downloading file: " + httpResult + " Result=" +
// curlResult);
// }
}
Also used : RequestBuilder(org.openstack.client.common.RequestBuilder) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) OpenstackStorageClient(org.openstack.client.storage.OpenstackStorageClient) CurlRequest(org.platformlayer.ops.helpers.CurlRequest)

Example 15 with Command

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

the class MountCgroups method handler.

@Handler
public void handler() throws OpsException, IOException {
    // TODO: Only if not installed
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    File cgroupsFile = new File("/cgroup");
    FilesystemInfo info = target.getFilesystemInfoFile(cgroupsFile);
    if (info != null) {
        // TODO: Better idempotency
        return;
    }
    String fstabLine = "\ncgroup\t/cgroup\tcgroup\tdefaults\t0\t0";
    File fstabFile = new File("/etc/fstab");
    String fstab = target.readTextFile(fstabFile);
    fstab += fstabLine;
    FileUpload.upload(target, fstabFile, fstab);
    target.mkdir(cgroupsFile);
    Command mountCommand = Command.build("mount cgroup");
    target.executeCommand(mountCommand);
// mkdir /cgroup
// echo -e "cgroup\t/cgroup\tcgroup\tdefaults\t0\t0" >> /etc/fstab
// mount cgroup
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) Command(org.platformlayer.ops.Command) File(java.io.File) 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