ELIOT Command Line Interface Commit
[eliot.git] / blueprints / common / elcli / elcli / cmd / util / centosinstaller.go
1 /*
2 Copyright 2019 The Kubeedge Authors.
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
17 package util
18
19 import (
20         "fmt"
21         "os/exec"
22
23         types "elcli/cmd/common"
24
25 )
26
27 //CentOS struct objects shall have information of the tools version to be installed
28 //on Hosts having Ubuntu OS.
29 //It implements OSTypeInstaller interface
30 type CentOS struct {
31         DockerVersion     string
32         KubernetesVersion string
33         IsEdgeNode        bool //True - Edgenode False - Cloudnode
34         K8SImageRepository string
35         K8SPodNetworkCidr  string
36 }
37
38 //SetDockerVersion sets the Docker version for the objects instance
39 func (c *CentOS) SetDockerVersion(version string) {
40         c.DockerVersion = version
41 }
42
43 //SetK8SVersionAndIsNodeFlag sets the K8S version for the objects instance
44 //It also sets if this host shall act as edge node or not
45 func (c *CentOS) SetK8SVersionAndIsNodeFlag(version string, flag bool) {
46         c.KubernetesVersion = version
47         c.IsEdgeNode = flag
48 }
49
50 //SetK8SImageRepoAndPodNetworkCidr sets the K8S image Repository and pod network
51 // cidr.
52 func (c *CentOS) SetK8SImageRepoAndPodNetworkCidr(repo, cidr string) {
53         c.K8SImageRepository = repo
54         c.K8SPodNetworkCidr = cidr
55 }
56
57 //IsDockerInstalled checks if docker is installed in the host or not
58 func (c *CentOS) IsDockerInstalled(string) (types.InstallState, error) {
59         //yum list installed | grep docker-ce | awk '{print $2}' | cut -d'-' -f 1
60         //18.06.1.ce
61
62         return types.VersionNAInRepo, nil
63         
64 }
65
66 //InstallDocker will install the specified docker in the host
67 func (c *CentOS) InstallDocker() error {
68         fmt.Println("InstallDocker called")
69         // yum install -y yum-utils
70         // yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
71         // yum makecache
72         // yum list --showduplicates 'docker-ce' | grep '17.06.0' | head -1 | awk '{print $2}'
73         // yum install -y docker-ce-17.06.0.ce-1.el7.centos
74         // [root@localhost ~]# systemctl start docker
75         // [root@localhost ~]# ---> Always restart  systemctl restart docker
76         // [root@localhost ~]#
77         // IF downgrade yum downgrade -y docker-ce-17.06.0.ce-1.el7.centos
78         // Check always for version, if it is a downgrade or upgrade
79
80         return nil
81 }
82
83 //IsToolVerInRepo checks if the tool mentioned in available in OS repo or not
84 func (c *CentOS) IsToolVerInRepo(toolName, version string) (bool, error) {
85         //yum --cacheonly list | grep openssl
86         //For K8S, dont check in repo, just install
87         fmt.Println("IsToolVerInRepo called")
88         return false, nil
89 }
90
91 //InstallMQTT checks if MQTT is already installed and running, if not then install it from OS repo
92 //Information is used from https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-centos-7
93 func (c *CentOS) InstallMQTT() error {
94
95         //yum -y install epel-release
96         cmd := &Command{Cmd: exec.Command("sh", "-c", "yum -y install epel-release")}
97         err := cmd.ExecuteCmdShowOutput()
98         stdout := cmd.GetStdOutput()
99         errout := cmd.GetStdErr()
100         if err != nil || errout != "" {
101                 return fmt.Errorf("%s", errout)
102         }
103         fmt.Println(stdout)
104
105         //yum -y install mosquitto      
106         cmd = &Command{Cmd: exec.Command("sh", "-c", "yum -y install mosquitto")}
107         err = cmd.ExecuteCmdShowOutput()
108         stdout = cmd.GetStdOutput()
109         errout = cmd.GetStdErr()
110         if err != nil || errout != "" {
111                 return fmt.Errorf("%s", errout)
112         }
113         fmt.Println(stdout)
114
115
116         //systemctl start mosquitto
117         cmd = &Command{Cmd: exec.Command("sh", "-c", "systemctl start mosquitto")}
118         cmd.ExecuteCommand()
119         stdout = cmd.GetStdOutput()
120         errout = cmd.GetStdErr()
121         if errout != "" {
122                 return fmt.Errorf("%s", errout)
123         }
124         fmt.Println(stdout)
125
126         //systemctl enable mosquitto
127         cmd = &Command{Cmd: exec.Command("sh", "-c", "systemctl enable mosquitto")}
128         cmd.ExecuteCommand()
129         stdout = cmd.GetStdOutput()
130         errout = cmd.GetStdErr()
131         if errout != "" {
132                 return fmt.Errorf("%s", errout)
133         }
134         fmt.Println(stdout)
135
136
137         return nil
138 }
139
140 //IsK8SComponentInstalled checks if said K8S version is already installed in the host
141 func (c *CentOS) IsK8SComponentInstalled(component, defVersion string) (types.InstallState, error) {
142         //      [root@localhost ~]# yum list installed | grep kubeadm | awk '{print $2}' | cut -d'-' -f 1
143         // 1.14.1
144         // [root@localhost ~]#
145         // [root@localhost ~]# yum list installed | grep kubeadm
146         // kubeadm.x86_64                          1.14.1-0                       @kubernetes
147         // [root@localhost ~]#
148
149         return types.VersionNAInRepo, nil
150 }
151
152 //InstallK8S will install kubeadm, kudectl and kubelet for the cloud node
153 func (c *CentOS) InstallK8S() error {
154         fmt.Println("InstallK8S called")
155         //Follow https://kubernetes.io/docs/setup/independent/install-kubeadm/
156         return nil
157 }
158
159 //StartK8Scluster will do "kubeadm init" and cluster will be started
160 func (c *CentOS) StartK8Scluster() error {
161         return nil
162 }