gomon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Gomon
2
+
3
+ Ruby wrappers around mongodb cli tools.
4
+
5
+ ## Prerequisites
6
+
7
+ This gem depends on no other gems. Ruby 1.9+
8
+ It assumes the presence of `mongorestore` and `mongodump` commands.
9
+ It was built around the latest tools at the moment of this writing, meaning version 2.2.2. So if is possible that arguments format and/or names have changed from previous versions, making them potentially not compatible with Gomon.
10
+
11
+ ## Installation
12
+
13
+ Directly : `gem install gomon`.
14
+ Gemfile : `gem 'gomon'`.
15
+
16
+ ## Usage
17
+
18
+ The tools currently supported are `mongodump` and `mongorestore`.
19
+ They are wrapped in `Gomon::Dump` and `Gomon::Restore` respectively.
20
+
21
+ These two classes are used in the same way: first, instantiate the object, then, invoke `call` without arguments on it. In most cases, the only difference of usage will be the options passed to the constructor.
22
+
23
+ ### Example
24
+
25
+ > gd = Gomon::Dump.new uri: "mongodb://user:pass@host:12345/database", other: '--journal'
26
+ > gd.cmd_string
27
+ => "mongodump --host 'host' --port '12345' --username 'user' --password 'pass' --db 'database' --journal"
28
+ > gd.call
29
+ # Dumping `database` from host...
30
+ # Executing: mongodump --host 'host' --port '12345' --username 'user' --password 'pass' --db 'database' --journal
31
+ # ....
32
+ # Done dumping.
33
+ => nil
34
+
35
+ ### Options
36
+
37
+ The two classes accepts a hash of options when initializing, as in `gd = Gomon::Dumper.new(db: 'gem_test')`. This hash can contain:
38
+
39
+ * Any option accepted by `mongodump` or `mongorestore`. You should refer to the [MongoDB Manual](http://docs.mongodb.org/manual/reference/mongorestore/) for more on them;
40
+ * Singular options that have no value, as '--drop', can be supplied with the key `:other`;
41
+ * If the key `:uri` is present, its value will be parsed and split to retrieve the username, password, host, port, and database;
42
+ * Specific to `Gomon::Restore`: the option value for the key `:path` will be used as its last argument, as is.
43
+
44
+ If you want to set options once the object is already created, you can use `add_option(key, value)` or `add_options({key: value, ...})`. However, there will be no special treatment for options: even if you supply `:uri`, or `:other`, they won't be handled the way they are when creating the object.
45
+
46
+
47
+ ## What's next
48
+
49
+ * There is no test at all yet. This shall change.
50
+ * Better support for manipulating options once the object is created.
51
+
52
+ ## Contributions
53
+
54
+ 1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug. In the first case, please include a use-case.
55
+ 2. Fork the repository on Github, make a feature branch, work on this branch.
56
+ 3. If necessary, write a test which shows that the bug was fixed or that the feature works as expected.
57
+ 4. Send a pull request and annoy us until we notice it. :)
58
+
59
+ ## Changelog
60
+
61
+ * `0.1`: First version. Provices basic functionnality for `Gomon::Dump` & `Gomon::Restore`
data/gomon.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ $:.unshift File.expand_path('../lib', __FILE__)
2
+ require 'gomon/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "gomon"
6
+ s.version = Gomon::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Novelys", "Kevin Soltysiak"]
9
+ s.email = ["contact@novelys.com"]
10
+ s.homepage = "http://github.com/novelys/gomon"
11
+ s.summary = "Ruby wrappers around mongo utilities."
12
+ s.description = "A set of ruby classing wrapping mongodb cli tools, making them pleasant to integrate to ruby scripts, rake tasks, etc"
13
+ s.rubyforge_project = s.name
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.files = `git ls-files`.split("\n")
16
+ s.require_path = 'lib'
17
+ end
data/lib/gomon.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'gomon/base'
2
+ require 'gomon/dump'
3
+ require 'gomon/restore'
4
+ require 'gomon/version'
data/lib/gomon/base.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'gomon/options'
2
+ require 'gomon/uri'
3
+
4
+ module Gomon
5
+ class Base
6
+ include Options
7
+ include Uri
8
+
9
+ def initialize(opts = {})
10
+ @tool = 'mongodump'
11
+
12
+ # Default options
13
+ @options = {host: 'localhost', port: '27017'}
14
+ @singular_options = opts.delete(:other)
15
+
16
+ # Extract uri into options
17
+ if (uri = opts.delete(:uri))
18
+ add_options parse_uri(uri)
19
+ end
20
+
21
+ # Keep track of other options
22
+ add_options opts
23
+ end
24
+
25
+ # String to execute
26
+ def cmd_string
27
+ "#{tool} #{cli_options}"
28
+ end
29
+
30
+ # Execute before/after hookes if any, execute command
31
+ def call
32
+ before_cmd
33
+ system(cmd_string)
34
+ after_cmd
35
+ end
36
+ end
37
+ end
data/lib/gomon/dump.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'gomon/base'
2
+
3
+ module Gomon
4
+ class Dump < Base
5
+ attr_reader :tool
6
+
7
+ def initialize(opts = {})
8
+ super
9
+
10
+ @tool = 'mongodump'
11
+ end
12
+
13
+ def before_cmd
14
+ puts "Dumping `#{@options[:db]}` from #{@options[:host]}..."
15
+ puts "Executing: #{cmd_string}"
16
+ end
17
+
18
+ def after_cmd
19
+ puts "Done dumping."
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ module Gomon
2
+ module Options
3
+ # Add a single options
4
+ def add_option(key, value)
5
+ add_options({key => value})
6
+ end
7
+
8
+ # Add many options
9
+ def add_options(opts = {})
10
+ @options.merge! opts
11
+ end
12
+
13
+ # Returns the options string
14
+ def cli_options
15
+ options_as_array.join(' ')
16
+ end
17
+
18
+ # Returns options as an array
19
+ def options_as_array
20
+ ary = @options.each_with_object([]) do |(key, value), memo|
21
+ memo << "--#{key} '#{value}'"
22
+ end
23
+
24
+ ary << @singular_options if @singular_options
25
+
26
+ ary
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require 'gomon/base'
2
+
3
+ module Gomon
4
+ class Restore < Base
5
+ attr_reader :tool
6
+
7
+ def initialize(opts = {})
8
+ @path = opts.delete(:path) || 'dump/*'
9
+ super(opts)
10
+ @tool = 'mongorestore'
11
+ end
12
+
13
+ # String to execute
14
+ def cmd_string
15
+ "#{super} '#{@path}'"
16
+ end
17
+
18
+ # Hook to execute before restoring
19
+ def before_cmd
20
+ puts "Restoring from dir `#{@path}` into `#{@options[:db]}` at #{@options[:host]}..."
21
+ puts "Executing: #{cmd_string}"
22
+ end
23
+
24
+ # Hook to execute after restoring
25
+ def after_cmd
26
+ puts "Done restoring."
27
+ end
28
+ end
29
+ end
data/lib/gomon/uri.rb ADDED
@@ -0,0 +1,16 @@
1
+ module Gomon
2
+ module Uri
3
+ # Parse mongodb uri into options
4
+ def parse_uri(uri)
5
+ matches = uri.match /mongodb:\/\/([^:]*):([^@]*)@([^:]*):(\d*)\/(.*)/i
6
+
7
+ {
8
+ username: matches[1],
9
+ password: matches[2],
10
+ host: matches[3],
11
+ port: matches[4],
12
+ db: matches[5]
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Gomon
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gomon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Novelys
9
+ - Kevin Soltysiak
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-01-13 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: A set of ruby classing wrapping mongodb cli tools, making them pleasant
16
+ to integrate to ruby scripts, rake tasks, etc
17
+ email:
18
+ - contact@novelys.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - README.md
24
+ - gomon.gemspec
25
+ - lib/gomon.rb
26
+ - lib/gomon/base.rb
27
+ - lib/gomon/dump.rb
28
+ - lib/gomon/options.rb
29
+ - lib/gomon/restore.rb
30
+ - lib/gomon/uri.rb
31
+ - lib/gomon/version.rb
32
+ homepage: http://github.com/novelys/gomon
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: 1.3.6
50
+ requirements: []
51
+ rubyforge_project: gomon
52
+ rubygems_version: 1.8.23
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Ruby wrappers around mongo utilities.
56
+ test_files: []