blob: fade52e7002b5f763ba24c3da15247948198500f [file] [log] [blame]
Jian Li138f51f2021-01-06 03:29:58 +09001/*
2 * Copyright 2020-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.kubevirtnode.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.kubevirtnode.api.KubevirtNode;
25import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService;
26
27import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE;
28import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT;
29
30/**
31 * Initializes nodes for node service.
32 */
33@Service
34@Command(scope = "onos", name = "kubevirt-init-node",
35 description = "Initializes nodes for KubeVirt node service")
36public class KubevirtInitNodeCommand extends AbstractShellCommand {
37
38 @Option(name = "-a", aliases = "--all", description = "Apply this command to all nodes",
39 required = false, multiValued = false)
40 private boolean isAll = false;
41
42 @Option(name = "-i", aliases = "--incomplete",
43 description = "Apply this command to incomplete nodes",
44 required = false, multiValued = false)
45 private boolean isIncomplete = false;
46
47 @Argument(index = 0, name = "hostnames", description = "Hostname(s) to apply this command",
48 required = false, multiValued = true)
49 @Completion(KubevirtHostnameCompleter.class)
50 private String[] hostnames = null;
51
52 @Override
53 protected void doExecute() throws Exception {
54 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
55
56 if ((!isAll && !isIncomplete && hostnames == null) ||
57 (isAll && isIncomplete) ||
58 (isIncomplete && hostnames != null) ||
59 (hostnames != null && isAll)) {
60 print("Please specify one of hostname, --all, and --incomplete options.");
61 return;
62 }
63
64 if (isAll) {
65 hostnames = service.nodes().stream()
66 .map(KubevirtNode::hostname).toArray(String[]::new);
67 } else if (isIncomplete) {
68 hostnames = service.nodes().stream()
69 .filter(node -> node.state() != COMPLETE)
70 .map(KubevirtNode::hostname).toArray(String[]::new);
71 }
72
73 for (String hostname : hostnames) {
74 KubevirtNode node = service.node(hostname);
75 if (node == null) {
76 print("Unable to find %s", hostname);
77 continue;
78 }
79 print("Initializing %s", hostname);
80 KubevirtNode updated = node.updateState(INIT);
81 service.updateNode(updated);
82 }
83 print("Done.");
84 }
85}