aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-controller-service-docker/dcae-controller-service-docker-adaptor/src/main/java/org/openecomp/dcae/controller/service/servers/docker/DcaeDockerServiceProvider.java
blob: 6d27af3817090e7a34d74a557b87bf5ada2d794b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*-
 * ============LICENSE_START==========================================
 * OPENECOMP - DCAE
 * ===================================================================
 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 */

package org.openecomp.dcae.controller.service.servers.docker;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.json.JSONObject;
import org.openecomp.dcae.controller.core.service.HealthTestResponse;
import org.openecomp.dcae.controller.core.service.HealthTestStatus;
import org.openecomp.dcae.controller.core.service.ServiceFactory;
import org.openecomp.dcae.controller.core.stream.DcaeStream;
import org.openecomp.dcae.controller.core.stream.DmaapStream;
import org.openecomp.dcae.controller.service.docker.DockerService;
import org.openecomp.dcae.controller.service.docker.DockerServiceInstance;
import org.openecomp.dcae.controller.service.docker.host.servers.manager.DcaeDockerHostManagerConsole;
import org.openecomp.dcae.controller.service.docker.host.servers.service.DcaeDockerHostServiceProvider;
import org.openecomp.dcae.controller.service.docker.host.service.DockerHostServiceInstance;
import org.openecomp.dcae.controller.service.vm.VirtualMachineService;
import org.openecomp.ncomp.core.DeploymentStatus;
import org.openecomp.ncomp.docker.ContainerOptions;
import org.openecomp.ncomp.docker.ContainerPortBindings;
import org.openecomp.ncomp.docker.DockerContainer;
import org.openecomp.ncomp.docker.DockerFactory;
import org.openecomp.ncomp.servers.docker.DockerDockerHostConsole;
import org.openecomp.ncomp.sirius.manager.AbstractClient;
import org.openecomp.ncomp.sirius.manager.BasicAdaptorProvider;
import org.openecomp.ncomp.sirius.manager.ISiriusProvider;
import org.openecomp.ncomp.sirius.manager.ISiriusServer;
import org.openecomp.ncomp.sirius.manager.JavaHttpClient;
import org.openecomp.ncomp.sirius.manager.ManagementServer;
import org.openecomp.ncomp.sirius.manager.ManagementServerUtils;
import org.openecomp.ncomp.sirius.manager.Subject;
import org.openecomp.ncomp.sirius.manager.metrics.MetricStore;

import java.util.Date;

public class DcaeDockerServiceProvider extends BasicAdaptorProvider {
    private static final Logger logger = Logger.getLogger(DcaeDockerServiceProvider.class);
    DockerService o;

    public DcaeDockerServiceProvider(ISiriusServer controller, DockerService o) {
        super(controller, o);
        this.o = o;
    }

    public void deploy(String instanceName, String containerPath) {
        DockerServiceInstance i = findInstance(instanceName);
        if (!(containerPath == null || containerPath.equals(""))) {
            Subject s = controller.getServer().find(containerPath);
            if (s == null || s.o == null) {
                throw new RuntimeException("Unable to find container object: " + containerPath);
            }
            if (!(s.o instanceof DockerHostServiceInstance)) {
                throw new RuntimeException("Unable to deploy to this non docker host container: " + containerPath);
            }
            i.setHostService((DockerHostServiceInstance) s.o);
        }
        if (i.getHostService() == null) {
            throw new RuntimeException("Null Container: unable to deploy: " + o.getName() + " " + i.getName());
        }
        i.setDockerName(dockerName(i));
        ContainerOptions opts = DockerFactory.eINSTANCE.createContainerOptions();
        opts.setName(i.getDockerName());
        opts.getVolumes().addAll(i.getVolumes());
        opts.getLinks().addAll(i.getLinks());
        opts.getEnv().addAll(i.getEnvs());
        for (ContainerPortBindings p : i.getPortBindings()) {
            opts.getPortBindings().add(EcoreUtil.copy(p));
        }
        console(instanceName).setupConfiguration(o.getName(), instanceName);
        consoleHost(i).dockerRunWithOptions("/resources/dockerHost", image(i), opts);
        long toolate = System.currentTimeMillis() + 15 * 60 * 1000;
        // wait 15 minutes for container manager
        while (i.getManagerPortNumber() > 0 && System.currentTimeMillis() < toolate) {
            try {
                o.pushManagerConfiguration(instanceName);
                break;
            } catch (Exception e) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e1) {
                    throw new RuntimeException("Deployment timeout: Deployment interrupted");
                }
            }
            if (System.currentTimeMillis() >= toolate) {
                throw new RuntimeException("Unable to configure Docker");
            }
        }
    }

    private String image(DockerServiceInstance i) {
        if (i.getImage().contains("/")) {
            return i.getImage();
        }
        String dns = i.getHostService().getServers().get(0).getNetworks().get(0).getDnsName();
        return dns + ":7113/" + i.getImage();
    }

    public void undeploy(String instanceName) {
        DockerServiceInstance i = findInstance(instanceName);
        DockerDockerHostConsole c = consoleHost(i);
//		c.stopContainer("/resources/dockerHost", i.getDockerName(), 10);
//		c.killContainer("/resources/dockerHost", i.getDockerName(), "KILL");
        c.removeContainer("/resources/dockerHost", i.getDockerName(), true, true);
    }

    private DcaeDockerHostManagerConsole console(String instanceName) {
        DockerServiceInstance i = findInstance(instanceName);
        return new DcaeDockerHostManagerConsole(getClient(i));
    }

    public AbstractClient getClient(DockerServiceInstance i) {
        DockerHostServiceInstance instance = i.getHostService();
        VirtualMachineService service = instance.getService();
        DcaeDockerHostServiceProvider provider = (DcaeDockerHostServiceProvider) ((ISiriusProvider) service).getSiriusProvider();
        return provider.getClient(instance);
    }

    private DockerDockerHostConsole consoleHost(DockerServiceInstance i) {
        DockerHostServiceInstance instance = i.getHostService();
        VirtualMachineService service = instance.getService();
        DcaeDockerHostServiceProvider provider = (DcaeDockerHostServiceProvider) ((ISiriusProvider) service).getSiriusProvider();
        AbstractClient c = provider.getClient(instance);
        return new DockerDockerHostConsole(c);
    }

    private String dockerName(DockerServiceInstance i) {
        return "dcae_" + o.getName() + "_" + i.getName();
        // return "dcae"+ new Date().getTime();
    }

    protected DockerServiceInstance findInstance(String instanceName) {
        for (DockerServiceInstance ii : o.getInstances()) {
            if (ii.getName().equals(instanceName)) {
                ii.setDockerName(dockerName(ii));
                return ii;
            }
        }
        throw new RuntimeException("Unable to find instance: " + instanceName);

    }

    public HealthTestResponse test(String instanceName) {
        DockerServiceInstance i = findInstance(instanceName);
        DockerContainer c = findContainer(i);
        HealthTestResponse h = ServiceFactory.eINSTANCE.createHealthTestResponse();
        if (c == null) {
            h.setStatus(HealthTestStatus.YELLOW);
            h.setMessageCode("Container not Deployed");
            return h;
        }
        System.out.println("DOCKER: test" + i.getDockerName() + " " + c.getStatus());
        if (c.getStatus().startsWith("Exited")) {
            h.setStatus(HealthTestStatus.RED);
            h.setMessageCode("Container not running");
            return h;
        }
        if (c.getStatus().startsWith("Created")) {
            h.setStatus(HealthTestStatus.RED);
            h.setMessageCode("Container never started");
            return h;
        }
        if (c.getStatus().startsWith("Restarting")) {
            h.setStatus(HealthTestStatus.RED);
            h.setMessageCode("Container restarting");
            return h;
        }
        if (i.getManagerPortNumber() > 0) {
            System.out.println("DOCKER: test" + i.getDockerName() + "HERE");
            return console(instanceName).testContainer(i.getDockerName());
        }
        h.setStatus(HealthTestStatus.GREEN);
        return h;
    }

    public void suspend(String instanceName) {
        DockerServiceInstance i = findInstance(instanceName);
        console(instanceName).suspendContainer(i.getDockerName());
    }

    public void resume(String instanceName) {
        DockerServiceInstance i = findInstance(instanceName);
        console(instanceName).resumeContainer(i.getDockerName());
    }

    public void pushManagerConfiguration(String instanceName) {
        DockerServiceInstance i = findInstance(instanceName);
        DcaeDockerHostManagerConsole console = console(instanceName);
        EObject config = o.managerConfiguration(instanceName);
        System.out.println("DDDDD " + ManagementServer.ecore2json(config, 1000, null, true).toString(2));
        EList<DcaeStream> l1 = new BasicEList<DcaeStream>();
        l1.addAll(EcoreUtil.copyAll(i.getInputStreams()));
        EList<DcaeStream> l2 = new BasicEList<DcaeStream>();
        l2.addAll(EcoreUtil.copyAll(i.getOutputStreams()));
        console.updateContainerConfiguration(i.getDockerName(), config, decrypt(l1), decrypt(l2));
    }

    private EList<DcaeStream> decrypt(EList<DcaeStream> l) {
        for (DcaeStream s : l) {
            if (s instanceof DmaapStream) {
                DmaapStream s1 = (DmaapStream) s;
                String pw = s1.getDmaapPassword();
                if (pw != null && pw.startsWith("rsa:")) {
                    try {
                        s1.setDmaapPassword(JavaHttpClient.decryptPassword(pw));
                    } catch (Exception e) {
                        System.err.println("ZZZZZ: bad pw: " + ManagementServer.object2ref(s));
                        s1.setDmaapPassword("decryption error");
                    }
                } else {
                    System.err.println("ZZZZZ: unencrypted pw: " + ManagementServer.object2ref(s));
                }
                continue;
            }
            System.err.println("ZZZZZ: unknown type: " + s.getClass().getName());
        }
        return l;
    }

    public void pollManagerConfiguration(String instanceName) {

        // TODO IMPLEMENT
        throw new UnsupportedOperationException();
    }

    public org.eclipse.emf.ecore.EObject managerConfiguration(String instanceName) {
        // TODO IMPLEMENT
        throw new UnsupportedOperationException();
    }

    public JSONObject managerOperation(String instanceName, String operation, JSONObject parameters) {
        // TODO IMPLEMENT
        throw new UnsupportedOperationException();
    }

    public void updateConfigurationFromPolicy(String instanceName) {

        // TODO IMPLEMENT
        throw new UnsupportedOperationException();
    }

    public void runHealthTests() {
        for (DockerServiceInstance i : o.getInstances()) {
            HealthTestResponse s = null;
            try {
                if (i.getStatus() == DeploymentStatus.DEPLOYED) {
                    s = test(i.getName());
                } else {
                    s = ServiceFactory.eINSTANCE.createHealthTestResponse();
                    s.setMessageCode("Not deployed");
                    s.setStatus(HealthTestStatus.YELLOW);
                }
            } catch (Exception e) {
                s = ServiceFactory.eINSTANCE.createHealthTestResponse();
                s.setMessageCode("Unable to determine health: " + e);
                s.setStatus(HealthTestStatus.RED);
            }
            i.setHealthTestStatus(s.getStatus());
            i.setHealthTestMessageCode(s.getMessageCode());
            MetricStore store = controller.getServer().metrics.getStore(i);
            Date now = new Date();
            String path = "/lastHealthTest";
            store.addLongValue(path, now, now.getTime(), "DateMetricAttribute", false);
        }
    }

    public void start() {

        Thread t = new Thread("docker service: " + o.getName()) {
            @Override
            public void run() {
                while (true) {
                    try {
                        if (!controller.getServer().isSlave) {
                            updateDeploymentStatus();
                        }
                        Thread.sleep(60000); // sleep 60 seconds
                    } catch (Exception e) {
                        ManagementServerUtils.printStackTrace(e);
                        logger.fatal("vm service: " + o.getName() + " : " + e);
                        try {
                            Thread.sleep(30000);
                        } catch (InterruptedException e1) {
                        }
                    }
                }
            }

        };
        t.start();
    }

    public void updateDeploymentStatus() {
        for (DockerServiceInstance instance : o.getInstances()) {
            instance.setDockerName(dockerName(instance));
            DockerContainer c = findContainer(instance);
            if (c != null) {
                if (c.getStatus() != null && c.getStatus().startsWith("Up")) {
                    instance.setStatus(DeploymentStatus.DEPLOYED);
                } else {
                    instance.setStatus(DeploymentStatus.DEPLOYED_PARTIALLY);
                }
            } else {
                instance.setStatus(DeploymentStatus.UNDEPLOYED);
            }
        }
    }

    private DockerContainer findContainer(DockerServiceInstance instance) {
        if (instance.getHostService() == null) {
            return null;
        }
        for (DockerContainer c : instance.getHostService().getDockerHost().getContainers()) {
            if (instance.getDockerName() != null && instance.getDockerName().equals(c.getName())) {
                return c;
            }
        }
        return null;
    }

    protected EObject convertToManagerConfiguration(DockerServiceInstance i) {
        // TODO Auto-generated method stub
        return null;
    }
}