use of org.platformlayer.core.model.ItemBase in project platformlayer by platformlayer.
the class StandardTemplateData method getAdditionalKeys.
public void getAdditionalKeys(Map<String, ManagedSecretKey> keys) throws OpsException {
for (Link link : getLinks()) {
ItemBase item = platformLayer.getItem(link.getTarget());
LinkTarget linkTarget = providers.toInterface(item, LinkTarget.class);
PlatformLayerKey caPath = linkTarget.getCaForClientKey();
if (caPath != null) {
String alias = links.buildKeyName(link);
keys.put(alias, findCaSignedKey(caPath, alias));
}
}
}
use of org.platformlayer.core.model.ItemBase 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);
}
}
use of org.platformlayer.core.model.ItemBase in project platformlayer by platformlayer.
the class HttpProxyHelper method findHttpProxies.
public List<String> findHttpProxies(OpsTarget target, URI uri) throws OpsException {
List<String> proxies = Lists.newArrayList();
for (ProviderOf<HttpProxyController> httpProxyProvider : providerHelper.listItemsProviding(HttpProxyController.class)) {
ItemBase item = httpProxyProvider.getItem();
if (item.getState() != ManagedItemState.ACTIVE) {
continue;
}
HttpProxyController httpProxy = httpProxyProvider.get();
NetworkPoint forNetworkPoint = NetworkPoint.forTarget(target);
String url = httpProxy.getUrl(httpProxyProvider.getItem(), forNetworkPoint, uri);
if (url == null) {
log.info("Could not get URL for proxy: " + item);
} else {
proxies.add(url);
}
}
return proxies;
}
use of org.platformlayer.core.model.ItemBase in project platformlayer by platformlayer.
the class StandardTemplateData method findPublicSslKey.
public ManagedSecretKey findPublicSslKey() throws OpsException {
PlatformLayerKey sslKey = getSslKeyPath();
if (sslKey == null) {
if (getCaPath() != null) {
ManagedSecretKey key = findCaSignedKey("public");
if (key != null) {
return key;
}
}
return null;
}
ItemBase sslKeyItem = (ItemBase) platformLayer.getItem(sslKey);
ManagedSecretKey key = providers.toInterface(sslKeyItem, ManagedSecretKey.class);
return key;
}
use of org.platformlayer.core.model.ItemBase in project platformlayer by platformlayer.
the class NetworkPoints method resolveAll.
public List<NetworkPoint> resolveAll(List<? extends ItemBase> items, boolean ignoreErrors) throws OpsException {
List<NetworkPoint> points = Lists.newArrayList();
for (ItemBase item : items) {
NetworkPoint point = findNetworkPoint(item);
if (point == null) {
if (ignoreErrors) {
log.debug("Ignoring unresolvable item");
} else {
throw new OpsException("Unable to resolve item: " + item.getKey());
}
}
points.add(point);
}
return points;
}
Aggregations