-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
17 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 e60ee18
ref: refactor GCP VM restart command to improve project and zone reso…
OliverTrautvetter 1cc5b49
Merge branch 'main' into restart_vm
OliverTrautvetter d3b1d3a
chore(docs): Auto-update docs and licenses
OliverTrautvetter 3c82464
Merge branch 'main' into restart_vm
joka134 df59ff6
chore(docs): Auto-update docs and licenses
OliverTrautvetter dec6c4f
fix: Update logging in GCP VM restart functions and improve documenta…
OliverTrautvetter f5a98ad
chore(docs): Auto-update docs and licenses
OliverTrautvetter cdd2554
fix: remove old comment
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
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,134 @@ | ||
| // 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 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) | ||
| } | ||
| if projectID == "" { | ||
| projectID = infraEnv.ProjectID | ||
| } | ||
| if zone == "" { | ||
| zone = infraEnv.Zone | ||
| } | ||
| } | ||
|
|
||
| if projectID == "" { | ||
| return fmt.Errorf("project ID is required; set --project-id or ensure the infra file exists") | ||
| } | ||
| if zone == "" { | ||
| return fmt.Errorf("zone is required; set --zone or ensure the infra file exists") | ||
| } | ||
|
OliverTrautvetter marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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, | ||
|
OliverTrautvetter marked this conversation as resolved.
Outdated
|
||
| 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
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.