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"]
  case cluster.topology
  when "open-source", "standalone"
    @role = cluster.topology
  when "tier"
    @role = "bootstrap_backend" if @server.name == cluster.bootstrap_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
  @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

  @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

Instance Attribute Details

#platform_container_nameObject (readonly)

Returns the value of attribute platform_container_name.



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

def platform_container_name
  @platform_container_name
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

#shared_container_nameObject (readonly)

Returns the value of attribute shared_container_name.



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

def shared_container_name
  @shared_container_name
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_manageObject



235
236
237
238
239
240
241
242
243
# File 'lib/dev-lxc/chef-server.rb', line 235

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



230
231
232
233
# File 'lib/dev-lxc/chef-server.rb', line 230

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

#configure_reportingObject



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

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



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

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 "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



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

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 %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
    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(@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 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 unique container #{unique_container.name}"
    @server.clone("#{unique_container.name}", {:flags => LXC::LXC_CLONE_SNAPSHOT|LXC::LXC_CLONE_KEEPMACADDR})
  end
end

#create_shared_containerObject



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

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
  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?
  shared_container.stop
  return shared_container
end

#create_usersObject



250
251
252
253
254
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
285
286
287
288
289
290
291
292
293
# File 'lib/dev-lxc/chef-server.rb', line 250

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



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

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



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

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

#destroy_container(type) ⇒ Object



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

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



245
246
247
248
# File 'lib/dev-lxc/chef-server.rb', line 245

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
# 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(@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



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

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