summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeadmin/NodeAdminMain.java
blob: f5f2deb8dcbf309fad2881b14c7ccf2669e27144 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.nodeadmin;

import com.yahoo.concurrent.classlock.ClassLocking;
import com.yahoo.vespa.defaults.Defaults;
import com.yahoo.vespa.hosted.dockerapi.Docker;
import com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper;

import java.io.File;
import java.util.Optional;

/**
 * NodeAdminMain is the main component of the node admin JDisc application:
 *  - It will read config and check its environment to figure out its responsibilities
 *  - It will "start" (only) the necessary components.
 *  - Other components MUST NOT try to start (typically in constructor) since the features
 *    they provide is NOT WANTED and possibly destructive, and/or the environment may be
 *    incompatible. For instance, trying to contact the Docker daemon too early will
 *    be fatal: the node admin may not have installed and started the docker daemon.
 */
public class NodeAdminMain implements AutoCloseable {
    private final Docker docker;
    private final MetricReceiverWrapper metricReceiver;
    private final ClassLocking classLocking;

    private Optional<DockerAdminComponent> dockerAdmin = Optional.empty();

    public NodeAdminMain(Docker docker,
                         MetricReceiverWrapper metricReceiver,
                         ClassLocking classLocking) {
        this.docker = docker;
        this.metricReceiver = metricReceiver;
        this.classLocking = classLocking;
    }

    @Override
    public void close() {
        dockerAdmin.ifPresent(DockerAdminComponent::disable);
    }

    public NodeAdminStateUpdater getNodeAdminStateUpdater() {
        return dockerAdmin.get().getNodeAdminStateUpdater();
    }

    public void start() {
        String staticConfigPath = Defaults.getDefaults().underVespaHome("conf/node-admin.json");
        NodeAdminConfig config = NodeAdminConfig.fromFile(new File(staticConfigPath));

        switch (config.mode) {
            case aws_tenant:
            case tenant:
                dockerAdmin = Optional.of(new DockerAdminComponent(
                        config,
                        docker,
                        metricReceiver,
                        classLocking));
                dockerAdmin.get().enable();
                return;
            case config_server_host:
                // TODO:
                //  - install and start docker daemon
                //  - Read config that specifies which containers to start how
                //  - use thin static backends for node repo and orchestrator
                //  - Start node admin state updater.
                return;
        }

        throw new IllegalStateException("Unknown bootstrap mode: " + config.mode.name());
    }
}