Skip to content

Commit

Permalink
Merge pull request #49 from IBM/fix-parallel-build
Browse files Browse the repository at this point in the history
Feature request - Parallel builds
  • Loading branch information
juanpinzon authored Jul 27, 2022
2 parents 121277a + b0ad0e3 commit 114a8ed
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 12 deletions.
9 changes: 5 additions & 4 deletions builder/ibmcloud/vpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {

// Naming temporary infrastructure created during packer execution
UniqueID := "packer-vpc"
c.VSIName = fmt.Sprintf("%s-vsi-%d%d%d", UniqueID, currentTime.Hour(), currentTime.Minute(), currentTime.Second())
c.VpcSshKeyName = fmt.Sprintf("%s-ssh-key-%d%d%d", UniqueID, currentTime.Hour(), currentTime.Minute(), currentTime.Second())
c.SecurityGroupName = fmt.Sprintf("%s-security-group-%d%d%d", UniqueID, currentTime.Hour(), currentTime.Minute(), currentTime.Second())
c.FloatingIPName = fmt.Sprintf("%s-floating-ip-%d%d%d", UniqueID, currentTime.Hour(), currentTime.Minute(), currentTime.Second())
timestamp := time.Now().UnixNano()
c.VSIName = fmt.Sprintf("%s-vsi-%d", UniqueID, timestamp)
c.VpcSshKeyName = fmt.Sprintf("%s-ssh-key-%d", UniqueID, timestamp)
c.SecurityGroupName = fmt.Sprintf("%s-security-group-%d", UniqueID, timestamp)
c.FloatingIPName = fmt.Sprintf("%s-floating-ip-%d", UniqueID, timestamp)

if errs != nil && len(errs.Errors) > 0 {
return nil, errs
Expand Down
11 changes: 7 additions & 4 deletions builder/ibmcloud/vpc/step_create_ssh_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
Expand All @@ -20,9 +22,10 @@ type stepCreateSshKeyPair struct{}

func (s *stepCreateSshKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)

nsec := time.Now().UnixNano()
ui.Say("Creating SSH Public and Private Key Pair...")
keysDirectory := "ssh_keys/"
keysDirectory := strconv.FormatInt(nsec, 10) + "ssh_keys/"
state.Put("keysDirectory", keysDirectory)
privatefilepath := keysDirectory + "id_rsa"
publicfilepath := keysDirectory + "id_rsa.pub"

Expand Down Expand Up @@ -113,9 +116,9 @@ func (s *stepCreateSshKeyPair) Run(_ context.Context, state multistep.StateBag)

func (s *stepCreateSshKeyPair) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)

keysDirectory := state.Get("keysDirectory").(string)
ui.Say("Deleting Public and Private SSH Key Pair...")
err := os.RemoveAll("ssh_keys")
err := os.RemoveAll(keysDirectory)
if err != nil {
err := fmt.Errorf("[ERROR] Failed to delete SSH Key folder: %s", err)
state.Put("error", err)
Expand Down
5 changes: 5 additions & 0 deletions developer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ run-centos:
cd ..; packer validate -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos.pkr.hcl
cd ..; packer build -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos.pkr.hcl

run-centos-parallel:
cd ..; packer validate -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos-parallel.pkr.hcl
cd ..; packer build -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos-parallel.pkr.hcl


run-centos-ansible:
cd ..; packer validate -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos-ansible.pkr.hcl
cd ..; packer build -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos-ansible.pkr.hcl
Expand Down
100 changes: 100 additions & 0 deletions developer/examples/build.vpc.centos-parallel.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// packer {
// required_plugins {
// ibmcloud = {
// version = ">=v3.0.0"
// source = "github.com/IBM/ibmcloud"
// }
// }
// }

variable "IBM_API_KEY" {
type = string
}

variable "SUBNET_ID" {
type = string
}

variable "REGION" {
type = string
}

variable "RESOURCE_GROUP_ID" {
type = string
}

variable "SECURITY_GROUP_ID" {
type = string
}
// variable "VPC_URL" {
// type = string
// }
// variable "IAM_URL" {
// type = string
// }


locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}

source "ibmcloud-vpc" "centos" {
api_key = var.IBM_API_KEY
region = var.REGION
subnet_id = var.SUBNET_ID
resource_group_id = var.RESOURCE_GROUP_ID
security_group_id = var.SECURITY_GROUP_ID

vsi_base_image_name = "ibm-centos-7-9-minimal-amd64-5"

vsi_profile = "bx2-2x8"
vsi_interface = "public"
vsi_user_data_file = ""

image_name = "packer-${local.timestamp}-1"

communicator = "ssh"
ssh_username = "root"
ssh_port = 22
ssh_timeout = "15m"

timeout = "30m"
}

source "ibmcloud-vpc" "centos-other" {
api_key = var.IBM_API_KEY
region = var.REGION
subnet_id = var.SUBNET_ID
resource_group_id = var.RESOURCE_GROUP_ID
security_group_id = var.SECURITY_GROUP_ID

vsi_base_image_name = "ibm-centos-7-9-minimal-amd64-5"

vsi_profile = "bx2-2x8"
vsi_interface = "public"
vsi_user_data_file = ""

image_name = "packer-${local.timestamp}-2"

communicator = "ssh"
ssh_username = "root"
ssh_port = 22
ssh_timeout = "15m"

timeout = "30m"
}

build {
sources = [
"source.ibmcloud-vpc.centos",
"source.ibmcloud-vpc.centos-other"
]

provisioner "shell" {
execute_command = "{{.Vars}} bash '{{.Path}}'"
inline = [
"echo 'Hello from IBM Cloud Packer Plugin - VPC Infrastructure'",
"echo 'Hello from IBM Cloud Packer Plugin - VPC Infrastructure' >> /hello.txt"
]
}
}
2 changes: 1 addition & 1 deletion developer/variables.pkrvars.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ SUBNET_ID = " "
REGION = " "
SECURITY_GROUP_ID = " "
RESOURCE_GROUP_ID = " "
IBM_API_KEY = " "
IBM_API_KEY = " "
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/hcl/v2 v2.13.0
github.com/hashicorp/packer-plugin-sdk v0.3.0
github.com/zclconf/go-cty v1.10.0
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down

0 comments on commit 114a8ed

Please sign in to comment.