summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeadmin/NodeAdminMain.java
blob: 95eb9e150a2eaa0dd210c99e158977385fdc1ece (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
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
// 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.component.ComponentId;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.concurrent.classlock.ClassLocking;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.hosted.dockerapi.Docker;
import com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper;
import com.yahoo.vespa.hosted.node.admin.component.AdminComponent;
import com.yahoo.vespa.hosted.node.admin.config.ConfigServerConfig;
import com.yahoo.vespa.hosted.node.admin.component.DockerAdminComponent;
import com.yahoo.vespa.hosted.node.admin.provider.NodeAdminStateUpdater;

import java.io.File;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * 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 static final Logger logger = Logger.getLogger(NodeAdminMain.class.getName());

    private final ComponentRegistry<AdminComponent> adminRegistry;
    private final ConfigServerConfig configServerConfig;
    private final Docker docker;
    private final MetricReceiverWrapper metricReceiver;
    private final ClassLocking classLocking;

    private AdminComponent mainAdminComponent = null;

    public NodeAdminMain(ComponentRegistry<AdminComponent> adminRegistry,
                         ConfigServerConfig configServerConfig,
                         Docker docker,
                         MetricReceiverWrapper metricReceiver,
                         ClassLocking classLocking) {
        this.adminRegistry = adminRegistry;
        this.configServerConfig = configServerConfig;
        this.docker = docker;
        this.metricReceiver = metricReceiver;
        this.classLocking = classLocking;
    }

    public static NodeAdminConfig getConfig() {
        return NodeAdminConfig.fromFile(new File("/etc/vespa/node-admin.json"));
    }

    public void start() {
        NodeAdminConfig config = getConfig();
        mainAdminComponent = selectAdminComponent(config);
        mainAdminComponent.enable();
    }

    private AdminComponent selectAdminComponent(NodeAdminConfig config) {
        if (config.mainComponent == null) {
            return new DockerAdminComponent(configServerConfig, docker, metricReceiver, classLocking);
        }

        logger.log(LogLevel.INFO, () -> {
            String registeredComponentsList = adminRegistry
                    .allComponentsById().keySet().stream()
                    .map(ComponentId::stringValue)
                    .collect(Collectors.joining(", "));

            return String.format(
                    "Components registered = '%s', enabled = '%s'",
                    registeredComponentsList,
                    config.mainComponent);
        });

        AdminComponent component = adminRegistry.getComponent(config.mainComponent);
        if (component == null) {
            throw new IllegalArgumentException("There is no component named '" +
                    config.mainComponent + "'");
        }

        return component;
    }

    @Override
    public void close() {
        if (mainAdminComponent != null) {
            mainAdminComponent.disable();
            mainAdminComponent = null;
        }
    }

    public NodeAdminStateUpdater getNodeAdminStateUpdater() {
        assert mainAdminComponent != null : "start() hasn't been called yet";
        return mainAdminComponent.getNodeAdminStateUpdater();
    }
}