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);
}
}
}
}
Aggregations