From a7518b96f973d2abcb9152470880e699c12d221e Mon Sep 17 00:00:00 2001 From: Alex Steel <130377221+asteel-gsa@users.noreply.github.com> Date: Fri, 3 May 2024 13:17:56 -0400 Subject: [PATCH] Add an install aws cli function So that we allow this package to manage aws installation on cloud.gov when it needs it. --- internal/util/installaws.go | 49 +++++++++++++++++++++++++++++++++++++ main.go | 10 +++++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 internal/util/installaws.go diff --git a/internal/util/installaws.go b/internal/util/installaws.go new file mode 100644 index 0000000..0b3b0d3 --- /dev/null +++ b/internal/util/installaws.go @@ -0,0 +1,49 @@ +package util + +import ( + "os" + "strings" + + "github.com/bitfield/script" + "gov.gsa.fac.cgov-util/internal/logging" +) + +func InstallAWS() { + // curl -x $https_proxy -L "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" + getaws := []string{ + "curl", + "-x", + os.Getenv("https_proxy"), + "-L", + "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip", + "-o", + "awscliv2.zip", + } + curlaws := strings.Join(getaws[:], " ") + logging.Logger.Printf("Fetching aws cli via curl...") + script.Exec(curlaws) + + // unzip awscliv2.zip && rm awscliv2.zip + unzipaws := []string{ + "unzip", + "awscliv2.zip", + "&&", + "rm", + "awscliv2.zip", + } + logging.Logger.Printf("Unzipping aws cli...") + unzip := strings.Join(unzipaws[:], " ") + script.Exec(unzip) + + // ./aws/install -i ~/usr -b ~/bin + installaws := []string{ + "./aws/install", + "-i", + "~/usr", + "-b", + "~/bin", + } + logging.Logger.Printf("Installing aws to bin...") + install := strings.Join(installaws[:], " ") + script.Exec(install) +} diff --git a/main.go b/main.go index 041b28d..8e265f5 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( "golang.org/x/exp/slices" "gov.gsa.fac.cgov-util/cmd" + "gov.gsa.fac.cgov-util/internal/logging" + "gov.gsa.fac.cgov-util/internal/util" "gov.gsa.fac.cgov-util/internal/vcap" ) @@ -34,7 +36,13 @@ func readConfig() { } func main() { - + if slices.Contains([]string{"DEV", "PREVIEW", "STAGING", "PRODUCTION"}, os.Getenv("ENV")) { + logging.Logger.Printf("ENV detected to be a cloud.gov environment. Installing AWS CLI.") + util.InstallAWS() + } else { + logging.Logger.Printf("ENV set to local, aws not necessary to install.") + } readConfig() + util.SetPaths(os.Getenv("ENV")) cmd.Execute() }