rig 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +67 -0
  5. data/Rakefile +2 -0
  6. data/bin/rig +15 -0
  7. data/lib/rig.rb +2 -0
  8. data/lib/rig/capistrano.rb +54 -0
  9. data/lib/rig/chef.rb +53 -0
  10. data/lib/rig/cloud.rb +4 -0
  11. data/lib/rig/cloud/balancer.rb +40 -0
  12. data/lib/rig/cloud/connection.rb +40 -0
  13. data/lib/rig/cloud/dns.rb +56 -0
  14. data/lib/rig/cloud/environment.rb +214 -0
  15. data/lib/rig/cloud/instance.rb +64 -0
  16. data/lib/rig/cloud/userdata.rb +111 -0
  17. data/lib/rig/command.rb +42 -0
  18. data/lib/rig/command/abstract.rb +36 -0
  19. data/lib/rig/command/balancer/destroy.rb +16 -0
  20. data/lib/rig/command/balancer/list.rb +21 -0
  21. data/lib/rig/command/balancer/main.rb +13 -0
  22. data/lib/rig/command/balancer/view.rb +19 -0
  23. data/lib/rig/command/dns/create.rb +19 -0
  24. data/lib/rig/command/dns/destroy.rb +16 -0
  25. data/lib/rig/command/dns/edit.rb +20 -0
  26. data/lib/rig/command/dns/list.rb +21 -0
  27. data/lib/rig/command/dns/main.rb +13 -0
  28. data/lib/rig/command/environment/create.rb +24 -0
  29. data/lib/rig/command/environment/destroy.rb +18 -0
  30. data/lib/rig/command/environment/list.rb +30 -0
  31. data/lib/rig/command/environment/main.rb +15 -0
  32. data/lib/rig/command/instance/create.rb +27 -0
  33. data/lib/rig/command/instance/destroy.rb +20 -0
  34. data/lib/rig/command/instance/list.rb +25 -0
  35. data/lib/rig/command/instance/main.rb +16 -0
  36. data/lib/rig/command/instance/ssh.rb +32 -0
  37. data/lib/rig/command/instance/tag/get.rb +41 -0
  38. data/lib/rig/command/instance/tag/main.rb +14 -0
  39. data/lib/rig/command/instance/tag/remove.rb +44 -0
  40. data/lib/rig/command/instance/tag/set.rb +47 -0
  41. data/lib/rig/command/instance/view.rb +20 -0
  42. data/lib/rig/command/keypair/list.rb +14 -0
  43. data/lib/rig/command/keypair/main.rb +11 -0
  44. data/lib/rig/command/main.rb +48 -0
  45. data/lib/rig/command/options.rb +97 -0
  46. data/lib/rig/config.rb +68 -0
  47. data/lib/rig/version.rb +10 -0
  48. data/rig.gemspec +39 -0
  49. metadata +196 -0
@@ -0,0 +1,64 @@
1
+ require 'fog'
2
+
3
+ module Rig
4
+ module Cloud
5
+ class Instance
6
+ class << self
7
+ def all(filters={})
8
+ Rig::Cloud.compute.servers.all(filters)
9
+ end
10
+
11
+ def running
12
+ self.all({"instance-state-name"=>"running"})
13
+ end
14
+
15
+ def find(id)
16
+ if id =~ /^i-/
17
+ Rig::Cloud.compute.servers.find(id)
18
+ else
19
+ # must be tag:Name
20
+ Rig::Cloud.compute.servers.all({"tag:Name"=>id})
21
+ end
22
+ end
23
+
24
+ def find_by_environment(env)
25
+ Rig::Cloud.compute.servers.all({"tag:Environment"=>env})
26
+ end
27
+
28
+ def create(opts, tags={})
29
+ fog = Rig::Cloud.compute
30
+ server = fog.servers.create(opts)
31
+ fog.create_tags(server.id, tags) if tags.count > 0
32
+ puts ".. created: #{server.id}"
33
+ server
34
+ end
35
+
36
+ def destroy(list)
37
+ list = [*list]
38
+
39
+ if list.count > 0
40
+ compute = Rig::Cloud.compute
41
+ ids = list.map {|e| e.kind_of?(String) ? e : e.id}
42
+ puts ".. destroying: #{ids.inspect}"
43
+
44
+ # if we've got a chef config, use it
45
+ if Rig.config.chef
46
+ list.each do |e|
47
+ Rig::Chef.client_delete(e.tags['Name']) if e.tags['Name']
48
+ Rig::Chef.node_delete(e.tags['Name']) if e.tags['Name']
49
+ end
50
+ end
51
+
52
+ # destroy the instances
53
+ list.each do |e|
54
+ e.destroy
55
+ end
56
+
57
+ tags = %w{Name Environment Role}.inject({}) {|h, e| h[e] = nil; h }
58
+ compute.create_tags(ids, tags)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,111 @@
1
+ require 'erubis'
2
+
3
+ module Rig
4
+ module Cloud
5
+ class << self
6
+ def userdata(name, role, environment, opts={ })
7
+ data = {
8
+ :name => name,
9
+ :role => role,
10
+ :environment => environment,
11
+ :zone => Rig.config.dns.zone,
12
+ :dependencies => %w{ ruby1.9.3 ruby1.9.1-dev rubygems ruby-switch libopenssl-ruby1.9.1 rdoc1.9.1 ri1.9.1 irb1.9.1
13
+ build-essential wget ssl-cert curl },
14
+ :gems => %w{chef},
15
+ :chef => false,
16
+ }.merge(opts)
17
+ template = <<-EOF
18
+ #!/bin/bash
19
+ set -e -x
20
+ exec > ~/userdata.log 2>&1
21
+ export DEBIAN_FRONTEND=noninteractive
22
+ apt-add-repository -y ppa:brightbox/ruby-ng-experimental
23
+
24
+ apt-get update && apt-get upgrade -y
25
+ apt-get -y install <%= @dependencies.join(' ') %>
26
+ mkdir -p /var/log/chef
27
+ mkdir -p /var/backups/chef
28
+ mkdir -p /var/run/chef
29
+ mkdir -p /var/cache/chef
30
+ mkdir -p /var/lib/chef
31
+ mkdir /etc/chef
32
+
33
+ # set up hostname
34
+ hostname <%= @name %>.<%= @zone %>
35
+ echo "<%= @name %>.<%= @zone %>" > /etc/hostname
36
+ echo "127.0.0.1 <%= @name %>.<%= @zone %> <%= @name.split('.')[0] %> <%= @name %> # entered by userdata script" >> /etc/hosts
37
+
38
+ gem install rubygems-update --no-ri --no-rdoc
39
+ update_rubygems
40
+
41
+ <% @gems.each do |gem| %>
42
+ gem install <%= gem %> --no-ri --no-rdoc
43
+ <% end %>
44
+
45
+ cat <<FACTS > /etc/facts.yml
46
+ inq_name: <%= @name %>
47
+ inq_role: <%= @role %>
48
+ inq_environment: <%= @environment %>
49
+ FACTS
50
+
51
+ cat <<JSON > /etc/chef/bootstrap.json
52
+ {
53
+ "run_list": [
54
+ "role[<%= @role %>]"
55
+ ]
56
+ }
57
+ JSON
58
+
59
+ cat <<CLIENT > /etc/chef/client.rb
60
+ log_level :info
61
+ log_location "/var/log/chef/client.log"
62
+ ssl_verify_mode :verify_none
63
+ validation_client_name "chef-validator"
64
+ validation_key "/etc/chef/validation.pem"
65
+ client_key "/etc/chef/client.pem"
66
+ chef_server_url "http://chef.inqlabs.com:4000";
67
+ file_cache_path "/var/cache/chef"
68
+ file_backup_path "/var/backups/chef"
69
+ pid_file "/var/run/chef/client.pid"
70
+ node_name "<%= @name %>";
71
+ Chef::Log::Formatter.show_time = true
72
+ CLIENT
73
+
74
+ cat <<VALID > /etc/chef/validation.pem
75
+ -----BEGIN RSA PRIVATE KEY-----
76
+ MIIEpgIBAAKCAQEAxFST0blraLl7mac91QzSxNNZQt7A4Mo4O297e+r6F5b1+Eae
77
+ RNAH9w4p9sctLttIT8rI77hWrgS04f6mbamSaY+pAyWje+MX2s5YXizEML1Fdyr6
78
+ hV1n/vIMOmhcVwGbF3fg6BKwjur7A1dS+aOM1sEYi77hfbY4NHhLCQiR4v2m4saC
79
+ C9SPX7uNIzKvGB2P/5U+HNPaJuo6GAUsrOrTA/YB53Bza4DPMpyp9+Hf/ZR9l6Hd
80
+ 4tCAp07dUNZ/LKKL4AmsnCqlT5EiYFIsaq2ezzPnS9t2FlFV60a2opbdEvq/QLbz
81
+ ouH6H+/4n/+BY9bVBCg1oBXHlLwbZmNl7Xl4wQIDAQABAoIBAQCFDIAkZ5C3psNx
82
+ bJoFkKwhQM1D+OsjK0eSV8mKP8J+TAqi11CYd5Z+QbYEjBQ9pdNKZ7VmZzkusvfW
83
+ E5m7xWf4a6fw/wfchBh9assN/y5xjULPMGwYByXb6zKIPoMdX3Q4IkBjZY8LO4ki
84
+ SYJstP5T5wX4ZkIvzFYokMDg/VLITUZPnxu+OunFF/vfl7mFOIrHtCj6LSpCk5xW
85
+ NsQqJWzK6/xjKH8KweWkl+E7k/ILUetXuCjKhj2XjiYUbzRH12e5jJIsEXRKWw8I
86
+ Iw5I8N4fWaXbTmoI+NUV+24BqwOfCH0RjoZXcc6LOxmxKDd8a00H3bCYj9GbpLed
87
+ oFcCY2OFAoGBAPSj4u2sSI1uMWNMivTbEYC5pYeltJgRYByazFHj5mHtPF7aDs2a
88
+ IWBfaaHbRIk7d9mIGYOrEiFFpDi5zWYo1gEbwqqOVw4cxL6coVA5mUMdZFDSGcpL
89
+ jwCIp9Yz92mOvygJEQosBIaMaBNTVY2GJPEo8Uhp0OstMDxIR9YrxdwjAoGBAM1y
90
+ atxCx8Z7Mua33x6gPxyyQVnqU/Hvpa9uTF0laAcwarsNlXczyfw/5TNJsEyTzbE1
91
+ 6yJdm5GDS7L6whprx20SCHgpfBcAK6UGMMYsRvRz8kThLI7jgsXQjNXO8MNvaYwO
92
+ z9uVp+i1OVN0jURPwu6NsHcduSCP4asZwBIIOoPLAoGBAJdZQM5rlKMy9gkJ1Lnx
93
+ Qd3SfK7z/EtMDJsFxbwv81rIK14/J4NZhKabwL0q6za/fMEuqyJDVr8Q161kfcdj
94
+ IiqjYwajcwY/FBUcz8vTJpdX+rTplp0Jq9nXsYYatkkuz+JT4Z/aZZ5cBRNXrgkV
95
+ YYt1GfkDxgWI+luBGDhO8mTjAoGBAJ5j3dreaWKuTLnvfEcCSGX5T8TgkcNufRBZ
96
+ Oup9EkyomUCI4ni6JrkhEbWO1CrVsLk01ojyHiP7U3szND5mZaPDSWBKq26YJQ3f
97
+ Te8a58FDOr670zx+Tq3vcQTNvHUQChF+weWK5Z7VSNL8goTt8V01Q50x/y4Jf3QI
98
+ MDo2udYtAoGBAKk596lHuH5/LEjT5hfVrbarSvUjlXswJusZGnwbtEUERY7abUbG
99
+ 5mtI+Xme2q6T0Wjf6N977ecNYAi5VwpUzoziZUPeSCQUlRWSyX2o6+fVBngOFpLZ
100
+ tRv2ngXlc2sYh9RbnlWSdaK0g+U7V8l2wSsQjcPQ1J2vbIGjwBHDSzjc
101
+ -----END RSA PRIVATE KEY-----
102
+ VALID
103
+
104
+ chef-client -N <%= @name %> <%= @chef && @environment ? "-E " + @environment : "" %> --once -j /etc/chef/bootstrap.json 2>&1
105
+ EOF
106
+ erb = Erubis::Eruby.new(template)
107
+ out = erb.evaluate(data)
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,42 @@
1
+ require 'rig'
2
+ require 'clamp'
3
+ require 'fog'
4
+ require 'yaml'
5
+
6
+ require 'rig/cloud'
7
+
8
+ require 'rig/command/abstract'
9
+ require 'rig/command/options'
10
+
11
+ require 'rig/command/main'
12
+
13
+ require 'rig/command/environment/main'
14
+ require 'rig/command/environment/list'
15
+ require 'rig/command/environment/create'
16
+ require 'rig/command/environment/destroy'
17
+
18
+ require 'rig/command/balancer/main'
19
+ require 'rig/command/balancer/list'
20
+ require 'rig/command/balancer/view'
21
+ require 'rig/command/balancer/destroy'
22
+
23
+ require 'rig/command/dns/main'
24
+ require 'rig/command/dns/list'
25
+ require 'rig/command/dns/create'
26
+ require 'rig/command/dns/edit'
27
+ require 'rig/command/dns/destroy'
28
+
29
+ require 'rig/command/instance/main'
30
+ require 'rig/command/instance/ssh'
31
+ require 'rig/command/instance/list'
32
+ require 'rig/command/instance/create'
33
+ require 'rig/command/instance/destroy'
34
+ require 'rig/command/instance/view'
35
+
36
+ require 'rig/command/instance/tag/main'
37
+ require 'rig/command/instance/tag/get'
38
+ require 'rig/command/instance/tag/set'
39
+ require 'rig/command/instance/tag/remove'
40
+
41
+ require 'rig/command/keypair/main'
42
+ require 'rig/command/keypair/list'
@@ -0,0 +1,36 @@
1
+ require 'terminal-table/import'
2
+
3
+ module Rig
4
+ module Command
5
+ class Abstract < Clamp::Command
6
+ option ["-n","--test"], :flag, "use Fog.mock! to test command without creating / destroying cloud resources" do |o|
7
+ Fog.mock!
8
+ end
9
+
10
+ def print_table(columns, rows)
11
+ puts table(columns, *rows)
12
+ end
13
+
14
+ def instance_list(list)
15
+ rows = []
16
+ list.each do |server|
17
+ name = server.tags['Name']
18
+ env = server.tags['Environment']
19
+ role = server.tags['Role']
20
+ ip = server.public_ip_address.to_s
21
+ flavor = server.flavor_id.to_s
22
+ id = server.id.to_s
23
+ state = server.state.to_s.downcase
24
+
25
+ rows << [name, env, role, ip, flavor, id, state]
26
+ end
27
+
28
+ print_table(%w{Name Environment Role PublicIP Size InstanceID State}, rows)
29
+ end
30
+
31
+ def execute
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Balancer
6
+ class Destroy < Abstract
7
+ parameter "NAME", "name of load balancer"
8
+ def execute
9
+ Rig::Cloud::Balancer.destroy(name)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ Rig::Command::Balancer::Main.subcommand 'destroy', 'destroy load balancer', Rig::Command::Balancer::Destroy
@@ -0,0 +1,21 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Balancer
6
+ class List < Abstract
7
+ def execute
8
+ list = Rig::Cloud::Balancer.all
9
+ list.each do |lb|
10
+ puts lb.id
11
+ lb.instances.each do |inst|
12
+ puts "- #{inst}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Rig::Command::Balancer::Main.subcommand 'list', 'list load balancers', Rig::Command::Balancer::List
@@ -0,0 +1,13 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Balancer
6
+ class Main < Abstract
7
+ self.default_subcommand = 'list'
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ Rig::Command::Main.subcommand 'balancer', 'view load balancers', Rig::Command::Balancer::Main
@@ -0,0 +1,19 @@
1
+ require 'awesome_print'
2
+
3
+ module Rig
4
+ module Command
5
+ module Balancer
6
+ class View < Abstract
7
+ include Options::ShowAll
8
+
9
+ parameter "NAME", "name of instance"
10
+
11
+ def execute
12
+ item = Rig::Cloud::Balancer.find(name)
13
+ puts item.attributes.to_yaml
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ Rig::Command::Balancer::Main.subcommand 'view', 'view instances', Rig::Command::Balancer::View
@@ -0,0 +1,19 @@
1
+ require 'rig'
2
+ require 'ipaddress'
3
+
4
+ module Rig
5
+ module Command
6
+ module Dns
7
+ class Create < Abstract
8
+ parameter 'NAME', 'the name to create'
9
+ parameter 'VALUE', 'where the name points to'
10
+
11
+ def execute
12
+ Rig::Cloud::Dns.create(name, value)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ Rig::Command::Dns::Main.subcommand 'create', 'create dns name', Rig::Command::Dns::Create
@@ -0,0 +1,16 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Dns
6
+ class Destroy < Abstract
7
+ parameter "NAME", "name of load balancer"
8
+ def execute
9
+ Rig::Cloud::Dns.destroy(name)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ Rig::Command::Dns::Main.subcommand 'destroy', 'destroy dns record', Rig::Command::Dns::Destroy
@@ -0,0 +1,20 @@
1
+ require 'rig'
2
+ require 'ipaddress'
3
+
4
+ module Rig
5
+ module Command
6
+ module Dns
7
+ class Edit < Abstract
8
+ parameter 'NAME', 'the name to create'
9
+ parameter 'VALUE', 'where the name points to'
10
+
11
+ def execute
12
+ Rig::Cloud::Dns.destroy(name)
13
+ Rig::Cloud::Dns.create(name, value)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ Rig::Command::Dns::Main.subcommand 'edit', 'edit dns name', Rig::Command::Dns::Edit
@@ -0,0 +1,21 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Dns
6
+ class List < Abstract
7
+ def execute
8
+ zone = Rig.config.dns.zone
9
+ records = Rig::Cloud::Dns.records("#{zone}")
10
+ rows = []
11
+ records.each do |record|
12
+ rows << [zone, record.type, record.ttl, record.name, record.value]
13
+ end
14
+ print_table(%w{Domain Type TTL Name Value}, rows)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Rig::Command::Dns::Main.subcommand 'list', 'list dns', Rig::Command::Dns::List
@@ -0,0 +1,13 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Dns
6
+ class Main < Abstract
7
+ self.default_subcommand = 'list'
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ Rig::Command::Main.subcommand 'dns', 'view dns zones', Rig::Command::Dns::Main
@@ -0,0 +1,24 @@
1
+ require 'rig'
2
+ require 'fog'
3
+
4
+ module Rig
5
+ module Command
6
+ module Environment
7
+ class Create < Abstract
8
+ include Options::AwsKey
9
+ include Options::AwsSecret
10
+
11
+ parameter "NAME", "the name of the environment"
12
+ parameter "TEMPLATE", "the template to use (solo, multi, etc)", :default => "solo"
13
+
14
+ def execute
15
+ #connection = Rig::Cloud.compute
16
+ env = Rig::Cloud::Environment.create(name, template.to_sym)
17
+ #ap env.servers
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Rig::Command::Environment::Main.subcommand 'create', 'create environment', Rig::Command::Environment::Create