Skip to content

Allow PrivateNetworkOnlyFlag option #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: go

go:
- 1.6.3
- 1.7
- tip

cache:
directories:
- $HOME/.glide

install:
- go get github.com/Masterminds/glide
- glide install --strip-vendor

script:
- go test -v $(glide novendor)
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# SoftLayer Builder (for packer.io)

[![Build Status](https://travis-ci.org/watson-platform/packer-builder-softlayer.svg?branch=master)](https://travis-ci.org/watson-platform/packer-builder-softlayer)

The softlayer builder is able to create new images for use with SoftLayer. The builder takes a source image (identified by it's global ID or reference name), runs any provisioning necessary on the image after launching it, then snapshots it into a reusable image. This reusable image can then be used as the foundation of new servers that are launched within SoftLayer.

The builder does not manage images. Once it creates an image, it is up to you to use it or delete it.
Expand All @@ -8,10 +10,13 @@ The builder does not manage images. Once it creates an image, it is up to you to

Download the Packer binaries [here](https://www.packer.io/downloads.html) or build Packer from source as described [here](https://github.com/mitchellh/packer#developing-packer).

Next, clone this repository into `$GOPATH/src/github.com/leonidlm/packer-builder-softlayer`. Then build the packer-softlayer-builder binary into the same folder as the packer binaries:
Install [glide](https://github.com/Masterminds/glide#install)

Next, clone this repository into `$GOPATH/src/github.com/watson-platform/packer-builder-softlayer`. Then build the packer-softlayer-builder binary into the same folder as the packer binaries:

```
cd $GOPATH/src/github.com/leonidlm/packer-builder-softlayer
cd $GOPATH/src/github.com/watson-platform/packer-builder-softlayer
glide install --strip-vendor
go build -o /usr/local/packer/packer-builder-softlayer main.go
```

Expand Down Expand Up @@ -94,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 All @@ -104,7 +111,7 @@ As already stated above, a good way of reviewing the available options is by ins
## Contribute

New contributors are always welcome!
When in doubt please feel free to ask questions, just [Create an issue](https://github.com/leonidlm/packer-builder-softlayer/issues/new) with your enquiries.
When in doubt please feel free to ask questions, just [Create an issue](https://github.com/watson-platform/packer-builder-softlayer/issues/new) with your enquiries.

### Development Environment

Expand All @@ -117,4 +124,3 @@ To run the unit tests, execute "go test ./..." from the root project directory.
* Configure travis CI or any alternative to automatically test and build the code
* Provide an easier way to install (with no need to compile from source)
* Add an option to configure multiple disks for the instance

2 changes: 1 addition & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ VM_GUI=false

GOROOT = '/opt/go'
GOPATH = '/opt/gopath'
PACKAGE_PATH = 'src/github.com/leonidlm/packer-builder-softlayer'
PACKAGE_PATH = 'src/github.com/watson-platform/packer-builder-softlayer'

script = <<SCRIPT
SRCROOT="#{GOROOT}"
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
2 changes: 1 addition & 1 deletion builder/softlayer/step_create_ssh_key.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package softlayer

import (
"code.google.com/p/gosshold/ssh"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
Expand All @@ -10,6 +9,7 @@ import (
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common/uuid"
"github.com/mitchellh/packer/packer"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"strings"
Expand Down
77 changes: 77 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package: github.com/watson-platform/packer-builder-softlayer
import:
- package: github.com/mitchellh/multistep
- package: github.com/mitchellh/packer
version: ~0.10.2
subpackages:
- common
- common/uuid
- helper/communicator
- helper/config
- packer
- packer/plugin
- template/interpolate
- package: golang.org/x/crypto
subpackages:
- ssh
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/leonidlm/packer-builder-softlayer/builder/softlayer"
"github.com/watson-platform/packer-builder-softlayer/builder/softlayer"
"github.com/mitchellh/packer/packer/plugin"
)

Expand Down