-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add command to restart GCP VMs and implement related functionality #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
OliverTrautvetter
wants to merge
9
commits into
main
Choose a base branch
from
restart_vm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+528
−0
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e929c10
feat: add command to restart GCP VMs and implement related functionality
OliverTrautvetter 4ce7f10
Merge branch 'main' into restart_vm
OliverTrautvetter c463a08
chore(docs): Auto-update docs and licenses
OliverTrautvetter 923ffd9
feat: add unit tests for BootstrapGcpRestartVMsCmd and update GCE tes…
OliverTrautvetter 07293ed
Merge branch 'restart_vm' of https://github.com/codesphere-cloud/oms …
OliverTrautvetter 60810d1
Merge branch 'main' into restart_vm
OliverTrautvetter 033ad95
chore(docs): Auto-update docs and licenses
OliverTrautvetter b0e6fd8
feat: enhance error handling for GCP VM restart and validate flags in…
OliverTrautvetter 64a6f2d
Merge branch 'main' into restart_vm
OliverTrautvetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
|
|
||
| csio "github.com/codesphere-cloud/cs-go/pkg/io" | ||
| "github.com/codesphere-cloud/oms/internal/bootstrap" | ||
| "github.com/codesphere-cloud/oms/internal/bootstrap/gcp" | ||
| "github.com/codesphere-cloud/oms/internal/util" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type BootstrapGcpRestartVMsCmd struct { | ||
| cmd *cobra.Command | ||
| Opts *BootstrapGcpRestartVMsOpts | ||
| } | ||
|
|
||
| type BootstrapGcpRestartVMsOpts struct { | ||
| *GlobalOptions | ||
| ProjectID string | ||
| Zone string | ||
| Name string | ||
| } | ||
|
|
||
| func (c *BootstrapGcpRestartVMsCmd) RunE(_ *cobra.Command, args []string) error { | ||
| ctx := c.cmd.Context() | ||
| stlog := bootstrap.NewStepLogger(false) | ||
| fw := util.NewFilesystemWriter() | ||
|
|
||
| projectID := c.Opts.ProjectID | ||
| zone := c.Opts.Zone | ||
|
|
||
| // If only one of --project-id/--zone is provided, require both | ||
| if (projectID == "") != (zone == "") { | ||
| return fmt.Errorf("--project-id and --zone must be provided together") | ||
| } | ||
|
|
||
| if projectID == "" && zone == "" { | ||
| infraFilePath := gcp.GetInfraFilePath() | ||
| infraEnv, exists, err := gcp.LoadInfraFile(fw, infraFilePath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load infra file: %w", err) | ||
| } | ||
| if !exists { | ||
| return fmt.Errorf("infra file not found at %s; use --project-id and --zone flags", infraFilePath) | ||
| } | ||
| projectID = infraEnv.ProjectID | ||
| zone = infraEnv.Zone | ||
| } | ||
|
|
||
| if projectID == "" { | ||
| return fmt.Errorf("project ID is required; set --project-id and --zone or ensure the infra file exists") | ||
| } | ||
| if zone == "" { | ||
| return fmt.Errorf("zone is required; set --project-id and --zone or ensure the infra file exists") | ||
| } | ||
|
|
||
| gcpClient := gcp.NewGCPClient(ctx, stlog, os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")) | ||
|
|
||
| csEnv := &gcp.CodesphereEnvironment{ | ||
| ProjectID: projectID, | ||
| Zone: zone, | ||
| } | ||
|
|
||
| bs, err := gcp.NewGCPBootstrapper( | ||
| ctx, | ||
| nil, | ||
| stlog, | ||
| csEnv, | ||
| nil, | ||
| gcpClient, | ||
| fw, | ||
| nil, | ||
| nil, | ||
| util.NewTime(), | ||
| nil, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create bootstrapper: %w", err) | ||
| } | ||
|
|
||
| if c.Opts.Name != "" { | ||
| log.Printf("Restarting VM %s in project %s (zone %s)...", c.Opts.Name, projectID, zone) | ||
| if err := bs.RestartVM(c.Opts.Name); err != nil { | ||
| return fmt.Errorf("failed to restart VM: %w", err) | ||
| } | ||
| log.Printf("VM %s restarted successfully.", c.Opts.Name) | ||
| } else { | ||
| log.Printf("Restarting all VMs in project %s (zone %s)...", projectID, zone) | ||
| if err := bs.RestartVMs(); err != nil { | ||
| return fmt.Errorf("failed to restart VMs: %w", err) | ||
| } | ||
| log.Printf("All VMs restarted successfully.") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func AddBootstrapGcpRestartVMsCmd(bootstrapGcp *cobra.Command, opts *GlobalOptions) { | ||
| restartVMs := BootstrapGcpRestartVMsCmd{ | ||
| cmd: &cobra.Command{ | ||
| Use: "restart-vms", | ||
| Short: "Restart stopped or terminated GCP VMs", | ||
| Long: csio.Long(`Restarts GCP compute instances that were stopped or terminated, | ||
| for example after spot VM preemption. | ||
| By default, restarts all VMs defined in the infrastructure. | ||
| Use --name to restart a single VM. | ||
| Project ID and zone are read from the local infra file if available, | ||
| or can be specified via flags.`), | ||
| Example: formatExamples("beta bootstrap-gcp restart-vms", []csio.Example{ | ||
| {Desc: "Restart all VMs using project info from the local infra file"}, | ||
| {Cmd: "--name jumpbox", Desc: "Restart only the jumpbox VM"}, | ||
| {Cmd: "--name k0s-1", Desc: "Restart a specific k0s node"}, | ||
| {Cmd: "--project-id my-project --zone us-central1-a", Desc: "Restart all VMs with explicit project and zone"}, | ||
| {Cmd: "--project-id my-project --zone us-central1-a --name ceph-1", Desc: "Restart a specific VM with explicit project and zone"}, | ||
| }), | ||
| }, | ||
| Opts: &BootstrapGcpRestartVMsOpts{ | ||
| GlobalOptions: opts, | ||
| }, | ||
| } | ||
|
|
||
| flags := restartVMs.cmd.Flags() | ||
| flags.StringVar(&restartVMs.Opts.ProjectID, "project-id", "", "GCP Project ID (optional, will use infra file if not provided)") | ||
| flags.StringVar(&restartVMs.Opts.Zone, "zone", "", "GCP Zone (optional, will use infra file if not provided)") | ||
| flags.StringVar(&restartVMs.Opts.Name, "name", "", "Name of a specific VM to restart (e.g. jumpbox, postgres, ceph-1, k0s-1). Restarts all VMs if not specified.") | ||
|
|
||
| restartVMs.cmd.RunE = restartVMs.RunE | ||
| bootstrapGcp.AddCommand(restartVMs.cmd) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd_test | ||
|
|
||
| import ( | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/codesphere-cloud/oms/cli/cmd" | ||
| ) | ||
|
|
||
| var _ = Describe("BootstrapGcpRestartVMsCmd", func() { | ||
| var globalOpts *cmd.GlobalOptions | ||
|
|
||
| BeforeEach(func() { | ||
| globalOpts = &cmd.GlobalOptions{} | ||
| }) | ||
|
|
||
| Describe("BootstrapGcpRestartVMsOpts structure", func() { | ||
| Context("when initialized", func() { | ||
| It("should have correct default values", func() { | ||
| opts := &cmd.BootstrapGcpRestartVMsOpts{ | ||
| GlobalOptions: globalOpts, | ||
| } | ||
| Expect(opts.ProjectID).To(Equal("")) | ||
| Expect(opts.Zone).To(Equal("")) | ||
| Expect(opts.Name).To(Equal("")) | ||
| }) | ||
|
|
||
| It("should store provided values", func() { | ||
| opts := &cmd.BootstrapGcpRestartVMsOpts{ | ||
| GlobalOptions: globalOpts, | ||
| ProjectID: "my-project", | ||
| Zone: "us-central1-a", | ||
| Name: "jumpbox", | ||
| } | ||
| Expect(opts.ProjectID).To(Equal("my-project")) | ||
| Expect(opts.Zone).To(Equal("us-central1-a")) | ||
| Expect(opts.Name).To(Equal("jumpbox")) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("AddBootstrapGcpRestartVMsCmd", func() { | ||
| Context("when adding command", func() { | ||
| It("should not panic when adding to parent command", func() { | ||
| Expect(func() { | ||
| parentCmd := &cobra.Command{ | ||
| Use: "bootstrap-gcp", | ||
| } | ||
| cmd.AddBootstrapGcpRestartVMsCmd(parentCmd, globalOpts) | ||
| }).NotTo(Panic()) | ||
| }) | ||
|
|
||
| It("should create command with correct flags", func() { | ||
| parentCmd := &cobra.Command{ | ||
| Use: "bootstrap-gcp", | ||
| } | ||
| cmd.AddBootstrapGcpRestartVMsCmd(parentCmd, globalOpts) | ||
|
|
||
| restartCmd, _, err := parentCmd.Find([]string{"restart-vms"}) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(restartCmd).NotTo(BeNil()) | ||
| Expect(restartCmd.Use).To(Equal("restart-vms")) | ||
|
|
||
| projectIDFlag := restartCmd.Flags().Lookup("project-id") | ||
| Expect(projectIDFlag).NotTo(BeNil()) | ||
|
|
||
| zoneFlag := restartCmd.Flags().Lookup("zone") | ||
| Expect(zoneFlag).NotTo(BeNil()) | ||
|
|
||
| nameFlag := restartCmd.Flags().Lookup("name") | ||
| Expect(nameFlag).NotTo(BeNil()) | ||
| }) | ||
|
|
||
| It("should bind flag values to opts", func() { | ||
| parentCmd := &cobra.Command{ | ||
| Use: "bootstrap-gcp", | ||
| } | ||
| cmd.AddBootstrapGcpRestartVMsCmd(parentCmd, globalOpts) | ||
|
|
||
| restartCmd, _, err := parentCmd.Find([]string{"restart-vms"}) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(restartCmd).NotTo(BeNil()) | ||
|
|
||
| err = restartCmd.Flags().Set("project-id", "flag-project") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| projectIDVal, err := restartCmd.Flags().GetString("project-id") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(projectIDVal).To(Equal("flag-project")) | ||
|
|
||
| err = restartCmd.Flags().Set("zone", "flag-zone") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| zoneVal, err := restartCmd.Flags().GetString("zone") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(zoneVal).To(Equal("flag-zone")) | ||
|
|
||
| err = restartCmd.Flags().Set("name", "jumpbox") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
OliverTrautvetter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| nameVal, err := restartCmd.Flags().GetString("name") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(nameVal).To(Equal("jumpbox")) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| ## oms beta bootstrap-gcp restart-vms | ||
|
|
||
| Restart stopped or terminated GCP VMs | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Restarts GCP compute instances that were stopped or terminated, | ||
| for example after spot VM preemption. | ||
| By default, restarts all VMs defined in the infrastructure. | ||
| Use --name to restart a single VM. | ||
| Project ID and zone are read from the local infra file if available, | ||
| or can be specified via flags. | ||
|
|
||
| ``` | ||
| oms beta bootstrap-gcp restart-vms [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| # Restart all VMs using project info from the local infra file | ||
| $ oms beta bootstrap-gcp restart-vms | ||
|
|
||
| # Restart only the jumpbox VM | ||
| $ oms beta bootstrap-gcp restart-vms --name jumpbox | ||
|
|
||
| # Restart a specific k0s node | ||
| $ oms beta bootstrap-gcp restart-vms --name k0s-1 | ||
|
|
||
| # Restart all VMs with explicit project and zone | ||
| $ oms beta bootstrap-gcp restart-vms --project-id my-project --zone us-central1-a | ||
|
|
||
| # Restart a specific VM with explicit project and zone | ||
| $ oms beta bootstrap-gcp restart-vms --project-id my-project --zone us-central1-a --name ceph-1 | ||
|
|
||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for restart-vms | ||
| --name string Name of a specific VM to restart (e.g. jumpbox, postgres, ceph-1, k0s-1). Restarts all VMs if not specified. | ||
| --project-id string GCP Project ID (optional, will use infra file if not provided) | ||
| --zone string GCP Zone (optional, will use infra file if not provided) | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [oms beta bootstrap-gcp](oms_beta_bootstrap-gcp.md) - Bootstrap GCP infrastructure for Codesphere | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.