use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class GitServerAssignment method handler.
@Handler
public void handler(GitRepository model) throws Exception {
PlatformLayerKey assignedTo = Tag.ASSIGNED_TO.findUnique(model.getTags());
if (OpsContext.isConfigure()) {
if (assignedTo == null) {
List<GitService> gitServices = platformLayer.listItems(GitService.class);
if (gitServices.size() == 0) {
throw new OpsException("No git service found");
}
GitService gitService = RandomChooser.chooseRandom(gitServices);
if (gitService == null) {
throw new IllegalStateException();
}
assignedTo = gitService.getKey();
platformLayer.addTag(model.getKey(), Tag.ASSIGNED_TO.build(assignedTo));
}
}
GitService gitService = null;
if (assignedTo != null) {
gitService = platformLayer.getItem(assignedTo, GitService.class);
}
if (OpsContext.isDelete()) {
if (gitService == null) {
log.info("Deleting, but not assigned to a server; nothing to do");
getRecursionState().setPreventRecursion(true);
return;
}
}
if (gitService == null) {
throw new OpsException("No git servers found");
}
if (gitService.getState() != ManagedItemState.ACTIVE) {
throw new OpsException("Server not yet active: " + gitService);
}
Machine machine = instances.findMachine(gitService);
if (machine == null) {
throw new OpsException("Server machine not found:" + gitService);
}
SshKey sshKey = service.getSshKey();
OpsTarget target = machine.getTarget(sshKey);
getRecursionState().pushChildScope(OpsTarget.class, target);
}
use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class DownloadImage method handler.
@Handler
public void handler(OpsTarget target) throws OpsException {
if (target.getFilesystemInfoFile(imageFile) == null) {
DirectCloud cloud = OpsContext.get().getInstance(DirectCloud.class);
if (cloud == null) {
throw new IllegalStateException("Cloud instance not found");
}
ImageStore imageStore = cloudHelpers.getImageStore(cloud);
MachineProvider machineProvider = providers.toInterface(cloud, MachineProvider.class);
CloudImage imageInfo = imageFactory.getOrCreateImageId(machineProvider, imageFormats, recipeKey);
if (imageStore == null) {
throw new OpsException("Image store not configured");
}
String fileName = imageInfo.getId() + ".image." + imageInfo.getFormat().name();
// TODO: We don't need rawImage; delete or just request a read-only version
File rawImage = new File(IMAGES_DIR, fileName);
target.mkdir(IMAGES_DIR);
// TODO: Caching / reuse ... need to check md5 though
imageStore.bringToMachine(imageInfo.getId(), target, rawImage);
ImageFormat imageFormat = imageInfo.getFormat();
switch(imageFormat) {
case Tar:
{
target.mkdir(imageFile);
target.executeCommand(Command.build("cd {0}; tar jxf {1}", imageFile, rawImage).setTimeout(TimeSpan.FIVE_MINUTES));
break;
}
case DiskRaw:
{
Command expand = Command.build("gunzip -c {0} | cp --sparse=always /proc/self/fd/0 {1}", rawImage, imageFile);
target.executeCommand(expand.setTimeout(TimeSpan.FIVE_MINUTES));
break;
}
case DiskQcow2:
{
Command expand = Command.build("cp {0} {1}", rawImage, imageFile);
target.executeCommand(expand.setTimeout(TimeSpan.FIVE_MINUTES));
break;
}
default:
throw new OpsException("Unknown image format: " + imageFormat);
}
}
}
use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class EnsureFirewallIngress method handler.
@Handler
public void handler(GoogleCloud cloud, GoogleComputeMachine machine) throws OpsException {
GoogleComputeClient client = googleComputeClientFactory.getComputeClient(cloud);
// Find the public address, although the Google Cloud firewall may be blocking it
publicAddress = machine.getNetworkPoint().getBestAddress(NetworkPoint.forPublicInternet());
String serverLink = machine.getServerSelfLink();
List<Firewall> rules = client.getInstanceFirewallRules(serverLink);
Firewall matchingRule = findMatchingRule(rules);
if (OpsContext.isConfigure()) {
if (matchingRule == null) {
Firewall rule = new Firewall();
rule.setSourceRanges(Arrays.asList("0.0.0.0/0"));
rule.setName("pl-" + UUID.randomUUID().toString());
Allowed allowed = new Allowed();
allowed.setIPProtocol("tcp");
allowed.setPorts(Arrays.asList("" + model.publicPort));
rule.setAllowed(Arrays.asList(allowed));
rule.setNetwork(client.buildNetworkUrl("default"));
client.createFirewallRule(rule);
}
}
if (OpsContext.isDelete()) {
if (matchingRule != null) {
client.deleteFirewallRule(matchingRule);
}
}
}
use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class MountCgroups method handler.
@Handler
public void handler() throws OpsException, IOException {
// TODO: Only if not installed
OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
File cgroupsFile = new File("/cgroup");
FilesystemInfo info = target.getFilesystemInfoFile(cgroupsFile);
if (info != null) {
// TODO: Better idempotency
return;
}
String fstabLine = "\ncgroup\t/cgroup\tcgroup\tdefaults\t0\t0";
File fstabFile = new File("/etc/fstab");
String fstab = target.readTextFile(fstabFile);
fstab += fstabLine;
FileUpload.upload(target, fstabFile, fstab);
target.mkdir(cgroupsFile);
Command mountCommand = Command.build("mount cgroup");
target.executeCommand(mountCommand);
// mkdir /cgroup
// echo -e "cgroup\t/cgroup\tcgroup\tdefaults\t0\t0" >> /etc/fstab
// mount cgroup
}
use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class CloudInstanceMapper method doOperation.
@Handler
public void doOperation() throws OpsException, IOException {
Tag tag = Tag.build(Tag.ASSIGNED, instance.getKey().getUrl());
List<DirectHost> hosts = Lists.newArrayList(platformLayer.listItems(DirectHost.class, TagFilter.byTag(tag)));
if (hosts.size() > 1) {
// Huh?
throw new OpsException("Multiple hosts already assigned");
}
DirectHost host;
if (hosts.isEmpty()) {
if (OpsContext.isDelete()) {
host = null;
} else {
if (createInstance) {
DirectCloudHost cloudHost = cloudMap.pickHost(instance);
host = cloudHost.getModel();
platformLayer.addTag(host.getKey(), tag);
} else {
throw new OpsException("Instance not yet assigned");
}
}
} else {
host = hosts.get(0);
}
RecursionState recursion = getRecursionState();
if (host != null) {
this.cloud = platformLayer.getItem(host.cloud, DirectCloud.class);
this.hostTarget = directHelpers.toTarget(host);
recursion.pushChildScope(cloud);
recursion.pushChildScope(host);
recursion.pushChildScope(hostTarget);
} else {
if (!OpsContext.isDelete()) {
throw new IllegalStateException();
}
log.info("No host set; won't recurse in");
recursion.setPreventRecursion(true);
}
}
Aggregations