hugo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,12 @@
1
+ bin/*
2
+ vendor/gems/*
3
+ !vendor/gems/cache/
4
+ !example/myhugo.rb
5
+ example/gmms.rb
6
+ example/gmms.yml
7
+ gmms.rb
8
+ oregano.rb
9
+ gmms.yml
10
+ oregano.yml
11
+ eirene.rb
12
+ eirene.yml
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ gem 'amazon-ec2'
2
+ gem 'net-ssh'
3
+ gem 'json'
4
+
5
+ only :test do
6
+ gem 'rspec'
7
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "hugo"
8
+ gemspec.summary = "Deploy Your Rack Apps to Cloud"
9
+ gemspec.description = "A easy to understand DSL that makes it dirt simple to deploy to the cloud."
10
+ gemspec.email = "tom@jackhq.com"
11
+ gemspec.homepage = "http://github.com/twilson63/hugo"
12
+ gemspec.authors = ["Tom Wilson", "Barrett Little"]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: gem install jeweler"
17
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
File without changes
@@ -0,0 +1,25 @@
1
+ require '../lib/hugo'
2
+
3
+ Hugo do
4
+ cloud "client" do
5
+ @instances = 2
6
+ @application = "example"
7
+
8
+ database do
9
+ @user = "dbsa"
10
+ @pass = "dbpass"
11
+ end
12
+
13
+ balancer
14
+
15
+ @package_list = [{"name"=>"mysql-client"}]
16
+ @gem_list = [{"name"=>"rack"}, {"name"=>"rails", "version"=>"2.3.5"}]
17
+ @run_list = ["role[web-app]"]
18
+
19
+ @github_url = "git@github.com:example"
20
+ @privatekey = "-----BEGIN RSA PRIVATE KEY-----\nxxxxxx-------\n-----END RSA PRIVATE KEY-----\n"
21
+ @publickey = "ssh-rsa xxxxxxxxx== progger@example.com\n"
22
+
23
+ @app_info = { :branch => "master", :migrate => true }
24
+ end
25
+ end
Binary file
@@ -0,0 +1,123 @@
1
+ # This makes sure the bundled gems are in our $LOAD_PATH
2
+ require File.expand_path(File.join(File.dirname(__FILE__) + "/..", 'vendor', 'gems', 'environment'))
3
+
4
+ # This actually requires the bundled gems
5
+ Bundler.require_env
6
+
7
+ require 'AWS'
8
+ require 'net/ssh'
9
+ require File.dirname(__FILE__) + '/hugo/rds'
10
+ require File.dirname(__FILE__) + '/hugo/elb'
11
+ require File.dirname(__FILE__) + '/hugo/ec2'
12
+
13
+ module Hugo
14
+
15
+ class << self
16
+ def build(infrastructure, application, instances = 1)
17
+ app_config = YAML.load_file("config/#{application}.yml")
18
+ @rds = Rds.new(:server => infrastructure, :db => application,
19
+ :user => app_config['database']['master_username'],
20
+ :pwd => app_config['database']['master_user_password']
21
+ )
22
+ @rds.save
23
+ @elb = Elb.new(:name => infrastructure)
24
+ @elb.save
25
+
26
+ instances.times do
27
+ @ec2 = Ec2.new()
28
+ @ec2.save
29
+ @elb.add(@ec2.name)
30
+ end
31
+
32
+ setup(infrastructure, application)
33
+
34
+ deploy(infrastructure, application)
35
+
36
+ end
37
+
38
+ def drop(infrastructure)
39
+ Rds.find(infrastructure).destroy
40
+ @elb = Elb.find(infrastructure)
41
+ @elb.instances.each do |i|
42
+ Ec2.find(i).destroy
43
+ end
44
+ @elb.destroy
45
+ end
46
+
47
+ def setup(infrastructure, application)
48
+ hugo_config = YAML.load_file("config/hugo.yml")
49
+
50
+ commands = []
51
+ commands << 'sudo apt-get update -y'
52
+ commands << 'sudo apt-get install ruby ruby1.8-dev libopenssl-ruby1.8 rdoc ri irb build-essential git-core xfsprogs -y'
53
+ commands << 'wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz && tar zxf rubygems-1.3.5.tgz'
54
+ commands << 'cd rubygems-1.3.5 && sudo ruby setup.rb && sudo ln -sfv /usr/bin/gem1.8 /usr/bin/gem'
55
+ commands << 'sudo gem update --system'
56
+ commands << 'sudo gem install gemcutter --no-ri --no-rdoc'
57
+ commands << 'sudo gem tumble'
58
+ commands << 'sudo gem install chef ohai --no-ri --no-rdoc'
59
+ commands << 'sudo gem source -a http://gems.github.com'
60
+ commands << 'sudo gem install chef-deploy --no-ri --no-rdoc'
61
+ commands << 'sudo gem install git --no-ri --no-rdoc'
62
+ commands << "git clone #{hugo_config['git']} ~/hugo-repos"
63
+ # Setup base role
64
+ dna = { :run_list => ["role[web-base]"],
65
+ :package_list => hugo_config['package_list'],
66
+ :gem_list => hugo_config['gem_list'],
67
+ :git => hugo_config['git'],
68
+ :github => hugo_config['github'],
69
+ :access_key => Ec2::ACCESS_KEY,
70
+ :secret_key => Ec2::SECRET_KEY,
71
+ :apache => hugo_config['apache']
72
+
73
+ }
74
+
75
+ commands << 'sudo chef-solo -c /home/ubuntu/hugo-repos/config/solo.rb -j /home/ubuntu/dna.json'
76
+
77
+ @elb = Elb.find(infrastructure)
78
+ @elb.instances.each do |i|
79
+ Ec2.find(i).ssh(commands, dna)
80
+ end
81
+
82
+ end
83
+
84
+ def deploy(infrastructure, application)
85
+ hugo_config = YAML.load_file("config/hugo.yml")
86
+ app_config = YAML.load_file("config/#{application}.yml")
87
+
88
+ commands = []
89
+ #commands << "git clone #{@@hugo_config['git']} ~/hugo-repos"
90
+ commands << "cd hugo-repos && git pull"
91
+ commands << 'sudo chef-solo -c /home/ubuntu/hugo-repos/config/solo.rb -j /home/ubuntu/dna.json'
92
+
93
+ dna = { :run_list => app_config['run_list'],
94
+ :package_list => app_config['package_list'] || {},
95
+ :gem_list => app_config['gem_list'] || {},
96
+ :application => application,
97
+ :customer => infrastructure,
98
+ :database => {
99
+ :uri => Rds.find(infrastructure).uri,
100
+ :user => app_config['database']['master_username'],
101
+ :password => app_config['database']['master_user_password'] },
102
+ :web => { :port => "8080", :ssl => "8443" },
103
+ :git => hugo_config['git'],
104
+ :github => hugo_config['github'],
105
+ :access_key => Ec2::ACCESS_KEY,
106
+ :secret_key => Ec2::SECRET_KEY,
107
+ :app => app_config['app'] || nil
108
+ }
109
+
110
+ @elb = Elb.find(infrastructure)
111
+ @elb.instances.each do |i|
112
+ Ec2.find(i).ssh(commands, dna)
113
+ end
114
+ end
115
+
116
+ def add(infrastructure, application)
117
+ @elb = Elb.find(infrastructure)
118
+
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,39 @@
1
+ # This makes sure the bundled gems are in our $LOAD_PATH
2
+ require File.expand_path(File.join(File.dirname(__FILE__) + "/..", 'vendor', 'gems', 'environment'))
3
+
4
+ # This actually requires the bundled gems
5
+ Bundler.require_env
6
+
7
+ require 'AWS'
8
+ require 'net/ssh'
9
+ require 'json'
10
+ require 'singleton'
11
+ require File.dirname(__FILE__) + '/hugo/mixin/params_validate'
12
+ require File.dirname(__FILE__) + '/hugo/cloud'
13
+ require File.dirname(__FILE__) + '/hugo/balancer'
14
+ require File.dirname(__FILE__) + '/hugo/database'
15
+ require File.dirname(__FILE__) + '/hugo/app'
16
+ require File.dirname(__FILE__) + '/hugo/aws/rds'
17
+ require File.dirname(__FILE__) + '/hugo/aws/elb'
18
+ require File.dirname(__FILE__) + '/hugo/aws/ec2'
19
+
20
+ module Hugo; end
21
+
22
+ class Hugo::Suite
23
+ include Singleton
24
+
25
+ def initialize
26
+ end
27
+
28
+ def cloud(name="DEFAULT", &block)
29
+ cloud = Hugo::Cloud.instance
30
+ cloud.name name
31
+ cloud.instance_eval(&block) if block_given?
32
+ end
33
+ end
34
+
35
+
36
+ def Hugo(&block)
37
+ Hugo::Suite.instance.instance_eval(&block)
38
+ end
39
+
Binary file
@@ -0,0 +1,230 @@
1
+ module Hugo; end
2
+
3
+ class Hugo::App
4
+ include Singleton
5
+ include Hugo::Mixin::ParamsValidate
6
+
7
+
8
+ def servers(instances=1)
9
+ if lb
10
+ if instances > lb.instances.length
11
+ build_ec2(instances - lb.instances.length)
12
+ elsif instances < lb.instances.length
13
+ delete_ec2(lb.instances.length - instances)
14
+ end
15
+ end
16
+ end
17
+
18
+ def setup
19
+ lb.instances.each do |i|
20
+ setup_ec2(i)
21
+ end
22
+ puts "Setup Completed"
23
+ end
24
+
25
+ def deploy
26
+ deploy_ec2
27
+ puts "Deploy Completed"
28
+ end
29
+
30
+ def destroy
31
+ lb.instances.each do |i|
32
+ Hugo::Aws::Ec2.find(i).destroy
33
+ end
34
+ end
35
+
36
+
37
+ def name(arg=nil)
38
+ set_or_return(:name, arg, :kind_of => [String])
39
+ end
40
+
41
+ def lb(arg=nil)
42
+ set_or_return(:lb, arg, :kind_of => [Hugo::Aws::Elb])
43
+ end
44
+
45
+ def db(arg=nil)
46
+ set_or_return(:db, arg, :kind_of => [Hugo::Aws::Rds])
47
+ end
48
+
49
+ def uri(arg=nil)
50
+ set_or_return(:uri, arg, :kind_of => [String])
51
+ end
52
+
53
+ def type(arg=nil)
54
+ set_or_return(:type, arg, :kind_of => [String])
55
+ end
56
+
57
+ def zone(arg=nil)
58
+ set_or_return(:zone, arg, :kind_of => [String])
59
+ end
60
+
61
+ def image_id(arg=nil)
62
+ set_or_return(:image_id, arg, :kind_of => [String])
63
+ end
64
+
65
+ def port(arg=nil)
66
+ set_or_return(:port, arg, :kind_of => [String])
67
+ end
68
+
69
+ def ssl(arg=nil)
70
+ set_or_return(:ssl, arg, :kind_of => [String])
71
+ end
72
+
73
+ def application(arg=nil)
74
+ set_or_return(:application, arg, :kind_of => [String])
75
+ end
76
+
77
+ def security_group(arg=nil)
78
+ set_or_return(:security_group, arg, :kind_of => [String])
79
+ end
80
+
81
+ def cloud_name(arg=nil)
82
+ set_or_return(:cloud_name, arg, :kind_of => [String])
83
+ end
84
+
85
+ def key_name(arg=nil)
86
+ set_or_return(:key_name, arg, :kind_of => [String])
87
+ end
88
+
89
+ def cookbook(arg=nil)
90
+ set_or_return(:cookbook, arg, :kind_of => [String])
91
+ end
92
+
93
+ def key_pair_file(arg=nil)
94
+ set_or_return(:key_pair_file, arg, :kind_of => [String])
95
+ end
96
+
97
+ def port(arg=nil)
98
+ set_or_return(:port, arg, :kind_of => [String])
99
+ end
100
+
101
+ def github_url(arg=nil)
102
+ set_or_return(:github_url, arg, :kind_of => [String])
103
+ end
104
+
105
+ def privatekey(arg=nil)
106
+ set_or_return(:privatekey, arg, :kind_of => [String])
107
+ end
108
+
109
+ def publickey(arg=nil)
110
+ set_or_return(:publickey, arg, :kind_of => [String])
111
+ end
112
+
113
+ def gem_list(arg=nil)
114
+ set_or_return(:gem_list, arg, :kind_of => [Array])
115
+ end
116
+
117
+ def package_list(arg=nil)
118
+ set_or_return(:package_list, arg, :kind_of => [Array])
119
+ end
120
+
121
+ def run_list(arg=nil)
122
+ set_or_return(:run_list, arg, :kind_of => [Array])
123
+ end
124
+
125
+ def deploy_info(arg=nil)
126
+ set_or_return(:deploy_info, arg, :kind_of => [Hash])
127
+ end
128
+
129
+
130
+
131
+
132
+
133
+ private
134
+
135
+ def build_ec2(i=1)
136
+ i.times do
137
+ instance_id = create_ec2
138
+ #setup_ec2(instance_id)
139
+ lb.add(instance_id)
140
+ end
141
+ end
142
+
143
+ def create_ec2
144
+ ec2 = Hugo::Aws::Ec2.new(:type => type,
145
+ :zone => zone,
146
+ :image_id => image_id,
147
+ :key_name => key_name,
148
+ :security_group => "default").create
149
+
150
+ new_ec2 = nil
151
+ sleep 10
152
+ loop do
153
+ new_ec2 = Hugo::Aws::Ec2.find(ec2.name)
154
+ puts new_ec2.status
155
+ if new_ec2.status == "running"
156
+ break
157
+ end
158
+ sleep 30
159
+ end
160
+
161
+ sleep 10
162
+ new_ec2.name
163
+ end
164
+
165
+ def setup_ec2(instance_id)
166
+
167
+ commands = []
168
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else sudo apt-get update -y; fi'
169
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else sudo apt-get install ruby ruby1.8-dev libopenssl-ruby1.8 rdoc ri irb build-essential git-core xfsprogs -y; fi'
170
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz && tar zxf rubygems-1.3.5.tgz; fi'
171
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else cd rubygems-1.3.5 && sudo ruby setup.rb && sudo ln -sfv /usr/bin/gem1.8 /usr/bin/gem; fi'
172
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else sudo gem update --system; fi'
173
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else sudo gem install gemcutter --no-ri --no-rdoc; fi'
174
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else sudo gem install chef ohai --no-ri --no-rdoc; fi'
175
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else sudo gem source -a http://gems.github.com; fi'
176
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else sudo gem install chef-deploy --no-ri --no-rdoc; fi'
177
+ commands << 'if [ -d "./hugo-repos" ]; then echo "setup already run"; else sudo gem install git --no-ri --no-rdoc; fi'
178
+ commands << "if [ -d \"./hugo-repos\" ]; then echo \"setup already run\"; else git clone #{self.cookbook} ~/hugo-repos; fi"
179
+ Hugo::Aws::Ec2.find(instance_id).ssh(commands, nil, key_pair_file)
180
+ end
181
+
182
+ def deploy_ec2
183
+
184
+ commands = []
185
+ commands << "cd hugo-repos && git pull"
186
+ commands << 'sudo chef-solo -c /home/ubuntu/hugo-repos/config/solo.rb -j /home/ubuntu/dna.json'
187
+
188
+ ports = [port]
189
+ ports << ssl unless ssl.nil?
190
+
191
+ database_info = {}
192
+ database_info = {
193
+ :uri => db.uri,
194
+ :name => db.db,
195
+ :user => db.user,
196
+ :password => db.password } unless db.nil?
197
+
198
+ dna = { :run_list => run_list,
199
+ :package_list => package_list,
200
+ :gem_list => gem_list,
201
+
202
+ :application => name,
203
+ :customer => cloud_name,
204
+ :database => database_info,
205
+ :web => { :port => port, :ssl => ssl },
206
+ :git => cookbook,
207
+ :github => { :url => github_url,
208
+ :publickey => publickey,
209
+ :privatekey => privatekey},
210
+ :access_key => Hugo::Aws::Ec2::ACCESS_KEY,
211
+ :secret_key => Hugo::Aws::Ec2::SECRET_KEY,
212
+ :apache => { :listen_ports => ports },
213
+ :app => deploy_info
214
+ }
215
+
216
+ lb.instances.each do |i|
217
+ Hugo::Aws::Ec2.find(i).ssh(commands, dna, key_pair_file)
218
+ end
219
+ end
220
+
221
+ def delete_ec2(i=1)
222
+ i.times do
223
+ instance_id = lb.instances[0]
224
+ Hugo::Aws::Ec2.find(instance_id).destroy
225
+ #lb.remove(instance_id)
226
+
227
+ end
228
+ end
229
+
230
+ end