Search in sources :

Example 56 with Handler

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);
        }
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) OpsException(org.platformlayer.ops.OpsException) Handler(org.platformlayer.ops.Handler)

Example 57 with Handler

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");
}
Also used : Command(org.platformlayer.ops.Command) Handler(org.platformlayer.ops.Handler)

Example 58 with Handler

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);
            }
        }
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Aggregations

Handler (org.platformlayer.ops.Handler)58 File (java.io.File)21 Command (org.platformlayer.ops.Command)17 OpsException (org.platformlayer.ops.OpsException)17 OpsTarget (org.platformlayer.ops.OpsTarget)17 Tag (org.platformlayer.core.model.Tag)8 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)5 SshKey (org.platformlayer.ops.helpers.SshKey)5 Machine (org.platformlayer.ops.Machine)4 PublicKey (java.security.PublicKey)3 Action (org.platformlayer.core.model.Action)3 EndpointInfo (org.platformlayer.core.model.EndpointInfo)3 ItemBase (org.platformlayer.core.model.ItemBase)3 Tags (org.platformlayer.core.model.Tags)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 Map (java.util.Map)2 OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)2 SecurityGroup (org.openstack.model.compute.SecurityGroup)2 Server (org.openstack.model.compute.Server)2 PlatformLayerClientException (org.platformlayer.PlatformLayerClientException)2