dev-lxc 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,339 +0,0 @@
1
- require "dev-lxc/container"
2
- require "dev-lxc/chef-cluster"
3
-
4
- module DevLXC
5
- class ChefServer
6
- attr_reader :role, :server
7
-
8
- def initialize(name, cluster_config)
9
- unless cluster_config["servers"].keys.include?(name)
10
- raise "Server #{name} is not defined in the cluster config"
11
- end
12
- cluster = DevLXC::ChefCluster.new(cluster_config)
13
- @server = DevLXC::Container.new(name)
14
- @config = cluster_config["servers"][@server.name]
15
- @ipaddress = @config["ipaddress"]
16
- @role = @config["role"] ? @config["role"] : cluster_config['topology']
17
- @mounts = cluster_config["mounts"]
18
- @bootstrap_backend = cluster.bootstrap_backend
19
- @chef_server_config = cluster.chef_server_config
20
- @api_fqdn = cluster_config["api_fqdn"]
21
- @analytics_fqdn = cluster_config["analytics_fqdn"]
22
- @platform_container_name = cluster_config["platform_container"]
23
- @packages = cluster_config["packages"]
24
-
25
- if File.basename(@packages["server"]).match(/^(\w+-\w+.*)[_-]((?:\d+\.?){3,})-/)
26
- @chef_server_type = Regexp.last_match[1]
27
- @chef_server_version = Regexp.last_match[2].gsub(".", "-")
28
- end
29
-
30
- if @role == 'analytics'
31
- @shared_container_name = "s#{@platform_container_name[1..-1]}"
32
- @shared_container_name += "-analytics-#{Regexp.last_match[1].gsub(".", "-")}" if @packages["analytics"].to_s.match(/[_-]((\d+\.?){3,})-/)
33
- else
34
- @shared_container_name = "s#{@platform_container_name[1..-1]}"
35
- case @chef_server_type
36
- when 'chef-server-core'
37
- @shared_container_name += '-cs'
38
- @server_ctl = 'chef-server'
39
- when 'private-chef'
40
- @shared_container_name += '-ec'
41
- @server_ctl = 'private-chef'
42
- when 'chef-server'
43
- @shared_container_name += '-osc'
44
- @server_ctl = 'chef-server'
45
- end
46
- @shared_container_name += "-#{@chef_server_version}"
47
- @shared_container_name += "-reporting-#{Regexp.last_match[1].gsub(".", "-")}" if @packages["reporting"].to_s.match(/[_-]((\d+\.?){3,})-/)
48
- @shared_container_name += "-pushy-#{Regexp.last_match[1].gsub(".", "-")}" if @packages["push-jobs-server"].to_s.match(/[_-]((\d+\.?){3,})-/)
49
- end
50
- end
51
-
52
- def status
53
- if @server.defined?
54
- state = @server.state
55
- ip_addresses = @server.ip_addresses.join(" ") if @server.state == :running
56
- else
57
- state = "not_created"
58
- end
59
- printf "%20s %-15s %s\n", @server.name, state, ip_addresses
60
- end
61
-
62
- def abspath(rootfs_path)
63
- "#{@server.config_item('lxc.rootfs')}#{rootfs_path}" if @server.defined?
64
- end
65
-
66
- def run_command(command)
67
- if @server.running?
68
- puts "Running '#{command}' in #{@server.name}"
69
- @server.run_command(command)
70
- else
71
- puts "#{@server.name} is not running"
72
- end
73
- end
74
-
75
- def start
76
- create
77
- hwaddr = @server.config_item("lxc.network.0.hwaddr")
78
- DevLXC.assign_ip_address(@ipaddress, @server.name, hwaddr)
79
- DevLXC.create_dns_record(@analytics_fqdn, @server.name, @ipaddress) if @role == 'analytics'
80
- DevLXC.create_dns_record(@api_fqdn, @server.name, @ipaddress) if %w(open-source standalone frontend).include?(@role)
81
- @server.sync_mounts(@mounts)
82
- @server.start
83
- end
84
-
85
- def stop
86
- hwaddr = @server.config_item("lxc.network.0.hwaddr") if @server.defined?
87
- @server.stop
88
- deregister_from_dnsmasq(hwaddr)
89
- end
90
-
91
- def destroy
92
- hwaddr = @server.config_item("lxc.network.0.hwaddr") if @server.defined?
93
- @server.destroy
94
- deregister_from_dnsmasq(hwaddr)
95
- end
96
-
97
- def deregister_from_dnsmasq(hwaddr)
98
- DevLXC.search_file_delete_line("/etc/lxc/addn-hosts.conf", /^#{@ipaddress}\s/)
99
- DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /,#{@ipaddress}$/)
100
- unless hwaddr.nil?
101
- DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /^#{hwaddr}/)
102
- end
103
- DevLXC.reload_dnsmasq
104
- end
105
-
106
- def destroy_container(type)
107
- case type
108
- when :unique
109
- DevLXC::Container.new("u-#{@server.name}").destroy
110
- when :shared
111
- DevLXC::Container.new(@shared_container_name).destroy
112
- when :platform
113
- DevLXC::Container.new(@platform_container_name).destroy
114
- end
115
- end
116
-
117
- def create
118
- if @server.defined?
119
- puts "Using existing container #{@server.name}"
120
- return
121
- end
122
- unique_container = DevLXC::Container.new("u-#{@server.name}")
123
- if unique_container.defined?
124
- puts "Cloning shared container #{unique_container.name} into container #{@server.name}"
125
- unique_container.clone(@server.name, {:flags => LXC::LXC_CLONE_SNAPSHOT|LXC::LXC_CLONE_KEEPMACADDR})
126
- @server = DevLXC::Container.new(@server.name)
127
- return
128
- else
129
- puts "Creating container #{@server.name}"
130
- unless @server.name == @bootstrap_backend || DevLXC::Container.new(@bootstrap_backend).defined?
131
- raise "The bootstrap backend server must be created first."
132
- end
133
- shared_container = create_shared_container
134
- puts "Cloning shared container #{shared_container.name} into container #{@server.name}"
135
- shared_container.clone(@server.name, {:flags => LXC::LXC_CLONE_SNAPSHOT})
136
- @server = DevLXC::Container.new(@server.name)
137
- puts "Adding lxc.hook.post-stop hook"
138
- @server.set_config_item("lxc.hook.post-stop", "/usr/local/share/lxc/hooks/post-stop-dhcp-release")
139
- @server.save_config
140
- hwaddr = @server.config_item("lxc.network.0.hwaddr")
141
- raise "#{@server.name} needs to have an lxc.network.hwaddr entry" if hwaddr.empty?
142
- DevLXC.assign_ip_address(@ipaddress, @server.name, hwaddr)
143
- DevLXC.create_dns_record(@analytics_fqdn, @server.name, @ipaddress) if @role == 'analytics'
144
- DevLXC.create_dns_record(@api_fqdn, @server.name, @ipaddress) if %w(open-source standalone frontend).include?(@role)
145
- @server.sync_mounts(@mounts)
146
- @server.start
147
- configure_analytics if @role == 'analytics'
148
- unless @role == 'analytics' || @packages["server"].nil?
149
- configure_server
150
- create_users if %w(standalone backend).include?(@role)
151
- if %w(standalone frontend).include?(@role) && ! @packages["manage"].nil?
152
- @server.install_package(@packages["manage"])
153
- configure_manage
154
- end
155
- if %w(standalone backend frontend).include?(@role)
156
- configure_reporting unless @packages["reporting"].nil?
157
- configure_push_jobs_server unless @packages["push-jobs-server"].nil?
158
- configure_chef_server_for_analytics unless ! %w(standalone backend).include?(@role) || @packages["analytics"].nil?
159
- end
160
- end
161
- @server.stop
162
- puts "Cloning container #{@server.name} into unique container #{unique_container.name}"
163
- @server.clone("#{unique_container.name}", {:flags => LXC::LXC_CLONE_SNAPSHOT|LXC::LXC_CLONE_KEEPMACADDR})
164
- end
165
- end
166
-
167
- def create_shared_container
168
- shared_container = DevLXC::Container.new(@shared_container_name)
169
- if shared_container.defined?
170
- puts "Using existing shared container #{shared_container.name}"
171
- return shared_container
172
- end
173
- platform_container = DevLXC.create_platform_container(@platform_container_name)
174
- puts "Cloning platform container #{platform_container.name} into shared container #{shared_container.name}"
175
- platform_container.clone(shared_container.name, {:flags => LXC::LXC_CLONE_SNAPSHOT})
176
- shared_container = DevLXC::Container.new(shared_container.name)
177
-
178
- # Disable certain sysctl.d files in Ubuntu 10.04, they cause `start procps` to fail
179
- # Enterprise Chef server's postgresql recipe expects to be able to `start procps`
180
- if platform_container.name == "p-ubuntu-1004"
181
- if File.exist?("#{shared_container.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf")
182
- FileUtils.mv("#{shared_container.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf",
183
- "#{shared_container.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf.orig")
184
- end
185
- end
186
- unless shared_container.config_item("lxc.mount.auto").empty?
187
- shared_container.set_config_item("lxc.mount.auto", "proc:rw sys:rw")
188
- shared_container.save_config
189
- end
190
- shared_container.sync_mounts(@mounts)
191
- shared_container.start
192
- if @role == 'analytics'
193
- shared_container.install_package(@packages["analytics"]) unless @packages["analytics"].nil?
194
- else
195
- shared_container.install_package(@packages["server"]) unless @packages["server"].nil?
196
- shared_container.install_package(@packages["reporting"]) unless @packages["reporting"].nil?
197
- shared_container.install_package(@packages["push-jobs-server"]) unless @packages["push-jobs-server"].nil?
198
- end
199
- shared_container.stop
200
- return shared_container
201
- end
202
-
203
- def configure_server
204
- case @role
205
- when "open-source"
206
- puts "Creating /etc/chef-server/chef-server.rb"
207
- FileUtils.mkdir_p("#{@server.config_item('lxc.rootfs')}/etc/chef-server")
208
- IO.write("#{@server.config_item('lxc.rootfs')}/etc/chef-server/chef-server.rb", @chef_server_config)
209
- run_ctl(@server_ctl, "reconfigure")
210
- when "standalone", "backend"
211
- case @chef_server_type
212
- when 'private-chef'
213
- puts "Creating /etc/opscode/private-chef.rb"
214
- FileUtils.mkdir_p("#{@server.config_item('lxc.rootfs')}/etc/opscode")
215
- IO.write("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb", @chef_server_config)
216
- when 'chef-server-core'
217
- puts "Creating /etc/opscode/chef-server.rb"
218
- FileUtils.mkdir_p("#{@server.config_item('lxc.rootfs')}/etc/opscode")
219
- IO.write("#{@server.config_item('lxc.rootfs')}/etc/opscode/chef-server.rb", @chef_server_config)
220
- end
221
- run_ctl(@server_ctl, "reconfigure")
222
- when "frontend"
223
- puts "Copying /etc/opscode from bootstrap backend"
224
- FileUtils.cp_r("#{LXC::Container.new(@bootstrap_backend).config_item('lxc.rootfs')}/etc/opscode",
225
- "#{@server.config_item('lxc.rootfs')}/etc")
226
- run_ctl(@server_ctl, "reconfigure")
227
- end
228
- end
229
-
230
- def configure_reporting
231
- if @role == 'frontend'
232
- puts "Copying /etc/opscode-reporting from bootstrap backend"
233
- FileUtils.cp_r("#{LXC::Container.new(@bootstrap_backend).config_item('lxc.rootfs')}/etc/opscode-reporting",
234
- "#{@server.config_item('lxc.rootfs')}/etc")
235
- end
236
- run_ctl(@server_ctl, "reconfigure")
237
- run_ctl("opscode-reporting", "reconfigure")
238
- end
239
-
240
- def configure_push_jobs_server
241
- run_ctl("opscode-push-jobs-server", "reconfigure")
242
- run_ctl(@server_ctl, "reconfigure")
243
- end
244
-
245
- def configure_manage
246
- if @chef_server_type == 'private-chef'
247
- puts "Disabling old opscode-webui in /etc/opscode/private-chef.rb"
248
- DevLXC.search_file_delete_line("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb", /opscode_webui[.enable.]/)
249
- DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb", "\nopscode_webui['enable'] = false\n")
250
- run_ctl(@server_ctl, "reconfigure")
251
- end
252
- run_ctl("opscode-manage", "reconfigure")
253
- end
254
-
255
- def configure_chef_server_for_analytics
256
- puts "Configuring for Analytics"
257
- case @chef_server_type
258
- when 'private-chef'
259
- DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb",
260
- "\noc_id['applications'] = {\n 'analytics' => {\n 'redirect_uri' => 'https://#{@analytics_fqdn}/'\n }\n}\n")
261
-
262
- DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb",
263
- "\nrabbitmq['vip'] = '#{@bootstrap_backend}'\nrabbitmq['node_ip_address'] = '0.0.0.0'\n")
264
- when 'chef-server-core'
265
- DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/chef-server.rb",
266
- "\noc_id['applications'] = {\n 'analytics' => {\n 'redirect_uri' => 'https://#{@analytics_fqdn}/'\n }\n}\n")
267
-
268
- DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/chef-server.rb",
269
- "\nrabbitmq['vip'] = '#{@bootstrap_backend}'\nrabbitmq['node_ip_address'] = '0.0.0.0'\n")
270
- end
271
-
272
- run_ctl(@server_ctl, "stop")
273
- run_ctl(@server_ctl, "reconfigure")
274
- run_ctl(@server_ctl, "restart")
275
- run_ctl("opscode-manage", "reconfigure") if @role == 'frontend'
276
- end
277
-
278
- def configure_analytics
279
- puts "Copying /etc/opscode-analytics from Chef Server bootstrap backend"
280
- FileUtils.cp_r("#{LXC::Container.new(@bootstrap_backend).config_item('lxc.rootfs')}/etc/opscode-analytics",
281
- "#{@server.config_item('lxc.rootfs')}/etc")
282
-
283
- IO.write("#{@server.config_item('lxc.rootfs')}/etc/opscode-analytics/opscode-analytics.rb",
284
- "analytics_fqdn '#{@analytics_fqdn}'\ntopology 'standalone'\n")
285
-
286
- run_ctl("opscode-analytics", "reconfigure")
287
- end
288
-
289
- def run_ctl(component, subcommand)
290
- puts "Running `#{component}-ctl #{subcommand}` in #{@server.name}"
291
- @server.run_command("#{component}-ctl #{subcommand}")
292
- end
293
-
294
- def create_users
295
- puts "Creating org, user, keys and knife.rb in /root/chef-repo/.chef"
296
- FileUtils.mkdir_p("#{@server.config_item('lxc.rootfs')}/root/chef-repo/.chef")
297
- knife_rb = %Q(
298
- current_dir = File.dirname(__FILE__)
299
-
300
- chef_server_url "https://127.0.0.1/organizations/ponyville"
301
-
302
- node_name "rainbowdash"
303
- client_key "\#{current_dir}/rainbowdash.pem"
304
-
305
- validation_client_name "ponyville-validator"
306
- validation_key "\#{current_dir}/ponyville-validator.pem"
307
-
308
- cookbook_path Dir.pwd + "/cookbooks"
309
- knife[:chef_repo_path] = Dir.pwd
310
- )
311
- IO.write("#{@server.config_item('lxc.rootfs')}/root/chef-repo/.chef/knife.rb", knife_rb)
312
- case @chef_server_type
313
- when 'private-chef'
314
- # give time for all services to come up completely
315
- sleep 60
316
- pivotal_rb = %Q(
317
- chef_server_root "https://127.0.0.1/"
318
- chef_server_url "https://127.0.0.1/"
319
-
320
- node_name "pivotal"
321
- client_key "/etc/opscode/pivotal.pem"
322
-
323
- knife[:chef_repo_path] = Dir.pwd
324
- )
325
- IO.write("#{@server.config_item('lxc.rootfs')}/root/chef-repo/.chef/pivotal.rb", pivotal_rb)
326
- @server.run_command("/opt/opscode/embedded/bin/gem install knife-opc --no-ri --no-rdoc")
327
- @server.run_command("/opt/opscode/embedded/bin/knife opc org create ponyville ponyville --filename /root/chef-repo/.chef/ponyville-validator.pem -c /root/chef-repo/.chef/pivotal.rb")
328
- @server.run_command("/opt/opscode/embedded/bin/knife opc user create rainbowdash rainbowdash rainbowdash rainbowdash@noreply.com rainbowdash --filename /root/chef-repo/.chef/rainbowdash.pem -c /root/chef-repo/.chef/pivotal.rb")
329
- @server.run_command("/opt/opscode/embedded/bin/knife opc org user add ponyville rainbowdash --admin")
330
- when 'chef-server-core'
331
- # give time for all services to come up completely
332
- sleep 10
333
- run_ctl(@server_ctl, "org-create ponyville ponyville --filename /root/chef-repo/.chef/ponyville-validator.pem")
334
- run_ctl(@server_ctl, "user-create rainbowdash rainbowdash rainbowdash rainbowdash@noreply.com rainbowdash --filename /root/chef-repo/.chef/rainbowdash.pem")
335
- run_ctl(@server_ctl, "org-user-add ponyville rainbowdash --admin")
336
- end
337
- end
338
- end
339
- end