blob: a414e15978579c0f1ce1a5df6a6b25c1717f20fa [file] [log] [blame]
Jian Lif16e8852019-01-22 22:55:31 +09001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.k8snode.cli;
17
18import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Completion;
21import org.apache.karaf.shell.api.action.Option;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.k8snode.api.K8sNode;
25import org.onosproject.k8snode.api.K8sNodeAdminService;
26import org.onosproject.k8snode.api.K8sNodeService;
27
28import static org.onosproject.k8snode.api.K8sNodeState.COMPLETE;
29import static org.onosproject.k8snode.api.K8sNodeState.INIT;
30
31/**
32 * Initializes nodes for node service.
33 */
34@Service
35@Command(scope = "onos", name = "k8s-node-init",
36 description = "Initializes nodes for kubernetes node service")
37public class K8sNodeInitCommand extends AbstractShellCommand {
38
39 @Option(name = "-a", aliases = "--all", description = "Apply this command to all nodes",
40 required = false, multiValued = false)
41 private boolean isAll = false;
42
43 @Option(name = "-i", aliases = "--incomplete",
44 description = "Apply this command to incomplete nodes",
45 required = false, multiValued = false)
46 private boolean isIncomplete = false;
47
48 @Argument(index = 0, name = "hostnames", description = "Hostname(s) to apply this command",
49 required = false, multiValued = true)
50 @Completion(K8sHostnameCompleter.class)
51 private String[] hostnames = null;
52
53 @Override
54 protected void doExecute() {
55 K8sNodeService nodeService = get(K8sNodeService.class);
56 K8sNodeAdminService nodeAdminService = get(K8sNodeAdminService.class);
57
58 if ((!isAll && !isIncomplete && hostnames == null) ||
59 (isAll && isIncomplete) ||
60 (isIncomplete && hostnames != null) ||
61 (hostnames != null && isAll)) {
62 print("Please specify one of hostname, --all, and --incomplete options.");
63 return;
64 }
65
66 if (isAll) {
67 hostnames = nodeService.nodes().stream()
68 .map(K8sNode::hostname).toArray(String[]::new);
69 } else if (isIncomplete) {
70 hostnames = nodeService.nodes().stream()
71 .filter(node -> node.state() != COMPLETE)
72 .map(K8sNode::hostname).toArray(String[]::new);
73 }
74
75 for (String hostname : hostnames) {
76 K8sNode node = nodeService.node(hostname);
77 if (node == null) {
78 print("Unable to find %s", hostname);
79 continue;
80 }
81 print("Initializing %s", hostname);
82 K8sNode updated = node.updateState(INIT);
83 nodeAdminService.updateNode(updated);
84 }
85 print("Done.");
86 }
87}