Skip to content

Commit e976b0e

Browse files
committed
Clean up ruby packer build code
This commit makes the ruby invoking packer more flexible in what is passed to the packer binary so that a broader set of IaaS accounts can be used.
1 parent e576b74 commit e976b0e

File tree

8 files changed

+121
-61
lines changed

8 files changed

+121
-61
lines changed

lib/packer/config/aws.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def assume_role_parameters
6464
end
6565

6666
def instance_type
67-
'm4.large'
67+
'm5.large'
6868
end
6969

7070
def launch_block_device_mappings

lib/packer/config/gcp.rb

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,22 @@
33
module Packer
44
module Config
55
class Gcp
6-
def initialize(account_json:, project_id:, source_image:, image_family:, os:, output_directory:, version:, vm_prefix: '', mount_ephemeral_disk: false)
6+
def initialize(
7+
account_json:,
8+
project_id:,
9+
source_image:,
10+
image_family:,
11+
os:,
12+
output_directory:,
13+
version:,
14+
vm_prefix: '',
15+
mount_ephemeral_disk: false,
16+
root_disk_size: 32,
17+
omit_external_ip: false,
18+
vm_tags: ['winrm'],
19+
network: nil,
20+
network_project_id: nil,
21+
subnetwork: nil)
722
@account_json = account_json
823
@project_id = project_id
924
@source_image = source_image
@@ -13,32 +28,42 @@ def initialize(account_json:, project_id:, source_image:, image_family:, os:, ou
1328
@version = version
1429
@vm_prefix = vm_prefix.empty? ? 'packer' : vm_prefix
1530
@mount_ephemeral_disk = mount_ephemeral_disk
31+
@root_disk_size = root_disk_size
32+
@omit_external_ip = omit_external_ip
33+
@vm_tags = vm_tags
34+
@network = network
35+
@network_project_id = network_project_id
36+
@subnetwork = subnetwork
1637
end
1738

1839
def builders
1940
[
20-
{
21-
'type' => 'googlecompute',
22-
'credentials_json' => @account_json,
23-
'project_id' => @project_id,
24-
'tags' => ['winrm'],
25-
'source_image' => @source_image,
26-
'image_family' => @image_family,
27-
'zone' => 'us-east1-c',
28-
'disk_size' => 32,
29-
'image_name' => "packer-#{Time.now.to_i}",
30-
'machine_type' => 'n1-standard-4',
31-
'omit_external_ip' => false,
32-
'communicator' => 'winrm',
33-
'winrm_username' => 'winrmuser',
34-
'winrm_use_ssl' => false,
35-
'winrm_timeout' => '1h',
36-
'state_timeout' => '10m',
37-
'metadata' => {
38-
'sysprep-specialize-script-url' => 'https://raw.githubusercontent.com/cloudfoundry/bosh-windows-stemcell-builder/master/scripts/gcp/setup-winrm.ps1',
39-
'name' => "#{@vm_prefix}-#{Time.now.to_i}",
40-
}
41-
}
41+
{
42+
'type' => 'googlecompute',
43+
'credentials_json' => @account_json,
44+
'project_id' => @project_id,
45+
'tags' => @vm_tags,
46+
'source_image' => @source_image,
47+
'image_family' => @image_family,
48+
'zone' => 'us-west1-c',
49+
'disk_size' => @root_disk_size,
50+
'image_name' => "packer-#{Time.now.to_i}",
51+
'machine_type' => 'n1-standard-4',
52+
'network' => @network,
53+
'network_project_id' => @network_project_id,
54+
'subnetwork' => @subnetwork,
55+
'omit_external_ip' => @omit_external_ip,
56+
'use_internal_ip' => @omit_external_ip,
57+
'communicator' => 'winrm',
58+
'winrm_username' => 'winrmuser',
59+
'winrm_use_ssl' => false,
60+
'winrm_timeout' => '1h',
61+
'state_timeout' => '10m',
62+
'metadata' => {
63+
'sysprep-specialize-script-url' => 'https://raw.githubusercontent.com/cloudfoundry/bosh-windows-stemcell-builder/master/scripts/gcp/setup-winrm.ps1',
64+
'name' => "#{@vm_prefix}-#{Time.now.to_i}",
65+
}.compact_blank!
66+
}
4267
]
4368
end
4469

@@ -48,8 +73,8 @@ def provisioners
4873

4974
def dump
5075
JSON.dump(
51-
'builders' => builders,
52-
'provisioners' => provisioners
76+
'builders' => builders,
77+
'provisioners' => provisioners
5378
)
5479
end
5580
end

lib/packer/config/provisioners.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def initialize(os, iaas, enable_ephemeral_disk, version, http_proxy = nil, https
1717
@ephemeral_disk_flag = enable_ephemeral_disk ? ' -EnableEphemeralDiskMounting' : ''
1818
@version = version
1919
@proxy_settings = http_proxy || https_proxy ? "\\\"#{http_proxy}\\\" \\\"#{https_proxy}\\\" \\\"#{bypass_list}\\\"" : ''
20-
@installWindowsUpdates = (build_context != :patchfile)
20+
@install_windows_updates = (build_context != :patchfile)
2121

2222
filename = File.expand_path("../templates/provision_#{os}.json.erb", __FILE__)
2323
@erb = ERB.new(File.read(filename))
@@ -31,7 +31,7 @@ def dump
3131
password: SecureRandom.hex(10) + "!",
3232
ephemeral_disk_flag: @ephemeral_disk_flag,
3333
proxy_settings: @proxy_settings,
34-
install_windows_updates: @installWindowsUpdates,
34+
install_windows_updates: @install_windows_updates,
3535
stemcell_version: @version,
3636
})
3737
JSON.parse(result)

lib/stemcell/builder/gcp.rb

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ class Builder
33
class Gcp < Base
44
attr_accessor :image_url
55

6-
def initialize(account_json:, source_image:, image_family:, vm_prefix:, **kwargs)
6+
def initialize(account_json:,
7+
source_image:,
8+
image_family:,
9+
vm_prefix:,
10+
network:,
11+
network_project_id:,
12+
subnetwork:,
13+
**kwargs)
714
@account_json = account_json
815
@project_id = JSON.parse(@account_json)['project_id']
916
@source_image = source_image
1017
@image_family = image_family
1118
@vm_prefix = vm_prefix
19+
@network = network
20+
@network_project_id = network_project_id
21+
@subnetwork = subnetwork
1222
super(**kwargs)
1323
end
1424

@@ -25,38 +35,42 @@ def build
2535
end
2636

2737
private
28-
def packer_config
29-
Packer::Config::Gcp.new(
30-
account_json: @account_json,
31-
project_id: @project_id,
32-
source_image: @source_image,
33-
output_directory: @output_directory,
34-
image_family: @image_family,
35-
os: @os,
36-
version: @version,
37-
vm_prefix: @vm_prefix,
38-
mount_ephemeral_disk: @mount_ephemeral_disk
39-
).dump
40-
end
4138

42-
def parse_packer_output(packer_output)
43-
image_name = nil
44-
packer_output.each_line do |line|
45-
Output.say line
46-
image_name ||= parse_image_name(line)
47-
end
48-
get_image_url(image_name)
49-
end
39+
def packer_config
40+
Packer::Config::Gcp.new(
41+
account_json: @account_json,
42+
project_id: @project_id,
43+
source_image: @source_image,
44+
output_directory: @output_directory,
45+
image_family: @image_family,
46+
network: @network,
47+
network_project_id: @network_project_id,
48+
subnetwork: @subnetwork,
49+
os: @os,
50+
version: @version,
51+
vm_prefix: @vm_prefix,
52+
mount_ephemeral_disk: @mount_ephemeral_disk
53+
).dump
54+
end
5055

51-
def parse_image_name(line)
52-
if line.include?(",artifact,0,id,")
53-
return line.split(",").last.chomp
54-
end
56+
def parse_packer_output(packer_output)
57+
image_name = nil
58+
packer_output.each_line do |line|
59+
Output.say line
60+
image_name ||= parse_image_name(line)
5561
end
62+
get_image_url(image_name)
63+
end
5664

57-
def get_image_url(image_name)
58-
"https://www.googleapis.com/compute/v1/projects/#{@project_id}/global/images/#{image_name}"
65+
def parse_image_name(line)
66+
if line.include?(",artifact,0,id,")
67+
line.split(",").last.chomp
5968
end
69+
end
70+
71+
def get_image_url(image_name)
72+
"https://www.googleapis.com/compute/v1/projects/#{@project_id}/global/images/#{image_name}"
73+
end
6074
end
6175
end
6276
end

lib/tasks/build/gcp.rake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ namespace :build do
2727
gcp_builder = Stemcell::Builder::Gcp.new(
2828
account_json: Stemcell::Builder::validate_env('ACCOUNT_JSON'),
2929
os: Stemcell::Builder::validate_env('OS_VERSION'),
30+
network: ENV.fetch('GCP_NETWORK', ''), # optional
31+
network_project_id: ENV.fetch('GCP_NETWORK_PROJECT_ID', ''), # optional
32+
subnetwork: ENV.fetch('GCP_SUBNETWORK', ''), # optional
3033
output_directory: output_directory,
3134
packer_vars: {},
3235
source_image: source_image,

spec/packer/config/aws_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
secret_key: 'some-aws-secret-key',
4444
region: 'region1',
4545
source_ami: 'baseami1',
46-
instance_type: 'm4.large',
46+
instance_type: 'm5.large',
4747
vpc_id: 'vpc1',
4848
subnet_id: 'subnet1',
4949
associate_public_ip_address: true,

spec/packer/config/gcp_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
'tags' => ['winrm'],
2929
'source_image' => 'some-source-image',
3030
'image_family' => 'some-image-family',
31-
'zone' => 'us-east1-c',
31+
'zone' => 'us-west1-c',
3232
'disk_size' => 32,
3333
'machine_type' => 'n1-standard-4',
3434
'omit_external_ip' => false,

spec/stemcell/builder/gcp_spec.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
source_image = "some-source-image"
2828
image_family= "some-family"
2929
vm_prefix = "some-vm-prefix"
30+
network = 'default'
31+
network_project_id = 'project-id'
32+
subnetwork = 'subnet'
3033

3134
packer_config = double(:packer_config)
3235
allow(packer_config).to receive(:dump).and_return(config)
@@ -39,7 +42,10 @@
3942
os: os,
4043
version: version,
4144
vm_prefix: vm_prefix,
42-
mount_ephemeral_disk: false
45+
mount_ephemeral_disk: false,
46+
network: network,
47+
network_project_id: network_project_id,
48+
subnetwork: subnetwork,
4349
).and_return(packer_config)
4450

4551
packer_runner = double(:packer_runner)
@@ -71,7 +77,10 @@
7177
source_image: source_image,
7278
image_family: image_family,
7379
vm_prefix: vm_prefix,
74-
mount_ephemeral_disk: "false"
80+
mount_ephemeral_disk: "false",
81+
network: network,
82+
network_project_id: network_project_id,
83+
subnetwork: subnetwork,
7584
).build
7685
expect(stemcell_path).to eq('path-to-stemcell')
7786
end
@@ -85,6 +94,9 @@
8594
packer_vars = 'some-packer-vars'
8695
os = 'windows2019'
8796
vm_prefix = 'some-vm-prefix'
97+
network = 'default'
98+
network_project_id = 'project-id'
99+
subnetwork = 'subnet'
88100

89101
packer_config = double(:packer_config)
90102
allow(packer_config).to receive(:dump).and_return('some-packer-config')
@@ -97,7 +109,10 @@
97109
os: os,
98110
version: '',
99111
vm_prefix: vm_prefix,
100-
mount_ephemeral_disk: false
112+
mount_ephemeral_disk: false,
113+
network: network,
114+
network_project_id: network_project_id,
115+
subnetwork: subnetwork,
101116
).and_return(packer_config)
102117

103118
packer_runner = double(:packer_runner)
@@ -110,6 +125,9 @@
110125
output_directory: output_directory,
111126
version: '',
112127
packer_vars: packer_vars,
128+
network: network,
129+
network_project_id: network_project_id,
130+
subnetwork: subnetwork,
113131
account_json: account_json,
114132
source_image: source_image,
115133
image_family: image_family,

0 commit comments

Comments
 (0)