builder instance now runs and is connected to

This commit is contained in:
Matthew Hooker 2018-09-07 16:18:47 -07:00
parent b3ffa975c3
commit 7c26ffef9c
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
5 changed files with 94 additions and 41 deletions

View File

@ -106,9 +106,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&stepTerminatePVMaster{},
&stepCreatePVBuilder{
name: fmt.Sprintf("builder-instance_%s", runID),
masterVolumeName: fmt.Sprintf("master-storage_%s", runID),
builderVolumeName: fmt.Sprintf("builder-storage_%s", runID),
},
&stepAttachVolume{
volumeName: fmt.Sprintf("master-storage_%s", runID),
index: 2,
instanceInfoKey: "builder_instance_info",
},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: ocommon.CommHost,

View File

@ -0,0 +1,63 @@
package classic
import (
"context"
"fmt"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type stepAttachVolume struct {
index int
volumeName string
instanceInfoKey string
}
func (s *stepAttachVolume) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*compute.ComputeClient)
ui := state.Get("ui").(packer.Ui)
instanceInfo := state.Get(s.instanceInfoKey).(*compute.InstanceInfo)
saClient := client.StorageAttachments()
saInput := &compute.CreateStorageAttachmentInput{
Index: s.index,
InstanceName: instanceInfo.Name + "/" + instanceInfo.ID,
StorageVolumeName: s.volumeName,
}
sa, err := saClient.CreateStorageAttachment(saInput)
if err != nil {
err = fmt.Errorf("Problem attaching master volume: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
state.Put(s.instanceInfoKey+"/attachment", sa)
ui.Message("Volume to master attached to builder instance")
return multistep.ActionContinue
}
func (s *stepAttachVolume) Cleanup(state multistep.StateBag) {
sa, ok := state.GetOk(s.instanceInfoKey + "/attachment")
if !ok {
return
}
client := state.Get("client").(*compute.ComputeClient)
ui := state.Get("ui").(packer.Ui)
saClient := client.StorageAttachments()
saI := &compute.DeleteStorageAttachmentInput{
Name: sa.(*compute.StorageAttachmentInfo).Name,
}
if err := saClient.DeleteStorageAttachment(saI); err != nil {
err = fmt.Errorf("Problem detaching storage volume: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return
}
}

View File

@ -2,6 +2,8 @@ package classic
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
@ -13,31 +15,27 @@ func (s *stepCreateImage) Run(_ context.Context, state multistep.StateBag) multi
//hook := state.Get("hook").(packer.Hook)
ui := state.Get("ui").(packer.Ui)
comm := state.Get("communicator").(packer.Communicator)
commands := []string{
"mkdir ./builder",
"sudo mkfs -t ext3 /dev/xvdb",
"sudo mount /dev/xvdb ./builder",
"sudo chown opc:opc ./builder",
"cd ./builder",
"sudo dd if=/dev/xvdc bs=8M status=progress | cp --sparse=always /dev/stdin diskimage.raw",
"tar czSf ./diskimage.tar.gz ./diskimage.raw",
}
for _, c := range commands {
cmd := packer.RemoteCmd{
Command: c,
}
cmd.StartWithUi(comm, ui)
}
// comm.Start("
command := `#!/bin/sh
set -e
mkdir /builder
mkfs -t ext3 /dev/xvdb
mount /dev/xvdb /builder
chown opc:opc /builder
cd /builder
dd if=/dev/xvdc bs=8M status=progress | cp --sparse=always /dev/stdin diskimage.raw
tar czSf ./diskimage.tar.gz ./diskimage.raw`
/*
// Provision
log.Println("Running the provision hook")
if err := hook.Run(packer.HookProvision, ui, comm, nil); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
*/
dest := "/tmp/create-packer-diskimage.sh"
comm.Upload(dest, strings.NewReader(command), nil)
cmd := &packer.RemoteCmd{
Command: fmt.Sprintf("sudo /bin/sh %s", dest),
}
if err := cmd.StartWithUi(comm, ui); err != nil {
err = fmt.Errorf("Problem creating volume: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
return multistep.ActionContinue
}

View File

@ -12,7 +12,6 @@ import (
type stepCreatePVBuilder struct {
name string
masterVolumeName string
builderVolumeName string
}
@ -59,22 +58,10 @@ func (s *stepCreatePVBuilder) Run(_ context.Context, state multistep.StateBag) m
}
log.Printf("Created instance %s", instanceInfo.Name)
saClient := client.StorageAttachments()
saInput := &compute.CreateStorageAttachmentInput{
Index: 3,
InstanceName: s.name,
StorageVolumeName: s.masterVolumeName,
}
if _, err := saClient.CreateStorageAttachment(saInput); err != nil {
err = fmt.Errorf("Problem attaching master volume: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("builder_instance_info", instanceInfo)
state.Put("builder_instance_id", instanceInfo.ID)
ui.Message(fmt.Sprintf("Created builder instance: %s.", instanceInfo.ID))
ui.Message(fmt.Sprintf("Created builder instance: %s.", instanceInfo.Name))
return multistep.ActionContinue
}
@ -105,4 +92,5 @@ func (s *stepCreatePVBuilder) Cleanup(state multistep.StateBag) {
}
// TODO wait for instance state to change to deleted?
ui.Say("Terminated builder instance.")
}

View File

@ -58,7 +58,7 @@ func (s *stepCreatePVMaster) Run(_ context.Context, state multistep.StateBag) mu
state.Put("master_instance_info", instanceInfo)
state.Put("master_instance_id", instanceInfo.ID)
ui.Message(fmt.Sprintf("Created master instance: %s.", instanceInfo.ID))
ui.Message(fmt.Sprintf("Created master instance: %s.", instanceInfo.Name))
return multistep.ActionContinue
}