Search in sources :

Example 41 with OpsTarget

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

the class ConfigureHostname method handle.

@Handler
public void handle() throws OpsException {
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    if (Strings.isNullOrEmpty(hostname)) {
        throw new IllegalArgumentException();
    }
    // Fix hostname
    File hostsFile = new File("/etc/hosts");
    String hostLine = "127.0.0.1\t" + hostname;
    String hosts = target.readTextFile(hostsFile);
    boolean found = false;
    for (String line : Splitter.on("\n").trimResults().split(hosts)) {
        if (line.equals(hostLine)) {
            found = true;
        }
    }
    if (!found) {
        hosts += "\n" + hostLine + "\n";
        FileUpload.upload(target, hostsFile, hosts);
    }
    FileUpload.upload(target, new File("/etc/hostname"), hostname);
    {
        ProcessExecution execution = target.executeCommand("hostname");
        String currentHostname = execution.getStdOut().trim();
        if (!currentHostname.equals(hostname)) {
            if (!DetectVirtualization.isLxc(target)) {
                // This actually can't be done within an LXC instance, which is why we go to extraordinary lengths
                // to
                // set it on creation
                target.executeCommand("hostname {0}", hostname);
            } else {
                log.warn("Unable to change hostname on LXC: " + currentHostname + " -> " + hostname);
            }
        }
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Aggregations

OpsTarget (org.platformlayer.ops.OpsTarget)41 Handler (org.platformlayer.ops.Handler)17 File (java.io.File)14 Machine (org.platformlayer.ops.Machine)12 OpsException (org.platformlayer.ops.OpsException)11 SshKey (org.platformlayer.ops.helpers.SshKey)10 Command (org.platformlayer.ops.Command)9 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)6 OpaqueMachine (org.platformlayer.ops.OpaqueMachine)6 OpsContext (org.platformlayer.ops.OpsContext)6 ItemBase (org.platformlayer.core.model.ItemBase)4 PublicKey (java.security.PublicKey)3 Tag (org.platformlayer.core.model.Tag)3 Tags (org.platformlayer.core.model.Tags)3 BindingScope (org.platformlayer.ops.BindingScope)3 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)3 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)3 ServiceType (org.platformlayer.ids.ServiceType)2