forked from ods94065/packer-puppet-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
154 lines (138 loc) · 4.13 KB
/
Rakefile
File metadata and controls
154 lines (138 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'erb'
require 'json'
require 'yaml'
require 'rake/clean'
current_dir = File.dirname(__FILE__)
build_dir = "#{current_dir}/build"
puppet_build_dir = "#{build_dir}/puppet"
packs_build_dir = "#{build_dir}/packs"
directory build_dir
directory puppet_build_dir => [build_dir]
directory packs_build_dir => [build_dir]
nodes_file = "#{current_dir}/nodes.yaml"
def generate_puppet_manifest(data, anIO)
data['classes'].each do |klass|
anIO.puts("include #{klass}")
end
end
def generate_packer_variables(node_name, data)
return {
'aws_access_key' => '',
'aws_secret_key' => '',
}
end
def generate_packer_builders(node_name, data)
return [{
'type' => 'amazon-ebs',
'access_key' => '{{user `aws_access_key`}}',
'secret_key' => '{{user `aws_secret_key`}}',
'region' => 'us-east-1',
'source_ami' => 'ami-de0d9eb7',
'instance_type' => 't1.micro',
'ssh_username' => 'ubuntu',
'ami_name' => "packer-#{node_name} {{timestamp}}",
}]
end
def generate_packer_provisioners(node_name, data)
return [
{
'type' => 'shell',
'inline' => [
"echo 'Waiting 30s for box to initialize...'",
'sleep 30',
]
},
{
'type' => 'shell',
'scripts' => [
'scripts/install_puppet.sh',
'scripts/install_common_puppet_modules.sh',
]
},
{
'type' => 'file',
'source' => 'puppet/hieradata',
'destination' => '/tmp/hieradata',
},
{
'type' => 'file',
'source' => 'puppet/modules',
'destination' => '/tmp/puppet-modules',
},
{
'type' => 'shell',
'inline' => [
'sudo cp -r /tmp/hieradata /etc/puppet/hieradata',
'sudo cp -r /tmp/puppet-modules/* /etc/puppet/modules',
'rm -rf /tmp/{hieradata,puppet-modules}',
]
},
{
'type' => 'puppet-masterless',
'manifest_file' => "puppet/manifests/#{node_name}.pp",
'hiera_config_path' => 'puppet/hiera.yaml',
'execute_command' => %Q[{{.FacterVars}}{{if .Sudo}} sudo -E {{end}}puppet apply --verbose --detailed-exitcodes {{if ne .HieraConfigPath ""}}--hiera_config={{.HieraConfigPath}} {{end}}{{.ManifestFile}}],
},
]
end
def generate_packer_obj(node_name, data)
return {
'variables' => generate_packer_variables(node_name, data),
'builders' => generate_packer_builders(node_name, data),
'provisioners' => generate_packer_provisioners(node_name, data),
}
end
namespace :packs do
namespace :all do
desc "Validates all Packer specs"
task :validate
desc "Inspects all Packer specs"
task :inspect
desc "Builds all Packer images"
task :build
end
end
nodes = YAML.load_file(nodes_file)
nodes.each_pair do |node_name, data|
if node_name == 'all'
raise NameError("'all' is a special name; you cannot define a node with that name.")
end
puppet_manifest_file = "#{puppet_build_dir}/#{node_name}.pp"
packer_build_file = "#{packs_build_dir}/#{node_name}.json"
file puppet_manifest_file => [puppet_build_dir] do |t|
puts "creating #{puppet_manifest_file}"
File.open(t.name, 'w') do |f|
generate_puppet_manifest(data, f)
end
end
file packer_build_file => [packs_build_dir] do |t|
puts "creating #{packer_build_file}"
File.open(t.name, 'w') do |f|
JSON.dump(generate_packer_obj(node_name, data), f)
end
end
namespace :packs do
namespace node_name do
desc "Validates the Packer spec for #{node_name}"
task :validate => [packer_build_file] do
sh "packer validate #{packer_build_file}"
end
desc "Inspecs the Packer spec for #{node_name}"
task :inspect => [packer_build_file] do
sh "packer inspect #{packer_build_file}"
end
desc "Builds Packer images for #{node_name}"
task :build => [puppet_manifest_file, packer_build_file] do
sh %Q[packer build -var-file #{current_dir}/credentials.json #{packer_build_file}]
end
end
namespace :all do
task :validate => ["packs:#{node_name}:validate"]
task :inspect => ["packs:#{node_name}:inspect"]
task :build => ["packs:#{node_name}:build"]
end
end
end
if File.directory? build_dir
CLEAN << build_dir
end