Search in sources :

Example 1 with OpsTarget

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

the class BackupDirectory method doBackup.

@Handler(BackupAction.class)
public void doBackup() throws OpsException, IOException {
    OpsContext opsContext = OpsContext.get();
    // Machine machine = opsContext.getInstance(Machine.class);
    OpsTarget target = opsContext.getInstance(OpsTarget.class);
    BackupContext backup = backups.getContext();
    DirectoryBackup request = new DirectoryBackup();
    request.target = target;
    request.rootDirectory = backupRoot;
    request.exclude.addAll(this.excludes);
    backup.doBackup(request);
    backup.add(new BackupItem(itemKey, FORMAT, request.objectName));
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) OpsContext(org.platformlayer.ops.OpsContext) Handler(org.platformlayer.ops.Handler)

Example 2 with OpsTarget

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

the class DnsResolverModuleBuilder method buildTemplateModel.

@Override
public void buildTemplateModel(Map<String, Object> model) throws OpsException {
    List<String> nameservers = Lists.newArrayList();
    for (ProviderOf<DnsResolverProvider> entry : providers.listItemsProviding(DnsResolverProvider.class)) {
        DnsResolverProvider dnsResolverProvider = entry.get();
        List<InetAddress> addresses = dnsResolverProvider.findAddresses(NetworkPoint.forTargetInContext());
        for (InetAddress address : addresses) {
            nameservers.add(address.getHostAddress());
        }
    }
    if (nameservers.isEmpty()) {
        log.warn("No (internal) resolvers found; will set up default public nameservers; reconfigure needed if this changes");
        if (usePrivateResolvers) {
            OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
            AsBlock as = AsBlock.find(target);
            if (as != null) {
            // We had problems with the SL resolvers...
            /*
					 * if (Objects.equal(AsBlock.SOFTLAYER, as)) { log.warn("Adding private Softlayer resolvers");
					 * nameservers.add("10.0.80.11"); nameservers.add("10.0.80.12"); }
					 * 
					 * if (Objects.equal(AsBlock.HETZNER, as)) { log.warn("Adding private Hetzner resolvers");
					 * nameservers.add("213.133.99.99"); nameservers.add("213.133.100.100");
					 * nameservers.add("213.133.98.98"); }
					 */
            }
        }
        nameservers.add("8.8.8.8");
        nameservers.add("8.8.4.4");
        nameservers.add("2001:4860:4860::8888");
        nameservers.add("2001:4860:4860::8844");
    // So a reconfigure is needed!
    }
    model.put("nameservers", nameservers);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) AsBlock(org.platformlayer.ops.packages.AsBlock) InetAddress(java.net.InetAddress)

Example 3 with OpsTarget

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

the class SshPostgresTarget method execute.

public ProcessExecution execute(String sql, String maskedSql) throws OpsException {
    OpsTarget target = getOpsTarget();
    // We probably want psql -A -t -c "command"
    Command command = Command.build("psql");
    command.addQuoted("--username=", username);
    command.addQuoted("--host=", hostname);
    command.addArgument(Argument.buildQuoted("--command=", sql).setMasked("--command=" + Command.MASKED));
    CommandEnvironment env = new CommandEnvironment();
    env.put("PGPASSWORD", password.plaintext());
    command.setEnvironment(env);
    return target.executeCommand(command);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) Command(org.platformlayer.ops.Command) CommandEnvironment(org.platformlayer.ops.CommandEnvironment)

Example 4 with OpsTarget

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

the class ForEach method doRecursion.

public void doRecursion(Object controller, SshKey sshKey, Class<? extends ItemBase> machineItemClass, Class<? extends ItemBase> dataItemClass) throws OpsException {
    boolean failed = false;
    OpsContext ops = OpsContext.get();
    List<ItemBase> dataItems = Lists.newArrayList();
    ItemBase contextDataItem = ops.getInstance(dataItemClass);
    if (contextDataItem != null) {
        dataItems.add(contextDataItem);
    } else {
        for (ItemBase dataItem : platformLayer.listItems(dataItemClass)) {
            dataItems.add(dataItem);
        }
    }
    Object contextMachine = ops.getInstance(machineItemClass);
    if (contextMachine != null) {
        // We are presumably building the machine item
        PlatformLayerKey targetItemKey = ops.getJobRecord().getTargetItemKey();
        ItemBase machineItem = (ItemBase) contextMachine;
        if (!Objects.equal(targetItemKey, machineItem.getKey())) {
            throw new OpsException("Expected to find same model");
        }
        Machine machine = instances.findMachine(machineItem);
        if (machine == null) {
            log.warn("Server instance not found: " + contextMachine);
            failed = true;
        } else {
            OpsTarget target = machine.getTarget(sshKey);
            failed |= processDataItems(controller, dataItems, machineItem, machine, target);
        }
    } else {
        // We are building a data item
        for (ItemBase machineItem : platformLayer.listItems(machineItemClass)) {
            if (machineItem.getState() != ManagedItemState.ACTIVE) {
                log.warn("Machine not yet active: " + machineItem);
                failed = true;
                continue;
            }
            Machine machine = instances.findMachine(machineItem);
            if (machine == null) {
                log.warn("Server instance not found: " + machineItem);
                failed = true;
                continue;
            }
            OpsTarget target = machine.getTarget(sshKey);
            failed |= processDataItems(controller, dataItems, machineItem, machine, target);
        }
    }
    if (failed) {
        throw new OpsException("Could not update all servers").setRetry(TimeSpan.ONE_MINUTE);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpsTarget(org.platformlayer.ops.OpsTarget) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) OpsContext(org.platformlayer.ops.OpsContext) Machine(org.platformlayer.ops.Machine)

Example 5 with OpsTarget

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

the class PosixGroup method doOperation.

@Handler
public void doOperation() throws OpsException {
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    Map<String, Group> groups = PosixGroupManagement.getGroups(target);
    Group group = groups.get(groupName);
    if (group == null) {
        target.executeCommand("groupadd {0}", groupName);
    }
}
Also used : Group(org.platformlayer.ops.users.PosixGroupManagement.Group) OpsTarget(org.platformlayer.ops.OpsTarget) 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