omniscient 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in dbme.gemspec
3
+ # Specify your gem's dependencies omniscient.gemspec
4
4
  gemspec
@@ -8,14 +8,18 @@ don't want to dump and clone data manually all the time.
8
8
  Getting Started
9
9
  ===============
10
10
 
11
- In your console, type the following, where aliasname is a name you choose
11
+ In your console:
12
+
13
+ $ gem install omniscient
14
+
15
+ Then, type the following, where _aliasname_ is a name you choose
12
16
  (i.e. home_computer, work etc):
13
17
 
14
18
  $ omniscient clone aliasname
15
19
 
16
- A setup will begin. The data will be save to ~/.omni_config.yml
20
+ A setup will begin. The configuration will be saved to ~/.omni_config.yml.
17
21
 
18
- Now that Omniscient knows where to connect, run the following command again
22
+ Now that Omniscient knows where to connect, run the following command again:
19
23
 
20
24
  $ omniscient clone aliasname
21
25
 
@@ -11,10 +11,11 @@ command = Shell::Parser::get_command ARGV
11
11
  unless command.empty? then
12
12
  command_file = File.expand_path('../../lib/omniscient/command/'+command+'.rb', __FILE__)
13
13
 
14
- # checks if class file exists
15
14
  if File.exists? command_file then
16
15
  require command_file
17
16
  command = command.capitalize
18
17
  command_obj = Omniscient::Command.const_get(command).new(ARGV)
19
18
  end
19
+ else
20
+ command_obj = Omniscient::Command::Run.new(ARGV)
20
21
  end
@@ -19,16 +19,22 @@ module Omniscient
19
19
  nil
20
20
  end
21
21
  end
22
-
22
+
23
+ def valid_configuration?
24
+ return false unless database && user
25
+ true
26
+ end
27
+
23
28
  def access_statements
24
29
  str = ''
25
30
  str << ' -u '+self.user unless self.user.nil?
26
- str << ' '+self.database unless self.database.nil?
27
31
  str << ' -p'+self.password unless self.password.empty?
32
+ str << ' '+self.database unless self.database.nil?
28
33
  str
29
34
  end
30
35
 
31
36
  def dump
37
+ return false unless valid_configuration?
32
38
  str = 'mysqldump'
33
39
  str << access_statements
34
40
  str << ' '+self.table unless self.database.nil? || self.table.nil?
@@ -38,6 +44,7 @@ module Omniscient
38
44
  end
39
45
 
40
46
  def import
47
+ return false unless valid_configuration?
41
48
  str = 'mysql'
42
49
  str << access_statements
43
50
  str << ' < '+Omniscient::DUMP_LOCAL_PATH
@@ -1,28 +1,63 @@
1
+ require "version"
2
+ require "configuration"
3
+
1
4
  module Omniscient
2
5
 
3
6
  module Command
4
-
7
+
5
8
  class Run
9
+
10
+ attr_accessor :argv, :configurations
11
+
6
12
  def initialize argv = ''
7
13
  @argv = argv
8
14
 
9
- @configurations = Omniscient::Configuration.new
15
+ @configurations = Omniscient::Configuration.load
10
16
 
11
- if Shell::Parser.is_option "help", argv
17
+ unless (["h", "help"] & Shell::Parser.get_options(argv)).empty?
12
18
  help if self.respond_to?('help')
13
19
  exit
14
20
  end
21
+ unless (["v", "version"] & Shell::Parser.get_options(argv)).empty?
22
+ version if self.respond_to?('version')
23
+ exit
24
+ end
15
25
 
16
- if( self.respond_to?('run') )
17
- run
18
- elsif( self.respond_to?('help') )
26
+ if self.respond_to?('run')
27
+ run unless defined? TESTING
28
+ elsif self.respond_to?('help')
19
29
  help
20
30
  end
21
-
22
31
  end
23
32
 
24
33
  def request_configuration
25
- @configurations.configuration @configurations.questions(:alias_name => @alias_name)
34
+ @configurations = Omniscient::Configuration::questions(:alias_name => @alias_name)
35
+ end
36
+
37
+ def help
38
+ print "Usage:\n"
39
+ print "\s\somniscient COMMAND [options]"
40
+ print "\n\n"
41
+ print "Commands available:"
42
+ print "\n"
43
+ print "\s\sclone\t\tClones a database from a remote server"
44
+ print "\n\n"
45
+ print "Examples:"
46
+ print "\n"
47
+ print "\s\somniscient clone alias_server_name"
48
+ print "\n"
49
+ print "\tClones a database from a remote server. If \n"
50
+ print "\talias_server_name is unknown to Ominiscient, \n"
51
+ print "\tit'll ask you all connection information."
52
+ print "\n\n"
53
+ print "For more information, try:\n"
54
+ print "\s\somniscient COMMAND -h"
55
+ print "\n"
56
+ end
57
+
58
+ def version
59
+ print "Omniscient v" + Omniscient::VERSION
60
+ print "\n"
26
61
  end
27
62
 
28
63
  end
@@ -1,29 +1,23 @@
1
1
  require 'yaml'
2
+ require 'ssh'
3
+ require 'scp'
4
+ require 'adapters/mysql'
2
5
 
3
6
  module Omniscient
4
-
5
7
  module Command
6
-
7
8
  class Clone < Omniscient::Command::Run
8
-
9
+
10
+ attr_accessor :ssh, :scp, :mysql, :alias_name
11
+
9
12
  def run
10
-
11
13
  @alias_name = Shell::Parser.get_arguments(@argv).first || ""
12
14
 
13
- has_conf = load_configuration_by_alias(@alias_name)
14
- unless has_conf
15
- request_configuration
16
- end
15
+ (help; exit) if @alias_name.empty?
17
16
 
18
- database = Shell::Parser.get_option_value('d', @argv) || nil
19
- @configurations['mysql']['database'] = database unless database.empty?
20
-
21
- require 'ssh'
22
- require 'scp'
23
- require 'adapters/mysql'
24
- @ssh = Omniscient::Ssh.new(@configurations['ssh'])
25
- @scp = Omniscient::Scp.new(@configurations['ssh'])
26
- @mysql = Omniscient::Adapter::MySQL.new(@configurations['mysql'])
17
+ request_configuration unless configurations_exist? @alias_name
18
+ load_configurations
19
+ set_custom_variables
20
+ instantiate_adapters
27
21
 
28
22
  # Dumps remotely
29
23
  command_to_issue = "#{@ssh.connect} '#{@mysql.dump}'"
@@ -39,26 +33,38 @@ module Omniscient
39
33
  command_to_issue = "#{@mysql.import}"
40
34
  puts "Running => #{command_to_issue}"
41
35
  exit unless system command_to_issue
42
-
43
36
  end
44
-
45
- def load_configuration_by_alias alias_name
46
- return false unless alias_name
47
- return false unless File.exist?(File.expand_path('~/.omniscient_conf.yml'))
48
-
49
- config_file = File.new(File.expand_path('~/.omniscient_conf.yml'), 'r')
50
- existing_configurations = YAML::load(config_file.read)
51
- if existing_configurations.kind_of?(Hash) && existing_configurations.has_key?(alias_name)
52
- @configurations = existing_configurations[alias_name]
53
- true
54
- else
55
- false
56
- end
37
+
38
+ def configurations_exist? alias_name
39
+ Omniscient::Configuration::load alias_name
40
+ end
41
+
42
+ def set_custom_variables argv = false
43
+ database = Shell::Parser.get_option_value('d', (argv || @argv)) || nil
44
+ @configurations['mysql']['database'] = database unless database.empty?
57
45
 
46
+ address = Shell::Parser.get_option_value('host', (argv || @argv)) || nil
47
+ @configurations['ssh']['address'] = address unless address.empty?
48
+ end
49
+
50
+ def instantiate_adapters
51
+ @ssh = Omniscient::Ssh.new @configurations['ssh']
52
+ @scp = Omniscient::Scp.new @configurations['ssh']
53
+ @mysql = Omniscient::Adapter::MySQL.new @configurations['mysql']
54
+ end
55
+
56
+ def load_configurations alias_name = false
57
+ @configurations = Omniscient::Configuration.load(alias_name || @alias_name)
58
58
  end
59
59
 
60
60
  def help
61
- puts 'Help is missing.'
61
+ print "Usage:\n"
62
+ print "\s\somniscient clone ALIAS_NAME [options]"
63
+ print "\n\n"
64
+ print "Options:"
65
+ print "\n"
66
+ print "\s\s-d\t\tSelect a different database\n"
67
+ print "\s\s--host\tSelect a different SSH address, e.g. foo@192.168.0.1\n"
62
68
  end
63
69
 
64
70
  end
@@ -2,19 +2,32 @@ require 'yaml'
2
2
 
3
3
  module Omniscient
4
4
  class Configuration
5
- def initialize( conf = {} )
6
- @configuration = conf
5
+
6
+ @PATH = File.expand_path('~/.omniscient_conf.yml')
7
+ @configuration = {}
8
+
9
+ class << self
10
+ attr_accessor :PATH, :configuration
7
11
  end
8
12
 
9
- def method_missing( name, *args, &block )
10
- if args.empty? && block.nil? && @attributes.has_key?(name)
11
- @attributes[name.to_sym]
13
+ def self.load alias_name = false
14
+ return false unless File.exist? @PATH
15
+
16
+ config_file = File.new @PATH, 'r'
17
+ existing_configurations = YAML::load config_file.read
18
+ if existing_configurations.kind_of? Hash
19
+ @configuration = existing_configurations
20
+ if alias_name
21
+ return @configuration[alias_name.to_s] unless @configuration[alias_name.to_s].nil?
22
+ return false
23
+ end
24
+ @configuration
12
25
  else
13
- nil
26
+ false
14
27
  end
15
28
  end
16
29
 
17
- def questions informations
30
+ def self.questions informations
18
31
 
19
32
  custom_alias = informations[:alias_name].nil? ? nil : informations[:alias_name]
20
33
 
@@ -49,10 +62,15 @@ module Omniscient
49
62
  end
50
63
 
51
64
  unless @configuration[custom_alias]['mysql'].has_key?('password')
52
- system "stty -echo"
53
- print "Database's password [leave it blank if none is needed]: "
54
- mysql_password = Shell::Input.text
55
- system "stty echo"
65
+ begin
66
+ system "stty -echo"
67
+ print "Database's password [leave it blank if none is needed]: "
68
+ mysql_password = Shell::Input.text
69
+ system "stty echo"
70
+ rescue NoMethodError, Interrupt
71
+ system "stty echo"
72
+ exit
73
+ end
56
74
  print "\n"
57
75
  @configuration[custom_alias]['mysql']['password'] = mysql_password.empty? ? '' : mysql_password
58
76
  end
@@ -1,3 +1,3 @@
1
1
  module Omniscient
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -76,9 +76,10 @@ module Shell
76
76
 
77
77
  end # get_command
78
78
 
79
- # get only options in ARGV (arguments starting with _ or __)
80
- def self.get_options argv
81
- @options = Array.new
79
+ # get only options in ARGV (arguments starting with - or --)
80
+ def self.get_options argv = []
81
+ return [] if argv.empty?
82
+ @options = []
82
83
 
83
84
  argv.each {
84
85
  |e|
@@ -92,7 +93,7 @@ module Shell
92
93
  @options
93
94
  end # get_options
94
95
 
95
- def self.get_option_value option, argv = Array.new
96
+ def self.get_option_value option, argv = []
96
97
  next_item, option_value = false
97
98
  argv.each { |e|
98
99
  if next_item
@@ -1,6 +1,5 @@
1
1
  require "test/unit"
2
- require "omniscient"
3
- require "adapters/mysql"
2
+ require "omniscient/adapters/mysql"
4
3
 
5
4
  class MySQLTest < Test::Unit::TestCase
6
5
 
@@ -18,20 +17,42 @@ class MySQLTest < Test::Unit::TestCase
18
17
  def test_initialization
19
18
  assert_equal 'omniscient', @mysql.database
20
19
  assert_equal 'root', @mysql.user
20
+ assert_equal 'pw', @mysql.password
21
+ end
22
+
23
+ def test_valid_configuration?
24
+ @mysql = Omniscient::Adapter::MySQL.new( :database => 'omniscient', :user => 'root', :password => 'pw' )
25
+ assert @mysql.valid_configuration?
26
+ @mysql = Omniscient::Adapter::MySQL.new( :database => 'omniscient', :user => 'root' )
27
+ assert @mysql.valid_configuration?
28
+ @mysql = Omniscient::Adapter::MySQL.new( :user => 'root', :password => 'pw' )
29
+ assert !@mysql.valid_configuration?
21
30
  end
22
31
 
23
32
  def test_dump
24
33
  assert_equal %Q{mysqldump -u root -ppw omniscient --single-transaction > .omni_dump.sql}, @mysql.dump()
25
34
  end
26
35
 
36
+ def test_dump_if_not_valid_configuration
37
+ @mysql = Omniscient::Adapter::MySQL.new( :user => 'root', :password => 'pw' )
38
+ assert !@mysql.dump
39
+ end
40
+
27
41
  def test_dump_a_specific_table
28
42
  @mysql.attributes[:table] = 'mytable'
29
43
  assert_equal %Q{mysqldump -u root -ppw omniscient mytable --single-transaction > .omni_dump.sql}, @mysql.dump()
30
44
  end
31
45
 
32
- def test_import
46
+ def test_importation
33
47
  assert_equal %Q{mysql -u root -ppw omniscient < }+Omniscient::DUMP_LOCAL_PATH, @mysql.import()
34
48
  end
35
49
 
50
+ def test_importation_if_not_valid_configuration
51
+ @mysql = Omniscient::Adapter::MySQL.new( :user => 'root', :password => 'pw' )
52
+ assert !@mysql.import()
53
+ @mysql = Omniscient::Adapter::MySQL.new( :database => 'anydb' )
54
+ assert !@mysql.import()
55
+ end
56
+
36
57
 
37
58
  end
@@ -1,13 +1,43 @@
1
1
  require "test/unit"
2
+ require File.expand_path("../../../../set_test_environment.rb", __FILE__)
2
3
  require "omniscient"
3
4
  require "command/clone"
4
5
 
5
6
  class CloneTest < Test::Unit::TestCase
6
7
  def setup
7
-
8
+ test_path = File.expand_path("../../../../resources/configurations/omniscient_conf.yml", __FILE__)
9
+ Omniscient::Configuration.PATH = test_path
10
+ @obj = Omniscient::Command::Clone.new
11
+ @obj.configurations = Omniscient::Configuration.load :foo
12
+ @obj.alias_name = "foo"
8
13
  end
9
14
 
10
- def test_start
15
+ def test_has_conf
16
+ assert @obj.configurations_exist? :foo
17
+ end
18
+
19
+ def test_load_configurations
20
+ assert @obj.load_configurations.kind_of? Hash
21
+ assert @obj.load_configurations.has_key? "ssh"
22
+ assert @obj.configurations.has_key? "ssh"
23
+ end
24
+
25
+ def test_set_custom_database
26
+ assert_equal @obj.configurations['mysql']['database'], "foo"
27
+ @obj.set_custom_variables ["-d", "custom_database"]
28
+ assert_equal @obj.configurations['mysql']['database'], "custom_database"
29
+ end
11
30
 
31
+ def test_set_custom_address
32
+ assert_equal @obj.configurations['ssh']['address'], "foo@192.168.0.100"
33
+ @obj.set_custom_variables ["--host", "custom_address"]
34
+ assert_equal "custom_address", @obj.configurations['ssh']['address']
35
+ end
36
+
37
+ def test_instantiate_adapters
38
+ @obj.instantiate_adapters
39
+ assert @obj.ssh.kind_of? Omniscient::Ssh
40
+ assert @obj.scp.kind_of? Omniscient::Scp
41
+ assert @obj.mysql.kind_of? Omniscient::Adapter::MySQL
12
42
  end
13
43
  end
@@ -0,0 +1,47 @@
1
+ require "test/unit"
2
+ require "omniscient"
3
+ require "configuration"
4
+
5
+ class ConfigurationTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @right_path = File.expand_path('~/.omniscient_conf.yml')
9
+ @test_path = File.expand_path("../../../resources/configurations/omniscient_conf.yml", __FILE__)
10
+ end
11
+
12
+ def test_right_path
13
+ Omniscient::Configuration.PATH = @right_path
14
+ assert_equal @right_path, Omniscient::Configuration.PATH
15
+ end
16
+
17
+ def test_change_path
18
+ Omniscient::Configuration.PATH = @test_path
19
+ assert_equal Omniscient::Configuration.PATH, @test_path
20
+ Omniscient::Configuration.PATH = @right_path
21
+ assert_equal Omniscient::Configuration.PATH, @right_path
22
+ end
23
+
24
+ def test_load_configuration
25
+ original_path = Omniscient::Configuration.PATH
26
+ Omniscient::Configuration.PATH = @test_path
27
+ conf = Omniscient::Configuration::load
28
+ assert conf.kind_of? Hash
29
+ assert conf["foo"]["ssh"]["address"] == "foo@192.168.0.100"
30
+ assert Omniscient::Configuration.configuration.kind_of? Hash
31
+ assert Omniscient::Configuration.configuration["foo"]["ssh"]["address"] == "foo@192.168.0.100"
32
+ conf = Omniscient::Configuration::load "foo"
33
+ assert conf.kind_of? Hash
34
+ assert conf["ssh"]["address"] == "foo@192.168.0.100"
35
+ conf = Omniscient::Configuration::load :foo
36
+ assert conf.kind_of? Hash
37
+ assert conf["ssh"]["address"] == "foo@192.168.0.100"
38
+
39
+ # inexistent key
40
+ conf = Omniscient::Configuration::load :lolwut
41
+ assert conf.kind_of? FalseClass
42
+ assert !conf
43
+
44
+ Omniscient::Configuration.PATH = original_path
45
+ end
46
+
47
+ end
@@ -15,14 +15,18 @@ class SshTest < Test::Unit::TestCase
15
15
  def test_initialization
16
16
  assert_equal 'user@localhost', @ssh.address
17
17
  end
18
+
19
+ def test_invalid_initialization
20
+ @ssh = Omniscient::Ssh.new :address => 'user@localhost'
21
+ end
18
22
 
19
23
  def test_address
20
- assert_equal 'user@localhost', @ssh.get_address()
24
+ assert_equal 'user@localhost', @ssh.get_address
21
25
  end
22
26
 
23
27
  def test_address_without_user
24
28
  @ssh = Omniscient::Ssh.new :address => 'localhost'
25
- assert_equal 'localhost', @ssh.get_address()
29
+ assert_equal 'localhost', @ssh.get_address
26
30
  end
27
31
 
28
32
  def test_address_with_port
@@ -0,0 +1,17 @@
1
+ ---
2
+ foo:
3
+ ssh:
4
+ address: foo@192.168.0.100
5
+ port: false
6
+ mysql:
7
+ user: root
8
+ password:
9
+ database: foo
10
+ bar:
11
+ ssh:
12
+ address: bar@192.168.0.101
13
+ port: 22
14
+ mysql:
15
+ user: root
16
+ password: ""
17
+ database: bar
@@ -0,0 +1 @@
1
+ TESTING = true
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alexandre de Oliveira
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-06 00:00:00 -03:00
17
+ date: 2011-05-18 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -47,8 +47,11 @@ files:
47
47
  - test/lib/omniscient/adapters/test_mysql.rb
48
48
  - test/lib/omniscient/command/test_clone.rb
49
49
  - test/lib/omniscient/test_command.rb
50
+ - test/lib/omniscient/test_configuration.rb
50
51
  - test/lib/omniscient/test_connection.rb
51
52
  - test/lib/omniscient/test_ssh.rb
53
+ - test/resources/configurations/omniscient_conf.yml
54
+ - test/set_test_environment.rb
52
55
  has_rdoc: true
53
56
  homepage: http://github.com/kurko/omniscient
54
57
  licenses: []
@@ -85,5 +88,8 @@ test_files:
85
88
  - test/lib/omniscient/adapters/test_mysql.rb
86
89
  - test/lib/omniscient/command/test_clone.rb
87
90
  - test/lib/omniscient/test_command.rb
91
+ - test/lib/omniscient/test_configuration.rb
88
92
  - test/lib/omniscient/test_connection.rb
89
93
  - test/lib/omniscient/test_ssh.rb
94
+ - test/resources/configurations/omniscient_conf.yml
95
+ - test/set_test_environment.rb