github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/internal/requirements/helpers.go (about) 1 package requirements 2 3 import ( 4 "fmt" 5 "github.com/go-ini/ini" 6 "github.com/hazelops/ize/pkg/term" 7 "github.com/pterm/pterm" 8 "io" 9 "log" 10 "net/http" 11 "os" 12 "os/exec" 13 "runtime" 14 ) 15 16 func CheckCommand(command string, subcommand []string) (bool, string) { 17 cmd := exec.Command(command, subcommand...) 18 19 out, _, _, err := term.New().Run(cmd) 20 if err != nil { 21 return false, out 22 } 23 24 return true, out 25 } 26 27 const ( 28 ssmLinuxUrl = "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/%s_%s/session-manager-plugin%s" 29 ssmMacOsUrl = "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip" 30 ) 31 32 func downloadSSMAgentPlugin() error { 33 switch goos := runtime.GOOS; goos { 34 case "darwin": 35 client := http.Client{ 36 CheckRedirect: func(r *http.Request, via []*http.Request) error { 37 r.URL.Opaque = r.URL.Path 38 return nil 39 }, 40 } 41 42 file, err := os.Create("session-manager-plugin.deb") 43 if err != nil { 44 log.Fatal(err) 45 } 46 47 resp, err := client.Get(ssmMacOsUrl) 48 if err != nil { 49 log.Fatal(err) 50 } 51 52 defer resp.Body.Close() 53 54 _, err = io.Copy(file, resp.Body) 55 if err != nil { 56 return err 57 } 58 59 defer file.Close() 60 case "linux": 61 distroName, err := ReadOSRelease("/etc/os-release") 62 if err != nil { 63 return err 64 } 65 66 arch := "" 67 68 switch runtime.GOARCH { 69 case "amd64": 70 arch = "64bit" 71 case "386": 72 arch = "32bit" 73 case "arm": 74 arch = "arm32" 75 case "arm64": 76 arch = "arm64" 77 } 78 79 client := http.Client{ 80 CheckRedirect: func(r *http.Request, via []*http.Request) error { 81 r.URL.Opaque = r.URL.Path 82 return nil 83 }, 84 } 85 86 switch distroName["ID"] { 87 case "ubuntu", "debian": 88 file, err := os.Create("session-manager-plugin.deb") 89 if err != nil { 90 log.Fatal(err) 91 } 92 93 defer file.Close() 94 95 resp, err := client.Get(fmt.Sprintf(ssmLinuxUrl, "ubuntu", arch, ".deb")) 96 if err != nil { 97 log.Fatal(err) 98 } 99 100 defer resp.Body.Close() 101 102 _, err = io.Copy(file, resp.Body) 103 if err != nil { 104 return err 105 } 106 default: 107 file, err := os.Create("session-manager-plugin.rpm") 108 if err != nil { 109 log.Fatal(err) 110 } 111 112 defer file.Close() 113 114 resp, err := client.Get(fmt.Sprintf(ssmLinuxUrl, "linux", arch, ".rpm")) 115 if err != nil { 116 log.Fatal(err) 117 } 118 119 defer resp.Body.Close() 120 121 _, err = io.Copy(file, resp.Body) 122 if err != nil { 123 return err 124 } 125 } 126 default: 127 return fmt.Errorf("unable to install automatically") 128 } 129 130 return nil 131 } 132 133 func cleanupSSMAgent() error { 134 command := []string{} 135 136 if runtime.GOOS == "darwin" { 137 command = []string{"rm", "-f", "sessionmanager-bundle sessionmanager-bundle.zip"} 138 } else if runtime.GOOS == "linux" { 139 distroName, err := ReadOSRelease("/etc/os-release") 140 if err != nil { 141 return err 142 } 143 switch distroName["ID"] { 144 case "ubuntu", "debian": 145 command = []string{"rm", "-rf", "session-manager-plugin.deb"} 146 default: 147 command = []string{"rm", "-f", "session-manager-plugin.rpm"} 148 } 149 } 150 151 cmd := exec.Command(command[0], command[1:]...) 152 153 err := cmd.Run() 154 if err != nil { 155 return err 156 } 157 158 return nil 159 } 160 161 func installSSMAgent() error { 162 command := []string{} 163 164 if runtime.GOOS == "darwin" { 165 command = []string{"sudo", "./sessionmanager-bundle/install", "-i /usr/local/sessionmanagerplugin", "-b", "/usr/local/bin/session-manager-plugin"} 166 } else if runtime.GOOS == "linux" { 167 command = []string{"sudo", "yum", "install", "-y", "-q", "session-manager-plugin.deb"} 168 169 distroName, err := ReadOSRelease("/etc/os-release") 170 if err != nil { 171 return err 172 } 173 switch distroName["ID"] { 174 case "ubuntu", "debian": 175 command = []string{"sudo", "dpkg", "-i", "session-manager-plugin.deb"} 176 case "fedora": 177 command = []string{"sudo", "dnf", "install", "session-manager-plugin.rpm"} 178 case "rhel": 179 command = []string{"sudo", "yum", "install", "session-manager-plugin.rpm"} 180 } 181 } else { 182 return fmt.Errorf("automatic installation of SSM Agent for your OS is not supported") 183 } 184 185 cmd := exec.Command(command[0], command[1:]...) 186 187 out, err := cmd.CombinedOutput() 188 if err != nil { 189 return err 190 } 191 192 pterm.Info.Println(string(out)) 193 194 return nil 195 } 196 197 func ReadOSRelease(configfile string) (map[string]string, error) { 198 cfg, err := ini.Load(configfile) 199 if err != nil { 200 return nil, err 201 } 202 203 ConfigParams := make(map[string]string) 204 ConfigParams["ID"] = cfg.Section("").Key("ID").String() 205 206 return ConfigParams, nil 207 }