github.com/sneal/packer@v0.5.2/builder/openstack/ssh.go (about) 1 package openstack 2 3 import ( 4 gossh "code.google.com/p/go.crypto/ssh" 5 "errors" 6 "fmt" 7 "github.com/mitchellh/multistep" 8 "github.com/mitchellh/packer/communicator/ssh" 9 "github.com/rackspace/gophercloud" 10 "time" 11 ) 12 13 // SSHAddress returns a function that can be given to the SSH communicator 14 // for determining the SSH address based on the server AccessIPv4 setting.. 15 func SSHAddress(csp gophercloud.CloudServersProvider, port int) func(multistep.StateBag) (string, error) { 16 return func(state multistep.StateBag) (string, error) { 17 for j := 0; j < 2; j++ { 18 s := state.Get("server").(*gophercloud.Server) 19 if s.AccessIPv4 != "" { 20 return fmt.Sprintf("%s:%d", s.AccessIPv4, port), nil 21 } 22 if s.AccessIPv6 != "" { 23 return fmt.Sprintf("[%s]:%d", s.AccessIPv6, port), nil 24 } 25 serverState, err := csp.ServerById(s.Id) 26 27 if err != nil { 28 return "", err 29 } 30 31 state.Put("server", serverState) 32 time.Sleep(1 * time.Second) 33 } 34 35 return "", errors.New("couldn't determine IP address for server") 36 } 37 } 38 39 // SSHConfig returns a function that can be used for the SSH communicator 40 // config for connecting to the instance created over SSH using the generated 41 // private key. 42 func SSHConfig(username string) func(multistep.StateBag) (*gossh.ClientConfig, error) { 43 return func(state multistep.StateBag) (*gossh.ClientConfig, error) { 44 privateKey := state.Get("privateKey").(string) 45 46 keyring := new(ssh.SimpleKeychain) 47 if err := keyring.AddPEMKey(privateKey); err != nil { 48 return nil, fmt.Errorf("Error setting up SSH config: %s", err) 49 } 50 51 return &gossh.ClientConfig{ 52 User: username, 53 Auth: []gossh.ClientAuth{ 54 gossh.ClientAuthKeyring(keyring), 55 }, 56 }, nil 57 } 58 }