sapoku 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/sapoku.rb +78 -0
  2. metadata +45 -0
@@ -0,0 +1,78 @@
1
+ require 'redis'
2
+ require 'erb'
3
+
4
+ class Gyro
5
+ attr_accessor :container_ip, :local_port, :app_name, :userid, :ram
6
+ $redis = Redis.new
7
+
8
+ # return an instance based on a given name, if it exists
9
+ def self.find(name)
10
+ # if this is false, return nil
11
+ return nil if !$redis.hexists(name, "ip")
12
+
13
+ gyro = self.new(name)
14
+ gyro.container_ip = $redis.hget(name, "ip")
15
+ gyro.local_port = $redis.hget(name, "localport")
16
+ gyro.userid = $redis.hget(name, "userid")
17
+ gyro.ram = $redis.hget(name, "ram")
18
+ return gyro
19
+ end
20
+
21
+ # create a new container stub (then requires saving)
22
+ def initialize(name)
23
+ @container_ip = getfreeip
24
+ @local_port = getlocalport
25
+ @app_name = name
26
+ @ram = 256
27
+ end
28
+
29
+ # creates and saves the new container
30
+ def save
31
+ $redis.hset(@app_name, "ip", @container_ip)
32
+ $redis.hset(@app_name, "localport", @local_port)
33
+ $redis.hset(@app_name, "userid", @userid)
34
+ $redis.hset(@app_name, "ram", @ram)
35
+ end
36
+
37
+ # actually creates and initializes the container
38
+ def bootstrap
39
+ system("lxc-clone -o frox -n #{@app_name}")
40
+ create_config
41
+ system("lxc-start -n #{@app_name} -d")
42
+ create_iptables
43
+ end
44
+
45
+ # returns a free IP to be used by the container being bootstrapped
46
+ def getfreeip
47
+ $redis.spop("sapoku:freeips")
48
+ end
49
+
50
+ # return a free port to be used to forward external->internal requests
51
+ def getlocalport
52
+ $redis.spop("sapoku:freeports")
53
+ end
54
+
55
+ def get_binding
56
+ binding
57
+ end
58
+
59
+ # generate a new config file
60
+ def create_config
61
+ @ip = self.container_ip
62
+ @ram = self.ram
63
+ @name = self.app_name
64
+
65
+ erb = ERB.new(File.read('config.erb'))
66
+
67
+ File.open("#{@name}_config", 'w') do |f|
68
+ f.write erb.result(self.get_binding)
69
+ end
70
+
71
+ system("mv #{@name}_config /var/lib/lxc/#{@name}/config")
72
+ end
73
+
74
+ # create the new iptables rule to fwd accesses into the container
75
+ def create_iptables
76
+ system("sudo iptables -t nat -A PREROUTING -p tcp --dport #{@local_port} -j DNAT --to-destination #{@container_ip}:8080")
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sapoku
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fred Oliveira
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Manages Sapoku instances
15
+ email: fred@helloform.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sapoku.rb
21
+ homepage: http://rubygems.org/gems/hola
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Manages Sapoku instances
45
+ test_files: []