Merge pull request #5174 from rickard-von-essen/cloudstack-temp-ssh-key

cloudstack: Setup temporary SSH keypair
This commit is contained in:
Megan Marsh 2017-08-30 16:49:11 -07:00 committed by GitHub
commit cc104cdf55
6 changed files with 185 additions and 30 deletions

View File

@ -1,6 +1,8 @@
package cloudstack package cloudstack
import ( import (
"fmt"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -61,8 +63,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
HTTPPortMin: b.config.HTTPPortMin, HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax, HTTPPortMax: b.config.HTTPPortMax,
}, },
&stepKeypair{
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName),
KeyPair: b.config.Keypair,
PrivateKeyFile: b.config.Comm.SSHPrivateKey,
SSHAgentAuth: b.config.Comm.SSHAgentAuth,
TemporaryKeyPairName: b.config.TemporaryKeypairName,
},
&stepCreateInstance{ &stepCreateInstance{
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug,
}, },
&stepSetupNetworking{}, &stepSetupNetworking{},
&communicator.StepConnect{ &communicator.StepConnect{

View File

@ -27,23 +27,24 @@ type Config struct {
HTTPGetOnly bool `mapstructure:"http_get_only"` HTTPGetOnly bool `mapstructure:"http_get_only"`
SSLNoVerify bool `mapstructure:"ssl_no_verify"` SSLNoVerify bool `mapstructure:"ssl_no_verify"`
CIDRList []string `mapstructure:"cidr_list"` CIDRList []string `mapstructure:"cidr_list"`
DiskOffering string `mapstructure:"disk_offering"` DiskOffering string `mapstructure:"disk_offering"`
DiskSize int64 `mapstructure:"disk_size"` DiskSize int64 `mapstructure:"disk_size"`
Expunge bool `mapstructure:"expunge"` Expunge bool `mapstructure:"expunge"`
Hypervisor string `mapstructure:"hypervisor"` Hypervisor string `mapstructure:"hypervisor"`
InstanceName string `mapstructure:"instance_name"` InstanceName string `mapstructure:"instance_name"`
Keypair string `mapstructure:"keypair"` Keypair string `mapstructure:"keypair"`
Network string `mapstructure:"network"` TemporaryKeypairName string `mapstructure:"temporary_keypair_name"`
Project string `mapstructure:"project"` Network string `mapstructure:"network"`
PublicIPAddress string `mapstructure:"public_ip_address"` Project string `mapstructure:"project"`
ServiceOffering string `mapstructure:"service_offering"` PublicIPAddress string `mapstructure:"public_ip_address"`
SourceTemplate string `mapstructure:"source_template"` ServiceOffering string `mapstructure:"service_offering"`
SourceISO string `mapstructure:"source_iso"` SourceTemplate string `mapstructure:"source_template"`
UserData string `mapstructure:"user_data"` SourceISO string `mapstructure:"source_iso"`
UserDataFile string `mapstructure:"user_data_file"` UserData string `mapstructure:"user_data"`
UseLocalIPAddress bool `mapstructure:"use_local_ip_address"` UserDataFile string `mapstructure:"user_data_file"`
Zone string `mapstructure:"zone"` UseLocalIPAddress bool `mapstructure:"use_local_ip_address"`
Zone string `mapstructure:"zone"`
TemplateName string `mapstructure:"template_name"` TemplateName string `mapstructure:"template_name"`
TemplateDisplayText string `mapstructure:"template_display_text"` TemplateDisplayText string `mapstructure:"template_display_text"`
@ -120,6 +121,14 @@ func NewConfig(raws ...interface{}) (*Config, error) {
c.TemplateDisplayText = c.TemplateName c.TemplateDisplayText = c.TemplateName
} }
// If we are not given an explicit keypair, ssh_password or ssh_private_key_file,
// then create a temporary one, but only if the temporary_keypair_name has not
// been provided.
if c.Keypair == "" && c.TemporaryKeypairName == "" &&
c.Comm.SSHPrivateKey == "" && c.Comm.SSHPassword == "" {
c.TemporaryKeypairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
}
// Process required parameters. // Process required parameters.
if c.APIURL == "" { if c.APIURL == "" {
errs = packer.MultiErrorAppend(errs, errors.New("a api_url must be specified")) errs = packer.MultiErrorAppend(errs, errors.New("a api_url must be specified"))

View File

@ -22,7 +22,8 @@ type userDataTemplateData struct {
// stepCreateInstance represents a Packer build step that creates CloudStack instances. // stepCreateInstance represents a Packer build step that creates CloudStack instances.
type stepCreateInstance struct { type stepCreateInstance struct {
Ctx interpolate.Context Debug bool
Ctx interpolate.Context
} }
// Run executes the Packer build step that creates a CloudStack instance. // Run executes the Packer build step that creates a CloudStack instance.
@ -44,8 +45,8 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
p.SetName(config.InstanceName) p.SetName(config.InstanceName)
p.SetDisplayname("Created by Packer") p.SetDisplayname("Created by Packer")
if config.Keypair != "" { if keypair, ok := state.GetOk("keypair"); ok {
p.SetKeypair(config.Keypair) p.SetKeypair(keypair.(string))
} }
// If we use an ISO, configure the disk offering. // If we use an ISO, configure the disk offering.
@ -115,6 +116,12 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
ui.Message("Instance has been created!") ui.Message("Instance has been created!")
// In debug-mode, we output the password
if s.Debug {
ui.Message(fmt.Sprintf(
"Password (since debug is enabled) \"%s\"", instance.Password))
}
// Set the auto generated password if a password was not explicitly configured. // Set the auto generated password if a password was not explicitly configured.
switch config.Comm.Type { switch config.Comm.Type {
case "ssh": case "ssh":

View File

@ -0,0 +1,133 @@
package cloudstack
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
"github.com/xanzy/go-cloudstack/cloudstack"
)
type stepKeypair struct {
Debug bool
DebugKeyPath string
KeyPair string
PrivateKeyFile string
SSHAgentAuth bool
TemporaryKeyPairName string
}
func (s *stepKeypair) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
if s.PrivateKeyFile != "" {
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
if err != nil {
state.Put("error", fmt.Errorf(
"Error loading configured private key file: %s", err))
return multistep.ActionHalt
}
state.Put("keypair", s.KeyPair)
state.Put("privateKey", string(privateKeyBytes))
return multistep.ActionContinue
}
if s.SSHAgentAuth && s.KeyPair == "" {
ui.Say("Using SSH Agent with keypair in Source image")
return multistep.ActionContinue
}
if s.SSHAgentAuth && s.KeyPair != "" {
ui.Say(fmt.Sprintf("Using SSH Agent for existing keypair %s", s.KeyPair))
state.Put("keypair", s.KeyPair)
return multistep.ActionContinue
}
if s.TemporaryKeyPairName == "" {
ui.Say("Not using a keypair")
state.Put("keypair", "")
return multistep.ActionContinue
}
client := state.Get("client").(*cloudstack.CloudStackClient)
ui.Say(fmt.Sprintf("Creating temporary keypair: %s ...", s.TemporaryKeyPairName))
p := client.SSH.NewCreateSSHKeyPairParams(s.TemporaryKeyPairName)
keypair, err := client.SSH.CreateSSHKeyPair(p)
if err != nil {
err := fmt.Errorf("Error creating temporary keypair: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if keypair.Privatekey == "" {
err := fmt.Errorf("The temporary keypair returned was blank")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Created temporary keypair: %s", s.TemporaryKeyPairName))
// If we're in debug mode, output the private key to the working directory.
if s.Debug {
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
f, err := os.Create(s.DebugKeyPath)
if err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
return multistep.ActionHalt
}
defer f.Close()
// Write the key out
if _, err := f.Write([]byte(keypair.Privatekey)); err != nil {
err := fmt.Errorf("Error saving debug key: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Chmod it so that it is SSH ready
if runtime.GOOS != "windows" {
if err := f.Chmod(0600); err != nil {
err := fmt.Errorf("Error setting permissions of debug key: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
}
// Set some state data for use in future steps
state.Put("keypair", s.TemporaryKeyPairName)
state.Put("privateKey", keypair.Privatekey)
return multistep.ActionContinue
}
func (s *stepKeypair) Cleanup(state multistep.StateBag) {
if s.TemporaryKeyPairName == "" {
return
}
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(*cloudstack.CloudStackClient)
ui.Say(fmt.Sprintf("Deleting temporary keypair: %s ...", s.TemporaryKeyPairName))
_, err := client.SSH.DeleteSSHKeyPair(client.SSH.NewDeleteSSHKeyPairParams(
s.TemporaryKeyPairName,
))
if err != nil {
ui.Error(err.Error())
ui.Error(fmt.Sprintf(
"Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName))
}
}

View File

@ -22,15 +22,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction {
var err error var err error
var errs *packer.MultiError var errs *packer.MultiError
if config.Comm.SSHPrivateKey != "" {
privateKey, err := ioutil.ReadFile(config.Comm.SSHPrivateKey)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Error loading configured private key file: %s", err))
}
state.Put("privateKey", privateKey)
}
// First get the project and zone UUID's so we can use them in other calls when needed. // First get the project and zone UUID's so we can use them in other calls when needed.
if config.Project != "" && !isUUID(config.Project) { if config.Project != "" && !isUUID(config.Project) {
config.Project, _, err = client.Project.GetProjectID(config.Project) config.Project, _, err = client.Project.GetProjectID(config.Project)

View File

@ -149,6 +149,10 @@ builder.
- `template_scalable` (boolean) - Set to `true` to indicate that the template - `template_scalable` (boolean) - Set to `true` to indicate that the template
contains tools to support dynamic scaling of VM cpu/memory. Defaults to `false`. contains tools to support dynamic scaling of VM cpu/memory. Defaults to `false`.
- `temporary_keypair_name` (string) - The name of the temporary SSH key pair
to generate. By default, Packer generates a name that looks like
`packer_<UUID>`, where &lt;UUID&gt; is a 36 character unique identifier.
- `user_data` (string) - User data to launch with the instance. This is a - `user_data` (string) - User data to launch with the instance. This is a
[template engine](/docs/templates/engine.html) see _User Data_ bellow for more [template engine](/docs/templates/engine.html) see _User Data_ bellow for more
details. details.