ELIOT Command Line Interface Commit
[eliot.git] / blueprints / common / elcli / elcli / cmd / init.go
1 // 
2 // Copyright © 2019 NAME HERE <EMAIL ADDRESS>
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 cmd
17
18 import (
19         "fmt"
20         
21
22         "github.com/spf13/cobra"
23
24         "elcli/cmd/util"
25 )
26
27 var (
28 elcliSetupCmdDescription = 
29 `
30 ELIOT init command is for setting up the ELIOT Cluster. 
31 The command has options to setup the complete setup which includes
32 ELIOT Manager and ELIOT Edge Nodes and to setup only ELIOT Manager
33 or ELIOT Edge Node. The command invokes setup.sh script which handles
34 the complete setup.
35
36 The Details of ELIOT Edge Nodes must be present in [nodelist] file.
37 `
38 )
39 // initCmd represents the init command
40 var initCmd = &cobra.Command{
41         Use:   "init",
42         Short: "Setup ELIOT Cluster !!",
43         Long:  elcliSetupCmdDescription,
44         //It will check if the kubernetes process is already running on the node. 
45         //Abort the operation if already running.
46         PreRunE: func(cmd *cobra.Command,args []string) error {
47                 isELIOTClusterRunning, err := util.IsK8SClusterRunning()
48                 if err != nil {
49                         return err
50                 } else if (isELIOTClusterRunning) {
51                         return fmt.Errorf("Kubernetes Cluster is running in the Node. Clean up the environment and then setup the Cluster")
52                 }
53                 return nil
54         },
55         RunE: func(cmd *cobra.Command, args []string) error{
56                 fmt.Println("init called")
57                 setupFlag := cmd.Flag("setup")
58                 setupflagoption := setupFlag.Value.String()
59
60                 switch setupflagoption {
61                 case "all":
62                         err:= util.EliotSetupAll()
63                         return err
64                         fmt.Println("Inside all option for setup flag")
65                 case "master":
66                         fmt.Println("Its eliot setup Master")
67                         err:= util.EliotSetupMaster()
68                         return err
69                 default:
70                         fmt.Println("Provide option for flag [--setup :- all | master] or [-s :- all | master]")                
71                 }
72                 return nil
73         },
74 }
75
76 func init() {
77         rootCmd.AddCommand(initCmd)
78         initCmd.Flags().StringP("setup","s","all","Eliot Topology setup options")
79
80 }