From 697c91b0b0e5a4c515383dda921ca41511087b89 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 16 Oct 2013 16:21:14 -1000 Subject: [PATCH] Remove dependency on identifier package, use time ordered UUID [GH-541] --- builder/amazon/common/step_security_group.go | 5 ++--- builder/digitalocean/step_create_droplet.go | 5 ++--- builder/digitalocean/step_create_ssh_key.go | 5 ++--- builder/openstack/step_key_pair.go | 5 ++--- common/uuid/uuid.go | 23 ++++++++++++++++++++ packer/config_template.go | 5 ++--- 6 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 common/uuid/uuid.go diff --git a/builder/amazon/common/step_security_group.go b/builder/amazon/common/step_security_group.go index 0f52548df..529ec4fff 100644 --- a/builder/amazon/common/step_security_group.go +++ b/builder/amazon/common/step_security_group.go @@ -1,11 +1,10 @@ package common import ( - "cgl.tideland.biz/identifier" - "encoding/hex" "fmt" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/common/uuid" "github.com/mitchellh/packer/packer" "log" "time" @@ -35,7 +34,7 @@ func (s *StepSecurityGroup) Run(state multistep.StateBag) multistep.StepAction { // Create the group ui.Say("Creating temporary security group for this instance...") - groupName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw())) + groupName := fmt.Sprintf("packer %s", uuid.TimeOrderedUUID()) log.Printf("Temporary group name: %s", groupName) group := ec2.SecurityGroup{ Name: groupName, diff --git a/builder/digitalocean/step_create_droplet.go b/builder/digitalocean/step_create_droplet.go index c97cd192d..cdf32db3e 100644 --- a/builder/digitalocean/step_create_droplet.go +++ b/builder/digitalocean/step_create_droplet.go @@ -1,10 +1,9 @@ package digitalocean import ( - "cgl.tideland.biz/identifier" - "encoding/hex" "fmt" "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/common/uuid" "github.com/mitchellh/packer/packer" ) @@ -21,7 +20,7 @@ func (s *stepCreateDroplet) Run(state multistep.StateBag) multistep.StepAction { ui.Say("Creating droplet...") // Some random droplet name as it's temporary - name := fmt.Sprintf("packer-%s", hex.EncodeToString(identifier.NewUUID().Raw())) + name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) // Create the droplet based on configuration dropletId, err := client.CreateDroplet(name, c.SizeID, c.ImageID, c.RegionID, sshKeyId) diff --git a/builder/digitalocean/step_create_ssh_key.go b/builder/digitalocean/step_create_ssh_key.go index 04699ec66..05e8b8067 100644 --- a/builder/digitalocean/step_create_ssh_key.go +++ b/builder/digitalocean/step_create_ssh_key.go @@ -1,15 +1,14 @@ package digitalocean import ( - "cgl.tideland.biz/identifier" "code.google.com/p/go.crypto/ssh" "crypto/rand" "crypto/rsa" "crypto/x509" - "encoding/hex" "encoding/pem" "fmt" "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/common/uuid" "github.com/mitchellh/packer/packer" "log" ) @@ -43,7 +42,7 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { pub_sshformat := string(ssh.MarshalAuthorizedKey(pub)) // The name of the public key on DO - name := fmt.Sprintf("packer-%s", hex.EncodeToString(identifier.NewUUID().Raw())) + name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) // Create the key! keyId, err := client.CreateKey(name, pub_sshformat) diff --git a/builder/openstack/step_key_pair.go b/builder/openstack/step_key_pair.go index a67579976..b9ffd5f4e 100644 --- a/builder/openstack/step_key_pair.go +++ b/builder/openstack/step_key_pair.go @@ -1,10 +1,9 @@ package openstack import ( - "cgl.tideland.biz/identifier" - "encoding/hex" "fmt" "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/common/uuid" "github.com/mitchellh/packer/packer" "github.com/rackspace/gophercloud" "log" @@ -19,7 +18,7 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) ui.Say("Creating temporary keypair for this instance...") - keyName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw())) + keyName := fmt.Sprintf("packer %s", uuid.TimeOrderedUUID()) log.Printf("temporary keypair name: %s", keyName) keyResp, err := csp.CreateKeyPair(gophercloud.NewKeyPair{Name: keyName}) if err != nil { diff --git a/common/uuid/uuid.go b/common/uuid/uuid.go new file mode 100644 index 000000000..496a2f6ef --- /dev/null +++ b/common/uuid/uuid.go @@ -0,0 +1,23 @@ +package uuid + +import ( + "fmt" + "math/rand" + "time" +) + +// Generates a time ordered UUID. Top 32 bits are a timestamp, +// bottom 96 are random. +func TimeOrderedUUID() string { + unix := uint32(time.Now().UTC().Unix()) + rand1 := rand.Uint32() + rand2 := rand.Uint32() + rand3 := rand.Uint32() + return fmt.Sprintf("%08x-%04x-%04x-%04x-%04x%08x", + unix, + uint16(rand1>>16), + uint16(rand1&0xffff), + uint16(rand2>>16), + uint16(rand2&0xffff), + rand3) +} diff --git a/packer/config_template.go b/packer/config_template.go index 895e11682..4be4d1c94 100644 --- a/packer/config_template.go +++ b/packer/config_template.go @@ -2,9 +2,8 @@ package packer import ( "bytes" - "cgl.tideland.biz/identifier" - "encoding/hex" "fmt" + "github.com/mitchellh/packer/common/uuid" "strconv" "text/template" "time" @@ -94,5 +93,5 @@ func templateTimestamp() string { } func templateUuid() string { - return hex.EncodeToString(identifier.NewUUID().Raw()) + return hex.EncodeToString(uuid.TimeOrderedUUID()) }