public Service getService()

in containers-marathon/src/main/java/org/apache/aries/containers/marathon/impl/MarathonServiceManager.java [83:165]


    public Service getService(ServiceConfig config) throws Exception {
        GetAppsResponse existing = marathonClient.getApps(
                Collections.singletonMap("label", SERVICE_NAME + "==" + config.getServiceName()));
        if (existing.getApps().size() > 0) {
            return createServiceFromExistingApp(existing.getApps(), config);
        }

        App app = new App();
        app.setId(config.getServiceName());
        app.setCpus(config.getRequestedCpuUnits());
        app.setMem(config.getRequestedMemory());
        app.setInstances(config.getRequestedInstances());
        app.setEnv(Collections.unmodifiableMap(config.getEnvVars()));
        app.addLabel(SERVICE_NAME, config.getServiceName());

        StringBuilder cmd = new StringBuilder();
        if (config.getEntryPoint() != null) {
            // TODO is this right?
            cmd.append(config.getEntryPoint());
        }

        if (config.getCommandLine().length > 0) {
            for (String c : config.getCommandLine()) {
                if (cmd.length() > 0)
                    cmd.append(' ');

                if (c.contains(" "))
                    c = "'" + c + "'";

                cmd.append(c);
            }
        }
        if (cmd.length() > 0)
            app.setCmd(cmd.toString());

        Docker docker = new Docker();
        docker.setImage(config.getContainerImage());
        docker.setNetwork("BRIDGE");
        List<Port> ports = new ArrayList<>();
        for (int p : config.getContainerPorts()) {
            Port port = new Port();
            port.setContainerPort(p);
            ports.add(port);
        }
        docker.setPortMappings(ports);

        Container container = new Container();
        container.setType("DOCKER");
        container.setDocker(docker);
        app.setContainer(container);

        List<HealthCheck> healthChecks = new ArrayList<>();
        for (org.apache.aries.containers.HealthCheck hc : config.getHealthChecks()) {
            HealthCheck healthCheck = new HealthCheck();
            healthCheck.setProtocol(hc.getType().toString());
            healthCheck.setGracePeriodSeconds(hc.getGracePeriod());
            healthCheck.setIntervalSeconds(hc.getInterval());
            healthCheck.setMaxConsecutiveFailures(hc.getMaxFailures());
            healthCheck.setTimeoutSeconds(hc.getTimeout());

            switch (hc.getType()) {
            case HTTP:
                healthCheck.setPath(hc.getParameters());
                // Fallthrough as the other params are the same as TCP
            case TCP:
                healthCheck.setPort(hc.getPort());
                healthCheck.setPortIndex(hc.getPortIndex());
                break;
            case COMMAND:
                Command command = new Command();
                command.setValue(hc.getParameters());
                healthCheck.setCommand(command);
                break;
            default:
                throw new OperationNotSupportedException(hc.getType() + " health checks are not yet supported");
            }
            healthChecks.add(healthCheck);
        }
        app.setHealthChecks(healthChecks);

        App res = marathonClient.createApp(app);
        return createServiceFromApp(res, config);
    }