use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.
the class InstanceHelpers method getMachines.
public List<Machine> getMachines(ItemBase item, boolean required) throws OpsException {
Class<? extends ItemBase> javaClass = item.getClass();
ModelClass<?> modelClass = serviceProviderDictionary.getModelClass(javaClass);
Object controller = modelClass.getProvider().getController(item);
// TODO: Should we just recurse down through children?
if (controller instanceof MachineCluster) {
MachineCluster machineCluster = (MachineCluster) controller;
return machineCluster.getMachines(required);
} else {
Machine machine = getMachine(item, required);
if (machine == null) {
return Collections.emptyList();
}
return Collections.singletonList(machine);
}
}
use of org.platformlayer.ops.Machine 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);
}
}
use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.
the class OpenstackBackupContextFactory method build.
@Override
public OpenstackBackupContext build(ItemBase item) throws OpsException {
Machine machine = instances.findMachine(item);
if (machine == null) {
throw new OpsException("Cannot determine machine for: " + item);
}
StorageConfiguration storageConfiguration = cloud.getStorageConfiguration(machine);
return build(storageConfiguration);
}
use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.
the class ImageStoreController method getImageStore.
@Override
public org.platformlayer.ops.images.ImageStore getImageStore() throws OpsException {
String endpoint = model.getTags().findUnique("endpoint");
if (endpoint == null) {
log.warn("ImageStore not yet active: " + model);
return null;
}
URI url;
try {
url = new URI(endpoint);
} catch (URISyntaxException e) {
throw new OpsException("Cannot parse endpoint: " + endpoint, e);
}
if (url.getScheme().equals("ssh")) {
String myAddress = url.getHost();
Machine machine = new OpaqueMachine(NetworkPoint.forPublicHostname(myAddress));
// This is nasty; we're in the context of another service here...
SshKey sshKey = sshKeys.findOtherServiceKey(new ServiceType("imagestore"));
OpsTarget target = machine.getTarget("imagestore", sshKey.getKeyPair());
DirectImageStore directImageStore = OpsContext.get().getInjector().getInstance(DirectImageStore.class);
directImageStore.connect(target);
return directImageStore;
} else {
throw new OpsException("Unknown protocol for endpoint: " + endpoint);
}
}
use of org.platformlayer.ops.Machine in project platformlayer by platformlayer.
the class PersistentInstanceMapper method handler.
@Handler
public void handler(PersistentInstance model) throws OpsException {
Machine machine = null;
Tag tagForInstance = Tag.buildParentTag(model.getKey());
// boolean instanceIsTagged = false;
// See if we have an instance id tag
{
// String instanceKey = model.getTags().findUnique(Tag.INSTANCE_KEY);
// if (instanceKey != null) {
// InstanceBase foundInstance = cloud.findInstanceByInstanceKey(PlatformLayerKey.parse(instanceKey));
// if (foundInstance == null) {
// throw new IllegalStateException("Tagged with instance id, but instance not found: " + instanceKey);
// }
// if (foundInstance.getState() == ManagedItemState.DELETED) {
// log.warn("Found deleted instance: " + foundInstance);
// } else {
// machine = cloudHelpers.toMachine(foundInstance);
// if (machine == null) {
// throw new IllegalStateException();
// }
// }
//
// // instanceIsTagged = true;
// }
}
if (machine == null) {
// We may have created a machine, but failed to tag the instance
machine = cloud.findMachine(tagForInstance);
}
if (!OpsContext.isDelete()) {
// We always PUT the machine
// TODO: Check if unchanged??
MachineCreationRequest request = buildMachineCreationRequest(model);
request.tags.add(tagForInstance);
machine = cloudHelpers.putInstanceByTag(request, model.getKey(), tagForInstance);
// if (machine == null) {
// // No machine
// MachineCreationRequest request = buildMachineCreationRequest(model);
// request.tags.add(tagForInstance);
//
// machine = cloudHelpers.createInstance(request, model.getKey(), tagForInstance);
// }
} else {
if (machine != null) {
cloudHelpers.terminateMachine(machine);
}
}
getRecursionState().pushChildScope(Machine.class, machine);
}
Aggregations