summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/restapi/legacy/ProvisionResource.java
blob: 867d45b4fcd7afacb13c2995ceeda2ab3dc75b7c (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
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.restapi.legacy;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.container.jaxrs.annotation.Component;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.Node.State;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.node.Configuration;
import com.yahoo.vespa.hosted.provision.node.NodeFlavors;
import com.yahoo.vespa.hosted.provision.restapi.NodeStateSerializer;
import com.yahoo.vespa.hosted.provision.restapi.legacy.ContainersForHost.DockerContainer;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * The provisioning web service used by the provisioning controller to provide nodes to a node repository.
 *
 * @author mortent
 */
@Path("/provision")
@Produces(MediaType.APPLICATION_JSON)
public class ProvisionResource {
    private static final Logger log = Logger.getLogger(ProvisionResource.class.getName());

    private final NodeRepository nodeRepository;

    private final NodeFlavors nodeFlavors;

    public ProvisionResource(@Component NodeRepository nodeRepository, @Component NodeFlavors nodeFlavors) {
        super();
        this.nodeRepository = nodeRepository;
        this.nodeFlavors = nodeFlavors;
    }


    @POST
    @Path("/node")
    @Consumes(MediaType.APPLICATION_JSON)
    public void addNodes(List<HostInfo> hostInfoList) {
        List<Node> nodes = new ArrayList<>();
        for (HostInfo hostInfo : hostInfoList)
            nodes.add(nodeRepository.createNode(hostInfo.openStackId, hostInfo.hostname, Optional.empty(), new Configuration(nodeFlavors.getFlavorOrThrow(hostInfo.flavor)), Node.Type.tenant));
        nodeRepository.addNodes(nodes);
    }

    @GET
    @Path("/node/required")
    public ProvisionStatus getStatus() {
        ProvisionStatus provisionStatus = new ProvisionStatus();
        provisionStatus.requiredNodes = 0; // This concept has no meaning any more ...
        provisionStatus.decomissionNodes = toHostInfo(nodeRepository.getInactive());
        provisionStatus.failedNodes = toHostInfo(nodeRepository.getFailed());

        return provisionStatus;
    }

    private List<HostInfo> toHostInfo(List<Node> nodes) {
        List<HostInfo> hostInfoList = new ArrayList<>(nodes.size());
        for (Node node : nodes)
            hostInfoList.add(HostInfo.createHostInfo(node.hostname(), node.openStackId(), "medium"));
        return hostInfoList;
    }


    @PUT
    @Path("/node/ready")
    public void setReady(String hostName) {
        if ( nodeRepository.getNode(Node.State.ready, hostName).isPresent()) return; // node already 'ready'

        Optional<Node> node = nodeRepository.getNode(Node.State.provisioned, hostName);
        if ( ! node.isPresent())
            node = nodeRepository.getNode(Node.State.dirty, hostName);
        if ( ! node.isPresent())
            throw new IllegalArgumentException("Could not set " + hostName + " ready: Not registered as provisioned or dirty");

        nodeRepository.setReady(Collections.singletonList(node.get()));
    }

    @GET
    @Path("/node/usage/{tenantId}")
    public TenantStatus getTenantUsage(@PathParam("tenantId") String tenantId) {
        TenantStatus ts = new TenantStatus();
        ts.tenantId = tenantId;
        ts.allocated = nodeRepository.getNodeCount(tenantId, Node.State.active);
        ts.reserved = nodeRepository.getNodeCount(tenantId, Node.State.reserved);

        Map<String, TenantStatus.ApplicationUsage> appinstanceUsageMap = new HashMap<>();

        nodeRepository.getNodes(Node.Type.tenant, Node.State.active).stream()
                .filter(node -> {
                    return node.allocation().get().owner().tenant().value().equals(tenantId);
                })
                .forEach(node -> {
                    ApplicationId owner = node.allocation().get().owner();
                    appinstanceUsageMap.merge(
                            String.format("%s:%s", owner.application().value(), owner.instance().value()),
                            TenantStatus.ApplicationUsage.create(owner.application().value(), owner.instance().value(), 1),
                            (a, b) -> {
                                a.usage += b.usage;
                                return a;
                            }
                    );
                });

        ts.applications = new ArrayList<>(appinstanceUsageMap.values());
        return ts;
    }

    //TODO: move this to nodes/v2/ when the spec for this has been nailed.
    //TODO: Change it to list host nodes, instead of hosts for tenant nodes.
    @GET
    @Path("/dockerhost/{hostname}")
    public ContainersForHost getContainersForHost(@PathParam("hostname") String hostname) {
        List<DockerContainer> dockerContainersForHost =
                nodeRepository.getNodes(Node.Type.tenant, State.active, State.inactive).stream()
                        .filter(runsOnDockerHost(hostname))
                        .flatMap(ProvisionResource::toDockerContainer)
                        .collect(Collectors.toList());

        return new ContainersForHost(dockerContainersForHost);
    }

    //returns stream since there is no conversion from optional to stream in java.
    private static Stream<DockerContainer> toDockerContainer(Node node) {
        try {
            String dockerImage = node.allocation().get().membership().cluster().dockerImage().orElseThrow(() ->
                    new Exception("Docker image not set for node " + node));

            return Stream.of(new DockerContainer(
                    node.hostname(),
                    dockerImage,
                    NodeStateSerializer.wireNameOf(node.state()),
                    node.allocation().get().restartGeneration().wanted(),
                    node.allocation().get().restartGeneration().current()));
        } catch (Exception e) {
            log.log(LogLevel.ERROR, "Ignoring docker container.", e);
            return Stream.empty();
        }
    }

    private static Predicate<Node> runsOnDockerHost(String hostname) {
        return node -> node.parentHostname().map(hostname::equals).orElse(false);
    }
}