Class: DevLXC::ChefServer

Inherits:
Object
  • Object
show all
Defined in:
lib/dev-lxc/chef-server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cluster_config) ⇒ ChefServer

Returns a new instance of ChefServer.



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
# File 'lib/dev-lxc/chef-server.rb', line 8

def initialize(name, cluster_config)
  unless cluster_config["servers"].keys.include?(name)
    raise "Server #{name} is not defined in the cluster config"
  end
  cluster = DevLXC::ChefCluster.new(cluster_config)
  @server = DevLXC::Container.new(name)
  @config = cluster_config["servers"][@server.name]
  @ipaddress = @config["ipaddress"]
  @role = @config["role"] ? @config["role"] : cluster_config['topology']
  @mounts = cluster_config["mounts"]
  @bootstrap_backend = cluster.bootstrap_backend
  @chef_server_config = cluster.chef_server_config
  @api_fqdn = cluster_config["api_fqdn"]
  @analytics_fqdn = cluster_config["analytics_fqdn"]
  @platform_container_name = cluster_config["platform_container"]
  @packages = cluster_config["packages"]

  if File.basename(@packages["server"]).match(/^(\w+-\w+.*)[_-]((?:\d+\.?){3,})-/)
    @chef_server_type = Regexp.last_match[1]
    @chef_server_version = Regexp.last_match[2].gsub(".", "-")
  end

  if @role == 'analytics'
    @shared_container_name = "s#{@platform_container_name[1..-1]}"
    @shared_container_name += "-analytics-#{Regexp.last_match[1].gsub(".", "-")}" if @packages["analytics"].to_s.match(/[_-]((\d+\.?){3,})-/)
  else
    @shared_container_name = "s#{@platform_container_name[1..-1]}"
    case @chef_server_type
    when 'chef-server-core'
      @shared_container_name += '-cs'
      @server_ctl = 'chef-server'
    when 'private-chef'
      @shared_container_name += '-ec'
      @server_ctl = 'private-chef'
    when 'chef-server'
      @shared_container_name += '-osc'
      @server_ctl = 'chef-server'
    end
    @shared_container_name += "-#{@chef_server_version}"
    @shared_container_name += "-reporting-#{Regexp.last_match[1].gsub(".", "-")}" if @packages["reporting"].to_s.match(/[_-]((\d+\.?){3,})-/)
    @shared_container_name += "-pushy-#{Regexp.last_match[1].gsub(".", "-")}" if @packages["push-jobs-server"].to_s.match(/[_-]((\d+\.?){3,})-/)
  end
end

Instance Attribute Details

#roleObject (readonly)

Returns the value of attribute role.



6
7
8
# File 'lib/dev-lxc/chef-server.rb', line 6

def role
  @role
end

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/dev-lxc/chef-server.rb', line 6

def server
  @server
end

Instance Method Details

#abspath(rootfs_path) ⇒ Object



62
63
64
# File 'lib/dev-lxc/chef-server.rb', line 62

def abspath(rootfs_path)
  "#{@server.config_item('lxc.rootfs')}#{rootfs_path}" if @server.defined?
end

#configure_analyticsObject



278
279
280
281
282
283
284
285
286
287
# File 'lib/dev-lxc/chef-server.rb', line 278

def configure_analytics
  puts "Copying /etc/opscode-analytics from Chef Server bootstrap backend"
  FileUtils.cp_r("#{LXC::Container.new(@bootstrap_backend).config_item('lxc.rootfs')}/etc/opscode-analytics",
                 "#{@server.config_item('lxc.rootfs')}/etc")

  IO.write("#{@server.config_item('lxc.rootfs')}/etc/opscode-analytics/opscode-analytics.rb",
    "analytics_fqdn '#{@analytics_fqdn}'\ntopology 'standalone'\n")

  run_ctl("opscode-analytics", "reconfigure")
end

#configure_chef_server_for_analyticsObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/dev-lxc/chef-server.rb', line 255

def configure_chef_server_for_analytics
  puts "Configuring for Analytics"
  case @chef_server_type
  when 'private-chef'
    DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb",
      "\noc_id['applications'] = {\n  'analytics' => {\n    'redirect_uri' => 'https://#{@analytics_fqdn}/'\n  }\n}\n")

    DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb",
      "\nrabbitmq['vip'] = '#{@bootstrap_backend}'\nrabbitmq['node_ip_address'] = '0.0.0.0'\n")
  when 'chef-server-core'
    DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/chef-server.rb",
      "\noc_id['applications'] = {\n  'analytics' => {\n    'redirect_uri' => 'https://#{@analytics_fqdn}/'\n  }\n}\n")

    DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/chef-server.rb",
      "\nrabbitmq['vip'] = '#{@bootstrap_backend}'\nrabbitmq['node_ip_address'] = '0.0.0.0'\n")
  end

  run_ctl(@server_ctl, "stop")
  run_ctl(@server_ctl, "reconfigure")
  run_ctl(@server_ctl, "restart")
  run_ctl("opscode-manage", "reconfigure") if @role == 'frontend'
end

#configure_manageObject



245
246
247
248
249
250
251
252
253
# File 'lib/dev-lxc/chef-server.rb', line 245

def configure_manage
  if @chef_server_type == 'private-chef'
    puts "Disabling old opscode-webui in /etc/opscode/private-chef.rb"
    DevLXC.search_file_delete_line("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb", /opscode_webui[.enable.]/)
    DevLXC.append_line_to_file("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb", "\nopscode_webui['enable'] = false\n")
    run_ctl(@server_ctl, "reconfigure")
  end
  run_ctl("opscode-manage", "reconfigure")
end

#configure_push_jobs_serverObject



240
241
242
243
# File 'lib/dev-lxc/chef-server.rb', line 240

def configure_push_jobs_server
  run_ctl("opscode-push-jobs-server", "reconfigure")
  run_ctl(@server_ctl, "reconfigure")
end

#configure_reportingObject



230
231
232
233
234
235
236
237
238
# File 'lib/dev-lxc/chef-server.rb', line 230

def configure_reporting
  if @role == 'frontend'
    puts "Copying /etc/opscode-reporting from bootstrap backend"
    FileUtils.cp_r("#{LXC::Container.new(@bootstrap_backend).config_item('lxc.rootfs')}/etc/opscode-reporting",
                   "#{@server.config_item('lxc.rootfs')}/etc")
  end
  run_ctl(@server_ctl, "reconfigure")
  run_ctl("opscode-reporting", "reconfigure")
end

#configure_serverObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dev-lxc/chef-server.rb', line 203

def configure_server
  case @role
  when "open-source"
    puts "Creating /etc/chef-server/chef-server.rb"
    FileUtils.mkdir_p("#{@server.config_item('lxc.rootfs')}/etc/chef-server")
    IO.write("#{@server.config_item('lxc.rootfs')}/etc/chef-server/chef-server.rb", @chef_server_config)
    run_ctl(@server_ctl, "reconfigure")
  when "standalone", "backend"
    case @chef_server_type
    when 'private-chef'
      puts "Creating /etc/opscode/private-chef.rb"
      FileUtils.mkdir_p("#{@server.config_item('lxc.rootfs')}/etc/opscode")
      IO.write("#{@server.config_item('lxc.rootfs')}/etc/opscode/private-chef.rb", @chef_server_config)
    when 'chef-server-core'
      puts "Creating /etc/opscode/chef-server.rb"
      FileUtils.mkdir_p("#{@server.config_item('lxc.rootfs')}/etc/opscode")
      IO.write("#{@server.config_item('lxc.rootfs')}/etc/opscode/chef-server.rb", @chef_server_config)
    end
    run_ctl(@server_ctl, "reconfigure")
  when "frontend"
    puts "Copying /etc/opscode from bootstrap backend"
    FileUtils.cp_r("#{LXC::Container.new(@bootstrap_backend).config_item('lxc.rootfs')}/etc/opscode",
                   "#{@server.config_item('lxc.rootfs')}/etc")
    run_ctl(@server_ctl, "reconfigure")
  end
end

#createObject



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
# File 'lib/dev-lxc/chef-server.rb', line 117

def create
  if @server.defined?
    puts "Using existing container #{@server.name}"
    return
  end
  unique_container = DevLXC::Container.new("u-#{@server.name}")
  if unique_container.defined?
    puts "Cloning shared container #{unique_container.name} into container #{@server.name}"
    unique_container.clone(@server.name, {:flags => LXC::LXC_CLONE_SNAPSHOT|LXC::LXC_CLONE_KEEPMACADDR})
    @server = DevLXC::Container.new(@server.name)
    return
  else
    puts "Creating container #{@server.name}"
    unless @server.name == @bootstrap_backend || DevLXC::Container.new(@bootstrap_backend).defined?
      raise "The bootstrap backend server must be created first."
    end
    shared_container = create_shared_container
    puts "Cloning shared container #{shared_container.name} into container #{@server.name}"
    shared_container.clone(@server.name, {:flags => LXC::LXC_CLONE_SNAPSHOT})
    @server = DevLXC::Container.new(@server.name)
    puts "Adding lxc.hook.post-stop hook"
    @server.set_config_item("lxc.hook.post-stop", "/usr/local/share/lxc/hooks/post-stop-dhcp-release")
    @server.save_config
    hwaddr = @server.config_item("lxc.network.0.hwaddr")
    raise "#{@server.name} needs to have an lxc.network.hwaddr entry" if hwaddr.empty?
    DevLXC.assign_ip_address(@ipaddress, @server.name, hwaddr)
    DevLXC.create_dns_record(@analytics_fqdn, @server.name, @ipaddress) if @role == 'analytics'
    DevLXC.create_dns_record(@api_fqdn, @server.name, @ipaddress) if %w(open-source standalone frontend).include?(@role)
    @server.sync_mounts(@mounts)
    @server.start
    configure_analytics if @role == 'analytics'
    unless @role == 'analytics' || @packages["server"].nil?
      configure_server
      create_users if %w(standalone backend).include?(@role)
      if %w(standalone frontend).include?(@role) && ! @packages["manage"].nil?
        @server.install_package(@packages["manage"])
        configure_manage
      end
      if %w(standalone backend frontend).include?(@role)
        configure_reporting unless @packages["reporting"].nil?
        configure_push_jobs_server unless @packages["push-jobs-server"].nil?
        configure_chef_server_for_analytics unless ! %w(standalone backend).include?(@role) || @packages["analytics"].nil?
      end
    end
    @server.stop
    puts "Cloning container #{@server.name} into unique container #{unique_container.name}"
    @server.clone("#{unique_container.name}", {:flags => LXC::LXC_CLONE_SNAPSHOT|LXC::LXC_CLONE_KEEPMACADDR})
  end
end

#create_shared_containerObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/dev-lxc/chef-server.rb', line 167

def create_shared_container
  shared_container = DevLXC::Container.new(@shared_container_name)
  if shared_container.defined?
    puts "Using existing shared container #{shared_container.name}"
    return shared_container
  end
  platform_container = DevLXC.create_platform_container(@platform_container_name)
  puts "Cloning platform container #{platform_container.name} into shared container #{shared_container.name}"
  platform_container.clone(shared_container.name, {:flags => LXC::LXC_CLONE_SNAPSHOT})
  shared_container = DevLXC::Container.new(shared_container.name)

  # Disable certain sysctl.d files in Ubuntu 10.04, they cause `start procps` to fail
  # Enterprise Chef server's postgresql recipe expects to be able to `start procps`
  if platform_container.name == "p-ubuntu-1004"
    if File.exist?("#{shared_container.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf")
      FileUtils.mv("#{shared_container.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf",
                   "#{shared_container.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf.orig")
    end
  end
  unless shared_container.config_item("lxc.mount.auto").empty?
    shared_container.set_config_item("lxc.mount.auto", "proc:rw sys:rw")
    shared_container.save_config
  end
  shared_container.sync_mounts(@mounts)
  shared_container.start
  if @role == 'analytics'
    shared_container.install_package(@packages["analytics"]) unless @packages["analytics"].nil?
  else
    shared_container.install_package(@packages["server"]) unless @packages["server"].nil?
    shared_container.install_package(@packages["reporting"]) unless @packages["reporting"].nil?
    shared_container.install_package(@packages["push-jobs-server"]) unless @packages["push-jobs-server"].nil?
  end
  shared_container.stop
  return shared_container
end

#create_usersObject



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/dev-lxc/chef-server.rb', line 294

def create_users
  puts "Creating org, user, keys and knife.rb in /root/chef-repo/.chef"
  FileUtils.mkdir_p("#{@server.config_item('lxc.rootfs')}/root/chef-repo/.chef")
  knife_rb = %Q(
current_dir = File.dirname(__FILE__)

chef_server_url "https://127.0.0.1/organizations/ponyville"

node_name "rainbowdash"
client_key "\#{current_dir}/rainbowdash.pem"

validation_client_name "ponyville-validator"
validation_key "\#{current_dir}/ponyville-validator.pem"

cookbook_path Dir.pwd + "/cookbooks"
knife[:chef_repo_path] = Dir.pwd
)
  IO.write("#{@server.config_item('lxc.rootfs')}/root/chef-repo/.chef/knife.rb", knife_rb)
  case @chef_server_type
  when 'private-chef'
    # give time for all services to come up completely
    sleep 60
    pivotal_rb = %Q(
chef_server_root "https://127.0.0.1/"
chef_server_url "https://127.0.0.1/"

node_name "pivotal"
client_key "/etc/opscode/pivotal.pem"

knife[:chef_repo_path] = Dir.pwd
)
    IO.write("#{@server.config_item('lxc.rootfs')}/root/chef-repo/.chef/pivotal.rb", pivotal_rb)
    @server.run_command("/opt/opscode/embedded/bin/gem install knife-opc --no-ri --no-rdoc")
    @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")
    @server.run_command("/opt/opscode/embedded/bin/knife opc user create rainbowdash rainbowdash rainbowdash [email protected] rainbowdash --filename /root/chef-repo/.chef/rainbowdash.pem -c /root/chef-repo/.chef/pivotal.rb")
    @server.run_command("/opt/opscode/embedded/bin/knife opc org user add ponyville rainbowdash --admin")
  when 'chef-server-core'
    # give time for all services to come up completely
    sleep 10
    run_ctl(@server_ctl, "org-create ponyville ponyville --filename /root/chef-repo/.chef/ponyville-validator.pem")
    run_ctl(@server_ctl, "user-create rainbowdash rainbowdash rainbowdash [email protected] rainbowdash --filename /root/chef-repo/.chef/rainbowdash.pem")
    run_ctl(@server_ctl, "org-user-add ponyville rainbowdash --admin")
  end
end

#deregister_from_dnsmasq(hwaddr) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/dev-lxc/chef-server.rb', line 97

def deregister_from_dnsmasq(hwaddr)
  DevLXC.search_file_delete_line("/etc/lxc/addn-hosts.conf", /^#{@ipaddress}\s/)
  DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /,#{@ipaddress}$/)
  unless hwaddr.nil?
    DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /^#{hwaddr}/)
  end
  DevLXC.reload_dnsmasq
end

#destroyObject



91
92
93
94
95
# File 'lib/dev-lxc/chef-server.rb', line 91

def destroy
  hwaddr = @server.config_item("lxc.network.0.hwaddr") if @server.defined?
  @server.destroy
  deregister_from_dnsmasq(hwaddr)
end

#destroy_container(type) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/dev-lxc/chef-server.rb', line 106

def destroy_container(type)
  case type
  when :unique
    DevLXC::Container.new("u-#{@server.name}").destroy
  when :shared
    DevLXC::Container.new(@shared_container_name).destroy
  when :platform
    DevLXC::Container.new(@platform_container_name).destroy
  end
end

#run_command(command) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/dev-lxc/chef-server.rb', line 66

def run_command(command)
  if @server.running?
    puts "Running '#{command}' in #{@server.name}"
    @server.run_command(command)
  else
    puts "#{@server.name} is not running"
  end
end

#run_ctl(component, subcommand) ⇒ Object



289
290
291
292
# File 'lib/dev-lxc/chef-server.rb', line 289

def run_ctl(component, subcommand)
  puts "Running `#{component}-ctl #{subcommand}` in #{@server.name}"
  @server.run_command("#{component}-ctl #{subcommand}")
end

#startObject



75
76
77
78
79
80
81
82
83
# File 'lib/dev-lxc/chef-server.rb', line 75

def start
  create
  hwaddr = @server.config_item("lxc.network.0.hwaddr")
  DevLXC.assign_ip_address(@ipaddress, @server.name, hwaddr)
  DevLXC.create_dns_record(@analytics_fqdn, @server.name, @ipaddress) if @role == 'analytics'
  DevLXC.create_dns_record(@api_fqdn, @server.name, @ipaddress) if %w(open-source standalone frontend).include?(@role)
  @server.sync_mounts(@mounts)
  @server.start
end

#statusObject



52
53
54
55
56
57
58
59
60
# File 'lib/dev-lxc/chef-server.rb', line 52

def status
  if @server.defined?
    state = @server.state
    ip_addresses = @server.ip_addresses.join(" ") if @server.state == :running
  else
    state = "not_created"
  end
  printf "%20s     %-15s %s\n", @server.name, state, ip_addresses
end

#stopObject



85
86
87
88
89
# File 'lib/dev-lxc/chef-server.rb', line 85

def stop
  hwaddr = @server.config_item("lxc.network.0.hwaddr") if @server.defined?
  @server.stop
  deregister_from_dnsmasq(hwaddr)
end