/
prepare.go
165 lines (149 loc) · 5.11 KB
/
prepare.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bytes"
"fmt"
"os/exec"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// prepareCmd represents the prepare command
var prepareCmd = &cobra.Command{
Use: "prepare",
Short: "Installs the dependencies for network setup",
Long: `
Installs the dependencies for network setup`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(fetchOsInfo()) //get os info
checkNetworkConn() //check network connection
installDocker() //install docker cc
installDockerCompose() //install docker cc
downloadDockerImage() //docker pull quorum image
},
}
func init() {
rootCmd.AddCommand(prepareCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// prepareCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// prepareCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func fetchOsInfo() string {
c := color.New(color.FgWhite, color.BgBlue) // create a new color object
var out bytes.Buffer //stores stdout data
fmt.Println("\nFetching OS Info...\n")
c.Print(" OS Info ")
fmt.Println()
cmdLSB := exec.Command("lsb_release", "-a") // define command to execute
cmdLSB.Stdout = &out
cmdLSB.Run() // execute the defined command
return out.String() // return stdout data string
}
func checkNetworkConn() {
c := color.New(color.FgWhite, color.BgBlue) // create a new color object
fmt.Println("Checking network connection...\n")
cmdCN := exec.Command("ping", "-c", "3", "4.2.2.1")
c.Print(" Network Status ")
err := cmdCN.Run()
if err == nil {
fmt.Print(" OK\n")
} else {
fmt.Print(" FAIL\n")
}
}
func installDocker() {
fmt.Println("\nInstalling Docker CE...")
downloadDockerSetupScr()
}
func downloadDockerSetupScr() {
cos := color.New(color.FgWhite, color.BgHiGreen)
cof := color.New(color.FgWhite, color.BgHiRed)
fmt.Println(" - Downloading docker setup script from get-docker.com")
cmdDSS := exec.Command("wget", "--tries=3", "get-docker.com", "-O", "docker-setup.sh")
err := cmdDSS.Run()
if err == nil {
fmt.Println(" - Docker setup script download complete - docker-setup.sh")
cmdEDS := exec.Command("sudo", "sh", "docker-setup.sh")
//cmdEDS:= exec.Command("ls")
fmt.Println(" - Executing docker-setup.sh")
err := cmdEDS.Run()
if err == nil {
fmt.Println()
cos.Print(" Docker CE installed ")
fmt.Println()
} else {
fmt.Println()
cof.Print(" Docker CE failed to install (Reason - Error while executing setup script) ")
fmt.Println()
}
} else {
fmt.Println()
cof.Print(" Docker CE failed to install (Reason - Could not download setup script) ")
fmt.Println()
}
}
func installDockerCompose() {
fmt.Println("\nInstalling Docker Compose...")
downloadDockerComposeScr()
}
func downloadDockerComposeScr() {
cos := color.New(color.FgWhite, color.BgHiGreen)
cof := color.New(color.FgWhite, color.BgHiRed)
fmt.Println(" - Downloading Docker Compose")
cmdDCS := exec.Command("/bin/sh", "-c", "wget --read-timeout=30 https://github.com/docker/compose/releases/download/1.21.1/docker-compose-$(uname -s)-$(uname -m) -O /usr/local/bin/docker-compose")
//cmdDCS:= exec.Command("ls")
cmdCHMOD := exec.Command("sudo", "chmod", "+x", "/usr/local/bin/docker-compose")
err := cmdDCS.Run()
if err != nil {
//cmdCHMOD.Run()
fmt.Println()
cof.Print(" Docker CE failed to install (Reason - Failed to download file) ")
fmt.Println()
} else {
cmdCHMOD.Run()
fmt.Println(" - Download complete")
dockVer, err := exec.Command("docker-compose", "-v").Output()
if err == nil {
fmt.Printf(" - Verifying installation OK %s", dockVer)
fmt.Println()
cos.Print(" Docker Composer installed ")
fmt.Println()
} else {
fmt.Println()
cof.Print(" Docker Composer failed to install ")
fmt.Println()
}
}
}
func downloadDockerImage() {
codis := color.New(color.FgWhite, color.BgHiGreen)
codfi := color.New(color.FgWhite, color.BgHiRed)
fmt.Println("\nDownloading docker image [xinfinorg/quorum:v2.1.0]...")
fmt.Println(" - Executing docker pull command")
cmdDID := exec.Command("docker", "pull", "xinfinorg/quorum:v2.1.0")
err := cmdDID.Run()
if err == nil {
fmt.Println()
codis.Print(" Docker image downloaded [xinfinorg/quorum:v2.1.0] ")
fmt.Print("\n\n")
} else {
fmt.Println()
codfi.Print(" Docker image download failed [xinfinorg/quorum:v2.1.0] ")
fmt.Print("\n\n")
}
}