Module: DevLXC

Defined in:
lib/dev-lxc.rb,
lib/dev-lxc/version.rb,
lib/dev-lxc/container.rb,
lib/dev-lxc/chef-server.rb,
lib/dev-lxc/chef-cluster.rb

Defined Under Namespace

Modules: CLI Classes: ChefCluster, ChefServer, Container

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.append_line_to_file(file_name, line) ⇒ Object



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

def self.append_line_to_file(file_name, line)
  content = IO.readlines(file_name)
  content[-1] = content[-1].chomp + "\n"
  content << line
  IO.write(file_name, content.join)
end

.assign_ip_address(ipaddress, container_name, hwaddr) ⇒ Object



46
47
48
49
50
51
# File 'lib/dev-lxc.rb', line 46

def self.assign_ip_address(ipaddress, container_name, hwaddr)
  puts "Assigning IP address #{ipaddress} to #{container_name} container's lxc.network.hwaddr #{hwaddr}"
  search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /(^#{hwaddr}|,#{ipaddress}$)/)
  append_line_to_file("/etc/lxc/dhcp-hosts.conf", "#{hwaddr},#{ipaddress}\n")
  reload_dnsmasq
end

.create_base_platform(base_platform_name) ⇒ Object



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

def self.create_base_platform(base_platform_name)
  base_platform = DevLXC::Container.new(base_platform_name)
  if base_platform.defined?
    puts "Using existing container #{base_platform.name}"
    return base_platform
  end
  puts "Creating container #{base_platform.name}"
  case base_platform.name
  when "b-ubuntu-1004"
    base_platform.create("download", "btrfs", 0, ["-d", "ubuntu", "-r", "lucid", "-a", "amd64"])
  when "b-ubuntu-1204"
    base_platform.create("download", "btrfs", 0, ["-d", "ubuntu", "-r", "precise", "-a", "amd64"])
  when "b-centos-5"
    base_platform.create("centos", "btrfs", 0, ["-R", "5"])
  when "b-centos-6"
    base_platform.create("download", "btrfs", 0, ["-d", "centos", "-r", "6", "-a", "amd64"])
  end
  hwaddr = '00:16:3e:' + Digest::SHA1.hexdigest(Time.now.to_s).slice(0..5).unpack('a2a2a2').join(':')
  puts "Setting #{base_platform.name} container's lxc.network.0.hwaddr to #{hwaddr}"
  base_platform.set_config_item("lxc.network.0.hwaddr", hwaddr)
  base_platform.save_config
  base_platform.start
  puts "Installing packages in container #{base_platform.name}"
  case base_platform.name
  when "b-ubuntu-1004"
    base_platform.run_command("apt-get update")
    base_platform.run_command("apt-get install -y standard^ server^ vim-nox emacs23-nox curl tree")
  when "b-ubuntu-1204"
    base_platform.run_command("apt-get update")
    base_platform.run_command("apt-get install -y standard^ server^ vim-nox emacs23-nox tree")
  when "b-centos-5", "b-centos-6"
    base_platform.run_command("yum install -y @base @core vim-enhanced emacs-nox tree")
  end
  base_platform.stop
  return base_platform
end

.create_dns_record(api_fqdn, container_name, ipaddress) ⇒ Object



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

def self.create_dns_record(api_fqdn, container_name, ipaddress)
  dns_record = "#{ipaddress} #{container_name} #{api_fqdn}\n"
  puts "Creating DNS record: #{dns_record}"
  search_file_delete_line("/etc/lxc/addn-hosts.conf", /^#{ipaddress}\s/)
  append_line_to_file("/etc/lxc/addn-hosts.conf", dns_record)
  reload_dnsmasq
end

.reload_dnsmasqObject



61
62
63
# File 'lib/dev-lxc.rb', line 61

def self.reload_dnsmasq
  system("pkill -HUP dnsmasq")
end

.search_file_delete_line(file_name, regex) ⇒ Object



65
66
67
# File 'lib/dev-lxc.rb', line 65

def self.search_file_delete_line(file_name, regex)
  IO.write(file_name, IO.readlines(file_name).delete_if {|line| line.match(Regexp.new(regex))}.join)
end

.search_file_replace(file_name, regex, replace) ⇒ Object



76
77
78
# File 'lib/dev-lxc.rb', line 76

def self.search_file_replace(file_name, regex, replace)
  IO.write(file_name, IO.readlines(file_name).map {|line| line.gsub(Regexp.new(regex), replace)}.join)
end