Search in sources :

Example 61 with OpsException

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

the class CreateUser method handler.

@Handler
public void handler(DatabaseTarget db) throws OpsException {
    if (OpsContext.isConfigure()) {
        try {
            String createUser = String.format("CREATE USER %s WITH PASSWORD '%s'", databaseUser, databasePassword.plaintext());
            db.execute(createUser);
        } catch (SQLException e) {
            String sqlState = e.getSQLState();
            if (Objects.equal(sqlState, "42710")) {
                // ProcessExecution execution = e.getExecution();
                // if (execution.getExitCode() == 1 && execution.getStdErr().contains("already exists")) {
                log.info("User already exists");
            } else {
                log.info("Unknown code: " + sqlState);
                throw new OpsException("Error creating user", e);
            }
        }
        String grant = String.format("GRANT ALL PRIVILEGES ON DATABASE %s to %s;", grantDatabaseName, databaseUser);
        try {
            db.execute(grant);
        } catch (SQLException e) {
            String sqlState = e.getSQLState();
            // if (Objects.equal(sqlState, "12345")) {
            // log.info("User already exists");
            // } else {
            log.info("Unknown code: " + sqlState);
            throw new OpsException("Error granting privileges", e);
        // }
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) SQLException(java.sql.SQLException) Handler(org.platformlayer.ops.Handler)

Example 62 with OpsException

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

the class SchedulerImpl method putJob.

@Override
public void putJob(SchedulerRecord record) throws OpsException {
    ensureStarted();
    scheduleRecord(record, false);
    try {
        repository.put(record);
    } catch (RepositoryException e) {
        throw new OpsException("Error persisting record", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) RepositoryException(org.platformlayer.RepositoryException)

Example 63 with OpsException

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

the class NetworkPoint method forMe.

public static NetworkPoint forMe() throws OpsException {
    if (myAddress == null) {
        List<InetAddress> localAddresses = InetAddressUtils.getLocalAddresses();
        List<InetAddress> valid = Lists.newArrayList();
        for (InetAddress localAddress : localAddresses) {
            if (InetAddressUtils.isIpv4(localAddress)) {
                continue;
            }
            if (localAddress.isLinkLocalAddress()) {
                continue;
            }
            if (localAddress.isLoopbackAddress()) {
                continue;
            }
            valid.add(localAddress);
        }
        InetAddress address = InetAddressChooser.preferIpv6().choose(valid);
        if (!InetAddressUtils.isIpv6(address)) {
            throw new OpsException("We must have an IPV6 address");
        }
        myAddress = address;
    }
    return new NetworkPoint(null, myAddress);
}
Also used : OpsException(org.platformlayer.ops.OpsException) InetAddress(java.net.InetAddress)

Example 64 with OpsException

use of org.platformlayer.ops.OpsException 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;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase)

Example 65 with OpsException

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

the class IpsecPresharedKey method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (OpsContext.isConfigure()) {
        File pskFile = new File("/etc/racoon/psk.txt");
        String psk = target.readTextFile(pskFile);
        if (psk == null) {
            psk = "# Managed by PlatformLayer\n";
        }
        boolean found = false;
        // TODO: Extend MapSplitter / add some helper functions??
        Splitter keyValueSpliter = Splitter.on(CharMatcher.WHITESPACE).limit(2).omitEmptyStrings().trimResults();
        Map<String, String> psks = Maps.newHashMap();
        for (String line : Splitter.on("\n").trimResults().omitEmptyStrings().split(psk)) {
            if (line.startsWith("#")) {
                continue;
            }
            List<String> tokens = Lists.newArrayList(keyValueSpliter.split(line));
            if (tokens.size() != 2) {
                throw new OpsException("Cannot parse PSK line: " + line);
            }
            String key = tokens.get(0);
            String value = tokens.get(1);
            if (psks.containsKey(key)) {
                // (We could check to see if they're the same, but this is generally not good)
                throw new OpsException("Found duplicate PSK");
            }
            psks.put(key, value);
            if (!key.equals(id)) {
                continue;
            }
            if (value.equals(secret.plaintext())) {
                found = true;
            }
        }
        if (!found) {
            psk = psk + "\n";
            psk += id + " " + secret.plaintext() + "\n";
            FileUpload.upload(target, pskFile, psk);
            target.executeCommand(Command.build("racoonctl reload-config"));
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Splitter(com.google.common.base.Splitter) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Aggregations

OpsException (org.platformlayer.ops.OpsException)142 IOException (java.io.IOException)39 File (java.io.File)19 ItemBase (org.platformlayer.core.model.ItemBase)19 RepositoryException (org.platformlayer.RepositoryException)18 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)17 Handler (org.platformlayer.ops.Handler)17 Tag (org.platformlayer.core.model.Tag)16 Command (org.platformlayer.ops.Command)16 Machine (org.platformlayer.ops.Machine)13 TagChanges (org.platformlayer.core.model.TagChanges)11 OpsTarget (org.platformlayer.ops.OpsTarget)11 TimeoutException (java.util.concurrent.TimeoutException)10 OpenstackException (org.openstack.client.OpenstackException)10 OpsContext (org.platformlayer.ops.OpsContext)10 X509Certificate (java.security.cert.X509Certificate)9 InetAddress (java.net.InetAddress)8 ProjectId (org.platformlayer.ids.ProjectId)8 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)8 List (java.util.List)7