standup 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/standup.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'settingslogic'
2
+ require 'AWS'
3
+ require 'aws/s3'
4
+ require 'net/ssh'
5
+ require 'highline'
6
+
7
+ require 'standup/core_ext'
8
+ require 'standup/railtie'
9
+ require 'standup/settings'
10
+ require 'standup/ec2'
11
+ require 'standup/remoting'
12
+ require 'standup/scripts/base'
13
+ require 'standup/node'
14
+
15
+ module Standup
16
+ module Scripts; end
17
+
18
+ def self.nodes
19
+ Settings.nodes.keys.map{|name| Node.new name}
20
+ end
21
+
22
+ def self.gem_scripts_path
23
+ File.expand_path('../../scripts', __FILE__)
24
+ end
25
+
26
+ def self.local_scripts_path
27
+ File.expand_path('config/standup', Rails.root) rescue nil
28
+ end
29
+
30
+ def self.scripts
31
+ unless class_variable_defined? :@@scripts
32
+ @@scripts = {}
33
+ loaded = Set.new
34
+ [local_scripts_path, gem_scripts_path].each do |dir|
35
+ Dir.foreach dir do |name|
36
+ next unless File.file? "#{dir}/#{name}"
37
+ next unless name =~ /\.rb$/
38
+ next if loaded.include? name
39
+ load "#{dir}/#{name}", true
40
+ loaded << name
41
+ end if dir && File.exists?(dir)
42
+ end
43
+ end
44
+ @@scripts
45
+ end
46
+
47
+ def self.script &block
48
+ name = block.__file__.match(/([^\/]*)\.rb$/)[1]
49
+ script_class = Class.new(Standup::Scripts::Base, &block)
50
+ script_class.name = name
51
+ Standup::Scripts.const_set name.camelize, script_class
52
+ scripts[name] = script_class
53
+ end
54
+ end
@@ -0,0 +1,19 @@
1
+ desc "List standup scripts"
2
+ task :standup do
3
+ puts
4
+ bright_p "Standup scripts list:"
5
+ offset = Standup.scripts.keys.map(&:length).max + 2
6
+ Standup.scripts.each do |name, script|
7
+ puts " rake standup:\e[1m#{"%-#{offset}s" % name}\e[0m #{script.description}"
8
+ end
9
+ puts
10
+ end
11
+
12
+ namespace :standup do
13
+ Standup.scripts.each do |name, _|
14
+ desc "Run script #{name} for each node"
15
+ task name.to_sym do
16
+ Standup.nodes.each {|node| node.run_script name}
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ Standup.script do
2
+ self.description = 'Request new Elasics IP to use in node config'
3
+
4
+ def run
5
+ bright_p "New IP: #{Standup::EC2::ElasticIP.create.ip}"
6
+ end
7
+ end
data/scripts/basics.rb ADDED
@@ -0,0 +1,6 @@
1
+ Standup.script do
2
+ def run
3
+ sudo 'apt-get -qq update'
4
+ install_packages 'build-essential libreadline5-dev mc'
5
+ end
6
+ end
data/scripts/ec2.rb ADDED
@@ -0,0 +1,60 @@
1
+ Standup.script do
2
+ def run
3
+ ensure_security_group
4
+
5
+ open_port 22
6
+
7
+ ensure_instance
8
+
9
+ configure_elastic_ip
10
+ end
11
+
12
+ def open_ports *ports
13
+ ports.map(&:to_s).each do |port|
14
+ rule = Standup::EC2::SecurityGroup::IPRule.new('0.0.0.0/0', 'tcp', port, port)
15
+ group = Standup::EC2::SecurityGroup.list[node.id_group]
16
+ unless group.rules.include? rule
17
+ bright_p "opening port #{port}"
18
+ group.add_rule rule
19
+ end
20
+ end
21
+ end
22
+ alias :open_port :open_ports
23
+
24
+ protected
25
+
26
+ def ensure_security_group
27
+ return if Standup::EC2::SecurityGroup.list[node.id_group]
28
+
29
+ bright_p "creating security group #{node.id_group}"
30
+ Standup::EC2::SecurityGroup.create node.id_group
31
+ end
32
+
33
+ def ensure_instance
34
+ return if instance
35
+
36
+ bright_p "launching #{params.instance_type} instance with image #{params.image_id}"
37
+ inst = Standup::EC2::Instance.create params.image_id,
38
+ params.instance_type,
39
+ [Standup::EC2::SecurityGroup.list[node.id_group]]
40
+ puts "waiting until it's up"
41
+ inst.wait_until {inst.state != :running}
42
+ end
43
+
44
+ def configure_elastic_ip
45
+ if params.elastic_ip
46
+ unless params.elastic_ip == instance.external_ip
47
+ bright_p "attaching elastic ip #{params.elastic_ip}"
48
+ Standup::EC2::ElasticIP.list[params.elastic_ip].attach_to instance
49
+
50
+ puts "waiting until it's attached"
51
+ instance.wait_until {instance.external_ip != params.elastic_ip}
52
+ end
53
+ else
54
+ if ip = Standup::EC2::ElasticIP.list[instance.external_ip]
55
+ bright_p "detaching elastic ip #{params.elastic_ip}"
56
+ ip.detach
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,7 @@
1
+ Standup.script do
2
+ self.description = 'Put description here'
3
+
4
+ def run
5
+ #todo Put code here
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ Standup.script do
2
+ self.description = 'Generate script'
3
+
4
+ def run
5
+ unless ENV['SCRIPT']
6
+ bright_p "Specify script name with SCRIPT=name argument"
7
+ return
8
+ end
9
+
10
+ FileUtils.mkdir_p(Standup.local_scripts_path)
11
+ FileUtils.copy script_file('script.rb'),
12
+ File.expand_path("#{ENV['SCRIPT']}.rb", Standup.local_scripts_path)
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ aws:
2
+ account_id:
3
+ access_key_id:
4
+ secret_access_key:
5
+ keypair_name:
6
+ availability_zone: us-east-1a
7
+
8
+ nodes:
9
+ main:
10
+ ec2:
11
+ image_id: ami-480df921 # Canonical Ubuntu 10.04, EBS boot
12
+ instance_type: m1.small
13
+ ssh_user: ubuntu
14
+ # elastic_ip:
15
+ webapp:
16
+ github_user:
17
+ github_repo:
data/scripts/init.rb ADDED
@@ -0,0 +1,7 @@
1
+ Standup.script do
2
+ self.description = 'Generate config file'
3
+
4
+ def run
5
+ FileUtils.copy script_file('standup.yml'), File.expand_path('config', Rails.root)
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ Standup.script do
2
+ self.description = 'Put specified script into project configuration'
3
+
4
+ def run
5
+ unless ENV['SCRIPT']
6
+ bright_p "Specify script name with SCRIPT=name argument"
7
+ return
8
+ end
9
+
10
+ FileUtils.mkdir_p(Standup.local_scripts_path)
11
+ [ENV['SCRIPT'], "#{ENV['SCRIPT']}.rb"].each do |path|
12
+ path = File.expand_path(path, Standup.gem_scripts_path)
13
+ if File.exists? path
14
+ FileUtils.cp_r path, Standup.local_scripts_path
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ #user nobody;
2
+ worker_processes 1;
3
+
4
+ events {
5
+ worker_connections 1024;
6
+ }
7
+
8
+ http {
9
+ passenger_root /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.0;
10
+ passenger_ruby /usr/local/bin/ruby;
11
+
12
+ include mime.types;
13
+ default_type application/octet-stream;
14
+
15
+ sendfile on;
16
+
17
+ #keepalive_timeout 0;
18
+ keepalive_timeout 65;
19
+
20
+ gzip on;
21
+ gzip_buffers 16 8k;
22
+ gzip_disable "MSIE [1-6]\.";
23
+ gzip_proxied any;
24
+ gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
25
+
26
+ client_max_body_size 100m;
27
+
28
+ passenger_max_pool_size 10;
29
+
30
+ # standup place server fragments here
31
+ # standup place server fragments here
32
+ }
@@ -0,0 +1,10 @@
1
+ description "nginx http daemon"
2
+
3
+ start on runlevel [2]
4
+ stop on runlevel [016]
5
+
6
+ console output
7
+
8
+ exec /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -g "daemon off;"
9
+
10
+ respawn
@@ -0,0 +1,31 @@
1
+ Standup.script do
2
+ def run
3
+ scripts.ec2.open_port 80
4
+
5
+ install_gem 'passenger', '3.0.0'
6
+
7
+ unless file_exists? '/opt/nginx/sbin/nginx'
8
+ install_package 'libcurl4-openssl-dev'
9
+ sudo 'passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx'
10
+ end
11
+
12
+ upload script_file('nginx.conf'),
13
+ :to =>'/opt/nginx/conf/nginx.conf',
14
+ :sudo => true
15
+
16
+ upload script_file('upstart.conf'),
17
+ :to =>'/etc/init/nginx.conf',
18
+ :sudo => true
19
+ sudo 'initctl reload-configuration'
20
+
21
+ restart_nginx
22
+ end
23
+
24
+ def restart_nginx
25
+ if exec('initctl status nginx') =~ /running/i
26
+ sudo 'initctl restart nginx'
27
+ else
28
+ sudo 'initctl start nginx'
29
+ end
30
+ end
31
+ end