change disk creation method to manual

This commit is contained in:
Roman Mingazeev 2020-11-13 16:11:58 +03:00
parent 61b9015415
commit fe9dca4c75
1 changed files with 45 additions and 11 deletions

View File

@ -56,6 +56,40 @@ func createNetwork(ctx context.Context, c *Config, d Driver) (*vpc.Network, erro
return network, nil return network, nil
} }
func createDisk(ctx context.Context, c *Config, d Driver, sourceImage *Image) (*compute.Disk, error) {
req := &compute.CreateDiskRequest{
Name: c.DiskName,
FolderId: c.FolderID,
TypeId: c.DiskType,
ZoneId: c.Zone,
Size: int64((datasize.ByteSize(c.DiskSizeGb) * datasize.GB).Bytes()),
Source: &compute.CreateDiskRequest_ImageId{
ImageId: sourceImage.ID,
},
}
sdk := d.SDK()
op, err := sdk.WrapOperation(sdk.Compute().Disk().Create(ctx, req))
if err != nil {
return nil, err
}
err = op.Wait(ctx)
if err != nil {
return nil, err
}
resp, err := op.Response()
if err != nil {
return nil, err
}
image, ok := resp.(*compute.Disk)
if !ok {
return nil, errors.New("disk create operation response doesn't contain Disk")
}
return image, nil
}
func createSubnet(ctx context.Context, c *Config, d Driver, networkID string) (*vpc.Subnet, error) { func createSubnet(ctx context.Context, c *Config, d Driver, networkID string) (*vpc.Subnet, error) {
req := &vpc.CreateSubnetRequest{ req := &vpc.CreateSubnetRequest{
FolderId: c.FolderID, FolderId: c.FolderID,
@ -154,6 +188,14 @@ func (s *StepCreateInstance) Run(ctx context.Context, state multistep.StateBag)
instanceSubnetID = config.SubnetID instanceSubnetID = config.SubnetID
} }
// Create a disk manually to have a delete ID
ui.Say("Creating disk...")
disk, err := createDisk(ctx, config, driver, sourceImage)
if err != nil {
return stepHaltWithError(state, fmt.Errorf("Error creating disk: %s", err))
}
state.Put("disk_id", disk.Id)
// Create an instance based on the configuration // Create an instance based on the configuration
ui.Say("Creating instance...") ui.Say("Creating instance...")
@ -189,15 +231,8 @@ runcmd:
Metadata: instanceMetadata, Metadata: instanceMetadata,
BootDiskSpec: &compute.AttachedDiskSpec{ BootDiskSpec: &compute.AttachedDiskSpec{
AutoDelete: false, AutoDelete: false,
Disk: &compute.AttachedDiskSpec_DiskSpec_{ Disk: &compute.AttachedDiskSpec_DiskId{
DiskSpec: &compute.AttachedDiskSpec_DiskSpec{ DiskId: disk.Id,
Name: config.DiskName,
TypeId: config.DiskType,
Size: int64((datasize.ByteSize(config.DiskSizeGb) * datasize.GB).Bytes()),
Source: &compute.AttachedDiskSpec_DiskSpec_ImageId{
ImageId: sourceImage.ID,
},
},
}, },
}, },
NetworkInterfaceSpecs: []*compute.NetworkInterfaceSpec{ NetworkInterfaceSpecs: []*compute.NetworkInterfaceSpec{
@ -255,14 +290,13 @@ runcmd:
return stepHaltWithError(state, fmt.Errorf("response doesn't contain Instance")) return stepHaltWithError(state, fmt.Errorf("response doesn't contain Instance"))
} }
state.Put("disk_id", instance.BootDisk.DiskId)
// instance_id is the generic term used so that users can have access to the // instance_id is the generic term used so that users can have access to the
// instance id inside of the provisioners, used in step_provision. // instance id inside of the provisioners, used in step_provision.
state.Put("instance_id", instance.Id) state.Put("instance_id", instance.Id)
if s.Debug { if s.Debug {
ui.Message(fmt.Sprintf("Instance ID %s started. Current instance status %s", instance.Id, instance.Status)) ui.Message(fmt.Sprintf("Instance ID %s started. Current instance status %s", instance.Id, instance.Status))
ui.Message(fmt.Sprintf("Disk ID %s. ", instance.BootDisk.DiskId)) ui.Message(fmt.Sprintf("Disk ID %s. ", disk.Id))
} }
// provision generated_data from declared in Builder.Prepare func // provision generated_data from declared in Builder.Prepare func