blob: c56af4becbccc1a4397797646d50c5dc19290a4e [file]
package kernel
import (
"context"
_ "embed"
"fmt"
"os"
"os/exec"
"strings"
)
//go:embed scripts/cos-set-dev-mode
var setDevModeScript string
// UnlockGCPInstance unlocks a GCP COS instance by running the cos-set-dev-mode script on it and rebooting.
//
// The gcloudArgs args are passed directly to `gcloud compute ssh` in order to specify
// the instance that should be unlocked.
func UnlockGCPInstance(ctx context.Context, gcloudArgs []string) error {
// This allows us to read the script from stdin while also passing positional args.
gcloudCmdArgs := append([]string{"compute", "ssh", "--command=sudo bash -s -- --confirm --reboot"}, gcloudArgs...)
cmd := exec.CommandContext(ctx, "gcloud", gcloudCmdArgs...)
cmd.Stdin = strings.NewReader(setDevModeScript)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to set dev mode: %w", err)
}
return nil
}
// UnlockGCPInstance unlocks the local COS instance by running the cos-set-dev-mode script on it and rebooting.
func UnlockLocalInstance() error {
cmd := exec.Command("bash", "-s")
cmd.Stdin = strings.NewReader(setDevModeScript)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to set dev mode: %w", err)
}
return nil
}