noveku 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,7 +23,7 @@ This gem exposes two executables :
23
23
  * `hrs` (heroku staging) : will execute the given commands for the `staging` remote
24
24
  * `hrp` (heroku production) : will execute the given commands for the `production` remote
25
25
 
26
- ## Commands
26
+ ## Heroku commands
27
27
 
28
28
  Supported commands and their equivalent :
29
29
 
@@ -32,12 +32,18 @@ Supported commands and their equivalent :
32
32
  * `migrate`: `heroku run rake db:migrate --remote ENV && heroku restart --remote ENV`
33
33
  * `tail`: `heroku logs --tail --remote ENV`
34
34
 
35
- When giving a command that is not specifically supported, it will be passed to `heroku` : `heroku ARGS --remote ENV".
35
+ When giving a command that is not specifically supported, it will be passed to `heroku` : `heroku ARGS --remote ENV`.
36
36
  This makes several other commands available, such as `restart`, `releases`, ...
37
37
 
38
- ## What's next
38
+ ## Advanced commands
39
39
 
40
- Commands that will be soon supported : commands for dumping mongolab database.
40
+ * `mongodump`: Dumps the mongo database. Shortcut for both `mongolab_dump` and `mongohq_dump`: tries mongolab first, then mongohq.
41
+ * `mongolab_dump`: Dumps the mongo database. Look in the config keys of `ENV` to find `MONGOLAB_URI`.
42
+ * `mongohq_dump`: Dumps the mongo database. Look in the config keys of `ENV` to find `MONGOHQ_URL`.
43
+
44
+ Those commands puts the dump in the `dump` dir, relatively to where you executed it.
45
+
46
+ Since the restoration of the database does not involve any interaction with heroku, it is out of the scope of this gem at the moment. However, it is pretty easy to integrate the commands above and the restoration in a rake task.
41
47
 
42
48
  ## Contributions
43
49
 
@@ -48,5 +54,6 @@ Commands that will be soon supported : commands for dumping mongolab database.
48
54
 
49
55
  ## Changelog
50
56
 
51
- * `0.2`: Added rake command
52
- * `0.1`: First version. Available commands : `console`, `migrate`, `tail`
57
+ * `0.3`: Added `mongodump`, `mongolab_dump`, `mongohq_dump`.
58
+ * `0.2`: Added `rake` command.
59
+ * `0.1`: First version. Available commands : `console`, `migrate`, `tail`.
data/lib/noveku/base.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  module Noveku
2
+ # Heroku config keys name
3
+ MONGOLAB_KEY = 'MONGOLAB_URI'
4
+ MONGOHQ_KEY = 'MONGOHQ_URL'
5
+
2
6
  # This exception will be raised if @environment is not present
3
- class NotAValidEnvironment < StandardError
4
- end
7
+ class NotAValidEnvironment < StandardError; end
5
8
 
6
9
  # Common functionnality
7
10
  class Base
@@ -42,6 +45,23 @@ module Noveku
42
45
  system "heroku logs --tail --remote #{@environment}"
43
46
  end
44
47
 
48
+ # Dump mongolab or mongohq db
49
+ def mongodump_cmd
50
+ uri = config_value_for(MONGOLAB_KEY) || config_value_for(MONGOHQ_KEY)
51
+
52
+ mongo_dump(uri)
53
+ end
54
+
55
+ # Dump mongolab db
56
+ def mongolab_dump_cmd
57
+ mongo_dump config_value_for(MONGOLAB_KEY)
58
+ end
59
+
60
+ # Dump mongohq db
61
+ def mongohq_dump_cmd
62
+ mongo_dump config_value_for(MONGOHQ_KEY)
63
+ end
64
+
45
65
  def method_missing(name, *args, &block)
46
66
  # If this is a command with no specific support, pass the raw arguments to `heroku` directly
47
67
  if name.to_s.end_with?('_cmd')
@@ -50,5 +70,39 @@ module Noveku
50
70
  super
51
71
  end
52
72
  end
73
+
74
+ private
75
+
76
+ # Returns config value for key
77
+ def config_value_for(key)
78
+ uri = `heroku config:get #{key} --remote #{@environment}`.strip
79
+ uri = nil if uri == ''
80
+ uri
81
+ end
82
+
83
+ # Returns a hash of data parsed from the mongo uri
84
+ def mongo_uri_parse(uri)
85
+ matches = uri.match /mongodb:\/\/([^:]*):([^@]*)@([^:]*):(\d*)\/(.*)/i
86
+
87
+ {
88
+ user: matches[1],
89
+ password: matches[2],
90
+ host: matches[3],
91
+ port: matches[4],
92
+ database: matches[5]
93
+ }
94
+ end
95
+
96
+ # Get a dump of the database
97
+ def mongo_dump(uri)
98
+ if uri
99
+ opts = mongo_uri_parse(uri)
100
+ puts "Dumping `#{opts[:database]}` from #{opts[:host]}..."
101
+ system "mongodump -h #{opts[:host]}:#{opts[:port]} -d #{opts[:database]} -u #{opts[:user]} -p #{opts[:password]}"
102
+ puts 'Done.'
103
+ else
104
+ puts 'No uri.'
105
+ end
106
+ end
53
107
  end
54
108
  end
@@ -1,3 +1,3 @@
1
1
  module Noveku
2
- VERSION = '0.2'
2
+ VERSION = '0.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noveku
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: