geordi 0.14.6 → 0.14.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/console-for CHANGED
@@ -1,13 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
- require File.dirname(__FILE__) + "/../lib/geordi"
3
- include Geordi
2
+ require File.dirname(__FILE__) + "/../lib/geordi/capistrano"
3
+ include Geordi::Capistrano
4
4
 
5
5
  catching_errors do
6
- retrieve_data!
7
-
8
- if File.exists? "#{root}/script/console" # => Rails 2
9
- system "shell-for #{stage} --no-bash script/console #{env}"
6
+ self.stage = ARGV.shift
7
+ if File.exists? "#{config.root}/script/console" # => Rails 2
8
+ shell_for("script/console #{config.env}")
10
9
  else
11
- system "shell-for #{stage} --no-bash bundle exec rails console #{env}"
10
+ shell_for("bundle exec rails console #{config.env}")
12
11
  end
13
12
  end
data/bin/dump-for CHANGED
@@ -1,29 +1,29 @@
1
1
  #!/usr/bin/env ruby
2
- require File.dirname(__FILE__) + "/../lib/geordi"
3
- include Geordi
2
+ require File.dirname(__FILE__) + "/../lib/geordi/capistrano"
3
+ require File.dirname(__FILE__) + "/../lib/geordi/dump_loader"
4
+ include Geordi::Capistrano
4
5
 
5
6
  catching_errors do
6
- retrieve_data!
7
+ self.stage = ARGV.shift
7
8
 
8
- command = %(ssh #{user}@#{server} -t "cd #{path} && dumple #{env} --for_download")
9
- success = system command
9
+ success = shell_for("dumple #{config.env} --for_download", :exec => false)
10
10
 
11
11
  if success
12
12
  source_path = "~/dumps/dump_for_download.dump"
13
- destination_directory = "#{root}/tmp"
13
+ destination_directory = "#{config.root}/tmp"
14
14
  system "mkdir -p #{destination_directory}" # Ensure the destination directory exists
15
15
  destination_path = "#{destination_directory}/#{stage}.dump"
16
16
 
17
17
  puts "Downloading dump_for_download..."
18
- system "scp #{user}@#{server}:#{source_path} #{destination_path}"
18
+ system "scp #{config.user}@#{config.server}:#{source_path} #{destination_path}"
19
19
  puts
20
- puts "Dumped the #{stage.upcase} database to: #{File.basename root}/tmp/#{stage}.dump"
20
+ puts "Dumped the #{stage.upcase} database to: #{File.basename config.root}/tmp/#{stage}.dump"
21
21
 
22
22
  # source dump if option was given
23
23
  if ARGV.include?("-s")
24
24
  puts "Sourcing dump into development database..."
25
- puts `load-dump #{destination_path}`
26
- if $?.to_i == 0
25
+ success = DumpLoader.new([destination_path]).execute
26
+ if success
27
27
  puts "Your database is now sourced with a fresh #{stage} dump."
28
28
  else
29
29
  $stderr.puts "Could not source the downloaded #{stage} dump."
data/bin/load-dump CHANGED
@@ -1,91 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubygems'
4
- require 'highline'
5
-
6
-
7
- class DumpLoader
8
-
9
- def initialize(argv)
10
- @argv = argv
11
- @verbose = !!@argv.delete('-v')
12
- end
13
-
14
- def dumps_dir
15
- require 'etc'
16
- user_dir = Etc.getpwuid.dir
17
- File.join(user_dir, 'dumps')
18
- end
19
-
20
- def db_console_command
21
- if File.exists?("script/dbconsole")
22
- "script/dbconsole -p"
23
- else
24
- "rails dbconsole -p"
25
- end
26
- end
27
-
28
- def source_dump(dump)
29
- require 'pty'
30
- output_buffer = StringIO.new
31
- PTY.spawn(db_console_command) do |output, input, pid|
32
- input.write("source #{dump};\nexit;\n")
33
- output_buffer.write output.read
34
- end
35
-
36
- output_and_errors = output_buffer.string.split("\n")
37
- output = output_and_errors.reject{ |line| line =~ /^ERROR / }
38
- errors = output_and_errors.select{ |line| line =~ /^ERROR / }
39
-
40
- [output, errors]
41
- end
42
-
43
- def choose_dump_file
44
- highline = HighLine.new
45
-
46
- available_dumps = Dir.glob("#{dumps_dir}/*.dump").sort
47
- selected_dump = highline.choose(*available_dumps) do |menu|
48
- menu.hidden('') { exit }
49
- end
50
- end
51
-
52
- def get_dump_file
53
- if ARGV[0] && File.exists?(ARGV[0])
54
- ARGV[0]
55
- else
56
- choose_dump_file
57
- end
58
- end
59
-
60
-
61
- def puts_info(msg = "")
62
- puts msg if @verbose
63
- end
64
-
65
- def execute!
66
- dump_to_load = get_dump_file
67
-
68
- puts_info
69
- puts_info "sourcing #{dump_to_load} into db ..."
70
-
71
- output, errors = source_dump(dump_to_load)
72
-
73
- puts_info
74
- puts_info output.join("\n")
75
-
76
- if errors.empty?
77
- puts_info "sourcing completed successfully."
78
- exit(0)
79
- else
80
- $stderr.puts "some errors occured while loading the dump #{File.basename(dump_to_load)}:"
81
- $stderr.puts errors.join("\n");
82
- exit(1)
83
- end
84
- end
85
-
86
- end
3
+ require File.dirname(__FILE__) + "/../lib/geordi/dump_loader"
87
4
 
88
5
 
89
6
  DumpLoader.new(ARGV).execute!
90
-
91
-
data/bin/shell-for CHANGED
@@ -1,17 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
- require File.dirname(__FILE__) + "/../lib/geordi"
3
- include Geordi
2
+ require File.dirname(__FILE__) + "/../lib/geordi/capistrano"
3
+ include Geordi::Capistrano
4
4
 
5
5
  catching_errors do
6
- retrieve_data!
7
- no_bash = ARGV.delete('--no-bash')
8
- remote_command = ARGV.join(' ').strip
9
-
10
- ssh = %(ssh #{user}@#{server})
11
-
12
- commands = [ "cd #{path}" ]
13
- commands << remote_command unless remote_command.empty?
14
- commands << "bash --login" unless no_bash
15
-
16
- exec ssh + %( -t "#{commands.join(' && ')}")
17
- end
6
+ self.stage = ARGV.shift
7
+ shell_for(ARGV)
8
+ end
@@ -0,0 +1,107 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'capistrano'
4
+ require 'singleton'
5
+
6
+ module Geordi
7
+ module Capistrano
8
+
9
+ class Config
10
+
11
+ attr_accessor :stage, :root
12
+
13
+ def initialize(stage)
14
+ @stage = stage
15
+ @root = find_project_root!
16
+ load_capistrano_config
17
+ end
18
+
19
+ def user
20
+ @capistrano_config.fetch(:user)
21
+ end
22
+
23
+ def server
24
+ @capistrano_config.find_servers(:roles => [:app]).first
25
+ end
26
+
27
+ def path
28
+ @capistrano_config.fetch(:deploy_to) + '/current'
29
+ end
30
+
31
+ def env
32
+ @capistrano_config.fetch(:rails_env, 'production')
33
+ end
34
+
35
+ def shell
36
+ @capistrano_config.fetch(:default_shell, 'bash --login')
37
+ end
38
+
39
+
40
+ private
41
+
42
+ def load_capistrano_config
43
+ config = ::Capistrano::Configuration.new
44
+ config.load('config/deploy')
45
+ if @stage and @stage != ''
46
+ config.stage = @stage
47
+ config.find_and_execute_task(stage)
48
+ end
49
+
50
+ @capistrano_config = config
51
+ end
52
+
53
+ def find_project_root!
54
+ current = Dir.pwd
55
+ until (File.exists? 'Capfile')
56
+ Dir.chdir '..'
57
+ raise 'Call me from inside a Rails project!' if current == Dir.pwd
58
+ current = Dir.pwd
59
+ end
60
+ current
61
+ end
62
+ end
63
+
64
+
65
+ attr_accessor :stage
66
+
67
+ def config
68
+ @config ||= {}
69
+ @config[stage] ||= Config.new(stage)
70
+ end
71
+
72
+ def catching_errors(&block)
73
+ begin
74
+ yield
75
+ rescue Exception => e
76
+ $stderr.puts e.message
77
+ exit 1
78
+ end
79
+ end
80
+
81
+ def shell_for(*args)
82
+ options = {}
83
+ if args.last.is_a?(Hash)
84
+ options = args.pop
85
+ end
86
+
87
+ remote_command = args.join(' ').strip
88
+
89
+ login = %(#{config.user}@#{config.server})
90
+
91
+ commands = [ "cd #{config.path}" ]
92
+ if remote_command == ''
93
+ commands << config.shell
94
+ else
95
+ commands << %{#{config.shell} -c '#{remote_command}'}
96
+ end
97
+
98
+ args = ['ssh', login, '-t', commands.join(' && ')]
99
+ if options.fetch(:exec, true)
100
+ exec(*args)
101
+ else
102
+ system(*args)
103
+ end
104
+ end
105
+
106
+ end
107
+ end
@@ -0,0 +1,93 @@
1
+ require 'rubygems'
2
+ require 'highline'
3
+
4
+
5
+ class DumpLoader
6
+
7
+ def initialize(argv)
8
+ @argv = argv
9
+ @verbose = !!@argv.delete('-v')
10
+ end
11
+
12
+ def dumps_dir
13
+ require 'etc'
14
+ user_dir = Etc.getpwuid.dir
15
+ File.join(user_dir, 'dumps')
16
+ end
17
+
18
+ def db_console_command
19
+ if File.exists?("script/dbconsole")
20
+ "script/dbconsole -p"
21
+ else
22
+ "rails dbconsole -p"
23
+ end
24
+ end
25
+
26
+ def source_dump(dump)
27
+ require 'pty'
28
+ output_buffer = StringIO.new
29
+ PTY.spawn(db_console_command) do |output, input, pid|
30
+ input.write("source #{dump};\nexit;\n")
31
+ output_buffer.write output.read
32
+ end
33
+
34
+ output_and_errors = output_buffer.string.split("\n")
35
+ output = output_and_errors.reject{ |line| line =~ /^ERROR / }
36
+ errors = output_and_errors.select{ |line| line =~ /^ERROR / }
37
+
38
+ [output, errors]
39
+ end
40
+
41
+ def choose_dump_file
42
+ highline = HighLine.new
43
+
44
+ available_dumps = Dir.glob("#{dumps_dir}/*.dump").sort
45
+ selected_dump = highline.choose(*available_dumps) do |menu|
46
+ menu.hidden('') { exit }
47
+ end
48
+ end
49
+
50
+ def get_dump_file
51
+ if @argv[0] && File.exists?(@argv[0])
52
+ @argv[0]
53
+ else
54
+ choose_dump_file
55
+ end
56
+ end
57
+
58
+
59
+ def puts_info(msg = "")
60
+ puts msg if @verbose
61
+ end
62
+
63
+ def execute
64
+ dump_to_load = get_dump_file
65
+
66
+ puts_info
67
+ puts_info "sourcing #{dump_to_load} into db ..."
68
+
69
+ output, errors = source_dump(dump_to_load)
70
+
71
+ puts_info
72
+ puts_info output.join("\n")
73
+
74
+ if errors.empty?
75
+ puts_info "sourcing completed successfully."
76
+ true
77
+ else
78
+ $stderr.puts "some errors occured while loading the dump #{File.basename(dump_to_load)}:"
79
+ $stderr.puts errors.join("\n");
80
+ false
81
+ end
82
+ end
83
+
84
+ def execute!
85
+ if execute
86
+ exit(0)
87
+ else
88
+ exit(1)
89
+ end
90
+ end
91
+
92
+ end
93
+
@@ -1,3 +1,3 @@
1
1
  module Geordi
2
- VERSION = '0.14.6'
2
+ VERSION = '0.14.7'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geordi
3
3
  version: !ruby/object:Gem::Version
4
- hash: 43
4
+ hash: 41
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 14
9
- - 6
10
- version: 0.14.6
9
+ - 7
10
+ version: 0.14.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henning Koch
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-18 00:00:00 +01:00
18
+ date: 2013-03-01 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -83,8 +83,9 @@ files:
83
83
  - bin/shell-for
84
84
  - bin/tests
85
85
  - geordi.gemspec
86
- - lib/geordi.rb
86
+ - lib/geordi/capistrano.rb
87
87
  - lib/geordi/cuc.rb
88
+ - lib/geordi/dump_loader.rb
88
89
  - lib/geordi/gitpt.rb
89
90
  - lib/geordi/setup_firefox_for_selenium.rb
90
91
  - lib/geordi/version.rb
data/lib/geordi.rb DELETED
@@ -1,64 +0,0 @@
1
- module Geordi
2
-
3
- attr_accessor :stage, :user, :server, :path, :env, :root
4
-
5
- def catching_errors(&block)
6
- begin
7
- yield
8
- rescue Exception => e
9
- $stderr.puts e.message
10
- exit 1
11
- end
12
- end
13
-
14
- def retrieve_data!
15
- @stage = ARGV.shift
16
- @root = find_project_root!
17
-
18
- {}.tap do |data|
19
- @lines = if stage
20
- deploy_file = Dir['config/deploy/*.rb'].find do |file|
21
- file.match(/\/#{stage}.rb$/)
22
- end
23
- deploy_file or raise "Stage does not exist: #{stage}"
24
-
25
- File.open(deploy_file).readlines
26
- else
27
- []
28
- end
29
- @lines += File.open("config/deploy.rb").readlines
30
-
31
- @user = retrieve! "user", /^set :user,/
32
- @server = retrieve! "server", /^server /
33
- @path = retrieve!("deploy_to", /^set :deploy_to,/) + '/current'
34
- @env = retrieve!("environment", /^set :rails_env,/, 'production')
35
-
36
- # fix
37
- %w[user server path env].each do |attr|
38
- self.send(attr).gsub! /#\{site_id\}/, stage.sub(/_.*/, '')
39
- end
40
- end
41
- end
42
-
43
- def find_project_root!
44
- current = Dir.pwd
45
- until (File.exists? 'Capfile')
46
- Dir.chdir '..'
47
- raise 'Call me from inside a Rails project!' if current == Dir.pwd
48
- current = Dir.pwd
49
- end
50
- current
51
- end
52
-
53
- def retrieve!(name, regex, default = nil)
54
- if line = @lines.find{ |line| line =~ regex }
55
- line.match(/["'](.*)["']/)
56
- $1
57
- elsif default
58
- default
59
- else
60
- raise "Could not find :#{name} for stage '#{stage}'!\nAborting..."
61
- end
62
- end
63
-
64
- end