Skip to content

Commit 02f2fa5

Browse files
committed
refactor
1 parent ec73fe8 commit 02f2fa5

File tree

12 files changed

+217
-119
lines changed

12 files changed

+217
-119
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.gem
2+
idea*

Capfile

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
$:.unshift(File.dirname(__FILE__) + '/lib')
22

3-
require 'data_provider'
3+
require 'overcast/data_provider'
4+
require 'overcast/configuration'
45

56

67
groups.each { |group|
@@ -21,36 +22,34 @@ set :repository, "[email protected]:davidx/overcast.git"
2122
set :timestamp, Time.now.strftime("%s")
2223
set :release_path, "#{dataroot}/releases/#{timestamp}"
2324

24-
set :bootstrap_packages, %w[
25-
dev-vcs/git
26-
dev-lang/ruby
27-
rubygems
28-
]
29-
set :bootstrap_gems, %w[
30-
rake
31-
ohai
32-
chef
33-
choice
34-
rvm
35-
bundler
36-
]
3725

38-
namespace :overcast do
3926

27+
def emerge(packages=[])
28+
run "emerge #{packages.join(" ")}"
29+
end
30+
31+
def gem_install(gems=[])
32+
run "gem install #{gems.join(' ')} --no-ri --no-rdoc"
33+
end
34+
35+
36+
def run_commands(commands)
37+
commands.each{ |c| "echo [command:][#{c}] && echo [result:][#{`#{c}`}]" }.join(" && ")
38+
end
39+
40+
namespace :overcast do
4041
task :bootstrap do
41-
run "emerge --sync"
42-
run "emerge portage"
43-
run "etc-update"
44-
run "emerge #{bootstrap_packages.join(' ')}"
45-
run "gem install #{bootstrap_gems.join(' ')} --no-ri --no-rdoc"
46-
run "rvm-install"
47-
# run "echo '[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm' >> ~/.bashrc"
48-
# this is to bypass the [email protected] ssh yes/no question, todo add not_if or echo "y\r\n"
42+
run_commands bootstrap_commands
43+
emerge bootstrap_packages
44+
gem_install bootstrap_gems
45+
strict_host_key_prehack
46+
end
47+
task :strict_host_key_prehack do
48+
# initial alter to allow git repo pull after which ssh_config is rendered from recipe
4949
run "su - -c \"echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config\" "
5050
end
5151

52-
53-
task :deploy, :role => :app do
52+
task :deploy do
5453
cmd = ["mkdir -p #{dataroot}/releases"]
5554
cmd << "cd #{dataroot} && git clone #{repository} #{release_path}"
5655
cmd << "rm -f #{dataroot}/current"
@@ -61,9 +60,8 @@ namespace :overcast do
6160
update
6261
solo
6362
end
64-
def run_commands(commands)
65-
commands.collect{|c| "echo [command:][#{c}] && echo [result:][#{`#{c}`}]" }.join(" && ")
66-
end
63+
64+
6765
task :solo do
6866
profile = ENV.key?('PROFILE') ? ENV['PROFILE'] : 'default'
6967

@@ -77,7 +75,7 @@ namespace :overcast do
7775
run "cd #{chefroot} && #{git_command}"
7876
end
7977
task :grow do
80-
profile = ENV.key?('PROFILE') ? ENV['PROFILE'] : 'default'
78+
profile = ENV.key?('PROFILE') ? ENV['PROFILE'] : 'default'
8179
system "ruby bin/grow.rb --profile=#{profile}"
8280
end
8381
end

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
source :gemcutter
22
gem 'choice'
33
gem 'fog'
4-
gem 'amazon-ec2'
4+
gem 'amazon-ec2'
5+
gem 'net-ssh'

bin/grow.rb

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77
require 'net/ssh'
88

99
$:.unshift(File.dirname(__FILE__) + '/../lib')
10-
require 'data_provider'
10+
require 'overcast/configuration'
11+
require 'overcast/data_provider'
1112

12-
main_config_file = File.dirname(__FILE__) + '/../config/cloud.yml'
13-
credential_config_file = ENV['HOME'] + '/.fog'
13+
main_config= Overcast::Configuration.read
1414

15-
[main_config_file, credential_config_file].each do |config_file|
16-
unless File.exists?(config_file)
17-
p "please create the config file #{config_file} "
18-
exit 1
19-
end
20-
end
21-
config = YAML.load(IO.read(main_config_file))
15+
credentials = Overcast::Configuration.credentials
16+
EC2 = Overcast::DataProvider::Ec2
17+
RDS = Overcast::DataProvider::RDS
2218

2319
Choice.options do
2420
header ''
@@ -27,9 +23,8 @@
2723
option :profile, :required => true do
2824
short '-p'
2925
long '--profile=PROFILE'
30-
desc "The profile name, determines ami and post install configuration. (required)
31-
#{IO.read(main_config_file)}"
32-
default 'tester'
26+
desc "The profile name, determines ami and post install configuration. (required)"
27+
default 'default'
3328
end
3429
option :bootstrap do
3530
short '-b'
@@ -41,51 +36,44 @@
4136
short '-k'
4237
long '--key=KEY'
4338
desc 'The key name'
44-
default config[:defaults][:key_name]
39+
default main_config[:defaults][:key_name]
4540
end
4641
end
4742

4843
choices = Choice.choices
4944

5045

51-
profile = choices[:profile]
52-
key_name = choices[:key_name]
53-
54-
unless config[:profiles].key?(profile.to_sym)
55-
p "no such profile defined in config, please define a profile"
56-
exit 1
57-
end
58-
59-
defaults = config[:defaults]
60-
profile_server_config = config[:profiles][profile.to_sym]
61-
profile_server_config = defaults.merge(profile_server_config)
62-
profile_server_config[:key_name] = key_name
46+
server = Overcast::Ec2.launch_instance(
47+
Overcast::Configuration.get_config_for_profile(choices[:profile])
48+
)
6349

64-
print profile_server_config.inspect if ENV.key?('DEBUG')
6550

51+
Overcast.bootstrap_server_instance(server)
6652

67-
print "launching instance of profile #{profile} #{profile_server_config.to_yaml}"
68-
roles = profile_server_config.delete(:roles)
6953

70-
server = EC2.servers.create(profile_server_config)
71-
print "Please wait while your #{profile} instance is allocated\n"
72-
server.wait_for { print "."; ready? }
54+
Net::SSH.start( server.ip_address, 'root',
55+
:forward_agent =>true,
56+
:keys =>["#{ENV['HOME']}/.ec2/#{choices[:key_name]}"] ) do |session|
7357

74-
print "\n"
75-
puts "Public IP Address: #{server.ip_address}"
76-
puts "Private IP Address: #{server.private_ip_address}"
58+
shell = session.shell.sync
7759

78-
puts "key is #{key_name}"
60+
out = shell.pwd
61+
p out.stdout
7962

80-
p 'sleeping 60 to let it bootup properly'
81-
sleep 60
63+
out = shell.uptime
64+
p out.stdout
65+
p out.status
8266

83-
command = ['eval `ssh-agent`']
84-
command << "ssh-add ~/.ec2/#{key_name}"
85-
command << "cap overcast:deploy HOSTS=#{server.ip_address}"
86-
command << "cap overcast:runsolo HOSTS=#{server.ip_address} PROFILE=#{profile}"
67+
p shell.exit
8768

88-
fullcommand = command.join(" && ")
89-
p fullcommand
69+
end
9070

91-
system(fullcommand)
71+
#command = ['eval `ssh-agent`']
72+
#command << "ssh-add ~/.ec2/#{key_name}"
73+
#command << "cap overcast:deploy HOSTS=#{server.ip_address}"
74+
#command << "cap overcast:runsolo HOSTS=#{server.ip_address} PROFILE=#{profile}"
75+
#
76+
#fullcommand = command.join(" && ")
77+
#p fullcommand
78+
#
79+
#system(fullcommand)

chef/roles/monitoring.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
name "monitoring"
22
description "monitoring role"
33
run_list(["recipe[base]", "recipe[nagios]"])
4-
4+
default_users = %{nagios}.join(',')
55

66
default_attributes(
77
:nagios => {
8+
:package => 'nagios-HEAD.tar.gz',
9+
:package_source_url => 'http://nagios.sourceforge.net/download/cvs/',
10+
:cgi => { :users => { :default => default_users,
11+
:authorized_for_system_information => default_users,
12+
:authorized_for_configuration_information => default_users,
13+
:authorized_for_system_commands => default_users,
14+
:authorized_for_all_services => default_users,
15+
:authorized_for_all_hosts => default_users,
16+
:authorized_for_all_service_commands => default_users,
17+
:authorized_for_all_host_commands => default_users,
18+
},
19+
:html_url_path => '/nagios',
20+
},
21+
},
822
:roles => {
923
:app => {
10-
:service_checks => ['http']
24+
:service_checks => ['http'],
25+
:hosts => Overcast::DataProvider.find_hosts_for_role(:app)
1126
},
1227
:smtp => {
13-
:service_checks => ['smtp']
28+
:service_checks => ['smtp'],
29+
:hosts => Overcast::DataProvider.find_hosts_for_role(:smtp)
1430
}
1531

1632
},
1733
:contacts => [
1834
{
19-
2035
:name => 'david',
2136
:alias => 'davidx ops',
2237
:email =>'[email protected]'

chef/roles/testapp.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name "testapp"
2+
description "testapp role"
3+
run_list(["recipe[ruby_custom]"])
4+
5+
# Attributes applied no matter what the node has set already.
6+
#override_attributes()

config/overcast.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
:defaults:
22
:image_id: ami-6e2eca07
33
:flavor_id: m1.small
4-
:availability_zone: us-east-1d
5-
:key_name: gsg-keypair
4+
:availability_zone: us-east-1a
5+
:key_name: bootstrap_hostkey
66
:profiles:
77
:default:
88
:groups:
@@ -17,6 +17,13 @@
1717
:roles:
1818
- base
1919
- app
20+
:tester:
21+
:image_id: ami-6e2eca07
22+
:flavor_id: m1.small
23+
:groups:
24+
- default
25+
:roles:
26+
- testapp
2027
:system_profiles:
2128
:defaults:
2229
- app-admin/syslog-ng

lib/overcast.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
require 'overcast/configuration'
22
require 'overcast/data_provider'
3+
require 'overcast/ec2'
4+
require 'overcast/chef'
5+

lib/overcast/chef.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Overcast
2+
Bootstrap = { :commands =>
3+
%w{
4+
"emerge --sync"
5+
"emerge portage"
6+
"etc-update"
7+
"rc-update add sshd default"
8+
},
9+
10+
:packages =>
11+
%w{
12+
dev-vcs/git
13+
dev-lang/ruby
14+
rubygems
15+
},
16+
17+
18+
:gems =>
19+
%w{
20+
rake
21+
ohai
22+
chef
23+
choice
24+
bundler
25+
}
26+
}
27+
class Chef
28+
def self.bootstrap
29+
30+
end
31+
end
32+
end

lib/overcast/configuration.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,33 @@
22

33
module Overcast
44
class Configuration
5-
5+
def self.check_exists(file)
6+
unless File.exists?(file)
7+
p "please create the config file #{file} "
8+
exit 1
9+
end
10+
end
611
def self.credentials
712
credentials_config_file = ENV['HOME'] + '/.fog'
8-
YAML.load(IO.read(credentials_config_file))
13+
check_exists credentials_config_file
14+
credentials = YAML.load(IO.read(credentials_config_file))
915
end
10-
def self.read
11-
main_config_file = File.dirname(__FILE__) + '/../config/overcast.yml'
16+
def self.read_config
17+
main_config_file = File.dirname(__FILE__) + '/../../config/overcast.yml'
18+
check_exists main_config_file
1219
YAML.load(IO.read(main_config_file))
1320
end
21+
def self.get_config_for_profile(profile_name)
22+
main_config = read_config
23+
defaults = main_config[:defaults]
24+
unless main_config[:profiles].key?(profile_name.to_sym)
25+
raise "no such profile defined in config, please define a profile"
26+
end
27+
profile_server_config = main_config[:profiles][profile_name.to_sym]
28+
profile_server_config = defaults.merge(profile_server_config)
29+
profile_server_config[:key_name] = key_name
30+
print profile_server_config.inspect if ENV.key?('DEBUG')
31+
profile_server_config
32+
end
1433
end
1534
end
16-

0 commit comments

Comments
 (0)