Skip to content

Commit

Permalink
Allow PrivateNetworkOnlyFlag option
Browse files Browse the repository at this point in the history
This commit adds the option in softlayer packer
plugin to set up an instance with private only
network.
Allows to specify private_network_only_flag parameter,
when set to true, the instance spun up will only
have a private ip. And it will try to ssh and run the
scripts using this private ip.
  • Loading branch information
ARUN SURESH committed Oct 4, 2016
1 parent bfca52b commit 56310fa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ The reference of available configuration options is listed below.
* `ssh_timeout` (string) - The time to wait for SSH to become available before timing out. The format of this value is a duration such as "5s" or "5m". The default SSH timeout is "1m". Defaults to "15m"
* `ssh_private_key_file` (string) - Use this ssh private key file instead of a generated ssh key pair for connecting to the instance.
* `instance_state_timeout` (string) - The time to wait, as a duration string, for an instance or image snapshot to enter a desired state (such as "active") before timing out. The default state timeout is "25m"
* `private_network_only_flag` (bool) - Specifies whether or not the instance only has access to the private network. When true this flag specifies that a compute instance is to only have access to the private network. Defaults to false.


As already stated above, a good way of reviewing the available options is by inspecting the output of the following API call:

Expand Down
37 changes: 19 additions & 18 deletions builder/softlayer/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`

Username string `mapstructure:"username"`
APIKey string `mapstructure:"api_key"`
DatacenterName string `mapstructure:"datacenter_name"`
ImageName string `mapstructure:"image_name"`
ImageDescription string `mapstructure:"image_description"`
ImageType string `mapstructure:"image_type"`
BaseImageId string `mapstructure:"base_image_id"`
BaseOsCode string `mapstructure:"base_os_code"`

InstanceName string `mapstructure:"instance_name"`
InstanceDomain string `mapstructure:"instance_domain"`
InstanceCpu int `mapstructure:"instance_cpu"`
InstanceMemory int64 `mapstructure:"instance_memory"`
InstanceNetworkSpeed int `mapstructure:"instance_network_speed"`
InstanceDiskCapacity int `mapstructure:"instance_disk_capacity"`

RawStateTimeout string `mapstructure:"instance_state_timeout"`
StateTimeout time.Duration
Username string `mapstructure:"username"`
APIKey string `mapstructure:"api_key"`
DatacenterName string `mapstructure:"datacenter_name"`
ImageName string `mapstructure:"image_name"`
ImageDescription string `mapstructure:"image_description"`
ImageType string `mapstructure:"image_type"`
BaseImageId string `mapstructure:"base_image_id"`
BaseOsCode string `mapstructure:"base_os_code"`
PrivateNetworkOnlyFlag bool `mapstructure:"private_network_only_flag"`

InstanceName string `mapstructure:"instance_name"`
InstanceDomain string `mapstructure:"instance_domain"`
InstanceCpu int `mapstructure:"instance_cpu"`
InstanceMemory int64 `mapstructure:"instance_memory"`
InstanceNetworkSpeed int `mapstructure:"instance_network_speed"`
InstanceDiskCapacity int `mapstructure:"instance_disk_capacity"`

RawStateTimeout string `mapstructure:"instance_state_timeout"`
StateTimeout time.Duration

ctx interpolate.Context
}
Expand Down
41 changes: 28 additions & 13 deletions builder/softlayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ type SoftLayerRequest struct {

// Based on: http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Virtual_Guest_Configuration/
type InstanceType struct {
HostName string `json:"hostname"`
Domain string
Datacenter string
Cpus int
Memory int64
HourlyBillingFlag bool
LocalDiskFlag bool
DiskCapacity int
NetworkSpeed int
ProvisioningSshKeyId int64
BaseImageId string
BaseOsCode string
HostName string `json:"hostname"`
Domain string
Datacenter string
Cpus int
Memory int64
HourlyBillingFlag bool
LocalDiskFlag bool
PrivateNetworkOnlyFlag bool
DiskCapacity int
NetworkSpeed int
ProvisioningSshKeyId int64
BaseImageId string
BaseOsCode string
}

type InstanceReq struct {
Expand All @@ -52,6 +53,7 @@ type InstanceReq struct {
Memory int64 `json:"maxMemory"`
HourlyBillingFlag bool `json:"hourlyBillingFlag"`
LocalDiskFlag bool `json:"localDiskFlag"`
PrivateNetworkOnlyFlag bool `json:"privateNetworkOnlyFlag"`
NetworkComponents []*NetworkComponent `json:"networkComponents"`
BlockDeviceTemplateGroup *BlockDeviceTemplateGroup `json:"blockDeviceTemplateGroup,omitempty"`
BlockDevices []*BlockDevice `json:"blockDevices,omitempty"`
Expand Down Expand Up @@ -195,7 +197,7 @@ func (self SoftlayerClient) doHttpRequest(path string, requestType string, reque
return []interface{} {v,}, nil

case nil:
return []interface{} {nil,}, nil
return []interface{} {nil,}, nil
default:
return nil, errors.New("Unexpected type in HTTP response")
}
Expand All @@ -221,6 +223,7 @@ func (self SoftlayerClient) CreateInstance(instance InstanceType) (map[string]in
Cpus: instance.Cpus,
Memory: instance.Memory,
HourlyBillingFlag: true,
PrivateNetworkOnlyFlag: instance.PrivateNetworkOnlyFlag,
LocalDiskFlag: false,
NetworkComponents: []*NetworkComponent{
&NetworkComponent{
Expand Down Expand Up @@ -320,6 +323,18 @@ func (self SoftlayerClient) getInstancePublicIp(instanceId string) (string, erro
return string(ipAddress), nil
}

func (self SoftlayerClient) getInstancePrivateIp(instanceId string) (string, error) {
response, err := self.doRawHttpRequest(fmt.Sprintf("SoftLayer_Virtual_Guest/%s/getPrimaryBackendIpAddress.json", instanceId), "GET", nil)
if err != nil {
return "", nil
}

var validIp = regexp.MustCompile(`[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}`)
ipAddress := validIp.Find(response)

return string(ipAddress), nil
}

func (self SoftlayerClient) getBlockDevices(instanceId string) ([]interface{}, error) {
data, err := self.doHttpRequest(fmt.Sprintf("SoftLayer_Virtual_Guest/%s/getBlockDevices.json?objectMask=mask.diskImage.name", instanceId), "GET", nil)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion builder/softlayer/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ func commHost(state multistep.StateBag) (string, error) {
client := state.Get("client").(*SoftlayerClient)
instance := state.Get("instance_data").(map[string]interface{})
instanceId := instance["globalIdentifier"].(string)
ipAddress, err := client.getInstancePublicIp(instanceId)
config := state.Get("config").(Config)
privateNetworkFlag := config.PrivateNetworkOnlyFlag
var ipAddress string
var err error
if privateNetworkFlag == true {
ipAddress, err = client.getInstancePrivateIp(instanceId)
} else {
ipAddress, err = client.getInstancePublicIp(instanceId)
}
if err != nil {
err := errors.New(fmt.Sprintf("Failed to fetch Public IP address for instance '%s'", instanceId))
return "", err
Expand Down
1 change: 1 addition & 0 deletions builder/softlayer/step_create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
Memory: config.InstanceMemory,
HourlyBillingFlag: true,
LocalDiskFlag: true,
PrivateNetworkOnlyFlag: config.PrivateNetworkOnlyFlag,
DiskCapacity: config.InstanceDiskCapacity,
NetworkSpeed: config.InstanceNetworkSpeed,
ProvisioningSshKeyId: ProvisioningSshKeyId,
Expand Down

0 comments on commit 56310fa

Please sign in to comment.