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
51
# 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"]
  case cluster.topology
  when "open-source", "standalone"
    @role = cluster.topology
  when "tier", "ha"
    @role = "bootstrap_backend" if @server.name == cluster.bootstrap_backend
    @role = "secondary_backend" if @server.name == cluster.secondary_backend
    @role = "frontend" if cluster.frontends.include?(@server.name)
  end
  @mounts = cluster_config["mounts"]
  @bootstrap_backend = cluster.bootstrap_backend
  @chef_server_config = cluster.chef_server_config
  @api_fqdn = cluster.api_fqdn
  @base_platform = cluster_config["base_platform"]
  @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

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

Instance Attribute Details

#base_platformObject (readonly)

Returns the value of attribute base_platform.



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

def base_platform
  @base_platform
end

#base_server_nameObject (readonly)

Returns the value of attribute base_server_name.



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

def base_server_name
  @base_server_name
end

Instance Method Details

#abspath(rootfs_path) ⇒ Object



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

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

#configure_manageObject



240
241
242
243
244
245
246
247
248
# File 'lib/dev-lxc/chef-server.rb', line 240

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



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

def configure_push_jobs_server
  run_ctl("opscode-push-jobs-server", "reconfigure")
  if %w(bootstrap_backend secondary_backend).include?(@role)
    run_ctl(@server_ctl, "reconfigure")
  end
  run_ctl(@server_ctl, "restart opscode-pushy-server")
end

#configure_reportingObject



222
223
224
225
226
227
228
229
230
# File 'lib/dev-lxc/chef-server.rb', line 222

def configure_reporting
  if %w(secondary_backend frontend).include?(@role)
    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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/dev-lxc/chef-server.rb', line 195

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

def create
  if @server.defined?
    puts "Using existing container #{@server.name}"
    return
  end
  server_clone = DevLXC::Container.new("b-#{@server.name}")
  if server_clone.defined?
    puts "Cloning container #{server_clone.name} into container #{@server.name}"
    server_clone.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 %w(open-source standalone).include?(@role) || @server.name == @bootstrap_backend || DevLXC::Container.new(@bootstrap_backend).defined?
      raise "The bootstrap backend server must be created first."
    end
    base_server = create_base_server
    puts "Cloning container #{base_server.name} into container #{@server.name}"
    base_server.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(@api_fqdn, @server.name, @ipaddress) if %w(open-source standalone frontend).include?(@role)
    @server.sync_mounts(@mounts)
    @server.start
    configure_server unless @packages["server"].nil?
    create_users if %w(standalone bootstrap_backend).include?(@role)
    if %w(standalone bootstrap_backend secondary_backend frontend).include?(@role)
      configure_reporting unless @packages["reporting"].nil?
      configure_push_jobs_server unless @packages["push-jobs-server"].nil?
    end
    if %w(standalone frontend).include?(@role) && ! @packages["manage"].nil?
      @server.install_package(@packages["manage"])
      configure_manage
    end
    @server.stop
    puts "Cloning container #{@server.name} into b-#{@server.name}"
    @server.clone("b-#{@server.name}", {:flags => LXC::LXC_CLONE_SNAPSHOT|LXC::LXC_CLONE_KEEPMACADDR})
  end
end

#create_base_serverObject



162
163
164
165
166
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
# File 'lib/dev-lxc/chef-server.rb', line 162

def create_base_server
  base_server = DevLXC::Container.new(@base_server_name)
  if base_server.defined?
    puts "Using existing container #{base_server.name}"
    return base_server
  end
  base_platform = DevLXC.create_base_platform(@base_platform)
  puts "Cloning container #{base_platform.name} into container #{base_server.name}"
  base_platform.clone(base_server.name, {:flags => LXC::LXC_CLONE_SNAPSHOT})
  base_server = DevLXC::Container.new(base_server.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 base_platform.name == "b-ubuntu-1004"
    if File.exist?("#{base_server.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf")
      FileUtils.mv("#{base_server.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf",
                   "#{base_server.config_item('lxc.rootfs')}/etc/sysctl.d/10-console-messages.conf.orig")
    end
  end
  # TODO when LXC 1.0.2 is released the following test can be done using #config_item("lxc.mount.auto")
  unless IO.readlines(base_server.config_file_name).select { |line| line.start_with?("lxc.mount.auto") }.empty?
    base_server.set_config_item("lxc.mount.auto", "proc:rw sys:rw")
    base_server.save_config
  end
  base_server.sync_mounts(@mounts)
  base_server.start
  base_server.install_package(@packages["server"]) unless @packages["server"].nil?
  base_server.install_package(@packages["reporting"]) unless @packages["reporting"].nil?
  base_server.install_package(@packages["push-jobs-server"]) unless @packages["push-jobs-server"].nil?
  base_server.stop
  return base_server
end

#create_usersObject



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

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 = "chef_server_url 'https://127.0.0.1/organizations/ponyville'\n"
  knife_rb += "node_name 'rainbowdash'\n"
  knife_rb += "client_key 'rainbowdash.pem'\n"
  knife_rb += "knife[:chef_repo_path] = Dir.pwd\n"
  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 = "chef_server_root 'https://127.0.0.1/'\n"
    pivotal_rb += "chef_server_url 'https://127.0.0.1/'\n"
    pivotal_rb += "node_name 'pivotal'\n"
    pivotal_rb += "client_key '/etc/opscode/pivotal.pem'\n"
    pivotal_rb += "knife[:chef_repo_path] = Dir.pwd\n"
    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_base_container(type) ⇒ Object



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

def destroy_base_container(type)
  case type
  when :unique
    DevLXC::Container.new("b-#{@server.name}").destroy
  when :shared
    DevLXC::Container.new(@base_server_name).destroy
  when :platform
    DevLXC::Container.new(@base_platform).destroy
  end
end

#run_command(command) ⇒ Object



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

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



250
251
252
253
# File 'lib/dev-lxc/chef-server.rb', line 250

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

#startObject



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

def start
  create
  hwaddr = @server.config_item("lxc.network.0.hwaddr")
  DevLXC.assign_ip_address(@ipaddress, @server.name, hwaddr)
  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



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

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