use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class PackageDependency method doOperation.
@Handler
public void doOperation() throws OpsException {
if (OpsContext.isDelete()) {
// Should we delete the packages? Probably not, because others may also need them
log.debug("Not removing package on delete (in case someone else also uses it)");
}
if (OpsContext.isConfigure() || OpsContext.isValidate()) {
OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
List<String> installedPackages = apt.getInstalledPackageInfo(target);
if (!installedPackages.contains(packageName)) {
log.info("Package not installed: " + packageName);
if (OpsContext.isValidate()) {
throw new OpsException("Package not installed: " + packageName);
}
if (repositoryKey != null) {
apt.addRepositoryKeyUrl(target, repositoryKey.getUrl());
}
if (repository != null) {
apt.addRepository(target, repository.getKey(), repository.getSource());
}
if (configuration != null) {
apt.preconfigurePackages(target, configuration);
}
// TODO: Only update once per operation?
// I think we do want to update aggressively though, because we want to be sure we're up to date
// as that could well be the reason we're running the operation!
// We definitely want to update if we added a repository etc above
apt.update(target, false);
apt.install(target, packageName);
} else {
log.debug("Package is installed: " + packageName);
}
}
}
use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class UpdatePackages method handler.
@Handler
public void handler(OpsTarget target) throws OpsException {
apt.update(target, true);
List<String> outOfDatePackage = apt.findOutOfDatePackages(target);
if (!outOfDatePackage.isEmpty()) {
// Pre-download any out-of-date files; will make any maintenance window smaller
Command command = Command.build("apt-get --yes --download-only dist-upgrade");
target.executeCommand(command);
}
Deviations.assertEquals(Collections.emptyList(), outOfDatePackage, "Packaged out of date");
}
use of org.platformlayer.ops.Handler 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