Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snode.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.Completion; |
| 21 | import org.apache.karaf.shell.api.action.Option; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.k8snode.api.K8sNode; |
| 25 | import org.onosproject.k8snode.api.K8sNodeAdminService; |
| 26 | import org.onosproject.k8snode.api.K8sNodeService; |
| 27 | |
| 28 | import static org.onosproject.k8snode.api.K8sNodeState.COMPLETE; |
| 29 | import 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") |
| 37 | public 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 | } |