WIP: register machine image.

remove passwords
This commit is contained in:
Matthew Hooker 2018-10-17 21:23:22 -07:00
parent 8207ba4fa4
commit 7c577abbcb
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
3 changed files with 61 additions and 11 deletions

View File

@ -119,7 +119,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&stepCreateImage{
uploadImageCommand: b.config.BuilderUploadImageCommand,
uploadImageCommand: b.config.BuilderUploadImageCommand,
destinationContainer: fmt.Sprintf("packer-pv-image-%s", runID),
},
&common.StepCleanupTempKeys{
Comm: &b.config.Comm,

View File

@ -30,17 +30,20 @@ func (c *PVConfig) Prepare(ctx *interpolate.Context) (errs *packer.MultiError) {
if c.BuilderUploadImageCommand == "" {
c.BuilderUploadImageCommand = `split -b 10m diskimage.tar.gz segment_
curl -D auth-headers -s -X GET -H "X-Storage-User: Storage-a459477:jake@hashicorp.com" -H "X-Storage-Pass: ***REMOVED***" https://a459477.storage.oraclecloud.com/auth/v1.0
curl -D auth-headers -s -X GET \
-H "X-Storage-User: Storage-{{.AccountID}}:{{.Username}}" \
-H "X-Storage-Pass: {{.Password}}" \
https://{{.AccountID}}.storage.oraclecloud.com/auth/v1.0
export AUTH_TOKEN=$(awk 'BEGIN {FS=": "; RS="\r\n"}/^X-Auth-Token/{print $2}' auth-headers)
export STORAGE_URL=$(awk 'BEGIN {FS=": "; RS="\r\n"}/^X-Storage-Url/{print $2}' auth-headers)
curl -v -X PUT -H "X-Auth-Token: $AUTH_TOKEN" ${STORAGE_URL}/mwhooker-test-1
curl -v -X PUT -H "X-Auth-Token: $AUTH_TOKEN" ${STORAGE_URL}/{{.Container}}
for i in segment_*; do
curl -v -X PUT -T $i \
-H "X-Auth-Token: $AUTH_TOKEN" \
${STORAGE_URL}/mwhooker-test-1/$i;
${STORAGE_URL}/{{.Container}}/$i;
done
curl -OL https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
@ -49,13 +52,17 @@ chmod u+x jq
(
for i in segment_*; do
./jq -n --arg path "mwhooker-test-1/$i" --arg etag $(md5sum $i | cut -f1 -d' ') --arg size_bytes $(stat --printf "%s" $i) '{path: $path, etag: $etag, size_bytes: $size_bytes}'
./jq -n --arg path "{{.Container}}/$i" \
--arg etag $(md5sum $i | cut -f1 -d' ') \
--arg size_bytes $(stat --printf "%s" $i) \
'{path: $path, etag: $etag, size_bytes: $size_bytes}'
done
) | ./jq -s . > manifest.json
curl -v -X PUT -H "X-Auth-Token: $AUTH_TOKEN" "${STORAGE_URL}/compute_images/mwhooker-diskimage-01.tar.gz?multipart-manifest=put" -T ./manifest.json
# curl -I -X HEAD -H "X-Auth-Token: $AUTH_TOKEN" "${STORAGE_URL}/mwhooker-test-1/diskimage.tar.gz"
curl -v -X PUT \
-H "X-Auth-Token: $AUTH_TOKEN" \
"${STORAGE_URL}/compute_images/{{.ImageName}}.tar.gz?multipart-manifest=put" \
-T ./manifest.json
'...'`
}
/*

View File

@ -3,29 +3,42 @@ package classic
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
type stepCreateImage struct {
uploadImageCommand string
uploadImageCommand string
destinationContainer string
imageName string
}
type uploadCmdData struct {
DiskImagePath string
Username string
Password string
AccountID string
Container string
ImageName string
}
func (s *stepCreateImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
//hook := state.Get("hook").(packer.Hook)
ui := state.Get("ui").(packer.Ui)
comm := state.Get("communicator").(packer.Communicator)
client := state.Get("client").(*compute.ComputeClient)
config := state.Get("config").(*Config)
config.ctx.Data = uploadCmdData{
DiskImagePath: "./diskimage.tar.gz",
Username: config.Username,
Password: config.Password,
AccountID: config.IdentityDomain,
Container: s.destinationContainer,
ImageName: s.imageName,
}
uploadImageCmd, err := interpolate.Render(s.uploadImageCommand, &config.ctx)
if err != nil {
@ -65,6 +78,35 @@ func (s *stepCreateImage) Run(_ context.Context, state multistep.StateBag) multi
return multistep.ActionHalt
}
// Image uploaded, let's register it
machineImageClient := client.MachineImages()
createMI := &compute.CreateMachineImageInput{
// Two-part name of the account
Account: "/Compute-identity_domain/cloud_storage",
Description: "Packer generated TODO",
// The three-part name of the object
Name: "/Compute-identity_domain/user/object",
// image_file.tar.gz, where image_file is the .tar.gz name of the machine image file that you have uploaded to Oracle Cloud Infrastructure Object Storage Classic.
File: fmt.Sprintf("%s.tar.gz", s.imageName),
}
mi, err := machineImageClient.CreateMachineImage(createMI)
if err != nil {
err = fmt.Errorf("Error creating machine image: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
log.Printf("Registered machine image: %+v", mi)
/* TODO:
1. POST /machineimage/, POST /imagelist/, and POST /imagelistentry/ methods, in that order.
2. re-use step_list_images
3. Don't push commits with passwords in them
4. Documentation
5. Configuration (master/builder images, entry, destination stuff, etc)
6. split master/builder image/connection config. i.e. build anything, master only linux
*/
//machineImageClient.CreateMachineImage()
return multistep.ActionContinue
}