/
Rakefile
180 lines (148 loc) · 7.7 KB
/
Rakefile
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#
# Copyright 2016 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'erb'
require 'fileutils'
require 'yaml'
namespace :gocd_vm_builder do
task :init do
vm_config = load_vm_config
FileUtils.mkdir_p "#{vm_config['gocd_vm_builder']['output_dir']}"
FileUtils.mkdir_p "#{vm_config['gocd_vm_builder']['packer_dir']}"
FileUtils.mkdir_p "#{vm_config['gocd_vm_builder']['http_dir']}"
end
task :cleanup do
vm_config = load_vm_config
FileUtils.rm_rf Dir.glob("#{vm_config['gocd_vm_builder']['output_dir']}/*")
FileUtils.rm_rf Dir.glob("#{vm_config['gocd_vm_builder']['http_dir']}/*")
FileUtils.rm_rf Dir.glob("#{vm_config['gocd_vm_builder']['packer_dir']}/*")
end
task :default do
Rake::Task["gocd_vm_builder:cleanup"].invoke
Rake::Task["gocd_vm_builder:init"].invoke
case ENV['TARGET'].nil? ? "not_exists" : ENV['TARGET']
when 'centos'
puts "Building CentOS VM"
Rake::Task["gocd_vm_builder:centos_vm"].invoke()
when 'ubuntu'
puts "Building Ubuntu VM"
Rake::Task["gocd_vm_builder:ubuntu_vm"].invoke()
else
puts "Nothing to build"
end
end
task :ubuntu_vm do
vm_distro_version = ENV['VERSION'].nil? ? "" : ENV['VERSION']
vm_target = ENV['TARGET'].nil? ? "" : ENV['TARGET']
vm_config = load_vm_config
ubuntu_url=vm_config['ubuntu'][vm_distro_version]['url']
ubuntu_iso_url=vm_config['ubuntu'][vm_distro_version]['iso_url']
ubuntu_iso_checksum=vm_config['ubuntu'][vm_distro_version]['iso_checksum']
ubuntu_iso_checksum_type=vm_config['ubuntu'][vm_distro_version]['iso_checksum_type']
puts "Generate Kickstart Script"
render_template("#{vm_config['gocd_vm_builder']['http_template_dir']}/#{vm_target}#{vm_distro_version.to_i.to_s}/preseed.cfg.erb",
"#{vm_config['gocd_vm_builder']['http_dir']}/preeed.cfg",
binding)
puts "Generate Packer Configuration"
render_template("#{vm_config['gocd_vm_builder']['packer_template_dir']}/#{vm_target}#{vm_distro_version.to_i.to_s}/packer.json.erb",
"#{vm_config['gocd_vm_builder']['packer_dir']}/packer.json",
binding)
puts "Packer start building VM"
output= %x{packer build "#{File.dirname(__FILE__)}"/"#{vm_config['gocd_vm_builder']['packer_dir']}"/packer.json}
puts output
upload_openstack_image({
:packer_vm_version => vm_distro_version,
:output_dir => vm_config['gocd_vm_builder']['output_dir'],
:vm_target => vm_target,
:gocd_agent => vm_config['gocd_vm_builder']['with_gocd_agent'],
:cloud_init => vm_config['gocd_vm_builder']['with_cloud_init']
})
end
task :centos_vm do
vm_distro_version = ENV['VERSION'].nil? ? "" : ENV['VERSION']
vm_target = ENV['TARGET'].nil? ? "" : ENV['TARGET']
vm_config = load_vm_config
centos_url=vm_config['centos'][vm_distro_version]['url']
epel_url=vm_config['epel'][vm_distro_version.to_i.to_s]['url']
centos_iso_url=vm_config['centos'][vm_distro_version]['iso_url']
centos_iso_checksum=vm_config['centos'][vm_distro_version]['iso_checksum']
centos_iso_checksum_type=vm_config['centos'][vm_distro_version]['iso_checksum_type']
puts "Generate Kickstart Script"
render_template("#{vm_config['gocd_vm_builder']['http_template_dir']}/#{vm_target}#{vm_distro_version.to_i.to_s}/ks.cfg.erb",
"#{vm_config['gocd_vm_builder']['http_dir']}/ks.cfg",
binding)
puts "Generate Packer Configuration"
render_template("#{vm_config['gocd_vm_builder']['packer_template_dir']}/#{vm_target}#{vm_distro_version.to_i.to_s}/packer.json.erb",
"#{vm_config['gocd_vm_builder']['packer_dir']}/packer.json",
binding)
puts "Packer start building VM"
output= %x{packer build "#{File.dirname(__FILE__)}"/"#{vm_config['gocd_vm_builder']['packer_dir']}"/packer.json}
puts output
upload_openstack_image({
:packer_vm_version => vm_distro_version,
:output_dir => vm_config['gocd_vm_builder']['output_dir'],
:vm_target => vm_target,
:gocd_agent => vm_config['gocd_vm_builder']['with_gocd_agent'],
:cloud_init => vm_config['gocd_vm_builder']['with_cloud_init']
})
end
end
def upload_openstack_image(options={})
puts "Upload the Image to OpenStack"
get_image_list = "glance image-list --owner #{ENV['OS_TENANT_ID']} --property-filter packer_vm_version=#{options[:vm_target]}-#{options[:packer_vm_version]} --property-filter gocd_agent=#{ENV['GOCD_AGENT'] || options[:gocd_agent]} --property-filter cloud_init=#{ENV['CLOUD_INIT'] || options[:cloud_init]}"
get_image = IO.popen(get_image_list)
output = get_image.readlines(sep="\n")
get_image.close
if output.size > 5
fail "Glance found more than one image. Please clean up the image before you run this task again"
elsif output.size < 5
puts "========================================================================"
puts "Glance cannot found image to delete : #{ENV['OS_TENANT_ID']} packer_vm_version=#{options[:vm_target]}-#{options[:packer_vm_version]} gocd_agent=#{ENV['GOCD_AGENT'] || options[:gocd_agent]} cloud_init=#{ENV['CLOUD_INIT'] || options[:cloud_init]}\n#{output}"
puts "========================================================================"
else
output.each do |line|
image_data = line.split(/[|\s]+/)
if image_data[6].eql?("active")
remove_image_list = "glance image-delete #{image_data[1]}"
remove_image = IO.popen(remove_image_list)
check_result = remove_image.readlines
remove_image.close
get_image_list = "glance image-list --owner #{ENV['OS_TENANT_ID']} --property-filter packer_vm_version=#{options[:vm_target]}-#{options[:packer_vm_version]} --property-filter gocd_agent=#{ENV['GOCD_AGENT'] || options[:gocd_agent]} --property-filter cloud_init=#{ENV['CLOUD_INIT'] || options[:cloud_init]}"
get_image = IO.popen(get_image_list)
output = get_image.readlines(sep="\n")
get_image.close
fail "Glance cannot delete image with id #{image_data[1]}" unless output.size == 4
end
end
end
upload_image_cmd = "glance image-create --owner #{ENV['OS_TENANT_ID']} --name packer-#{options[:vm_target]}-#{options[:packer_vm_version]} --property vm_build_version=#{ENV['vm_build_version'] || "0.0"} --property packer_vm_version=#{options[:vm_target]}-#{options[:packer_vm_version]} --property gocd_agent=#{ENV['GOCD_AGENT'] || options[:gocd_agent]} --property cloud_init=#{ENV['CLOUD_INIT'] || options[:cloud_init]} --human-readable --disk-format qcow2 --container-format bare --progress --file #{options[:output_dir]}/#{options[:vm_target]}#{options[:packer_vm_version].to_i.to_s}/qemu-#{options[:vm_target]}#{options[:packer_vm_version]}#{ (ENV['CLOUD_INIT'] || options[:cloud_init]) ? "-cloud_init" : ""}#{ (ENV['GOCD_AGENT'] || options[:gocd_agent]) ? "-goagent" : ""}-x86_64.img"
puts upload_image_cmd
upload_image = IO.popen(upload_image_cmd)
output = upload_image.readlines(sep="\n")
puts output
upload_image.close
end
def render_template(template, output, scope)
tmpl = File.read(template)
erb = ERB.new(tmpl, 0, "<>")
File.open(output, "w") do |f|
f.puts erb.result(scope)
end
end
def load_vm_config
if File.exists? "vm_config.yaml"
YAML.load_file("vm_config.yaml")
end
end