rig 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.3-p125
data/Gemfile CHANGED
@@ -6,3 +6,5 @@ gemspec
6
6
  gem 'fog'
7
7
  gem 'chef', '~> 0.10.8'
8
8
  gem 'net-ssh', '~> 2.1.4'
9
+ gem 'mongodb'
10
+ gem 'bson_ext'
@@ -0,0 +1,43 @@
1
+ require 'mongo'
2
+ require 'awesome_print'
3
+
4
+ module Rig
5
+ module Command
6
+ module Database
7
+ class List < Abstract
8
+ def execute
9
+ db = Rig::Connection.database
10
+
11
+ list = db.all.sort
12
+ print_table(%w{Database Size}, list)
13
+ end
14
+ end
15
+
16
+ class Create < Abstract
17
+ parameter "NAME", "name of the database"
18
+ parameter "[USER]", "user to add to database", :default => Rig::Connection.database.user
19
+ parameter "[PASS]", "password of user to add to database", :default => Rig::Connection.database.pass
20
+ def execute
21
+ db = Rig::Connection.database
22
+ db.create_database(name, user, pass) unless test?
23
+ end
24
+ end
25
+
26
+ class Destroy < Abstract
27
+ parameter "NAME", "name of the database"
28
+ def execute
29
+ db = Rig::Connection.database
30
+ db.destroy_database(name) unless test?
31
+ end
32
+ end
33
+
34
+ class Main < Abstract
35
+ subcommand "list", "list all databases", List
36
+ subcommand "create", "create a database", Create
37
+ subcommand "destroy", "destroy a database", Destroy
38
+ self.default_subcommand = "list"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ Rig::Command::Main.subcommand "database", "manage mongodb or mysql databases", Rig::Command::Database::Main
data/lib/rig/command.rb CHANGED
@@ -12,6 +12,7 @@ require 'rig/command/main'
12
12
  require 'rig/command/ssh'
13
13
  require 'rig/command/account'
14
14
  require 'rig/command/config'
15
+ require 'rig/command/database'
15
16
 
16
17
  require 'rig/command/environment/main'
17
18
  require 'rig/command/environment/list'
@@ -20,6 +20,12 @@ module Rig
20
20
  Fog::DNS.new(Rig.account[:dns])
21
21
  end
22
22
  end
23
+
24
+ def database
25
+ @database ||= begin
26
+ Rig::Model::Database.new(Rig.account[:database])
27
+ end
28
+ end
23
29
  end
24
30
  end
25
31
  end
@@ -0,0 +1,11 @@
1
+
2
+
3
+ module Rig
4
+ module Model
5
+ module Database
6
+ class Base
7
+
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,70 @@
1
+ require 'rubygems'
2
+ require 'mongo'
3
+ require 'bson'
4
+
5
+ module Rig
6
+ module Model
7
+ module Database
8
+ class MongoDB
9
+
10
+ def initialize(opts)
11
+ @options = {
12
+ :host => "localhost",
13
+ :port => 27017,
14
+ :user => nil,
15
+ :pass => nil,
16
+ :admin_user => nil,
17
+ :admin_pass => nil
18
+ }.merge(opts)
19
+ connect
20
+ end
21
+
22
+ # accessors
23
+ def user; @options[:user]; end
24
+ def host; @options[:host]; end
25
+ def port; @options[:port]; end
26
+ def pass; @options[:pass]; end
27
+ def password; @options[:pass]; end
28
+ def admin_user; @options[:admin_user]; end
29
+ def admin_pass; @options[:admin_pass]; end
30
+ def admin_password; @options[:admin_pass]; end
31
+
32
+ def connect
33
+ @cxn ||= begin
34
+ Mongo::Connection.new(@options[:host], @options[:port])
35
+ end
36
+ end
37
+
38
+ def auth_admin
39
+ db = @cxn.db("admin")
40
+ db.authenticate(@options[:admin_user], @options[:admin_pass])
41
+ end
42
+
43
+ def all
44
+ begin
45
+ connect.database_info
46
+ rescue Mongo::OperationFailure => e
47
+ raise unless e.message =~ /need to login/
48
+ raise unless auth_admin
49
+ connect.database_info
50
+ rescue => e
51
+ puts "exception: <#{e.class}> #{e.message} at #{e.backtrace.first}"
52
+ end
53
+ end
54
+
55
+ def create_database(name, user, pass)
56
+ db = connect.db("admin")
57
+ raise "failed to authenticate" unless db.authenticate(@options[:admin_user], @options[:admin_pass])
58
+ newdb = connect.db(name)
59
+ newdb.add_user(user, pass)
60
+ end
61
+
62
+ def destroy_database(name)
63
+ db = connect.db("admin")
64
+ raise "failed to authenticate" unless db.authenticate(@options[:admin_user], @options[:admin_pass])
65
+ connect.drop_database(name)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,12 @@
1
+
2
+
3
+
4
+ module Rig
5
+ module Model
6
+ module Database
7
+ class MySQL < Base
8
+
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ require 'rig/model/database/base'
2
+ require 'rig/model/database/mongodb'
3
+ require 'rig/model/database/mysql'
4
+
5
+ module Rig
6
+ module Model
7
+ module Database
8
+ class << self
9
+ def new(opts)
10
+ provider = opts.delete(:provider)
11
+ case provider.downcase
12
+ when 'mongodb'
13
+ Rig::Model::Database::MongoDB.new(opts)
14
+ when 'mysql'
15
+ Rig::Model::Database::MySQL.new(opts)
16
+ else
17
+ raise "unknown database #{provider.inspect}"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
data/lib/rig/version.rb CHANGED
@@ -3,7 +3,7 @@ unless defined?(Rig::Version)
3
3
  module Version
4
4
  MAJOR = 0
5
5
  MINOR = 4
6
- TINY = 3
6
+ TINY = 4
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-14 00:00:00.000000000 Z
12
+ date: 2012-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: clamp
@@ -116,6 +116,7 @@ extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
118
  - .gitignore
119
+ - .rvmrc
119
120
  - Gemfile
120
121
  - LICENSE
121
122
  - README.md
@@ -137,6 +138,7 @@ files:
137
138
  - lib/rig/command/balancer/main.rb
138
139
  - lib/rig/command/balancer/view.rb
139
140
  - lib/rig/command/config.rb
141
+ - lib/rig/command/database.rb
140
142
  - lib/rig/command/dns/create.rb
141
143
  - lib/rig/command/dns/destroy.rb
142
144
  - lib/rig/command/dns/edit.rb
@@ -166,6 +168,10 @@ files:
166
168
  - lib/rig/model/account.rb
167
169
  - lib/rig/model/balancer.rb
168
170
  - lib/rig/model/connection.rb
171
+ - lib/rig/model/database.rb
172
+ - lib/rig/model/database/base.rb
173
+ - lib/rig/model/database/mongodb.rb
174
+ - lib/rig/model/database/mysql.rb
169
175
  - lib/rig/model/dns.rb
170
176
  - lib/rig/model/environment.rb
171
177
  - lib/rig/model/instance.rb