use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class AptPackageManager method getInstalledPackageInfo.
public List<String> getInstalledPackageInfo(OpsTarget target) throws OpsException {
AptInfoCache cached = getCache(target);
if (cached.installedPackages == null) {
Command command = Command.build("/usr/bin/dpkg --get-selections");
ProcessExecution execution = target.executeCommand(command);
final List<String> packages = Lists.newArrayList();
for (String line : Splitter.on("\n").split(execution.getStdOut())) {
line = line.trim();
if (line.isEmpty()) {
continue;
}
List<String> tokens = Lists.newArrayList(Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings().split(line));
if (tokens.size() != 2) {
throw new OpsException("Error parsing line; expected 2 items: " + line);
}
String state = tokens.get(1);
if (state.equals("install")) {
String packageName = tokens.get(0);
int colonIndex = packageName.indexOf(':');
if (colonIndex != -1) {
// Architecture sometimes follows package name
packageName = packageName.substring(0, colonIndex);
}
packages.add(packageName);
} else if (state.equals("deinstall")) {
// Not installed (?)
} else {
throw new OpsException("Unknown package state in line: " + line);
}
}
cached.installedPackages = packages;
} else {
log.debug("Re-using cached package info");
}
return cached.installedPackages;
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class FilesystemBackedPool method release.
@Override
public void release(PlatformLayerKey owner, T item) throws OpsException {
File symlink = new File(assignedDir, toKey(item));
FilesystemInfo info = target.getFilesystemInfoFile(symlink);
if (info == null) {
throw new OpsException("Symlink not found");
}
if (!Objects.equal(info.symlinkTarget, toFile(owner).getAbsolutePath())) {
throw new OpsException("Resource not assigned to owner");
}
target.rm(symlink);
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class IpsecHelpers method getIpsecSecret.
public Secret getIpsecSecret() throws OpsException {
Secret secret = null;
for (ProviderOf<HasIpsecPolicy> ipsecPolicyProvider : providerHelper.listItemsProviding(HasIpsecPolicy.class)) {
ItemBase item = ipsecPolicyProvider.getItem();
if (item.getState() != ManagedItemState.ACTIVE) {
continue;
}
HasIpsecPolicy ipsec = ipsecPolicyProvider.get();
if (secret != null) {
throw new IllegalStateException("Multiple IPSEC policies found");
}
secret = ipsec.getIpsecPreSharedKey(item);
if (secret == null) {
// Should we allow this?
throw new IllegalStateException();
}
}
if (secret == null) {
throw new OpsException("Ipsec policy not found");
}
return secret;
}
use of org.platformlayer.ops.OpsException 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.OpsException in project platformlayer by platformlayer.
the class ZookeeperStatusChecker method handler.
@Handler
public void handler(OpsTarget target, ZookeeperServer zookeeperServer) throws OpsException {
if (OpsContext.isConfigure() || OpsContext.isValidate()) {
int port = ZookeeperConstants.ZK_PUBLIC_PORT;
List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(zookeeperServer.getTags(), port);
EndpointInfo endpoint = EndpointChooser.any().choose(endpoints);
if (endpoint == null) {
throw new OpsException("Cannot find endpoint for zookeeper");
}
InetSocketAddress socketAddress = endpoint.asSocketAddress();
{
ZookeeperResponse response;
try {
// IPV6 requires ipsec; use the IPV4 loopback instead
socketAddress = new InetSocketAddress(InetAddresses.forString("127.0.0.1"), socketAddress.getPort());
response = ZookeeperUtils.sendCommand(target, socketAddress, "ruok");
Deviations.assertEquals("imok", response.getRaw(), "Zookeeper ruok status");
} catch (OpsException e) {
Deviations.fail("Unable to connect to zookeeper", e);
}
}
{
ZookeeperResponse response;
try {
response = ZookeeperUtils.sendCommand(target, socketAddress, "srvr");
Map<String, String> responseMap = response.asMap();
String mode = responseMap.get("Mode");
Deviations.assertIn(Arrays.asList("follower", "leader"), mode, "Zookeeper mode");
} catch (OpsException e) {
Deviations.fail("Unable to connect to zookeeper", e);
}
}
}
}
Aggregations