Class: Tadpole

Inherits:
Object
  • Object
show all
Defined in:
lib/sapoku.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, stack = "ruby") ⇒ Tadpole

create a new container stub (then requires saving)



34
35
36
37
38
39
40
# File 'lib/sapoku.rb', line 34

def initialize(name, stack = "ruby")
	@container_ip = getfreeip
	@local_port = getlocalport
	@app_name = name
	@stack = stack
	@ram = 512
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



5
6
7
# File 'lib/sapoku.rb', line 5

def app_name
  @app_name
end

#container_ipObject

Returns the value of attribute container_ip.



5
6
7
# File 'lib/sapoku.rb', line 5

def container_ip
  @container_ip
end

#local_portObject

Returns the value of attribute local_port.



5
6
7
# File 'lib/sapoku.rb', line 5

def local_port
  @local_port
end

#ramObject

Returns the value of attribute ram.



5
6
7
# File 'lib/sapoku.rb', line 5

def ram
  @ram
end

#stackObject

Returns the value of attribute stack.



5
6
7
# File 'lib/sapoku.rb', line 5

def stack
  @stack
end

#useridObject

Returns the value of attribute userid.



5
6
7
# File 'lib/sapoku.rb', line 5

def userid
  @userid
end

Class Method Details

.allObject

returns an array of known Tadpoles - doesn’t scale, obviously



23
24
25
26
27
28
29
30
31
# File 'lib/sapoku.rb', line 23

def self.all
	tadpoles = `sudo lxc-ls -1`.split(/\n/).uniq
	output = []
	for t in tadpoles do
		pole = self.find(t)
		output << pole if !pole.nil?
	end
	return output
end

.find(name) ⇒ Object

return an instance based on a given name, if it exists



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sapoku.rb', line 9

def self.find(name)
	# if this is false, return nil
	return nil if !$redis.hexists(name, "ip")

	tadpole = self.new(name)
	tadpole.container_ip = $redis.hget(name, "ip")
	tadpole.local_port = $redis.hget(name, "localport")
	tadpole.userid = $redis.hget(name, "userid")
	tadpole.ram = $redis.hget(name, "ram")
	tadpole.stack = $redis.hget(name, "stack")
	return tadpole
end

Instance Method Details

#bootstrapObject

actually creates and initializes the container returns the actual raw console output of the generated commands



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sapoku.rb', line 71

def bootstrap
	self.save
	output = "Creating new container for your app using the #{@stack} stack"
	output += `sudo lxc-clone -o #{@stack} -n #{@app_name}`
	output += "Applying new config file to container"
	create_interfaces_file
	create_lxc_config
	output += "Booting your new container"
	output += `sudo lxc-start -n #{@app_name} -d`
	create_iptables
	output += "Creating nginx configuration file"
	create_nginx_config
	output += "Rehashing nginx configuration"
	rehash_nginx
	return output
end

#create_interfaces_fileObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sapoku.rb', line 102

def create_interfaces_file
	@ip = self.container_ip
	@name = self.app_name

	template = %{
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address #{@ip}
netmask 255.255.255.0
gateway 10.0.3.1
}

	erb = ERB.new(template)

	File.open("#{@name}_interfaces", 'w') do |f|
		f.write erb.result(self.get_binding)
	end

	system("sudo mv #{@name}_interfaces /var/lib/lxc/#{@name}/rootfs/etc/network/interfaces")
end

#create_iptablesObject

create the new iptables rule to fwd accesses into the container



225
226
227
# File 'lib/sapoku.rb', line 225

def create_iptables
	system("sudo iptables -t nat -A PREROUTING -p tcp --dport #{@local_port} -j DNAT --to-destination #{@container_ip}:8080")
end

#create_lxc_configObject

generate a new config file



160
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
192
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
219
220
221
222
# File 'lib/sapoku.rb', line 160

def create_lxc_config
	@ip = self.container_ip
	@ram = self.ram
	@name = self.app_name

	template = %{
lxc.utsname = <%= @name %>
lxc.mount = /var/lib/lxc/<%= @name %>/fstab
lxc.rootfs = /var/lib/lxc/<%= @name %>/rootfs

# networking
lxc.network.type=veth
lxc.network.flags=up
lxc.network.link=lxcbr0
#lxc.network.hwaddr=00:16:3e:85:68:c1
lxc.network.name = eth0
lxc.network.ipv4=<%= @ip %>

lxc.devttydir = lxc
lxc.tty = 4
lxc.pts = 1024
lxc.arch = amd64
lxc.cap.drop = sys_module mac_admin
lxc.pivotdir = lxc_putold

lxc.cgroup.memory.limit_in_bytes = <%= @ram %>M

# uncomment the next line to run the container unconfined:
#lxc.aa_profile = unconfined

lxc.cgroup.devices.deny = a
# Allow any mknod (but not using the node)
lxc.cgroup.devices.allow = c *:* m
lxc.cgroup.devices.allow = b *:* m
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
#lxc.cgroup.devices.allow = c 4:0 rwm
#lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
lxc.cgroup.devices.allow = c 254:0 rwm
lxc.cgroup.devices.allow = c 10:229 rwm
lxc.cgroup.devices.allow = c 10:200 rwm
lxc.cgroup.devices.allow = c 1:7 rwm
lxc.cgroup.devices.allow = c 10:228 rwm
lxc.cgroup.devices.allow = c 10:232 rwm
}

	erb = ERB.new(template)

	File.open("#{@name}_config", 'w') do |f|
		f.write erb.result(self.get_binding)
	end

	system("sudo mv #{@name}_config /var/lib/lxc/#{@name}/config")
end

#create_nginx_configObject



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
# File 'lib/sapoku.rb', line 126

def create_nginx_config
	@ip = self.container_ip
	@ram = self.ram
	@name = self.app_name

	template = %{
		server {
			listen 80;
			server_name #{@name}.sapoku.webreakstuff.com;
			access_log off;
			error_log off;

			location / {
				proxy_pass http://#{@ip}:8080;
				proxy_set_header X-Real-IP $remote_addr;
			}
		}
	}

	erb = ERB.new(template)

	File.open("#{@name}_nginx_config", 'w') do |f|
		f.write erb.result(self.get_binding)
	end

	system("sudo mv #{@name}_nginx_config /opt/nginx/conf/containers/#{@name}.conf")
end

#destroyObject

wipes a container from HDD and redis



52
53
54
55
56
57
58
59
60
# File 'lib/sapoku.rb', line 52

def destroy
	# delete the hash from redis
	$redis.del(@app_name)
	# add the ip back to our free ips list
	$redis.sadd("sapoku:freeips", @container_ip)
	`sudo lxc-stop -n #{@app_name}`
	`sudo lxc-destroy -n #{@app_name}`
	`sudo rm /opt/nginx/conf/containers/#{@app_name}.conf`
end

#get_bindingObject



98
99
100
# File 'lib/sapoku.rb', line 98

def get_binding
	binding
end

#getfreeipObject

returns a free IP to be used by the container being bootstrapped



89
90
91
# File 'lib/sapoku.rb', line 89

def getfreeip
	$redis.spop("sapoku:freeips")
end

#getlocalportObject

return a free port to be used to forward external->internal requests



94
95
96
# File 'lib/sapoku.rb', line 94

def getlocalport
	$redis.spop("sapoku:freeports")
end

#rehash_nginxObject

reload nginx configuration



155
156
157
# File 'lib/sapoku.rb', line 155

def rehash_nginx
	`sudo kill -HUP $(cat /opt/nginx/logs/nginx.pid)`
end

#running?Boolean

returns true if the instance is running

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/sapoku.rb', line 63

def running?
	$regex = /RUNNING/
	return false if $regex.match(`sudo lxc-info -n #{@app_name}`).nil?
	return true
end

#saveObject

creates and saves the new container



43
44
45
46
47
48
49
# File 'lib/sapoku.rb', line 43

def save
	$redis.hset(@app_name, "ip", @container_ip)
	$redis.hset(@app_name, "localport", @local_port)
	$redis.hset(@app_name, "userid", @userid)
	$redis.hset(@app_name, "ram", @ram)
	$redis.hset(@app_name, "stack", @stack)
end