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,18 @@
1
+ require 'rig'
2
+ require 'fog'
3
+
4
+ module Rig
5
+ module Command
6
+ module Environment
7
+ class Destroy < Abstract
8
+ parameter "NAME", "name of the environment to destroy"
9
+
10
+ def execute
11
+ Rig::Cloud::Environment.destroy(name)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Rig::Command::Environment::Main.subcommand 'destroy', 'destroy environment', Rig::Command::Environment::Destroy
@@ -0,0 +1,30 @@
1
+ require 'rig'
2
+ require 'fog'
3
+
4
+ module Rig
5
+ module Command
6
+ module Environment
7
+ class List < Abstract
8
+
9
+ option %w{-s --simple}, :flag, "just show list of names"
10
+
11
+ def execute
12
+ envs = Rig::Cloud::Environment.list
13
+
14
+ if simple?
15
+ print_table(%w{Name}, envs)
16
+ else
17
+ rows = []
18
+ envs.each do |e|
19
+ env = Rig::Cloud::Environment.find(e)
20
+ rows << [env.name, env.template, env.region, env.servers.count, env.balancers.count]
21
+ end
22
+ print_table(%w{Name Template Region Servers# Balancers#}, rows)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Rig::Command::Environment::Main.subcommand 'list', 'List environments', Rig::Command::Environment::List
@@ -0,0 +1,15 @@
1
+ require 'clamp'
2
+ require 'rig'
3
+
4
+ module Rig
5
+ module Command
6
+ module Environment
7
+ class Main < Abstract
8
+ self.default_subcommand = "list"
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ Rig::Command::Main.subcommand 'environment', 'Commands related to environments', Rig::Command::Environment::Main
15
+
@@ -0,0 +1,27 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Instance
6
+ class Create < Abstract
7
+ include Options::Config
8
+
9
+ include Options::AwsKey
10
+ include Options::AwsSecret
11
+ include Options::AwsRegion
12
+ include Options::AwsAmi
13
+ include Options::AwsKeypair
14
+
15
+ parameter "CLUSTER", "the cluster this instance should belong to"
16
+ parameter "ENVIRONMENT", "the environment this instance should belong to"
17
+ parameter "ROLE", "the instance role", :default => 'solo'
18
+
19
+ def execute
20
+ puts "create: ami:#{ami} region:#{region} keypair:#{keypair} cluster:#{cluster} env:#{environment} role:#{role}"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ Rig::Command::Instance::Main.subcommand 'create', 'Create an instance', Rig::Command::Instance::Create
@@ -0,0 +1,20 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Instance
6
+ class Destroy < Abstract
7
+ include Options::Config
8
+
9
+ parameter "NAME", "the name of the instance to connect to"
10
+
11
+ def execute
12
+ list = Rig::Cloud::Instance.find(name)
13
+ Rig::Cloud::Instance.destroy(list)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ Rig::Command::Instance::Main.subcommand 'destroy', 'destroy (terminate) instance', Rig::Command::Instance::Destroy
@@ -0,0 +1,25 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Instance
6
+ class List < Abstract
7
+ include Options::ShowAll
8
+
9
+ parameter "[ENV]", "environment"
10
+
11
+ def execute
12
+ connection = Rig::Cloud.compute
13
+ filters = {}
14
+ filters["instance-state-name"] = "running" unless showall?
15
+ filters["tag:Environment"] = env if env
16
+
17
+ list = connection.servers.all(filters)
18
+ instance_list(list)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ Rig::Command::Instance::Main.subcommand 'list', 'List instances', Rig::Command::Instance::List
@@ -0,0 +1,16 @@
1
+ module Rig
2
+ module Command
3
+ module Instance
4
+ class Main < Abstract
5
+ #subcommand 'list', 'List instances', InstanceList
6
+ #subcommand 'create', 'Create an instance', InstanceCreate
7
+ #subcommand 'ssh', 'Connect to instance', InstanceSsh
8
+ #subcommand 'tag', 'Manage tags on instance', InstanceTag
9
+ self.default_subcommand = "list"
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ Rig::Command::Main.subcommand 'instance', 'Commands related to instances', Rig::Command::Instance::Main
16
+
@@ -0,0 +1,32 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Instance
6
+ class Ssh < Abstract
7
+ include Options::Config
8
+
9
+ parameter "NAME", "the name of the instance to connect to"
10
+
11
+ def execute
12
+ connection = Rig::Cloud.compute
13
+
14
+ filters = {}
15
+ filters["tag:Name"] = name if name
16
+
17
+ list = connection.servers.all(filters)
18
+ if list.count > 1
19
+ puts "there is more than one instance that matches:"
20
+ instance_list(list)
21
+ return
22
+ end
23
+
24
+ server = list.first
25
+ puts "ssh root@#{server.public_ip_address}"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ Rig::Command::Instance::Main.subcommand 'ssh', 'Connect to instance', Rig::Command::Instance::Ssh
@@ -0,0 +1,41 @@
1
+ module Rig
2
+ module Command
3
+ module Instance
4
+ module Tag
5
+ class Get < Abstract
6
+ include Options::Config
7
+ include Options::Instance
8
+ include Options::InstanceName
9
+
10
+ def execute
11
+ unless iname || iid
12
+ raise "Must set either instance name or instance id (-iname or -iid)"
13
+ end
14
+
15
+ connection = Rig::Cloud.compute
16
+ if iid
17
+ instance = connection.servers.get(iid)
18
+ else
19
+ list = connection.servers.all({"tag:Name" => iname})
20
+ if list.count > 1
21
+ puts "there is more than one instance that matches:"
22
+ instance_list(list)
23
+ return
24
+ end
25
+ instance = list.first
26
+ end
27
+
28
+ unless instance
29
+ puts "could not find instance"
30
+ return
31
+ end
32
+
33
+ puts "#{instance.tags.inspect}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ Rig::Command::Instance::Tag::Main.subcommand 'get', 'get tag(s) for this instance', Rig::Command::Instance::Tag::Get
@@ -0,0 +1,14 @@
1
+ require 'rig'
2
+
3
+ module Rig
4
+ module Command
5
+ module Instance
6
+ module Tag
7
+ class Main < Abstract
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ Rig::Command::Instance::Main.subcommand 'tag', 'Manage tags on instance', Rig::Command::Instance::Tag::Main
@@ -0,0 +1,44 @@
1
+
2
+ module Rig
3
+ module Command
4
+ module Instance
5
+ module Tag
6
+ class Remove < Abstract
7
+ include Options::Config
8
+ include Options::Instance
9
+ include Options::InstanceName
10
+
11
+ parameter "TAG", "tag key to remove", :attribute_name => :raw_tag
12
+
13
+ def execute
14
+ unless iname || iid
15
+ raise "Must set either instance name or instance id (-iname or -iid)"
16
+ end
17
+
18
+ connection = Rig::Cloud.compute
19
+ if iid
20
+ instance = connection.servers.get(iid)
21
+ else
22
+ list = connection.servers.all({"tag:Name" => iname})
23
+ if list.count > 1
24
+ puts "there is more than one instance that matches:"
25
+ instance_list(list)
26
+ return
27
+ end
28
+ instance = list.first
29
+ end
30
+
31
+ unless instance
32
+ puts "could not find instance"
33
+ return
34
+ end
35
+
36
+ ap connection.delete_tags(instance.id, raw_tag => instance.tags[raw_tag])
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ Rig::Command::Instance::Tag::Main.subcommand 'rm', 'remove tag(s) for this instance', Rig::Command::Instance::Tag::Remove
@@ -0,0 +1,47 @@
1
+ module Rig
2
+ module Command
3
+ module Instance
4
+ module Tag
5
+ class Set < Abstract
6
+ include Options::Config
7
+ include Options::Instance
8
+ include Options::InstanceName
9
+
10
+ parameter "TAGS ...", "tags of the form key:value", :attribute_name => :raw_tags
11
+
12
+ def execute
13
+ unless iname || iid
14
+ raise "Must set either instance name or instance id (-iname or -iid)"
15
+ end
16
+
17
+ connection = Rig::Cloud.compute
18
+ if iid
19
+ instance = connection.servers.get(iid)
20
+ else
21
+ list = connection.servers.all({"tag:Name" => iname})
22
+ if list.count > 1
23
+ puts "there is more than one instance that matches:"
24
+ instance_list(list)
25
+ return
26
+ end
27
+ instance = list.first
28
+ end
29
+
30
+ unless instance
31
+ puts "could not find instance"
32
+ return
33
+ end
34
+
35
+ raw_tags.each do |s|
36
+ (k,v) = s.split(':',2)
37
+ #tags[k] = v if k && v
38
+ connection.create_tags(instance.id, k => v)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ Rig::Command::Instance::Tag::Main.subcommand 'set', 'set tag(s) for this instance', Rig::Command::Instance::Tag::Set
@@ -0,0 +1,20 @@
1
+ module Rig
2
+ module Command
3
+ module Instance
4
+ class View < Abstract
5
+ include Options::ShowAll
6
+
7
+ parameter "NAME", "name of instance"
8
+
9
+ def execute
10
+ list = Rig::Cloud::Instance.find(name)
11
+ list.each do |item|
12
+ puts item.attributes.to_yaml
13
+ puts
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ Rig::Command::Instance::Main.subcommand 'view', 'view instances', Rig::Command::Instance::View
@@ -0,0 +1,14 @@
1
+ module Rig
2
+ module Command
3
+ module Keypair
4
+ class List < Abstract
5
+ def execute
6
+ connection = Rig::Cloud.compute
7
+ list = connection.key_pairs.all
8
+ puts list.inspect
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ Rig::Command::Keypair::Main.subcommand 'list', 'list keypairs', Rig::Command::Keypair::List
@@ -0,0 +1,11 @@
1
+
2
+ module Rig
3
+ module Command
4
+ module Keypair
5
+ class Main < Abstract
6
+ self.default_subcommand = "list"
7
+ end
8
+ end
9
+ end
10
+ end
11
+ Rig::Command::Main.subcommand 'keypair', 'Commands related to keypairs', Rig::Command::Keypair::Main
@@ -0,0 +1,48 @@
1
+
2
+ module Rig
3
+ module Command
4
+ class Main < Abstract
5
+ option %w{--config}, :flag, "print configuration and exit" do |c|
6
+ puts "Rig Version: #{Rig::Version::STRING}"
7
+ puts Rig.config.to_yaml
8
+ exit(0)
9
+ end
10
+
11
+ option %w{--init}, :flag, "initialize the ~/.rig configuration file" do |o|
12
+ dir = ENV['RIG_CONFIG'] ? File.dirname(ENV['RIG_CONFIG']) : ENV['HOME']
13
+
14
+ unless dir
15
+ puts "directory could not be found: tried environment variables RIG_CONFIG (#{ENV['RIG_CONFIG']}) and HOME (#{ENV['HOME']}"
16
+ exit(1)
17
+ end
18
+
19
+ file = "#{dir}/.rig"
20
+ puts "[initializing] #{file}"
21
+ data = <<-EOF
22
+ rig:
23
+ provider: AWS
24
+ key: AKIAIWUGNGSUZWW5XVCQ
25
+ secret: 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
26
+ ami: ami-8baa73e2
27
+ region: us-east-1
28
+ keypair: inqcloud-dev
29
+
30
+ dns:
31
+ zone: inqlabs.com
32
+
33
+ # if you're using chef, set this to the knife.rb configuration file
34
+ # this will allow Rig to manage the chef nodes, environments and such.
35
+ #chef:
36
+ # knife: ~/work/chef/.chef/knife.rb
37
+ # TODO: Figure out the same thing for puppet
38
+
39
+ # eventually we should be able to put default options and such in the config file
40
+
41
+ EOF
42
+ #puts data
43
+ File.open(file, 'w') { |f| f.write(data) }
44
+ exit(0)
45
+ end
46
+ end
47
+ end
48
+ end