Skip to content

Commit 114a8ed

Browse files
authored
Merge pull request #49 from IBM/fix-parallel-build
Feature request - Parallel builds
2 parents 121277a + b0ad0e3 commit 114a8ed

File tree

7 files changed

+121
-12
lines changed

7 files changed

+121
-12
lines changed

builder/ibmcloud/vpc/config.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
126126

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

134135
if errs != nil && len(errs.Errors) > 0 {
135136
return nil, errs

builder/ibmcloud/vpc/step_create_ssh_key_pair.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"fmt"
1010
"io/ioutil"
1111
"os"
12+
"strconv"
1213
"strings"
14+
"time"
1315

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

2123
func (s *stepCreateSshKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2224
ui := state.Get("ui").(packer.Ui)
23-
25+
nsec := time.Now().UnixNano()
2426
ui.Say("Creating SSH Public and Private Key Pair...")
25-
keysDirectory := "ssh_keys/"
27+
keysDirectory := strconv.FormatInt(nsec, 10) + "ssh_keys/"
28+
state.Put("keysDirectory", keysDirectory)
2629
privatefilepath := keysDirectory + "id_rsa"
2730
publicfilepath := keysDirectory + "id_rsa.pub"
2831

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

114117
func (s *stepCreateSshKeyPair) Cleanup(state multistep.StateBag) {
115118
ui := state.Get("ui").(packer.Ui)
116-
119+
keysDirectory := state.Get("keysDirectory").(string)
117120
ui.Say("Deleting Public and Private SSH Key Pair...")
118-
err := os.RemoveAll("ssh_keys")
121+
err := os.RemoveAll(keysDirectory)
119122
if err != nil {
120123
err := fmt.Errorf("[ERROR] Failed to delete SSH Key folder: %s", err)
121124
state.Put("error", err)

developer/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ run-centos:
2424
cd ..; packer validate -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos.pkr.hcl
2525
cd ..; packer build -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos.pkr.hcl
2626

27+
run-centos-parallel:
28+
cd ..; packer validate -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos-parallel.pkr.hcl
29+
cd ..; packer build -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos-parallel.pkr.hcl
30+
31+
2732
run-centos-ansible:
2833
cd ..; packer validate -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos-ansible.pkr.hcl
2934
cd ..; packer build -var-file="developer/variables.pkrvars.hcl" developer/examples/build.vpc.centos-ansible.pkr.hcl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// packer {
2+
// required_plugins {
3+
// ibmcloud = {
4+
// version = ">=v3.0.0"
5+
// source = "github.com/IBM/ibmcloud"
6+
// }
7+
// }
8+
// }
9+
10+
variable "IBM_API_KEY" {
11+
type = string
12+
}
13+
14+
variable "SUBNET_ID" {
15+
type = string
16+
}
17+
18+
variable "REGION" {
19+
type = string
20+
}
21+
22+
variable "RESOURCE_GROUP_ID" {
23+
type = string
24+
}
25+
26+
variable "SECURITY_GROUP_ID" {
27+
type = string
28+
}
29+
// variable "VPC_URL" {
30+
// type = string
31+
// }
32+
// variable "IAM_URL" {
33+
// type = string
34+
// }
35+
36+
37+
locals {
38+
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
39+
}
40+
41+
source "ibmcloud-vpc" "centos" {
42+
api_key = var.IBM_API_KEY
43+
region = var.REGION
44+
subnet_id = var.SUBNET_ID
45+
resource_group_id = var.RESOURCE_GROUP_ID
46+
security_group_id = var.SECURITY_GROUP_ID
47+
48+
vsi_base_image_name = "ibm-centos-7-9-minimal-amd64-5"
49+
50+
vsi_profile = "bx2-2x8"
51+
vsi_interface = "public"
52+
vsi_user_data_file = ""
53+
54+
image_name = "packer-${local.timestamp}-1"
55+
56+
communicator = "ssh"
57+
ssh_username = "root"
58+
ssh_port = 22
59+
ssh_timeout = "15m"
60+
61+
timeout = "30m"
62+
}
63+
64+
source "ibmcloud-vpc" "centos-other" {
65+
api_key = var.IBM_API_KEY
66+
region = var.REGION
67+
subnet_id = var.SUBNET_ID
68+
resource_group_id = var.RESOURCE_GROUP_ID
69+
security_group_id = var.SECURITY_GROUP_ID
70+
71+
vsi_base_image_name = "ibm-centos-7-9-minimal-amd64-5"
72+
73+
vsi_profile = "bx2-2x8"
74+
vsi_interface = "public"
75+
vsi_user_data_file = ""
76+
77+
image_name = "packer-${local.timestamp}-2"
78+
79+
communicator = "ssh"
80+
ssh_username = "root"
81+
ssh_port = 22
82+
ssh_timeout = "15m"
83+
84+
timeout = "30m"
85+
}
86+
87+
build {
88+
sources = [
89+
"source.ibmcloud-vpc.centos",
90+
"source.ibmcloud-vpc.centos-other"
91+
]
92+
93+
provisioner "shell" {
94+
execute_command = "{{.Vars}} bash '{{.Path}}'"
95+
inline = [
96+
"echo 'Hello from IBM Cloud Packer Plugin - VPC Infrastructure'",
97+
"echo 'Hello from IBM Cloud Packer Plugin - VPC Infrastructure' >> /hello.txt"
98+
]
99+
}
100+
}

developer/variables.pkrvars.hcl

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ SUBNET_ID = " "
22
REGION = " "
33
SECURITY_GROUP_ID = " "
44
RESOURCE_GROUP_ID = " "
5-
IBM_API_KEY = " "
5+
IBM_API_KEY = " "

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/hashicorp/hcl/v2 v2.13.0
99
github.com/hashicorp/packer-plugin-sdk v0.3.0
1010
github.com/zclconf/go-cty v1.10.0
11-
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
11+
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
1212
)
1313

1414
require (

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
716716
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
717717
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
718718
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
719-
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
720-
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
719+
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
720+
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
721721
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
722722
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
723723
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=

0 commit comments

Comments
 (0)