Search in sources :

Example 1 with BindingScope

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

the class OperationInvoker method invoke.

public void invoke(Object controller) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    final BindingScope scope = BindingScope.get();
    Method method = findTargetMethod(controller, scope);
    if (method == null) {
        throw new IllegalStateException("Cannot find handler for operation on " + controller.getClass());
    }
    MethodInvoker invoker = invokerProvider.get();
    if (scope != null) {
        invoker.addProvider(new Function<Class<?>, Object>() {

            @Override
            public Object apply(Class<?> clazz) {
                return scope.getInstance(clazz);
            }
        });
    }
    invoker.invokeMethod(controller, method);
}
Also used : Method(java.lang.reflect.Method) BindingScope(org.platformlayer.ops.BindingScope) MethodInvoker(org.platformlayer.ops.reflection.MethodInvoker)

Example 2 with BindingScope

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

the class OperationWorker method doOperation.

Object doOperation() throws OpsException {
    final Action action = activeJob.getAction();
    final PlatformLayerKey targetItemKey = activeJob.getTargetItemKey();
    RenameThread rename = new RenameThread(action.getClass().getSimpleName() + " " + targetItemKey);
    try {
        OpsContextBuilder opsContextBuilder = opsSystem.getInjector().getInstance(OpsContextBuilder.class);
        final ProjectAuthorization project = activeJob.getProjectAuthorization();
        final OpsContext opsContext = opsContextBuilder.buildOpsContext(activeJob);
        final ServiceType serviceType = activeJob.getServiceType();
        final ServiceProvider serviceProvider = opsSystem.getServiceProvider(serviceType);
        try {
            return OpsContext.runInContext(opsContext, new CheckedCallable<Object, Exception>() {

                @Override
                public Object call() throws Exception {
                    log.info("Starting job");
                    activeJob.setState(JobState.RUNNING);
                    ItemBase item;
                    ManagedItemRepository repository = opsSystem.getManagedItemRepository();
                    try {
                        boolean fetchTags = true;
                        item = repository.getManagedItem(targetItemKey, fetchTags, SecretProvider.from(project));
                    } catch (RepositoryException e) {
                        throw new OpsException("Error reading item from repository", e);
                    }
                    if (item == null) {
                        throw new WebApplicationException(404);
                    }
                    List<Object> scopeItems = Lists.newArrayList();
                    addActionScopeItems(action, item, scopeItems);
                    Object controller = serviceProvider.getController(item);
                    scopeItems.add(item);
                    scopeItems.add(action);
                    BindingScope scope = BindingScope.push(scopeItems);
                    opsContext.recurseOperation(scope, controller);
                    // TODO: Should we run a verify operation before -> ACTIVE??
                    // (we need to fix the states as well)
                    ManagedItemState newState = finishAction(action, scope);
                    if (newState != null) {
                        repository.changeState(targetItemKey, newState);
                        item.state = newState;
                    }
                    log.info("Job finished with SUCCESS");
                    activeJob.setState(JobState.SUCCESS);
                    return null;
                }

                private ManagedItemState finishAction(Action action, BindingScope scope) throws OpsException {
                    ManagedItemState newState = null;
                    if (action instanceof ConfigureAction) {
                        newState = ManagedItemState.ACTIVE;
                    }
                    if (action instanceof ValidateAction) {
                    // TODO: Change state to healthy??
                    }
                    if (action instanceof DeleteAction) {
                        newState = ManagedItemState.DELETED;
                    }
                    if (action instanceof BackupAction) {
                        BackupContext backupContext = scope.getInstance(BackupContext.class);
                        backupContext.writeDescriptor();
                    }
                    return newState;
                }

                private void addActionScopeItems(Action action, ItemBase item, List<Object> scopeItems) throws OpsException {
                    if (action instanceof BackupAction) {
                        // TODO: Don't hard-code this
                        BackupHelpers backupHelpers = opsSystem.getInjector().getInstance(BackupHelpers.class);
                        BackupContext backupContext = backupHelpers.createBackupContext(item);
                        scopeItems.add(backupContext);
                    }
                }
            });
        } catch (Throwable e) {
            log.warn("Error running operation", e);
            log.warn("Job finished with FAILED");
            // boolean isDone = false; // We will retry
            activeJob.setState(JobState.FAILED);
            TimeSpan retry = null;
            HasRetryInfo retryInfo = ExceptionHelpers.findRetryInfo(e);
            if (retryInfo != null) {
                retry = retryInfo.getRetry();
            }
            if (retry == null) {
                // TODO: Eventually give up??
                retry = ApplicationMode.isDevelopment() ? TimeSpan.ONE_MINUTE : TimeSpan.FIVE_MINUTES;
            }
            // TODO: State transition??
            // managedItem.setState(ManagedItemState.ACTIVE, true);
            log.warn("Scheduling retry in " + retry);
            activeJob.enqueueRetry(retry);
            return null;
        } finally {
            try {
                activeJob.recordJobEnd();
            } catch (OpsException e) {
                log.error("Error recording job in registry", e);
            }
        }
    } finally {
        IoUtils.safeClose(rename);
    }
}
Also used : ValidateAction(org.platformlayer.core.model.ValidateAction) ConfigureAction(org.platformlayer.core.model.ConfigureAction) DeleteAction(org.platformlayer.core.model.DeleteAction) BackupAction(org.platformlayer.core.model.BackupAction) Action(org.platformlayer.core.model.Action) OpsException(org.platformlayer.ops.OpsException) WebApplicationException(javax.ws.rs.WebApplicationException) BackupHelpers(org.platformlayer.ops.backups.BackupHelpers) ManagedItemRepository(org.platformlayer.xaas.repository.ManagedItemRepository) OpsContext(org.platformlayer.ops.OpsContext) TimeSpan(com.fathomdb.TimeSpan) BackupAction(org.platformlayer.core.model.BackupAction) ServiceType(org.platformlayer.ids.ServiceType) BackupContext(org.platformlayer.ops.backups.BackupContext) HasRetryInfo(org.platformlayer.exceptions.HasRetryInfo) List(java.util.List) BindingScope(org.platformlayer.ops.BindingScope) ConfigureAction(org.platformlayer.core.model.ConfigureAction) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) WebApplicationException(javax.ws.rs.WebApplicationException) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) ProjectAuthorization(org.platformlayer.model.ProjectAuthorization) DeleteAction(org.platformlayer.core.model.DeleteAction) ValidateAction(org.platformlayer.core.model.ValidateAction) ManagedItemState(org.platformlayer.core.model.ManagedItemState)

Example 3 with BindingScope

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

the class DirectTarget method doRecurseOperation.

@Override
public void doRecurseOperation() throws OpsException {
    Machine machine = new OpaqueMachine(address);
    OpsTarget target = machine.getTarget(sshKey);
    BindingScope scope = BindingScope.push(machine, target);
    try {
        OpsContext opsContext = OpsContext.get();
        OperationRecursor.doRecurseChildren(opsContext, this);
    } finally {
        scope.pop();
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) OpaqueMachine(org.platformlayer.ops.OpaqueMachine) OpsContext(org.platformlayer.ops.OpsContext) OpaqueMachine(org.platformlayer.ops.OpaqueMachine) Machine(org.platformlayer.ops.Machine) BindingScope(org.platformlayer.ops.BindingScope)

Example 4 with BindingScope

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

the class MachineResolver method doRecurseOperation.

@Override
public void doRecurseOperation() throws OpsException {
    ItemBase dest = platformLayerHelpers.getItem(key);
    boolean required = !OpsContext.isDelete();
    for (Machine machine : instanceHelpers.getMachines(dest, required)) {
        OpsTarget target = instanceHelpers.getTarget(dest, machine);
        BindingScope scope = BindingScope.push(machine, target);
        try {
            OpsContext opsContext = OpsContext.get();
            OperationRecursor.doRecurseChildren(opsContext, this);
        } finally {
            scope.pop();
        }
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) ItemBase(org.platformlayer.core.model.ItemBase) OpsContext(org.platformlayer.ops.OpsContext) Machine(org.platformlayer.ops.Machine) BindingScope(org.platformlayer.ops.BindingScope)

Example 5 with BindingScope

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

the class MetricsResource method listMetrics.

@GET
@Produces({ XML, JSON })
public MetricInfoCollection listMetrics() throws OpsException, RepositoryException {
    final ItemBase managedItem = getManagedItem();
    final ServiceProvider serviceProvider = getServiceProvider();
    OpsContextBuilder opsContextBuilder = objectInjector.getInstance(OpsContextBuilder.class);
    final OpsContext opsContext = opsContextBuilder.buildTemporaryOpsContext(getServiceType(), getProjectAuthorization());
    return OpsContext.runInContext(opsContext, new CheckedCallable<MetricInfoCollection, Exception>() {

        @Override
        public MetricInfoCollection call() throws Exception {
            BindingScope bindingScope = BindingScope.push(managedItem, managedItem);
            try {
                Object controller = serviceProvider.getController(managedItem);
                MetricConfig metricConfig = opsContext.getMetricInfo(controller);
                return MetricCollector.toMetricInfo(metricConfig);
            } finally {
                bindingScope.pop();
            }
        }
    });
}
Also used : MetricConfig(org.platformlayer.ops.model.metrics.MetricConfig) ItemBase(org.platformlayer.core.model.ItemBase) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) MetricInfoCollection(org.platformlayer.metrics.model.MetricInfoCollection) OpsContext(org.platformlayer.ops.OpsContext) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) OpsContextBuilder(org.platformlayer.ops.tasks.OpsContextBuilder) BindingScope(org.platformlayer.ops.BindingScope) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

BindingScope (org.platformlayer.ops.BindingScope)11 OpsContext (org.platformlayer.ops.OpsContext)10 ItemBase (org.platformlayer.core.model.ItemBase)7 OpsException (org.platformlayer.ops.OpsException)5 Machine (org.platformlayer.ops.Machine)4 RepositoryException (org.platformlayer.RepositoryException)3 OpsTarget (org.platformlayer.ops.OpsTarget)3 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)3 Produces (javax.ws.rs.Produces)2 OpsContextBuilder (org.platformlayer.ops.tasks.OpsContextBuilder)2 TimeSpan (com.fathomdb.TimeSpan)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Action (org.platformlayer.core.model.Action)1 BackupAction (org.platformlayer.core.model.BackupAction)1 ConfigureAction (org.platformlayer.core.model.ConfigureAction)1 DeleteAction (org.platformlayer.core.model.DeleteAction)1