From 612d5d98684d42e102d1aa8f0eccf47752c49e56 Mon Sep 17 00:00:00 2001 From: Domas Monkus Date: Thu, 8 Sep 2022 17:03:15 +0300 Subject: [PATCH 01/11] Separate leo and tpi. --- Makefile | 4 ++-- cmd/{ => leo}/create/create.go | 0 cmd/{ => leo}/delete/delete.go | 0 cmd/{ => leo}/list/list.go | 0 cmd/leo/main.go | 15 +++++++++++++++ cmd/{ => leo}/read/read.go | 0 cmd/{ => leo}/root.go | 24 ++++++++---------------- cmd/{ => leo}/stop/stop.go | 0 main.go => cmd/tpi/main.go | 13 ++----------- 9 files changed, 27 insertions(+), 29 deletions(-) rename cmd/{ => leo}/create/create.go (100%) rename cmd/{ => leo}/delete/delete.go (100%) rename cmd/{ => leo}/list/list.go (100%) create mode 100644 cmd/leo/main.go rename cmd/{ => leo}/read/read.go (100%) rename cmd/{ => leo}/root.go (91%) rename cmd/{ => leo}/stop/stop.go (100%) rename main.go => cmd/tpi/main.go (63%) diff --git a/Makefile b/Makefile index c1cf0489..b5735605 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,8 @@ default: build build: go build -install: - GOBIN=${INSTALL_PATH} go install +install_tpi: + GOBIN=${INSTALL_PATH} go install ./cmd/tpi test: go test ./... ${TESTARGS} -timeout=30s -parallel=4 diff --git a/cmd/create/create.go b/cmd/leo/create/create.go similarity index 100% rename from cmd/create/create.go rename to cmd/leo/create/create.go diff --git a/cmd/delete/delete.go b/cmd/leo/delete/delete.go similarity index 100% rename from cmd/delete/delete.go rename to cmd/leo/delete/delete.go diff --git a/cmd/list/list.go b/cmd/leo/list/list.go similarity index 100% rename from cmd/list/list.go rename to cmd/leo/list/list.go diff --git a/cmd/leo/main.go b/cmd/leo/main.go new file mode 100644 index 00000000..715edc06 --- /dev/null +++ b/cmd/leo/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "os" + "terraform-provider-iterative/iterative/utils" +) + +func main() { + defer utils.WaitForAnalyticsAndHandlePanics() + cmd := NewCmd() + err := cmd.Execute() + if err != nil { + os.Exit(1) + } +} diff --git a/cmd/read/read.go b/cmd/leo/read/read.go similarity index 100% rename from cmd/read/read.go rename to cmd/leo/read/read.go diff --git a/cmd/root.go b/cmd/leo/root.go similarity index 91% rename from cmd/root.go rename to cmd/leo/root.go index e1d4ef54..91931087 100644 --- a/cmd/root.go +++ b/cmd/leo/root.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "fmt" @@ -11,13 +11,12 @@ import ( "github.com/spf13/pflag" "github.com/spf13/viper" + "terraform-provider-iterative/cmd/leo/create" + "terraform-provider-iterative/cmd/leo/delete" + "terraform-provider-iterative/cmd/leo/list" + "terraform-provider-iterative/cmd/leo/read" + "terraform-provider-iterative/cmd/leo/stop" "terraform-provider-iterative/task/common" - - "terraform-provider-iterative/cmd/create" - "terraform-provider-iterative/cmd/delete" - "terraform-provider-iterative/cmd/list" - "terraform-provider-iterative/cmd/read" - "terraform-provider-iterative/cmd/stop" ) type Options struct { @@ -27,15 +26,8 @@ type Options struct { common.Cloud } -func Execute() { - cmd := New() - err := cmd.Execute() - if err != nil { - os.Exit(1) - } -} - -func New() *cobra.Command { +// NewCmd initializes the subcommand structure. +func NewCmd() *cobra.Command { o := Options{ Cloud: common.Cloud{ Timeouts: common.Timeouts{ diff --git a/cmd/stop/stop.go b/cmd/leo/stop/stop.go similarity index 100% rename from cmd/stop/stop.go rename to cmd/leo/stop/stop.go diff --git a/main.go b/cmd/tpi/main.go similarity index 63% rename from main.go rename to cmd/tpi/main.go index b1867aac..52ea8965 100644 --- a/main.go +++ b/cmd/tpi/main.go @@ -1,24 +1,15 @@ package main import ( - "os" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" - "terraform-provider-iterative/iterative/utils" "terraform-provider-iterative/iterative" - "terraform-provider-iterative/cmd" + "terraform-provider-iterative/iterative/utils" ) func main() { - defer utils.WaitForAnalyticsAndHandlePanics() - - if os.Getenv(plugin.Handshake.MagicCookieKey) != plugin.Handshake.MagicCookieValue { - cmd.Execute() - return - } - + defer utils.WaitForAnalyticsAndHandlePanics() plugin.Serve(&plugin.ServeOpts{ ProviderFunc: func() *schema.Provider { return iterative.Provider() From 1112ff4f5fe4a4fddf4715620f62ff22bd49f35b Mon Sep 17 00:00:00 2001 From: Domas Monkus Date: Fri, 9 Sep 2022 11:12:15 +0300 Subject: [PATCH 02/11] Update goreleaser config to build leo and tpi separately. --- .goreleaser.yml | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 31a2e035..ee6d0c3b 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,5 +1,8 @@ builds: -- env: +- + id: "tpi" + main: ./cmd/tpi + env: - CGO_ENABLED=0 mod_timestamp: '{{ .CommitTimestamp }}' flags: @@ -7,9 +10,6 @@ builds: ldflags: - -s -w - -X terraform-provider-iterative/iterative/utils.Version={{.Version}} - - -X main.version={{.Version}} - - -X main.commit={{.Commit}} - - -X main.date={{.Date}} goos: # - freebsd - windows @@ -27,14 +27,43 @@ builds: goarch: arm64 - goos: windows goarch: arm - binary: '{{ .ProjectName }}_v{{ .Version }}' + binary: 'terraform-provider-iterative' +- + id: "leo" + main: ./cmd/leo + env: + - CGO_ENABLED=0 + mod_timestamp: '{{ .CommitTimestamp }}' + flags: + - -trimpath + ldflags: + - -s -w + - -X terraform-provider-iterative/iterative/utils.Version={{.Version}} + goos: + # - freebsd + - windows + - linux + - darwin + goarch: + - amd64 + - '386' + - arm + - arm64 + ignore: + - goos: darwin + goarch: '386' + - goos: windows + goarch: arm64 + - goos: windows + goarch: arm + binary: 'leo' archives: - id: default format: zip - name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}' + name_template: '{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}' - id: agent format: binary - name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}' + name_template: '{{ .Binary }}_{{ .Os }}_{{ .Arch }}' checksum: name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS' algorithm: sha256 @@ -43,7 +72,7 @@ checksum: signs: - artifacts: checksum args: - # if you are using this is a GitHub action or some other automated pipeline, you + # if you are using this is a GitHub action or some other automated pipeline, you # need to pass the batch flag to indicate its not interactive. - "--batch" - "--output" From f0cb41008b79588e20b5b58f68d3e668eac84f48 Mon Sep 17 00:00:00 2001 From: Domas Monkus Date: Fri, 9 Sep 2022 12:28:43 +0300 Subject: [PATCH 03/11] Add dist to .gitignore. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 928af133..ff0f28bf 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ bin /*.tfstate* /crash.log +# Build artefacts +/dist + # Ignore any .tfvars files that are generated automatically for each Terraform run. Most # .tfvars files are managed as part of configuration and so should be included in # version control. From ee5a0bbee54980f4182f071484329634452f0166 Mon Sep 17 00:00:00 2001 From: Domas Monkus Date: Fri, 9 Sep 2022 12:29:36 +0300 Subject: [PATCH 04/11] Use leo name. --- cmd/leo/root.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/leo/root.go b/cmd/leo/root.go index 91931087..dbd526ec 100644 --- a/cmd/leo/root.go +++ b/cmd/leo/root.go @@ -40,10 +40,9 @@ func NewCmd() *cobra.Command { } cmd := &cobra.Command{ - Use: "task", + Use: "leo", Short: "Run code in the cloud", - Long: `Task is a command-line tool that allows - data scientists to run code in the cloud.`, + Long: `leo is a command-line tool that allows data scientists to run code in the cloud.`, } cmd.AddCommand(create.New(&o.Cloud)) From cd38434edfa04b1e320ca48caebe011c988fd085 Mon Sep 17 00:00:00 2001 From: Domas Monkus Date: Fri, 9 Sep 2022 12:35:42 +0300 Subject: [PATCH 05/11] Minor Makefile update. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b5735605..ffc8c1b5 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ INSTALL_PATH=~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/ default: build build: - go build + go build ./... install_tpi: GOBIN=${INSTALL_PATH} go install ./cmd/tpi From fe87be575096e42a0b5a7642037d5036568a88bc Mon Sep 17 00:00:00 2001 From: Domas Monkus Date: Fri, 9 Sep 2022 12:37:54 +0300 Subject: [PATCH 06/11] Update machine script. --- task/common/machine/machine-script.sh.tpl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/task/common/machine/machine-script.sh.tpl b/task/common/machine/machine-script.sh.tpl index 2711b322..7726653d 100755 --- a/task/common/machine/machine-script.sh.tpl +++ b/task/common/machine/machine-script.sh.tpl @@ -14,7 +14,7 @@ source /opt/task/credentials if ! test -z "$CI"; then cml rerun-workflow fi -(systemctl is-system-running | grep stopping) || tpi stop --cloud="$TPI_TASK_CLOUD_PROVIDER" --region="$TPI_TASK_CLOUD_REGION" "$TPI_TASK_IDENTIFIER"; +(systemctl is-system-running | grep stopping) || leo stop --cloud="$TPI_TASK_CLOUD_PROVIDER" --region="$TPI_TASK_CLOUD_REGION" "$TPI_TASK_IDENTIFIER"; END chmod u=rwx,g=rx,o=rx /usr/bin/tpi-task-shutdown @@ -61,10 +61,10 @@ sudo tee /etc/systemd/system/tpi-task.service > /dev/null < Date: Fri, 9 Sep 2022 13:02:50 +0300 Subject: [PATCH 07/11] Update golden files. --- .../common/machine/testdata/machine_script_full.golden | 10 +++++----- .../machine/testdata/machine_script_minimal.golden | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/task/common/machine/testdata/machine_script_full.golden b/task/common/machine/testdata/machine_script_full.golden index 292dd61c..26480580 100644 --- a/task/common/machine/testdata/machine_script_full.golden +++ b/task/common/machine/testdata/machine_script_full.golden @@ -14,7 +14,7 @@ source /opt/task/credentials if ! test -z "$CI"; then cml rerun-workflow fi -(systemctl is-system-running | grep stopping) || tpi stop --cloud="$TPI_TASK_CLOUD_PROVIDER" --region="$TPI_TASK_CLOUD_REGION" "$TPI_TASK_IDENTIFIER"; +(systemctl is-system-running | grep stopping) || leo stop --cloud="$TPI_TASK_CLOUD_PROVIDER" --region="$TPI_TASK_CLOUD_REGION" "$TPI_TASK_IDENTIFIER"; END chmod u=rwx,g=rx,o=rx /usr/bin/tpi-task-shutdown @@ -61,10 +61,10 @@ sudo tee /etc/systemd/system/tpi-task.service > /dev/null < /dev/null < Date: Mon, 12 Sep 2022 14:39:51 +0300 Subject: [PATCH 08/11] Keep terraform-provider-iterative in package root. --- .goreleaser.yml | 2 +- Makefile | 2 +- cmd/tpi/main.go => main.go | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename cmd/tpi/main.go => main.go (100%) diff --git a/.goreleaser.yml b/.goreleaser.yml index ee6d0c3b..ae2e7c7c 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,7 +1,7 @@ builds: - id: "tpi" - main: ./cmd/tpi + main: ./main.go env: - CGO_ENABLED=0 mod_timestamp: '{{ .CommitTimestamp }}' diff --git a/Makefile b/Makefile index ffc8c1b5..c9549dee 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ build: go build ./... install_tpi: - GOBIN=${INSTALL_PATH} go install ./cmd/tpi + GOBIN=${INSTALL_PATH} go install ./... test: go test ./... ${TESTARGS} -timeout=30s -parallel=4 diff --git a/cmd/tpi/main.go b/main.go similarity index 100% rename from cmd/tpi/main.go rename to main.go From a16955a9afe8930f207c290c2107862c90f72943 Mon Sep 17 00:00:00 2001 From: Domas Monkus Date: Thu, 15 Sep 2022 16:33:49 +0300 Subject: [PATCH 09/11] Keep using existing tpi release in machine script. --- task/common/machine/machine-script.sh.tpl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/task/common/machine/machine-script.sh.tpl b/task/common/machine/machine-script.sh.tpl index 290c2d67..72cdcbcd 100755 --- a/task/common/machine/machine-script.sh.tpl +++ b/task/common/machine/machine-script.sh.tpl @@ -61,7 +61,8 @@ sudo tee /etc/systemd/system/tpi-task.service > /dev/null < Date: Thu, 15 Sep 2022 16:55:14 +0300 Subject: [PATCH 10/11] Update golden files. --- task/common/machine/testdata/machine_script_full.golden | 3 ++- task/common/machine/testdata/machine_script_minimal.golden | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/task/common/machine/testdata/machine_script_full.golden b/task/common/machine/testdata/machine_script_full.golden index ded623fb..4b108905 100644 --- a/task/common/machine/testdata/machine_script_full.golden +++ b/task/common/machine/testdata/machine_script_full.golden @@ -61,7 +61,8 @@ sudo tee /etc/systemd/system/tpi-task.service > /dev/null < /dev/null < Date: Fri, 16 Sep 2022 16:34:26 +0300 Subject: [PATCH 11/11] Address review comments. --- cmd/leo/main.go | 2 -- task/common/machine/machine-script.sh.tpl | 2 +- task/common/machine/testdata/machine_script_full.golden | 2 +- task/common/machine/testdata/machine_script_minimal.golden | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/cmd/leo/main.go b/cmd/leo/main.go index 715edc06..52c5ed26 100644 --- a/cmd/leo/main.go +++ b/cmd/leo/main.go @@ -2,11 +2,9 @@ package main import ( "os" - "terraform-provider-iterative/iterative/utils" ) func main() { - defer utils.WaitForAnalyticsAndHandlePanics() cmd := NewCmd() err := cmd.Execute() if err != nil { diff --git a/task/common/machine/machine-script.sh.tpl b/task/common/machine/machine-script.sh.tpl index 72cdcbcd..7f7cc5e6 100755 --- a/task/common/machine/machine-script.sh.tpl +++ b/task/common/machine/machine-script.sh.tpl @@ -63,7 +63,7 @@ END curl --location --remote-name https://github.com/iterative/terraform-provider-iterative/releases/latest/download/terraform-provider-iterative_linux_amd64 # TODO: replace download location with https://github.com/iterative/terraform-provider-iterative/releases/latest/download/leo_linux_amd64 -sudo mv leo* /usr/bin/leo +sudo mv terraform-provider-iterative* /usr/bin/leo sudo chmod u=rwx,g=rx,o=rx /usr/bin/leo sudo chown root:root /usr/bin/leo diff --git a/task/common/machine/testdata/machine_script_full.golden b/task/common/machine/testdata/machine_script_full.golden index 4b108905..26d1a270 100644 --- a/task/common/machine/testdata/machine_script_full.golden +++ b/task/common/machine/testdata/machine_script_full.golden @@ -63,7 +63,7 @@ END curl --location --remote-name https://github.com/iterative/terraform-provider-iterative/releases/latest/download/terraform-provider-iterative_linux_amd64 # TODO: replace download location with https://github.com/iterative/terraform-provider-iterative/releases/latest/download/leo_linux_amd64 -sudo mv leo* /usr/bin/leo +sudo mv terraform-provider-iterative* /usr/bin/leo sudo chmod u=rwx,g=rx,o=rx /usr/bin/leo sudo chown root:root /usr/bin/leo diff --git a/task/common/machine/testdata/machine_script_minimal.golden b/task/common/machine/testdata/machine_script_minimal.golden index e2171c60..a87c2b45 100644 --- a/task/common/machine/testdata/machine_script_minimal.golden +++ b/task/common/machine/testdata/machine_script_minimal.golden @@ -63,7 +63,7 @@ END curl --location --remote-name https://github.com/iterative/terraform-provider-iterative/releases/latest/download/terraform-provider-iterative_linux_amd64 # TODO: replace download location with https://github.com/iterative/terraform-provider-iterative/releases/latest/download/leo_linux_amd64 -sudo mv leo* /usr/bin/leo +sudo mv terraform-provider-iterative* /usr/bin/leo sudo chmod u=rwx,g=rx,o=rx /usr/bin/leo sudo chown root:root /usr/bin/leo