mina 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md CHANGED
@@ -1,5 +1,25 @@
1
- v0.2.0
2
- ------
1
+ v0.2.1 - Sep 08, 2012
2
+ ---------------------
3
+
4
+ This release is to fix some issues that should've been cleaned up in the
5
+ previous release, but wasn't.
6
+
7
+ ### Fixed:
8
+ * **Fix SSH helpers giving a 'class required' error.**
9
+ * **Send stdout even in term_mode = :pretty mode.**
10
+ * Rbenv: Fix compatibility with Debian, Arch, Fedora. (#44)
11
+ * Supress the "--depth is ignored in local clones" warning. (#56)
12
+
13
+ ### Added:
14
+ * Add the `:ssh_options` setting. (#23)
15
+ * Add the `:forward_agent` setting. (#23)
16
+
17
+ ### Changed:
18
+ * Make the `:term_mode` setting accept strings, not just symbols. (eg: `set
19
+ :term_mode, 'exec'`)
20
+
21
+ v0.2.0 - Sep 08, 2012
22
+ ---------------------
3
23
 
4
24
  This release had two pre releases:
5
25
 
data/lib/mina.rb CHANGED
@@ -6,6 +6,7 @@ module Mina
6
6
 
7
7
  autoload :DeployHelpers, 'mina/deploy_helpers'
8
8
  autoload :OutputHelpers, 'mina/output_helpers'
9
+ autoload :SshHelpers, 'mina/ssh_helpers'
9
10
  autoload :Helpers, 'mina/helpers'
10
11
  autoload :Settings, 'mina/settings'
11
12
  autoload :Tools, 'mina/tools'
data/lib/mina/default.rb CHANGED
@@ -1,7 +1,27 @@
1
1
  # # Modules: Default
2
2
  # This module is loaded when invoking `mina` with or without a project.
3
3
 
4
- set_default :bash_options, '-i'
4
+ # ## Settings
5
+ # Here are some of the common settings. All settings are optional unless
6
+ # otherwise noted.
7
+ #
8
+ # ### deploy_to
9
+ # (Required) Path to deploy to.
10
+ #
11
+ # ### domain
12
+ # (Required) Host name to deploy to.
13
+ #
14
+ # ### port
15
+ # SSH port number.
16
+ #
17
+ # ### forward_agent
18
+ # If set to `true`, enables SSH agent forwarding.
19
+ #
20
+ # ### identity_file
21
+ # The local path to the SSH private key file.
22
+ #
23
+ # ### ssh_options
24
+ # Switches to be passed to the `ssh` command.
5
25
 
6
26
  # ## Tasks
7
27
  # Any and all of these settings can be overriden in your `deploy.rb`.
data/lib/mina/git.rb CHANGED
@@ -9,7 +9,7 @@
9
9
  # ### branch
10
10
  # Sets the branch to be deployed.
11
11
 
12
- set_default :branch, "master"
12
+ set_default :branch, 'master'
13
13
 
14
14
  namespace :git do
15
15
  # ## Deploy tasks
@@ -18,6 +18,7 @@ namespace :git do
18
18
 
19
19
  # ### git:clone
20
20
  # Clones the Git repository. Meant to be used inside a deploy script.
21
+
21
22
  desc "Clones the Git repository to the release path."
22
23
  task :clone do
23
24
  if revision?
@@ -42,7 +43,7 @@ namespace :git do
42
43
  #{echo_cmd %[(cd "#{deploy_to}/scm" && git fetch "#{repository!}" "#{branch}:#{branch}" --force)]}
43
44
  fi &&
44
45
  echo "-----> Using git branch '#{branch}'" &&
45
- #{echo_cmd %[git clone "#{deploy_to}/scm" . --depth 1 --recursive --branch "#{branch}"]} &&
46
+ #{echo_cmd %[git clone "#{deploy_to}/scm" . --recursive --branch "#{branch}"]} &&
46
47
  }
47
48
  end
48
49
 
data/lib/mina/helpers.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # # Helpers: Default helpers
2
+
2
3
  module Mina
3
4
  module Helpers
4
5
 
@@ -9,7 +10,7 @@ module Mina
9
10
  #
10
11
  # invoke :'git:clone'
11
12
  # invoke :restart
12
- #
13
+
13
14
  def invoke(task)
14
15
  Rake.application.invoke_task task
15
16
  end
@@ -26,7 +27,7 @@ module Mina
26
27
  # #=> "1 and 2"
27
28
  #
28
29
  # Returns the output string of the ERB template.
29
- #
30
+
30
31
  def erb(file, b=binding)
31
32
  require 'erb'
32
33
  erb = ERB.new(File.read(file))
@@ -43,7 +44,7 @@ module Mina
43
44
  # run!
44
45
  #
45
46
  # Returns nothing.
46
- #
47
+
47
48
  def run!
48
49
  report_time { ssh commands(:default) }
49
50
  end
@@ -51,6 +52,15 @@ module Mina
51
52
  # ### report_time
52
53
  # Report time elapsed in the block.
53
54
  # Returns the output of the block.
55
+ #
56
+ # report_time do
57
+ # sleep 2
58
+ # # do other things
59
+ # end
60
+ #
61
+ # # Output:
62
+ # # Elapsed time: 2.0 seconds
63
+
54
64
  def report_time(&blk)
55
65
  time, output = measure &blk
56
66
  print_str "Elapsed time: %.2f seconds" % [time]
@@ -60,88 +70,18 @@ module Mina
60
70
  # ### measure
61
71
  # Measures the time (in ms) a block takes.
62
72
  # Returns a [time, output] tuple.
73
+
63
74
  def measure(&blk)
64
75
  t = Time.now
65
76
  output = yield
66
77
  [(Time.now - t).to_i, output]
67
78
  end
68
79
 
69
- # ## SSH helpers
70
- # You don't need to invoke these helpers, they're already invoked automatically.
71
-
72
- # ### ssh
73
- # Executes a command via SSH.
74
- #
75
- # Returns nothing usually, but if `{ return: true }` is given, returns the
76
- # STDOUT output of the SSH session.
77
- #
78
- # `options` is a hash of options:
79
- #
80
- # - `:pretty` - Prettify the output if true.
81
- # - `:return` - If set to true, returns the output.
82
- #
83
- # Example
84
- #
85
- # ssh("ls", return: true)
86
- #
87
- def ssh(cmd, options={})
88
- cmd = cmd.join("\n") if cmd.is_a?(Array)
89
-
90
- require 'shellwords'
91
-
92
- result = 0
93
- script = Shellwords.escape(cmd)
94
-
95
- if options[:return] == true
96
- result = `#{ssh_command} -- #{script}`
97
-
98
- elsif simulate_mode?
99
- str = "Executing the following via '#{ssh_command}':"
100
- puts "#!/usr/bin/env bash"
101
- puts "# #{str}"
102
- puts "#"
103
-
104
- puts cmd
105
-
106
- else
107
- code = "#{ssh_command} -- #{script}"
108
- if settings.term_mode == :pretty
109
- result = pretty_system(code)
110
- elsif settings.term_mode == :exec
111
- exec code
112
- else
113
- system code
114
- result = $?.to_i
115
- end
116
- end
117
-
118
- die result if result.is_a? Fixnum && result > 0
119
- result
120
- end
121
-
122
- # ### ssh_command
123
- # Returns the SSH command to be executed.
124
- #
125
- # set :domain, 'foo.com'
126
- # set :user, 'diggity'
127
- #
128
- # puts ssh_command
129
- # #=> 'ssh diggity@foo.com'
130
- #
131
- def ssh_command
132
- args = domain!
133
- args = "#{user}@#{args}" if user?
134
- args << " -i #{identity_file}" if identity_file?
135
- args << " -p #{port}" if port?
136
- args << " -t"
137
- "ssh #{args}"
138
- end
139
-
140
80
  # ### mina_cleanup
141
81
  # __Internal:__ Invoked when Rake exits.
142
82
  #
143
83
  # Returns nothing.
144
- #
84
+
145
85
  def mina_cleanup!
146
86
  run! if commands.any?
147
87
  end
@@ -154,7 +94,7 @@ module Mina
154
94
  #
155
95
  # die 2
156
96
  # die 2, "Tests failed"
157
- #
97
+
158
98
  def die(code=1, msg=null)
159
99
  str = "Failed with status #{code}"
160
100
  str += " (#{msg})" if msg
@@ -166,6 +106,7 @@ module Mina
166
106
  # ### error
167
107
  # __Internal:__ Prints to stdout.
168
108
  # Consider using `print_error` instead.
109
+
169
110
  def error(str)
170
111
  $stderr.write "#{str}\n"
171
112
  end
@@ -184,7 +125,7 @@ module Mina
184
125
  # queue "true"
185
126
  #
186
127
  # commands == ['sudo restart', 'true']
187
- #
128
+
188
129
  def queue(code)
189
130
  commands
190
131
  commands(@to) << unindent(code)
@@ -207,7 +148,7 @@ module Mina
207
148
  #
208
149
  # echo_cmd("ln -nfs releases/2 current")
209
150
  # #=> echo "$ ln -nfs releases/2 current" && ln -nfs releases/2 current
210
- #
151
+
211
152
  def echo_cmd(str)
212
153
  if verbose_mode?
213
154
  "echo #{Shellwords.escape("$ " + str)} &&\n#{str}"
@@ -234,7 +175,7 @@ module Mina
234
175
  #
235
176
  # commands == ["sudo restart", "true"]
236
177
  # commands(:clean) == ["rm"]
237
- #
178
+
238
179
  def commands(aspect=:default)
239
180
  (@commands ||= begin
240
181
  @to = :default
@@ -257,7 +198,7 @@ module Mina
257
198
  # end
258
199
  #
259
200
  # commands.should == ['sudo restart', 'true']
260
- #
201
+
261
202
  def isolate(&blk)
262
203
  old, @commands = @commands, nil
263
204
  result = yield
@@ -275,7 +216,7 @@ module Mina
275
216
  # end
276
217
  #
277
218
  # commands.should == ['cd ./webapp && (./reload && true)']
278
- #
219
+
279
220
  def in_directory(path, &blk)
280
221
  isolated_commands = isolate { yield; commands }
281
222
  isolated_commands.each { |cmd| queue "(cd #{path} && (#{cmd}))" }
@@ -295,7 +236,7 @@ module Mina
295
236
  #
296
237
  # commands(:prepare) == ["bundle install"]
297
238
  # commands(:restart) == ["nginx -s restart"]
298
- #
239
+
299
240
  def to(name, &blk)
300
241
  old, @to = @to, name
301
242
  yield
@@ -312,7 +253,7 @@ module Mina
312
253
  # Returns the value.
313
254
  #
314
255
  # set :domain, 'kickflip.me'
315
- #
256
+
316
257
  def set(key, value)
317
258
  settings.send :"#{key}=", value
318
259
  end
@@ -330,7 +271,7 @@ module Mina
330
271
  # set :term_mode, :system
331
272
  # set_default :term_mode, :pretty
332
273
  # settings.term_mode.should == :system
333
- #
274
+
334
275
  def set_default(key, value)
335
276
  settings.send :"#{key}=", value unless settings.send(:"#{key}?")
336
277
  end
@@ -342,7 +283,7 @@ module Mina
342
283
  #
343
284
  # settings.domain #=> 'kickflip.me'
344
285
  # domain #=> 'kickflip.me'
345
- #
286
+
346
287
  def settings
347
288
  @settings ||= Settings.new
348
289
  end
@@ -352,7 +293,7 @@ module Mina
352
293
  # See #settings for an explanation.
353
294
  #
354
295
  # Returns things.
355
- #
296
+
356
297
  def method_missing(meth, *args, &blk)
357
298
  settings.send meth, *args
358
299
  end
@@ -363,7 +304,7 @@ module Mina
363
304
  # Checks if Rake was invoked with --verbose.
364
305
  #
365
306
  # Returns true or false.
366
- #
307
+
367
308
  def verbose_mode?
368
309
  if Rake.respond_to?(:verbose)
369
310
  # Rake 0.9.x
@@ -378,7 +319,7 @@ module Mina
378
319
  # Checks if Rake was invoked with --simulate.
379
320
  #
380
321
  # Returns true or false.
381
- #
322
+
382
323
  def simulate_mode?
383
324
  !! ENV['simulate']
384
325
  end
@@ -387,6 +328,7 @@ module Mina
387
328
 
388
329
  # ### indent
389
330
  # Indents a given code block with `count` spaces before it.
331
+
390
332
  def indent(count, str)
391
333
  str.gsub(/^/, " "*count)
392
334
  end
@@ -403,7 +345,7 @@ module Mina
403
345
  # # Output:
404
346
  # # Hello
405
347
  # # There
406
- #
348
+
407
349
  def unindent(code)
408
350
  if code =~ /^\n([ \t]+)/
409
351
  code = code.gsub(/^#{$1}/, '')
@@ -414,6 +356,7 @@ module Mina
414
356
 
415
357
  # ### reindent
416
358
  # Resets the indentation on a given code block.
359
+
417
360
  def reindent(n, code)
418
361
  indent n, unindent(code)
419
362
  end
@@ -4,6 +4,33 @@
4
4
  module Mina
5
5
  module OutputHelpers
6
6
 
7
+ # ### print_str
8
+ # Prints a string by delegating it to the proper output helper.
9
+ #
10
+ # It takes an input with text and prints them nicely. The text block can
11
+ # have statuses (prefixed with `-----> `), errors (prefixed with `! `),
12
+ # commands (prefixed with `$ `) or anything else. Depending on the type of
13
+ # the message, they will be delegated to the proper print_* helper.
14
+ #
15
+ # -----> Unlocking
16
+ # $ unlock foo
17
+ # Unlocked.
18
+ # ! ERROR: Failed
19
+ #
20
+ # Returns nothing.
21
+ #
22
+ def print_str(str)
23
+ if str =~ /^\-+> (.*?)$/
24
+ print_status $1
25
+ elsif str =~ /^! (.*?)$/
26
+ print_error $1
27
+ elsif str =~ /^\$ (.*?)$/
28
+ print_command $1
29
+ else
30
+ print_stdout str
31
+ end
32
+ end
33
+
7
34
  # ### print_status
8
35
  # Prints a status message. (`----->`)
9
36
  def print_status(msg)
@@ -42,33 +69,6 @@ module Mina
42
69
  ENV['NO_COLOR'] ? str : "\033[#{c}m#{str}\033[0m"
43
70
  end
44
71
 
45
- # ### print_str
46
- # Prints a string by delegating it to the proper output helper.
47
- #
48
- # It takes an input with text and prints them nicely. The text block can
49
- # have statuses (prefixed with `-----> `), errors (prefixed with `! `),
50
- # commands (prefixed with `$ `) or anything else. Depending on the type of
51
- # the message, they will be delegated to the proper print_* helper.
52
- #
53
- # -----> Unlocking
54
- # $ unlock foo
55
- # Unlocked.
56
- # ! ERROR: Failed
57
- #
58
- # Returns nothing.
59
- #
60
- def print_str(str)
61
- if str =~ /^\-+> (.*?)$/
62
- print_status $1
63
- elsif str =~ /^! (.*?)$/
64
- print_error $1
65
- elsif str =~ /^\$ (.*?)$/
66
- print_command $1
67
- else
68
- print_stdout str
69
- end
70
- end
71
-
72
72
  # ### pretty_system
73
73
  # __Internal:__ Works like `system`, but indents and puts color.
74
74
  #
@@ -104,6 +104,14 @@ module Mina
104
104
  end
105
105
  end
106
106
 
107
+ # Read stdin in the background and send it out.
108
+ p2 = fork do
109
+ trap("INT") {}
110
+ while (char = STDIN.getbyte rescue nil)
111
+ i.putc char if char
112
+ end
113
+ end
114
+
107
115
  # Read stdout.
108
116
  while str = o.gets
109
117
  print_str str
data/lib/mina/rails.rb CHANGED
@@ -13,25 +13,28 @@ require 'mina/bundler'
13
13
  #
14
14
  # Note that changing this will NOT change the environment that your application
15
15
  # is ran in.
16
+
16
17
  set_default :rails_env, 'production'
17
18
 
18
19
  # ### bundle_prefix
19
- # Prefix for Bundler commands.
20
- # TODO: This should be lambda
20
+ # Prefix for Bundler commands. Often to something like `RAILS_ENV=production
21
+ # bundle exec`.
22
+ #
23
+ # queue! "#{bundle_prefix} annotate -r"
21
24
 
22
25
  set_default :bundle_prefix, lambda { %{RAILS_ENV="#{rails_env}" #{bundle_bin} exec} }
23
26
 
24
27
  # ### rake
25
28
  # The prefix for `rake` commands. Use like so:
26
29
  #
27
- # queue "#{rake} db:migrate"
30
+ # queue! "#{rake} db:migrate"
28
31
 
29
32
  set_default :rake, lambda { %{#{bundle_prefix} rake} }
30
33
 
31
34
  # ### rails
32
35
  # The prefix for `rails` commands. Use like so:
33
36
  #
34
- # queue "#{rails} console"
37
+ # queue! "#{rails} console"
35
38
 
36
39
  set_default :rails, lambda { %{#{bundle_prefix} rails} }
37
40
 
@@ -62,7 +65,7 @@ make_run_task = lambda { |name, sample_args|
62
65
  task name, [:arguments] => :environment do |t, args|
63
66
  arguments = args[:arguments]
64
67
  command = send name
65
- unless command
68
+ unless arguments
66
69
  puts %{You need to provide arguments. Try: mina "#{name}[#{sample_args}]"}
67
70
  exit 1
68
71
  end
data/lib/mina/rake.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # This file is invoked from Rake.
2
2
  extend Mina::Helpers
3
3
  extend Mina::DeployHelpers
4
+ extend Mina::SshHelpers
4
5
  extend Mina::OutputHelpers
5
6
 
6
7
  require 'mina/default'
data/lib/mina/rbenv.rb CHANGED
@@ -35,7 +35,7 @@ task :'rbenv:load' do
35
35
  echo "-----> Loading rbenv"
36
36
  #{echo_cmd %{export PATH="#{rbenv_path}/bin:$PATH"}}
37
37
 
38
- if ! which -s rbenv >/dev/null; then
38
+ if ! which rbenv >/dev/null; then
39
39
  echo "! rbenv not found"
40
40
  echo "! If rbenv is installed, check your :rbenv_path setting."
41
41
  exit 1
@@ -0,0 +1,76 @@
1
+ # # Helpers: SSH helpers
2
+ # You don't need to invoke these helpers, they're already invoked automatically.
3
+ #
4
+ module Mina
5
+ module SshHelpers
6
+ # ### ssh
7
+ # Executes a command via SSH.
8
+ #
9
+ # Returns nothing usually, but if `{ return: true }` is given, returns the
10
+ # STDOUT output of the SSH session.
11
+ #
12
+ # `options` is a hash of options:
13
+ #
14
+ # - `:pretty` - Prettify the output if true.
15
+ # - `:return` - If set to true, returns the output.
16
+ #
17
+ # Example
18
+ #
19
+ # ssh("ls", return: true)
20
+
21
+ def ssh(cmd, options={})
22
+ cmd = cmd.join("\n") if cmd.is_a?(Array)
23
+
24
+ require 'shellwords'
25
+
26
+ result = 0
27
+ script = Shellwords.escape(cmd)
28
+
29
+ if options[:return] == true
30
+ result = `#{ssh_command} -- #{script}`
31
+
32
+ elsif simulate_mode?
33
+ str = "Executing the following via '#{ssh_command}':"
34
+ puts "#!/usr/bin/env bash"
35
+ puts "# #{str}"
36
+ puts "#"
37
+
38
+ puts cmd
39
+
40
+ else
41
+ code = "#{ssh_command} -- #{script}"
42
+ if settings.term_mode.to_s == 'pretty'
43
+ result = pretty_system(code)
44
+ elsif settings.term_mode.to_s == 'exec'
45
+ exec code
46
+ else
47
+ system code
48
+ result = $?.to_i
49
+ end
50
+ end
51
+
52
+ die result if result.is_a?(Fixnum) && result > 0
53
+ result
54
+ end
55
+
56
+ # ### ssh_command
57
+ # Returns the SSH command to be executed.
58
+ #
59
+ # set :domain, 'foo.com'
60
+ # set :user, 'diggity'
61
+ #
62
+ # puts ssh_command
63
+ # #=> 'ssh diggity@foo.com'
64
+
65
+ def ssh_command
66
+ args = domain!
67
+ args = "#{user}@#{args}" if user?
68
+ args << " -i #{identity_file}" if identity_file?
69
+ args << " -p #{port}" if port?
70
+ args << " -A" if forward_agent?
71
+ args << " #{ssh_options}" if ssh_options?
72
+ args << " -t"
73
+ "ssh #{args}"
74
+ end
75
+ end
76
+ end
data/lib/mina/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Mina
2
2
  def self.version
3
- "0.2.0"
3
+ "0.2.1"
4
4
  end
5
5
  end
data/lib/mina/whenever.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # # Modules: Whenever
2
- # Adds settings and tasks for managing projects with [whenever}.
3
- #
2
+ # Adds settings and tasks for managing projects with [whenever].
4
3
  # [whenever]: http://rubygems.org/gems/whenever
5
4
 
6
5
  namespace :whenever do
@@ -60,3 +60,8 @@ namespace :passenger do
60
60
  }
61
61
  end
62
62
  end
63
+
64
+ task :rofl do
65
+ set :term_mode, :pretty
66
+ queue %[echo "Password:"; read x; echo out: $x;]
67
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mina
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -93,6 +93,7 @@ files:
93
93
  - lib/mina/rbenv.rb
94
94
  - lib/mina/rvm.rb
95
95
  - lib/mina/settings.rb
96
+ - lib/mina/ssh_helpers.rb
96
97
  - lib/mina/tools.rb
97
98
  - lib/mina/version.rb
98
99
  - lib/mina/whenever.rb