|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/lf-edge/eden/pkg/defaults" |
| 6 | + "github.com/lf-edge/eden/pkg/linuxkit" |
| 7 | + "github.com/lf-edge/eden/pkg/utils" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/spf13/viper" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + gcpVMName string |
| 15 | + gcpImageName string |
| 16 | + gcpProjectName string |
| 17 | + gcpKey string |
| 18 | + gcpBucketName string |
| 19 | + gcpZone string |
| 20 | + gcpMachineType string |
| 21 | +) |
| 22 | + |
| 23 | +var gcpCmd = &cobra.Command{ |
| 24 | + Use: "gcp", |
| 25 | + Short: `Manage images and VMs in Google Cloud Platform`, |
| 26 | + Long: `Manage images and VMs in Google Cloud Platform (you need to provide a key, set it in config in gcp.key or use a gcloud login)`, |
| 27 | + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + assignCobraToViper(cmd) |
| 29 | + |
| 30 | + viperLoaded, err := utils.LoadConfigFile(configFile) |
| 31 | + |
| 32 | + if viperLoaded && err == nil { |
| 33 | + gcpKey = viper.GetString("gcp.key") //use variable from config |
| 34 | + } |
| 35 | + |
| 36 | + if gcpKey == "" { |
| 37 | + context, err := utils.ContextLoad() |
| 38 | + if err != nil { //we have no current context |
| 39 | + log.Warn(`You didn't specify the '--key' argument. Something might break`) |
| 40 | + } else { |
| 41 | + log.Warnf(`You didn't specify the '--key' argument, or set it wia 'eden config set %s --key gcp.key --value YOUR_PATH_TO_KEY'. Something might break`, context.Current) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return nil |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +var gcpImageCmd = &cobra.Command{ |
| 50 | + Use: "image", |
| 51 | + Short: `Manage images in gcp`, |
| 52 | +} |
| 53 | + |
| 54 | +var gcpVMCmd = &cobra.Command{ |
| 55 | + Use: "vm", |
| 56 | + Short: `Manage VMs in gcp`, |
| 57 | +} |
| 58 | + |
| 59 | +var gcpImageList = &cobra.Command{ |
| 60 | + Use: "list", |
| 61 | + Short: "list images uploaded to gcp", |
| 62 | + Long: `Show list of images from gcp project.`, |
| 63 | + Run: func(cmd *cobra.Command, args []string) { |
| 64 | + gcpClient, err := linuxkit.NewGCPClient(gcpKey, gcpProjectName) |
| 65 | + if err != nil { |
| 66 | + log.Fatalf("Unable to connect to GCP: %v", err) |
| 67 | + } |
| 68 | + imageList, err := gcpClient.ListImages() |
| 69 | + if err != nil { |
| 70 | + log.Fatal(err) |
| 71 | + } |
| 72 | + for _, el := range imageList { |
| 73 | + fmt.Println(el) |
| 74 | + } |
| 75 | + }, |
| 76 | +} |
| 77 | + |
| 78 | +var gcpImageUpload = &cobra.Command{ |
| 79 | + Use: "upload", |
| 80 | + Short: "upload image to gcp", |
| 81 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 82 | + assignCobraToViper(cmd) |
| 83 | + viperLoaded, err := utils.LoadConfigFile(configFile) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("error reading config: %s", err.Error()) |
| 86 | + } |
| 87 | + |
| 88 | + if viperLoaded { |
| 89 | + eveImageFile = utils.ResolveAbsPath(viper.GetString("eve.image-file")) |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | + }, |
| 94 | + Run: func(cmd *cobra.Command, args []string) { |
| 95 | + gcpClient, err := linuxkit.NewGCPClient(gcpKey, gcpProjectName) |
| 96 | + if err != nil { |
| 97 | + log.Fatalf("Unable to connect to GCP: %v", err) |
| 98 | + } |
| 99 | + fileName := fmt.Sprintf("%s.img.tar.gz", gcpImageName) |
| 100 | + if err := gcpClient.UploadFile(eveImageFile, fileName, gcpBucketName, false); err != nil { |
| 101 | + log.Fatalf("Error copying to Google Storage: %v", err) |
| 102 | + } |
| 103 | + err = gcpClient.CreateImage(gcpImageName, "https://storage.googleapis.com/"+gcpBucketName+"/"+fileName, "", true, true) |
| 104 | + if err != nil { |
| 105 | + log.Fatalf("Error creating Google Compute Image: %v", err) |
| 106 | + } |
| 107 | + }, |
| 108 | +} |
| 109 | + |
| 110 | +var gcpImageDelete = &cobra.Command{ |
| 111 | + Use: "delete", |
| 112 | + Short: "delete image from gcp", |
| 113 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 114 | + assignCobraToViper(cmd) |
| 115 | + |
| 116 | + viperLoaded, err := utils.LoadConfigFile(configFile) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("error reading config: %s", err.Error()) |
| 119 | + } |
| 120 | + |
| 121 | + if viperLoaded { |
| 122 | + eveImageFile = utils.ResolveAbsPath(viper.GetString("eve.image-file")) |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | + }, |
| 127 | + Run: func(cmd *cobra.Command, args []string) { |
| 128 | + gcpClient, err := linuxkit.NewGCPClient(gcpKey, gcpProjectName) |
| 129 | + if err != nil { |
| 130 | + log.Fatalf("Unable to connect to GCP: %v", err) |
| 131 | + } |
| 132 | + fileName := fmt.Sprintf("%s.img.tar.gz", gcpImageName) |
| 133 | + err = gcpClient.DeleteImage(gcpImageName) |
| 134 | + if err != nil { |
| 135 | + log.Fatalf("Error in delete of Google Compute Image: %v", err) |
| 136 | + } |
| 137 | + if err := gcpClient.RemoveFile(fileName, gcpBucketName); err != nil { |
| 138 | + log.Fatalf("Error id delete from Google Storage: %v", err) |
| 139 | + } |
| 140 | + }, |
| 141 | +} |
| 142 | + |
| 143 | +var gcpRun = &cobra.Command{ |
| 144 | + Use: "run", |
| 145 | + Short: "run vm in gcp", |
| 146 | + Run: func(cmd *cobra.Command, args []string) { |
| 147 | + gcpClient, err := linuxkit.NewGCPClient(gcpKey, gcpProjectName) |
| 148 | + if err != nil { |
| 149 | + log.Fatalf("Unable to connect to GCP: %v", err) |
| 150 | + } |
| 151 | + if err := gcpClient.CreateInstance(gcpVMName, gcpImageName, gcpZone, gcpMachineType, nil, nil, true, true); err != nil { |
| 152 | + log.Fatalf("") |
| 153 | + } |
| 154 | + }, |
| 155 | +} |
| 156 | + |
| 157 | +var gcpDelete = &cobra.Command{ |
| 158 | + Use: "delete", |
| 159 | + Short: "delete vm from gcp", |
| 160 | + Run: func(cmd *cobra.Command, args []string) { |
| 161 | + gcpClient, err := linuxkit.NewGCPClient(gcpKey, gcpProjectName) |
| 162 | + if err != nil { |
| 163 | + log.Fatalf("Unable to connect to GCP: %v", err) |
| 164 | + } |
| 165 | + if err := gcpClient.DeleteInstance(gcpVMName, gcpZone, true); err != nil { |
| 166 | + log.Fatalf("") |
| 167 | + } |
| 168 | + }, |
| 169 | +} |
| 170 | + |
| 171 | +var gcpConsole = &cobra.Command{ |
| 172 | + Use: "console", |
| 173 | + Short: "connect to vm console gcp", |
| 174 | + Run: func(cmd *cobra.Command, args []string) { |
| 175 | + gcpClient, err := linuxkit.NewGCPClient(gcpKey, gcpProjectName) |
| 176 | + if err != nil { |
| 177 | + log.Fatalf("Unable to connect to GCP: %v", err) |
| 178 | + } |
| 179 | + if err := gcpClient.ConnectToInstanceSerialPort(gcpVMName, gcpZone); err != nil { |
| 180 | + log.Fatalf("") |
| 181 | + } |
| 182 | + }, |
| 183 | +} |
| 184 | + |
| 185 | +func gcpInit() { |
| 186 | + gcpCmd.AddCommand(gcpImageCmd) |
| 187 | + gcpCmd.AddCommand(gcpVMCmd) |
| 188 | + gcpCmd.PersistentFlags().StringVarP(&gcpProjectName, "project", "p", defaults.DefaultGcpProjectName, "project name on gcp") |
| 189 | + gcpCmd.PersistentFlags().StringVarP(&gcpKey, "key", "k", "", "gcp key file") |
| 190 | + gcpImageCmd.AddCommand(gcpImageList) |
| 191 | + gcpImageCmd.AddCommand(gcpImageUpload) |
| 192 | + gcpImageUpload.Flags().StringVar(&gcpImageName, "image-name", defaults.DefaultGcpImageName, "image name") |
| 193 | + gcpImageUpload.Flags().StringVar(&eveImageFile, "image-file", "", "image file to upload") |
| 194 | + gcpImageUpload.Flags().StringVar(&gcpBucketName, "bucket-name", defaults.DefaultGcpBucketName, "bucket name to upload into") |
| 195 | + gcpImageCmd.AddCommand(gcpImageDelete) |
| 196 | + gcpImageDelete.Flags().StringVar(&gcpImageName, "image-name", defaults.DefaultGcpImageName, "image name") |
| 197 | + gcpImageDelete.Flags().StringVar(&gcpBucketName, "bucket-name", defaults.DefaultGcpBucketName, "bucket name to upload into") |
| 198 | + gcpVMCmd.AddCommand(gcpRun) |
| 199 | + gcpRun.Flags().StringVar(&gcpImageName, "image-name", defaults.DefaultGcpImageName, "image name") |
| 200 | + gcpRun.Flags().StringVar(&gcpVMName, "vm-name", defaults.DefaultGcpImageName, "vm name") |
| 201 | + gcpRun.Flags().StringVar(&gcpZone, "zone", defaults.DefaultGcpZone, "gcp zone") |
| 202 | + gcpRun.Flags().StringVar(&gcpMachineType, "machine-type", defaults.DefaultGcpMachineType, "gcp machine type") |
| 203 | + gcpVMCmd.AddCommand(gcpDelete) |
| 204 | + gcpDelete.Flags().StringVar(&gcpVMName, "vm-name", defaults.DefaultGcpImageName, "vm name") |
| 205 | + gcpDelete.Flags().StringVar(&gcpZone, "zone", defaults.DefaultGcpZone, "gcp zone") |
| 206 | + gcpVMCmd.AddCommand(gcpConsole) |
| 207 | + gcpConsole.Flags().StringVar(&gcpVMName, "vm-name", defaults.DefaultGcpImageName, "vm name") |
| 208 | + gcpConsole.Flags().StringVar(&gcpZone, "zone", defaults.DefaultGcpZone, "gcp zone") |
| 209 | +} |
0 commit comments