dev-lxc 3.2.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dev-lxc.rb CHANGED
@@ -1,118 +1,118 @@
1
- require "fileutils"
2
- require "digest/sha1"
3
- require "lxc"
4
- require "dev-lxc/container"
5
- require "dev-lxc/server"
6
- require "dev-lxc/cluster"
7
-
8
- module DevLXC
9
- def self.create_base_container(base_container_name, base_container_options)
10
- base_container = DevLXC::Container.new(base_container_name)
11
- if base_container.defined?
12
- puts "Base container '#{base_container.name}' already exists"
13
- return base_container
14
- end
15
- puts "Creating base container '#{base_container.name}'"
16
- template = "download"
17
- case base_container.name
18
- when "b-ubuntu-1204"
19
- options = ["-d", "ubuntu", "-r", "precise", "-a", "amd64"]
20
- when "b-ubuntu-1404"
21
- options = ["-d", "ubuntu", "-r", "trusty", "-a", "amd64"]
22
- when "b-ubuntu-1604"
23
- options = ["-d", "ubuntu", "-r", "xenial", "-a", "amd64"]
24
- when "b-centos-5"
25
- template = "centos"
26
- options = ["-R", "5"]
27
- when "b-centos-6"
28
- options = ["-d", "centos", "-r", "6", "-a", "amd64"]
29
- when "b-centos-7"
30
- options = ["-d", "centos", "-r", "7", "-a", "amd64"]
31
- end
32
- options.concat(base_container_options.split) unless base_container_options.nil?
33
- base_container.create(template, "btrfs", {}, 0, options)
34
-
35
- # if base container is centos then `/etc/hosts` file needs to be modified so `hostname -f`
36
- # provides the FQDN instead of `localhost`
37
- if base_container.name.start_with?('b-centos-')
38
- IO.write("#{base_container.config_item('lxc.rootfs')}/etc/hosts", "127.0.0.1 localhost\n127.0.1.1 #{base_container.name}\n")
39
- end
40
-
41
- # Centos 7 needs setpcap capabilities
42
- # ref: https://bugzilla.redhat.com/show_bug.cgi?id=1176816
43
- # ref: https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/1339781
44
- # ref: http://vfamilyserver.org/blog/2015/05/centos-7-lxc-container-slow-boot/
45
- if base_container.name == "b-centos-7"
46
- DevLXC.search_file_replace(base_container.config_file_name, /centos.common.conf/, 'fedora.common.conf')
47
- base_container.clear_config
48
- base_container.load_config
49
- end
50
-
51
- unless base_container.config_item("lxc.mount.auto").nil?
52
- base_container.set_config_item("lxc.mount.auto", "proc:rw sys:rw")
53
- end
54
- if base_container.config_item("lxc.network.0.hwaddr").nil?
55
- hwaddr = '00:16:3e:' + Digest::SHA1.hexdigest(Time.now.to_s).slice(0..5).unpack('a2a2a2').join(':')
56
- puts "Setting '#{base_container.name}' base container's lxc.network.hwaddr to #{hwaddr}"
57
- base_container.set_config_item("lxc.network.hwaddr", hwaddr)
58
- end
59
- base_container.save_config
60
- base_container.start
61
- puts "Installing packages in base container '#{base_container.name}'"
62
- case base_container.name
63
- when "b-ubuntu-1204", "b-ubuntu-1404"
64
- base_container.run_command("apt-get update")
65
- base_container.run_command("apt-get install -y standard^ server^ vim-nox emacs23-nox tree openssh-server")
66
- IO.write("#{base_container.config_item('lxc.rootfs')}/etc/rc.local", "#!/usr/bin/env bash\n\n/usr/sbin/dpkg-reconfigure openssh-server\n")
67
- FileUtils.chmod(0755, "#{base_container.config_item('lxc.rootfs')}/etc/rc.local")
68
- when "b-ubuntu-1604"
69
- base_container.run_command("apt-get update")
70
- base_container.run_command("apt-get install -y standard^ server^ vim-nox emacs24-nox tree openssh-server")
71
- IO.write("#{base_container.config_item('lxc.rootfs')}/etc/rc.local", "#!/usr/bin/env bash\n\n/usr/sbin/dpkg-reconfigure openssh-server\n")
72
- FileUtils.chmod(0755, "#{base_container.config_item('lxc.rootfs')}/etc/rc.local")
73
- when "b-centos-5"
74
- # downgrade openssl temporarily to overcome an install bug
75
- # reference: http://www.hack.net.br/blog/2014/02/12/openssl-conflicts-with-file-from-package-openssl/
76
- base_container.run_command("yum downgrade -y openssl")
77
- base_container.run_command("yum install -y @base @core vim-enhanced emacs-nox tree openssh-server")
78
- FileUtils.mkdir_p("#{base_container.config_item('lxc.rootfs')}/etc/sudoers.d")
79
- FileUtils.chmod(0750, "#{base_container.config_item('lxc.rootfs')}/etc/sudoers.d")
80
- append_line_to_file("#{base_container.config_item('lxc.rootfs')}/etc/sudoers", "\n#includedir /etc/sudoers.d\n")
81
- when "b-centos-6"
82
- base_container.run_command("yum install -y @base @core vim-enhanced emacs-nox tree openssh-server")
83
- when "b-centos-7"
84
- base_container.run_command("yum install -y @base @core vim-enhanced emacs-nox tree openssh-server")
85
- end
86
- base_container.run_command("useradd --create-home --shell /bin/bash --password $6$q3FDMpMZ$zfahCxEWHbzuEV98QPzhGZ7fLtGcLNZrbKK7OAYGXmJXZc07WbcxVnDwrMyX/cL6vSp4/IjlrVUZFBp7Orhyu1 dev-lxc")
87
-
88
- FileUtils.mkdir_p("#{base_container.config_item('lxc.rootfs')}/home/dev-lxc/.ssh")
89
- FileUtils.chmod(0700, "#{base_container.config_item('lxc.rootfs')}/home/dev-lxc/.ssh")
90
- FileUtils.touch("#{base_container.config_item('lxc.rootfs')}/home/dev-lxc/.ssh/authorized_keys")
91
- FileUtils.chmod(0600, "#{base_container.config_item('lxc.rootfs')}/home/dev-lxc/.ssh/authorized_keys")
92
- base_container.run_command("chown -R dev-lxc:dev-lxc /home/dev-lxc/.ssh")
93
-
94
- IO.write("#{base_container.config_item('lxc.rootfs')}/etc/sudoers.d/dev-lxc", "dev-lxc ALL=NOPASSWD:ALL\n")
95
- FileUtils.chmod(0440, "#{base_container.config_item('lxc.rootfs')}/etc/sudoers.d/dev-lxc")
96
- base_container.shutdown
97
- return base_container
98
- end
99
-
100
- def self.reload_dnsmasq
101
- system("pkill -HUP dnsmasq")
102
- end
103
-
104
- def self.search_file_delete_line(file_name, regex)
105
- IO.write(file_name, IO.readlines(file_name).delete_if {|line| line.match(Regexp.new(regex))}.join)
106
- end
107
-
108
- def self.append_line_to_file(file_name, line)
109
- content = IO.readlines(file_name)
110
- content[-1] = content[-1].chomp + "\n" unless content.empty?
111
- content << line
112
- IO.write(file_name, content.join)
113
- end
114
-
115
- def self.search_file_replace(file_name, regex, replace)
116
- IO.write(file_name, IO.readlines(file_name).map {|line| line.gsub(Regexp.new(regex), replace)}.join)
117
- end
118
- end
1
+ require "fileutils"
2
+ require "digest/sha1"
3
+ require "lxc"
4
+ require "dev-lxc/container"
5
+ require "dev-lxc/server"
6
+ require "dev-lxc/cluster"
7
+
8
+ module DevLXC
9
+ def self.create_base_container(base_container_name, base_container_options)
10
+ base_container = DevLXC::Container.new(base_container_name)
11
+ if base_container.defined?
12
+ puts "Base container '#{base_container.name}' already exists"
13
+ return base_container
14
+ end
15
+ puts "Creating base container '#{base_container.name}'"
16
+ template = "download"
17
+ case base_container.name
18
+ when "b-ubuntu-1204"
19
+ options = ["-d", "ubuntu", "-r", "precise", "-a", "amd64"]
20
+ when "b-ubuntu-1404"
21
+ options = ["-d", "ubuntu", "-r", "trusty", "-a", "amd64"]
22
+ when "b-ubuntu-1604"
23
+ options = ["-d", "ubuntu", "-r", "xenial", "-a", "amd64"]
24
+ when "b-centos-5"
25
+ template = "centos"
26
+ options = ["-R", "5"]
27
+ when "b-centos-6"
28
+ options = ["-d", "centos", "-r", "6", "-a", "amd64"]
29
+ when "b-centos-7"
30
+ options = ["-d", "centos", "-r", "7", "-a", "amd64"]
31
+ end
32
+ options.concat(base_container_options.split) unless base_container_options.nil?
33
+ base_container.create(template, "btrfs", {}, 0, options)
34
+
35
+ # if base container is centos then `/etc/hosts` file needs to be modified so `hostname -f`
36
+ # provides the FQDN instead of `localhost`
37
+ if base_container.name.start_with?('b-centos-')
38
+ IO.write("#{base_container.config_item('lxc.rootfs')}/etc/hosts", "127.0.0.1 localhost\n127.0.1.1 #{base_container.name}\n")
39
+ end
40
+
41
+ # Centos 7 needs setpcap capabilities
42
+ # ref: https://bugzilla.redhat.com/show_bug.cgi?id=1176816
43
+ # ref: https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/1339781
44
+ # ref: http://vfamilyserver.org/blog/2015/05/centos-7-lxc-container-slow-boot/
45
+ if base_container.name == "b-centos-7"
46
+ DevLXC.search_file_replace(base_container.config_file_name, /centos.common.conf/, 'fedora.common.conf')
47
+ base_container.clear_config
48
+ base_container.load_config
49
+ end
50
+
51
+ unless base_container.config_item("lxc.mount.auto").nil?
52
+ base_container.set_config_item("lxc.mount.auto", "proc:rw sys:rw")
53
+ end
54
+ if base_container.config_item("lxc.network.0.hwaddr").nil?
55
+ hwaddr = '00:16:3e:' + Digest::SHA1.hexdigest(Time.now.to_s).slice(0..5).unpack('a2a2a2').join(':')
56
+ puts "Setting '#{base_container.name}' base container's lxc.network.hwaddr to #{hwaddr}"
57
+ base_container.set_config_item("lxc.network.hwaddr", hwaddr)
58
+ end
59
+ base_container.save_config
60
+ base_container.start
61
+ puts "Installing packages in base container '#{base_container.name}'"
62
+ case base_container.name
63
+ when "b-ubuntu-1204", "b-ubuntu-1404"
64
+ base_container.run_command("apt-get update")
65
+ base_container.run_command("apt-get install -y standard^ server^ vim-nox emacs23-nox tree openssh-server")
66
+ IO.write("#{base_container.config_item('lxc.rootfs')}/etc/rc.local", "#!/usr/bin/env bash\n\n/usr/sbin/dpkg-reconfigure openssh-server\n")
67
+ FileUtils.chmod(0755, "#{base_container.config_item('lxc.rootfs')}/etc/rc.local")
68
+ when "b-ubuntu-1604"
69
+ base_container.run_command("apt-get update")
70
+ base_container.run_command("apt-get install -y standard^ server^ vim-nox emacs24-nox tree openssh-server")
71
+ IO.write("#{base_container.config_item('lxc.rootfs')}/etc/rc.local", "#!/usr/bin/env bash\n\n/usr/sbin/dpkg-reconfigure openssh-server\n")
72
+ FileUtils.chmod(0755, "#{base_container.config_item('lxc.rootfs')}/etc/rc.local")
73
+ when "b-centos-5"
74
+ # downgrade openssl temporarily to overcome an install bug
75
+ # reference: http://www.hack.net.br/blog/2014/02/12/openssl-conflicts-with-file-from-package-openssl/
76
+ base_container.run_command("yum downgrade -y openssl")
77
+ base_container.run_command("yum install -y @base @core vim-enhanced emacs-nox tree openssh-server")
78
+ FileUtils.mkdir_p("#{base_container.config_item('lxc.rootfs')}/etc/sudoers.d")
79
+ FileUtils.chmod(0750, "#{base_container.config_item('lxc.rootfs')}/etc/sudoers.d")
80
+ append_line_to_file("#{base_container.config_item('lxc.rootfs')}/etc/sudoers", "\n#includedir /etc/sudoers.d\n")
81
+ when "b-centos-6"
82
+ base_container.run_command("yum install -y @base @core vim-enhanced emacs-nox tree openssh-server")
83
+ when "b-centos-7"
84
+ base_container.run_command("yum install -y @base @core vim-enhanced emacs-nox tree openssh-server")
85
+ end
86
+ base_container.run_command("useradd --create-home --shell /bin/bash --password $6$q3FDMpMZ$zfahCxEWHbzuEV98QPzhGZ7fLtGcLNZrbKK7OAYGXmJXZc07WbcxVnDwrMyX/cL6vSp4/IjlrVUZFBp7Orhyu1 dev-lxc")
87
+
88
+ FileUtils.mkdir_p("#{base_container.config_item('lxc.rootfs')}/home/dev-lxc/.ssh")
89
+ FileUtils.chmod(0700, "#{base_container.config_item('lxc.rootfs')}/home/dev-lxc/.ssh")
90
+ FileUtils.touch("#{base_container.config_item('lxc.rootfs')}/home/dev-lxc/.ssh/authorized_keys")
91
+ FileUtils.chmod(0600, "#{base_container.config_item('lxc.rootfs')}/home/dev-lxc/.ssh/authorized_keys")
92
+ base_container.run_command("chown -R dev-lxc:dev-lxc /home/dev-lxc/.ssh")
93
+
94
+ IO.write("#{base_container.config_item('lxc.rootfs')}/etc/sudoers.d/dev-lxc", "dev-lxc ALL=NOPASSWD:ALL\n")
95
+ FileUtils.chmod(0440, "#{base_container.config_item('lxc.rootfs')}/etc/sudoers.d/dev-lxc")
96
+ base_container.shutdown
97
+ return base_container
98
+ end
99
+
100
+ def self.reload_dnsmasq
101
+ system("pkill -HUP dnsmasq")
102
+ end
103
+
104
+ def self.search_file_delete_line(file_name, regex)
105
+ IO.write(file_name, IO.readlines(file_name).delete_if {|line| line.match(Regexp.new(regex))}.join)
106
+ end
107
+
108
+ def self.append_line_to_file(file_name, line)
109
+ content = IO.readlines(file_name)
110
+ content[-1] = content[-1].chomp + "\n" unless content.empty?
111
+ content << line
112
+ IO.write(file_name, content.join)
113
+ end
114
+
115
+ def self.search_file_replace(file_name, regex, replace)
116
+ IO.write(file_name, IO.readlines(file_name).map {|line| line.gsub(Regexp.new(regex), replace)}.join)
117
+ end
118
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dev-lxc
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremiah Snapp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-10 00:00:00.000000000 Z
11
+ date: 2017-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ executables:
88
88
  extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
+ - ".gitattributes"
91
92
  - ".gitignore"
92
93
  - CHANGELOG.md
93
94
  - Gemfile
@@ -107,6 +108,10 @@ files:
107
108
  - example-clusters/README.md
108
109
  - example-clusters/automate_dev-lxc.yml
109
110
  - example-clusters/chef-backend_dev-lxc.yml
111
+ - example-clusters/conf-files/chef-server/elasticsearch-partial.rb
112
+ - example-clusters/conf-files/chef-server/ldap-partial.rb
113
+ - example-clusters/conf-files/chef-server/postgres-partial.rb
114
+ - example-clusters/external_dev-lxc.yml
110
115
  - example-clusters/tier_dev-lxc.yml
111
116
  - lib/dev-lxc.rb
112
117
  - lib/dev-lxc/cli.rb