| package actions |
| |
| import ( |
| "context" |
| _ "embed" |
| "fmt" |
| "path/filepath" |
| "time" |
| |
| "cos.googlesource.com/cos/tools.git/src/pkg/utils" |
| "cos.googlesource.com/cos/tools.git/src/pkg/kernel" |
| "github.com/spf13/cobra" |
| ) |
| |
| func deployKernelToGcpInstance(ctx context.Context, path string, gcloudArgs []string) error { |
| opts := utils.RetryOptions { |
| Attempts: 5, |
| Delay: 10 * time.Second, |
| ShouldRetry: ShouldRetryGcloudComputeSsh, |
| } |
| |
| copyCtx, cancel := context.WithTimeout(ctx, 2 * time.Minute) |
| defer cancel() |
| |
| _, tarballName := filepath.Split(path) |
| remotePath := filepath.Join("/tmp", tarballName) |
| fmt.Printf("Copying kernel tarball %s to remote path %s on instance\n", path, remotePath) |
| err := utils.Retry(copyCtx, func(ctx context.Context) error { |
| return kernel.CopyFileToGcpInstance(ctx, path, remotePath, gcloudArgs) |
| }, opts) |
| if err != nil { |
| return fmt.Errorf("failed to copy kernel tarball %s to GCP instance: %v", path, err) |
| } |
| |
| deployCtx, cancel := context.WithTimeout(ctx, 2 * time.Minute) |
| defer cancel() |
| |
| fmt.Printf("Deploying kernel tarball from remote path %s on instance\n", remotePath) |
| return utils.Retry(deployCtx, func(ctx context.Context) error { |
| return kernel.DeployKernelToGcpInstance(ctx, remotePath, gcloudArgs) |
| }, opts) |
| } |
| |
| var deployCmd = &cobra.Command{ |
| Use: "deploy", |
| Short: "Deploys a kernel to a local instance or a remote GCP instance", |
| Long: `Deploys a kernel from a tarball to a local instance or a remote GCP instance. |
| The instance may reboot up to twice in the process; once when it is unlocked, and once |
| when the new kernel is deployed. If the instance is already unlocked, the first reboot |
| is skipped.`, |
| RunE: func(cmd *cobra.Command, args []string) error { |
| if len(args) == 0 { |
| return fmt.Errorf("must provide a path to a kernel tarball") |
| } else if len(args) == 1 { |
| fmt.Println("Unlocking instance") |
| if err := kernel.UnlockLocalInstance(); err != nil { |
| return err |
| } |
| |
| fmt.Printf("Deploying kernel from path %s on instance\n", args[0]) |
| return kernel.DeployKernelToLocalInstance(args[0]) |
| } else { |
| fmt.Println("Unlocking instance") |
| if err := unlockGCPInstance(cmd.Context(), args[1:]); err != nil { |
| return err |
| } |
| |
| return deployKernelToGcpInstance(cmd.Context(), args[0], args[1:]) |
| } |
| }, |
| } |
| |
| func init() { |
| rootCmd.AddCommand(deployCmd) |
| } |