do 0.1.4 → 0.2.0.a

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.1.5 (Not yet released)
2
+
3
+ * Append now add \n at the end of line if not present
4
+ * Added options :recursive to download and upload methods
5
+ * Added a new method for execute local task inside a remote server
6
+
1
7
  ## 0.1.4 - August 31, 2011
2
8
 
3
9
  * Added support to auto completion
data/lib/do/commands.rb CHANGED
@@ -12,10 +12,19 @@ module DO
12
12
  @_servers ||= []
13
13
  end
14
14
 
15
- def remote
15
+ def all_remotes
16
16
  servers.map(&:name)
17
17
  end
18
18
 
19
+ def remotes
20
+ @_remotes ||= begin
21
+ name = ARGV[0]
22
+ servers.select { |s| s.name.to_s == name } + servers.select { |s| s.role.to_s == name }
23
+ end
24
+ @_remotes.map(&:name)
25
+ end
26
+ alias :remote :remotes
27
+
19
28
  ##
20
29
  # Returns the current server
21
30
  #
@@ -23,11 +32,21 @@ module DO
23
32
  @_current_server
24
33
  end
25
34
 
35
+ ##
36
+ # Run a specific(s) command(s) outside the server
37
+ #
38
+ def locally(&block)
39
+ server_was, @_current_server = @_current_server, nil
40
+ block.call
41
+ ensure
42
+ @_current_server = server_was
43
+ end
44
+
26
45
  ##
27
46
  # Set an option to the given value
28
47
  #
29
- def set(option, value)
30
- define_method(option) { value }
48
+ def set(name, value)
49
+ (class << self; self; end).send(:define_method, name) { value }
31
50
  end
32
51
 
33
52
  ##
@@ -52,11 +71,27 @@ module DO
52
71
  recipes.each { |f| load_recipe(f) }
53
72
  end
54
73
 
74
+ def load_common
75
+ load File.expand_path('../common.rb', __FILE__)
76
+ end
77
+
55
78
  def load_recipe(path)
56
79
  instance_eval(File.read(path), __FILE__, __LINE__)
57
80
  end
58
81
  alias :load :load_recipe
59
82
 
83
+ def role(name)
84
+ servers_was = servers.dup
85
+ yield self
86
+ (servers-servers_was).each do |s|
87
+ s.instance_variable_set(:'@role', name)
88
+ end
89
+ end
90
+
91
+ def roles
92
+ servers.map(&:role).compact.uniq
93
+ end
94
+
60
95
  ##
61
96
  # This method define our servers
62
97
  #
@@ -69,18 +104,15 @@ module DO
69
104
  #
70
105
  def server(name, host, user, options={})
71
106
  servers.push(DO::Server.new(name, host, user, options))
107
+ current = servers[-1]
108
+ set current.name, current
109
+ set current.role, servers.select { |s| s.role == current.role } if current.role
72
110
  task name do |opts, b|
73
- allowed = opts.map { |k,v| k if remote.include?(k) && v }.compact
74
- denied = opts.map { |k,v| k if remote.include?(k) && v == false }.compact
75
- if (allowed.empty? && denied.empty?) ||
76
- (!allowed.empty? && allowed.include?(name)) ||
77
- (!denied.empty? && !denied.include?(name))
78
- @_current_server = servers.find { |s| s.name == name }
79
- begin
80
- b.arity == 1 ? b.call(opts) : b.call
81
- ensure
82
- @_current_server = nil
83
- end
111
+ @_current_server = servers.find { |s| s.name == current.name }
112
+ begin
113
+ b.arity == 1 ? b.call(opts) : b.call
114
+ ensure
115
+ @_current_server = nil
84
116
  end
85
117
  end
86
118
  end
data/lib/do/server.rb CHANGED
@@ -5,7 +5,7 @@ module DO
5
5
  class Server
6
6
  include DO::Utils
7
7
 
8
- attr_reader :name, :host, :user, :options
8
+ attr_reader :name, :host, :user, :role, :options
9
9
 
10
10
  ##
11
11
  # Initialize a new DO Server
@@ -19,7 +19,7 @@ module DO
19
19
  # srv1 = DO::Server.new(:srv1, 'srv1.lipsiasoft.biz', 'root', :keys => %w[/path/to/key.pem]
20
20
  #
21
21
  def initialize(name, host, user, options={})
22
- @name, @host, @user, @options = name, host, user, options
22
+ @name, @host, @user, @role, @options = name, host, user, options.delete(:role), options
23
23
  end
24
24
 
25
25
  ##
@@ -72,9 +72,9 @@ module DO
72
72
  cmd = args.join(" ")
73
73
  if options[:as]
74
74
  if options[:as] == 'root'
75
- cmd = "sudo #{cmd.gsub(/'/, "\'")}"
75
+ cmd = "sudo #{cmd}"
76
76
  else
77
- cmd = "su #{options[:as]} -c '#{cmd.gsub(/'/, "\'")}'"
77
+ cmd = "su #{options[:as]} -c '#{cmd.gsub("'", "'\\\\''")}'"
78
78
  end
79
79
  end
80
80
  log cmd
@@ -126,8 +126,16 @@ module DO
126
126
  # up(/my/dir, /tmp, :recursive => true)
127
127
  #
128
128
  def upload(from, to, options={})
129
- log "upload from '%s' to '%s'" % [from, to]
130
- sftp.upload!(from, to)
129
+ sftp.upload!(from, to, options) do |event, uploader, *args|
130
+ case event
131
+ when :put
132
+ DO_LOGGER.print("\r" << DO_LOGGER_FORMAT % [user, name, "writing: #{to} (#{args[1] * 100 / args[0].size}%)"]); DO_LOGGER.flush
133
+ when :finish
134
+ DO_LOGGER.puts("\r" << DO_LOGGER_FORMAT % [user, name, "writing: #{to} (100%)"]); DO_LOGGER.flush
135
+ when :mkdir
136
+ log "creating directory #{args[0]}"
137
+ end
138
+ end
131
139
  end
132
140
  alias :up :upload
133
141
 
@@ -142,8 +150,16 @@ module DO
142
150
  # get(/tmp/dir, /my, :recursive => true)
143
151
  #
144
152
  def download(from, to, options={})
145
- log "download from '%s' to '%s'" % [from, to]
146
- sftp.download!(from, to)
153
+ sftp.download!(from, to, options) do |event, downloader, *args|
154
+ case event
155
+ when :get
156
+ DO_LOGGER.print("\r" << DO_LOGGER_FORMAT % [user, name, "sending: #{from} (#{args[1] * 100 / args[0].size}%)"]); DO_LOGGER.flush
157
+ when :finish
158
+ DO_LOGGER.puts("\r" << DO_LOGGER_FORMAT % [user, name, "sending: #{from} (100%)"]); DO_LOGGER.flush
159
+ when :mkdir
160
+ log "creating directory #{args[0]}"
161
+ end
162
+ end
147
163
  end
148
164
  alias :get :download
149
165
 
@@ -205,6 +221,7 @@ module DO
205
221
  log "'%s' already match your pattern" % file
206
222
  return false
207
223
  else
224
+ pattern += "\n" unless pattern[-1] == ?\n
208
225
  replacement = case where
209
226
  when :top, :start then pattern+was
210
227
  when :bottom, :end then was+pattern
data/lib/do/tasks.rb CHANGED
@@ -52,8 +52,9 @@ module DO
52
52
  if task[:in].empty?
53
53
  task[:block].arity == 1 ? task[:block].call(opts) : task[:block].call if task[:block]
54
54
  else
55
- task[:in] = send(task[:in][0]) if task[:in].size == 1 && method_defined?(task[:in][0])
56
- task[:in].each do |d|
55
+ task[:in] = send(task[:in][0]) if task[:in].size == 1 && (class << self; self; end).method_defined?(task[:in][0])
56
+ task[:in] = task[:in].name if task[:in].is_a?(DO::Server)
57
+ Array(task[:in]).each do |d|
57
58
  parent = task_find(d)
58
59
  case parent[:block].arity
59
60
  when 1 then parent[:block].call(task[:block])
@@ -63,7 +64,7 @@ module DO
63
64
  end
64
65
  end
65
66
  rescue NotFound => e
66
- method_defined?(args_was[0]) ? send(args_was.shift) : raise(e)
67
+ (class << self; self; end).method_defined?(args_was[0]) ? send(args_was.shift) : raise(e)
67
68
  end
68
69
  alias :run_task :task_run
69
70
 
data/lib/do/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module DO
2
- VERSION = "0.1.4" unless defined?(DO::VERSION)
2
+ VERSION = "0.2.0.a" unless defined?(DO::VERSION)
3
3
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: do
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease:
4
+ hash: 58
5
+ prerelease: 6
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 4
10
- version: 0.1.4
8
+ - 2
9
+ - 0
10
+ - a
11
+ version: 0.2.0.a
11
12
  platform: ruby
12
13
  authors:
13
14
  - Davide D'Agostino
@@ -15,7 +16,7 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2011-08-31 00:00:00 +02:00
19
+ date: 2011-11-28 00:00:00 +01:00
19
20
  default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
@@ -109,12 +110,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
110
  required_rubygems_version: !ruby/object:Gem::Requirement
110
111
  none: false
111
112
  requirements:
112
- - - ">="
113
+ - - ">"
113
114
  - !ruby/object:Gem::Version
114
- hash: 3
115
+ hash: 25
115
116
  segments:
116
- - 0
117
- version: "0"
117
+ - 1
118
+ - 3
119
+ - 1
120
+ version: 1.3.1
118
121
  requirements: []
119
122
 
120
123
  rubyforge_project: