Search in sources :

Example 31 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 32 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)

Example 33 with Command

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

the class NetworkBridge method addChildren.

@Override
protected void addChildren() throws OpsException {
    // We would need proxy_arp if they were on the same IPV4 network
    // Do we need proxy_ndp ???
    // echo 1 > /proc/sys/net/ipv6/conf/eth0/proxy_ndp
    addChild(SysctlSetting.build("net.ipv4.ip_forward", "1"));
    addChild(SysctlSetting.build("net.ipv6.conf.all.forwarding", "1"));
    addChild(SysctlSetting.build("net.ipv6.conf.all.proxy_ndp", "1"));
    {
        File scriptPath = new File("/etc/network/if-up.d/nat-for-bridge");
        TemplatedFile nat = addChild(TemplatedFile.build(template, scriptPath));
        nat.setFileMode("0755");
        // Simulate an ifup run
        Command command = Command.build(scriptPath);
        CommandEnvironment env = new CommandEnvironment();
        env.put("IFACE", template.getPublicInterface());
        env.put("MODE", "start");
        env.put("ADDRFAM", "inet");
        command.setEnvironment(env);
        nat.setUpdateAction(command);
    }
}
Also used : TemplatedFile(org.platformlayer.ops.filesystem.TemplatedFile) Command(org.platformlayer.ops.Command) CommandEnvironment(org.platformlayer.ops.CommandEnvironment) TemplatedFile(org.platformlayer.ops.filesystem.TemplatedFile) File(java.io.File)

Example 34 with Command

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

the class ManagedKvmInstance method buildKvmCommand.

private Command buildKvmCommand() {
    Command command = Command.build("/usr/bin/kvm");
    if (this.vnc != null) {
        InetSocketAddress vnc = this.vnc.get();
        if (vnc != null) {
            int port = vnc.getPort();
            int vncPort = port - 5900;
            command.addLiteral("-vnc");
            InetAddress address = vnc.getAddress();
            if (!address.isAnyLocalAddress()) {
                command.addQuoted(address.getHostAddress() + ":" + vncPort);
            } else {
                command.addQuoted("0.0.0.0:" + vncPort);
            }
        }
    }
    command.addLiteral("-m").addQuoted(Integer.toString(memoryMb));
    command.addLiteral("-smp").addQuoted(Integer.toString(vcpus));
    command.addLiteral("-name").addQuoted(id);
    // command.addLiteral("-nodefconfig");
    command.addLiteral("-nodefaults");
    command.addLiteral("-vga").addQuoted("cirrus");
    command.addLiteral("-usb");
    command.addLiteral("-readconfig").addFile(getDeviceConfigPath());
    if (!disableKvm) {
        command.addLiteral("-enable-kvm");
    }
    return command;
}
Also used : Command(org.platformlayer.ops.Command) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 35 with Command

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

the class NetworkTunDevice method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (OpsContext.isConfigure()) {
        Command findCommand = Command.build("/sbin/ifconfig {0}", interfaceName);
        boolean found = false;
        try {
            target.executeCommand(findCommand);
            found = true;
        } catch (ProcessExecutionException e) {
            ProcessExecution execution = e.getExecution();
            if (execution.getExitCode() == 1 && execution.getStdErr().contains("Device not found")) {
                found = false;
            } else {
                throw new OpsException("Error checking for interface", e);
            }
        }
        if (!found) {
            // This is actually idempotent, but I think it's scary to rely on it being so
            Command command = Command.build("/usr/sbin/tunctl -t {0}", interfaceName);
            target.executeCommand(command);
        }
        {
            // TODO: Safe to re-run?
            Command command = Command.build("ifconfig {0} up", interfaceName);
            target.executeCommand(command);
        }
        if (bridgeName != null) {
            // TODO: Safe to re-run?
            Command command = Command.build("brctl addif {0} {1}", bridgeName.get(), interfaceName);
            try {
                target.executeCommand(command);
            } catch (ProcessExecutionException e) {
                ProcessExecution execution = e.getExecution();
                if (execution.getExitCode() == 1 && execution.getStdErr().contains("already a member of a bridge")) {
                // OK
                // TODO: Check that the bridge is bridgeName
                } else {
                    throw new OpsException("Error attaching interface to bridge", e);
                }
            }
        }
    }
    if (OpsContext.isDelete()) {
        // TODO: Implement
        log.warn("NetworkTunDevice delete not implemented");
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) 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