Search in sources :

Example 6 with ServicePorts

use of com.spotify.helios.common.descriptors.ServicePorts in project helios by spotify.

the class TaskConfig method registration.

public ServiceRegistration registration() throws InterruptedException {
    final ServiceRegistration.Builder builder = ServiceRegistration.newBuilder();
    for (final Map.Entry<ServiceEndpoint, ServicePorts> entry : job.getRegistration().entrySet()) {
        final ServiceEndpoint registration = entry.getKey();
        final ServicePorts servicePorts = entry.getValue();
        for (final Entry<String, ServicePortParameters> portEntry : servicePorts.getPorts().entrySet()) {
            final String portName = portEntry.getKey();
            final ServicePortParameters portParameters = portEntry.getValue();
            final PortMapping mapping = job.getPorts().get(portName);
            if (mapping == null) {
                log.error("no '{}' port mapped for registration: '{}'", portName, registration);
                continue;
            }
            final Integer externalPort;
            if (mapping.getExternalPort() != null) {
                // Use the statically assigned port if one is specified
                externalPort = mapping.getExternalPort();
            } else {
                // Otherwise use the dynamically allocated port
                externalPort = ports.get(portName);
            }
            if (externalPort == null) {
                log.error("no external '{}' port for registration: '{}'", portName, registration);
                continue;
            }
            builder.endpoint(registration.getName(), registration.getProtocol(), externalPort, fullyQualifiedRegistrationDomain(), host, portParameters.getTags(), endpointHealthCheck(portName));
        }
    }
    return builder.build();
}
Also used : ServicePorts(com.spotify.helios.common.descriptors.ServicePorts) ServicePortParameters(com.spotify.helios.common.descriptors.ServicePortParameters) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) ServiceRegistration(com.spotify.helios.serviceregistration.ServiceRegistration)

Example 7 with ServicePorts

use of com.spotify.helios.common.descriptors.ServicePorts in project helios by spotify.

the class CliDeploymentTest method testDeployAndUndeployJob.

@Test
public void testDeployAndUndeployJob() throws Exception {
    startDefaultMaster();
    // Wait for master to come up
    Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<String>() {

        @Override
        public String call() throws Exception {
            final String output = cli("masters");
            return output.contains(masterName()) ? output : null;
        }
    });
    startDefaultAgent(testHost());
    final String image = BUSYBOX;
    final Map<String, PortMapping> ports = ImmutableMap.of("foo", PortMapping.of(4711), "bar", PortMapping.of(5000, externalPort));
    final Map<ServiceEndpoint, ServicePorts> registration = ImmutableMap.of(ServiceEndpoint.of("foo-service", "tcp"), ServicePorts.of("foo"), ServiceEndpoint.of("bar-service", "http"), ServicePorts.of("bar"));
    final Map<String, String> env = ImmutableMap.of("BAD", "f00d");
    // Wait for agent to come up
    awaitHostRegistered(testHost(), LONG_WAIT_SECONDS, SECONDS);
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Create job
    final JobId jobId = createJob(testJobName, testJobVersion, image, IDLE_COMMAND, env, ports, registration);
    // Query for job
    final Job expected = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(image).setCommand(IDLE_COMMAND).setEnv(env).setPorts(ports).setRegistration(registration).setCreatingUser(TEST_USER).build();
    final String inspectOutput = cli("inspect", "--json", jobId.toString());
    final Job parsed = Json.read(inspectOutput, Job.class);
    assertJobEquals(expected, parsed);
    assertThat(cli("jobs", testJobName, "-q"), containsString(jobId.toString()));
    assertThat(cli("jobs", testJobName + ":" + testJobVersion, "-q"), containsString(jobId.toString()));
    assertEquals("job pattern foozbarz matched no jobs", cli("jobs", "foozbarz").trim());
    assertTrue(cli("jobs", "foozbarz", "-q").isEmpty());
    // Create a new job using the first job as a template
    final Job expectedCloned = expected.toBuilder().setVersion(expected.getId().getVersion() + "-cloned").build();
    final JobId clonedJobId = JobId.parse(WHITESPACE.trimFrom(cli("create", "-q", "-t", testJobName + ":" + testJobVersion, testJobName + ":" + testJobVersion + "-cloned")));
    final String clonedInspectOutput = cli("inspect", "--json", clonedJobId.toString());
    final Job clonedParsed = Json.read(clonedInspectOutput, Job.class);
    assertJobEquals(expectedCloned, clonedParsed);
    // Verify that port mapping and environment variables are correct
    final String statusString = cli("status", "--job", jobId.toString(), "--json");
    final Map<JobId, JobStatus> statuses = Json.read(statusString, STATUSES_TYPE);
    final Job job = statuses.get(jobId).getJob();
    assertEquals(ServicePorts.of("foo"), job.getRegistration().get(ServiceEndpoint.of("foo-service", "tcp")));
    assertEquals(ServicePorts.of("bar"), job.getRegistration().get(ServiceEndpoint.of("bar-service", "http")));
    assertEquals(4711, job.getPorts().get("foo").getInternalPort());
    assertEquals(PortMapping.of(5000, externalPort), job.getPorts().get("bar"));
    assertEquals("f00d", job.getEnv().get("BAD"));
    final String duplicateJob = cli("create", testJobName + ":" + testJobVersion, image, "--", IDLE_COMMAND);
    assertThat(duplicateJob, containsString("JOB_ALREADY_EXISTS"));
    final String prestop = stopJob(jobId, testHost());
    assertThat(prestop, containsString("JOB_NOT_DEPLOYED"));
    // Deploy job
    deployJob(jobId, testHost());
    // Stop job
    final String stop1 = stopJob(jobId, BOGUS_HOST);
    assertThat(stop1, containsString("HOST_NOT_FOUND"));
    final String stop2 = stopJob(BOGUS_JOB, testHost());
    assertThat(stop2, containsString("Unknown job"));
    final String stop3 = stopJob(jobId, testHost());
    assertThat(stop3, containsString(testHost() + ": done"));
    // Verify that undeploying the job from a nonexistent host fails
    assertThat(cli("undeploy", jobId.toString(), BOGUS_HOST), containsString("HOST_NOT_FOUND"));
    // Verify that undeploying a nonexistent job from the host fails
    assertThat(cli("undeploy", BOGUS_JOB.toString(), testHost()), containsString("Unknown job"));
    // Undeploy job
    undeployJob(jobId, testHost());
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) JobStatus(com.spotify.helios.common.descriptors.JobStatus) ServicePorts(com.spotify.helios.common.descriptors.ServicePorts) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Job(com.spotify.helios.common.descriptors.Job) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 8 with ServicePorts

use of com.spotify.helios.common.descriptors.ServicePorts in project helios by spotify.

the class CliJobCreationTest method testMergeFileAndCliArgsEnvPrecedence.

@Test
public void testMergeFileAndCliArgsEnvPrecedence() throws Exception {
    final String redundantEnvKey = "BAD";
    final Map<String, PortMapping> ports = ImmutableMap.of("foo", PortMapping.of(4711), "bar", PortMapping.of(5000, externalPort));
    final Map<ServiceEndpoint, ServicePorts> registration = ImmutableMap.of(ServiceEndpoint.of("foo-service", "tcp"), ServicePorts.of("foo"), ServiceEndpoint.of("bar-service", "http"), ServicePorts.of("bar"));
    final Map<String, String> env = ImmutableMap.of(redundantEnvKey, "f00d");
    final Map<String, String> volumes = Maps.newHashMap();
    volumes.put("/etc/spotify/secret-keys.yaml:ro", "/etc/spotify/secret-keys.yaml");
    // Create a new job, serialize it to JSON
    final Job.Builder builder = Job.newBuilder().setCommand(Lists.newArrayList("server", "foo-service.yaml")).setEnv(env).setPorts(ports).setRegistration(registration).setVolumes(volumes).setCreatingUser(TEST_USER);
    final Job job = builder.build();
    final String jobConfigJsonString = job.toJsonString();
    // Create temporary job config file
    final File file = temporaryFolder.newFile();
    final String absolutePath = file.getAbsolutePath();
    // Write JSON config to temp file
    try (final FileOutputStream outFile = new FileOutputStream(file)) {
        outFile.write(Charsets.UTF_8.encode(jobConfigJsonString).array());
        // Create job and specify the temp file.
        cli("create", "--file", absolutePath, testJobNameAndVersion, BUSYBOX, "--env", redundantEnvKey + "=FOOD");
        // Inspect the job.
        final String actualJobConfigJson = cli("inspect", testJobNameAndVersion, "--json");
        // Compare to make sure the created job has the expected configuration,
        // i.e. the configuration resulting from a merge of the JSON file and CLI args.
        final Job actualJob = Json.read(actualJobConfigJson, Job.class);
        final Job.Builder actualJobBuilder = actualJob.toBuilder();
        builder.setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setEnv(ImmutableMap.of(redundantEnvKey, "FOOD"));
        assertJobEquals(builder.build(), actualJobBuilder.build());
    }
}
Also used : ServicePorts(com.spotify.helios.common.descriptors.ServicePorts) FileOutputStream(java.io.FileOutputStream) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Job(com.spotify.helios.common.descriptors.Job) File(java.io.File) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) Test(org.junit.Test)

Example 9 with ServicePorts

use of com.spotify.helios.common.descriptors.ServicePorts in project helios by spotify.

the class ConfigFileJobCreationTest method test.

@Test
public void test() throws Exception {
    startDefaultMaster();
    final HeliosClient client = defaultClient();
    final String name = testJobName;
    final String version = "17";
    final String image = BUSYBOX;
    final Map<String, PortMapping> ports = ImmutableMap.of("foo", PortMapping.of(4711), "bar", PortMapping.of(5000, externalPort));
    final Map<ServiceEndpoint, ServicePorts> registration = ImmutableMap.of(ServiceEndpoint.of("foo-service", "tcp"), ServicePorts.of("foo"), ServiceEndpoint.of("bar-service", "http"), ServicePorts.of("bar"));
    final Map<String, String> env = ImmutableMap.of("BAD", "f00d");
    final Map<String, Object> configuration = ImmutableMap.of("id", name + ":" + version, "image", image, "ports", ports, "registration", registration, "env", env);
    final Path file = temporaryFolder.newFile().toPath();
    Files.write(file, Json.asBytes(configuration));
    final String output = cli("create", "-q", "-f", file.toAbsolutePath().toString());
    final JobId jobId = JobId.parse(WHITESPACE.trimFrom(output));
    final Map<JobId, Job> jobs = client.jobs().get();
    final Job job = jobs.get(jobId);
    assertEquals(name, job.getId().getName());
    assertEquals(version, job.getId().getVersion());
    assertEquals(ports, job.getPorts());
    assertEquals(env, job.getEnv());
    assertEquals(registration, job.getRegistration());
}
Also used : Path(java.nio.file.Path) HeliosClient(com.spotify.helios.client.HeliosClient) ServicePorts(com.spotify.helios.common.descriptors.ServicePorts) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Job(com.spotify.helios.common.descriptors.Job) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Aggregations

PortMapping (com.spotify.helios.common.descriptors.PortMapping)9 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)9 ServicePorts (com.spotify.helios.common.descriptors.ServicePorts)9 Job (com.spotify.helios.common.descriptors.Job)6 Test (org.junit.Test)6 ImmutableMap (com.google.common.collect.ImmutableMap)3 JobId (com.spotify.helios.common.descriptors.JobId)3 File (java.io.File)3 Map (java.util.Map)3 HeliosClient (com.spotify.helios.client.HeliosClient)2 ServicePortParameters (com.spotify.helios.common.descriptors.ServicePortParameters)2 ServiceRegistration (com.spotify.helios.serviceregistration.ServiceRegistration)2 FileOutputStream (java.io.FileOutputStream)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 JobStatus (com.spotify.helios.common.descriptors.JobStatus)1 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)1 Endpoint (com.spotify.helios.serviceregistration.ServiceRegistration.Endpoint)1 Path (java.nio.file.Path)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1