Unit test for docker local health checks.
[aries-containers.git] / containers-examples / containers-example-javaapp / src / main / java / org / apache / aries / containers / examples / javaapp / Main.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.aries.containers.examples.javaapp;
20
21 import java.util.Scanner;
22
23 import org.apache.aries.containers.HealthCheck;
24 import org.apache.aries.containers.HealthCheck.Type;
25 import org.apache.aries.containers.Service;
26 import org.apache.aries.containers.ServiceConfig;
27 import org.apache.aries.containers.ServiceManager;
28 import org.apache.aries.containers.docker.local.impl.LocalDockerServiceManager;
29
30 public class Main {
31 public static void main(String [] args) {
32 try {
33 ServiceConfig sc = ServiceConfig.builder("mytesthttpd", "httpd").
34 cpu(0.2).
35 memory(32).
36 port(80).
37 healthCheck(HealthCheck.builder(Type.HTTP).parameters("/index.html").portIndex(0).
38 build()).
39 build();
40
41 ServiceManager sm = new LocalDockerServiceManager();
42
43 // If you want to run with Marathon, use the following line
44 // ServiceManager sm = new MarathonServiceManager("http://192.168.99.100:8080/");
45
46 System.out.println("Currently known services: " + sm.listServices());
47
48 Service svc = sm.getService(sc);
49
50 int desiredCount;
51 try (Scanner scanner = new Scanner(System.in)) {
52 do {
53 System.out.println("Containers: (" + svc.getActualInstanceCount() + ")");
54 svc.listContainers().stream().map(s -> " " + s).forEach(System.out::println);
55
56 System.out.print("\nEnter desired container count (-1 = refresh, 0 = destroy): ");
57 desiredCount = scanner.nextInt();
58 if (desiredCount > 0) {
59 svc.setInstanceCount(desiredCount);
60 } else if (desiredCount == -1) {
61 svc.refresh();
62 }
63 } while (desiredCount != 0);
64 }
65
66 svc.destroy();
67 System.out.println("Service Destroyed");
68
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72 }
73 }