CreateInstance creates and starts an instance on GCP
(name, image, zone, machineType string, disks Disks, data *string, nested, vtpm, replace bool)
| 197 | |
| 198 | // CreateInstance creates and starts an instance on GCP |
| 199 | func (g GCPClient) CreateInstance(name, image, zone, machineType string, disks Disks, data *string, nested, vtpm, replace bool) error { |
| 200 | if replace { |
| 201 | if err := g.DeleteInstance(name, zone, true); err != nil { |
| 202 | return err |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | log.Infof("Creating instance %s from image %s (type: %s in %s)", name, image, machineType, zone) |
| 207 | |
| 208 | enabled := new(string) |
| 209 | *enabled = "1" |
| 210 | |
| 211 | k, err := ssh.NewPublicKey(g.privKey.Public()) |
| 212 | if err != nil { |
| 213 | return err |
| 214 | } |
| 215 | sshKey := new(string) |
| 216 | *sshKey = fmt.Sprintf("moby:%s moby", string(ssh.MarshalAuthorizedKey(k))) |
| 217 | |
| 218 | // check provided image to be compatible with provided options |
| 219 | op, err := g.compute.Images.Get(g.projectName, image).Do() |
| 220 | if err != nil { |
| 221 | return err |
| 222 | } |
| 223 | uefiCompatible := false |
| 224 | for _, feature := range op.GuestOsFeatures { |
| 225 | if feature != nil && feature.Type == uefiCompatibleFeature { |
| 226 | uefiCompatible = true |
| 227 | break |
| 228 | } |
| 229 | } |
| 230 | if vtpm && !uefiCompatible { |
| 231 | return fmt.Errorf("cannot use vTPM without UEFI_COMPATIBLE image") |
| 232 | } |
| 233 | // we should check for nested |
| 234 | vmxLicense := false |
| 235 | for _, license := range op.Licenses { |
| 236 | // we omit hostname and version when define license |
| 237 | if strings.HasSuffix(license, vmxImageLicence) { |
| 238 | vmxLicense = true |
| 239 | break |
| 240 | } |
| 241 | } |
| 242 | if nested && !vmxLicense { |
| 243 | return fmt.Errorf("cannot use nested virtualization without enable-vmx image") |
| 244 | } |
| 245 | |
| 246 | instanceDisks := []*compute.AttachedDisk{ |
| 247 | { |
| 248 | AutoDelete: true, |
| 249 | Boot: true, |
| 250 | InitializeParams: &compute.AttachedDiskInitializeParams{ |
| 251 | SourceImage: fmt.Sprintf("global/images/%s", image), |
| 252 | }, |
| 253 | }, |
| 254 | } |
| 255 | |
| 256 | for i, disk := range disks { |
no test coverage detected