Module: Mina::Helpers

Defined in:
lib/mina/helpers.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object

### method_missing Hook to get settings. See #settings for an explanation.

Returns things.



307
308
309
# File 'lib/mina/helpers.rb', line 307

def method_missing(meth, *args, &blk)
  settings.send meth, *args
end

Instance Method Details

#capture(cmd, options = {}) ⇒ Object

### capture Returns the output of command via SSH.



381
382
383
# File 'lib/mina/helpers.rb', line 381

def capture(cmd, options={})
  ssh cmd, options.merge(:return => true)
end

#commands(aspect = :default) ⇒ Object

### commands Returns an array of queued code strings.

You may give an optional ‘aspect`.

Returns an array of strings.

queue "sudo restart"
queue "true"

to :clean do
  queue "rm"
end

commands == ["sudo restart", "true"]
commands(:clean) == ["rm"]


188
189
190
191
192
193
# File 'lib/mina/helpers.rb', line 188

def commands(aspect=:default)
  (@commands ||= begin
    @to = :default
    Hash.new { |h, k| h[k] = Array.new }
  end)[aspect]
end

#die(code = 1, msg = null) ⇒ Object

### die Exits with a nice looking message. Returns nothing.

die 2
die 2, "Tests failed"


106
107
108
109
110
111
112
# File 'lib/mina/helpers.rb', line 106

def die(code=1, msg=null)
  str = "Failed with status #{code}"
  str += " (#{msg})" if msg
  err = Failed.new(str)
  err.exitstatus = code
  raise err
end

#echo_cmd(str) ⇒ Object

### echo_cmd Converts a bash command to a command that echoes before execution. Used to show commands in verbose mode. This does nothing unless verbose mode is on.

Returns a string of the compound bash command, typically in the format of ‘echo xx && xx`. However, if `verbose_mode?` is false, it returns the input string unharmed.

echo_cmd("ln -nfs releases/2 current")
#=> echo "$ ln -nfs releases/2 current" && ln -nfs releases/2 current


160
161
162
163
164
165
166
167
# File 'lib/mina/helpers.rb', line 160

def echo_cmd(str)
  if verbose_mode?
    require 'shellwords'
    "echo #{Shellwords.escape("$ " + str)} &&\n#{str}"
  else
    str
  end
end

#erb(file, b = binding) ⇒ Object

### erb Evaluates an ERB block in the current scope and returns a string.

a = 1
b = 2

# Assuming foo.erb is <%= a %> and <%= b %>
puts erb('foo.erb')

#=> "1 and 2"

Returns the output string of the ERB template.



39
40
41
42
43
# File 'lib/mina/helpers.rb', line 39

def erb(file, b=binding)
  require 'erb'
  erb = ERB.new(File.read(file))
  erb.result b
end

#error(str) ⇒ Object

### error Internal: Prints to stdout. Consider using ‘print_error` instead.



118
119
120
# File 'lib/mina/helpers.rb', line 118

def error(str)
  $stderr.write "#{str}\n"
end

#in_directory(path, &blk) ⇒ Object

### in_directory Starts a new block where #commands are collected, to be executed inside ‘path`.

Returns nothing.

in_directory './webapp' do
  queue "./reload"
end

commands.should == ['cd ./webapp && (./reload && true)']


229
230
231
232
# File 'lib/mina/helpers.rb', line 229

def in_directory(path, &blk)
  isolated_commands = isolate { yield; commands }
  isolated_commands.each { |cmd| queue "(cd #{path} && (#{cmd}))" }
end

#indent(count, str) ⇒ Object

### indent Indents a given code block with ‘count` spaces before it.



346
347
348
# File 'lib/mina/helpers.rb', line 346

def indent(count, str)
  str.gsub(/^/, " "*count)
end

#invoke(task, options = {}) ⇒ Object

### invoke Invokes another Rake task.

Invokes the task given in ‘task`. Returns nothing.

invoke :'git:clone'
invoke :restart

Options:

reenable (bool) - Execute the task even next time.


18
19
20
21
22
23
24
# File 'lib/mina/helpers.rb', line 18

def invoke(task, options = {})
  Rake.application.invoke_task task
  if options[:reenable]
    name = Rake.application.parse_task_string(task).first
    Rake::Task[name].reenable
  end
end

#isolate(&blk) ⇒ Object

### isolate Starts a new block where new ‘commands` are collected.

Returns nothing.

queue "sudo restart"
queue "true"
commands.should == ['sudo restart', 'true']

isolate do
  queue "reload"
  commands.should == ['reload']
end

commands.should == ['sudo restart', 'true']


211
212
213
214
215
216
# File 'lib/mina/helpers.rb', line 211

def isolate(&blk)
  old, @commands = @commands, nil
  result = yield
  new_code, @commands = @commands, old
  result
end

#measure(&blk) ⇒ Object

### measure Measures the time (in seconds) a block takes. Returns a [time, output] tuple.



82
83
84
85
86
# File 'lib/mina/helpers.rb', line 82

def measure(&blk)
  t = Time.now
  output = yield
  [Time.now - t, output]
end

#mina_cleanup!Object

### mina_cleanup Internal: Invoked when Rake exits.

Returns nothing.



93
94
95
# File 'lib/mina/helpers.rb', line 93

def mina_cleanup!
  run! if commands.any?
end

#queue(code) ⇒ Object

### queue Queues code to be run.

This queues code to be run to the current code bucket (defaults to ‘:default`). To get the things that have been queued, use commands

Returns nothing.

queue "sudo restart"
queue "true"

commands == ['sudo restart', 'true']


137
138
139
140
# File 'lib/mina/helpers.rb', line 137

def queue(code)
  commands
  commands(@to) << unindent(code)
end

#queue!(code) ⇒ Object

### queue! Shortcut for ‘queue`ing a command that shows up in verbose mode.



145
146
147
# File 'lib/mina/helpers.rb', line 145

def queue!(code)
  queue echo_cmd(code)
end

#reindent(n, code) ⇒ Object

### reindent Resets the indentation on a given code block.



374
375
376
# File 'lib/mina/helpers.rb', line 374

def reindent(n, code)
  indent n, unindent(code)
end

#report_time(&blk) ⇒ Object

### report_time Report time elapsed in the block. Returns the output of the block.

report_time do
  sleep 2
  # do other things
end

# Output:
# Elapsed time: 2.00 seconds


72
73
74
75
76
# File 'lib/mina/helpers.rb', line 72

def report_time(&blk)
  time, output = measure &blk
  print_str "Elapsed time: %.2f seconds" % [time]
  output
end

#run!Object

### run! SSHs into the host and runs the code that has been queued.

This is already automatically invoked before Rake exits to run all commands that have been queued up.

queue "sudo restart"
run!

Returns nothing.



56
57
58
# File 'lib/mina/helpers.rb', line 56

def run!
  report_time { ssh commands(:default) }
end

#set(key, value) ⇒ Object

### set Sets settings. Sets given symbol ‘key` to value in `value`.

Returns the value.

set :domain, 'kickflip.me'


267
268
269
# File 'lib/mina/helpers.rb', line 267

def set(key, value)
  settings.send :"#{key}=", value
end

#set_default(key, value) ⇒ Object

### set_default Sets default settings. Sets given symbol ‘key` to value in `value` only if the key isn’t set yet.

Returns the value.

set_default :term_mode, :pretty
set :term_mode, :system
settings.term_mode.should == :system

set :term_mode, :system
set_default :term_mode, :pretty
settings.term_mode.should == :system


285
286
287
# File 'lib/mina/helpers.rb', line 285

def set_default(key, value)
  settings.send :"#{key}=", value  unless settings.send(:"#{key}?")
end

#settingsObject

### settings Accesses the settings hash.

set :domain, 'kickflip.me'

settings.domain  #=> 'kickflip.me'
domain           #=> 'kickflip.me'


297
298
299
# File 'lib/mina/helpers.rb', line 297

def settings
  @settings ||= Settings.new
end

#simulate_mode?Boolean

### simulate_mode? Checks if Rake was invoked with –simulate.

Returns true or false.

Returns:

  • (Boolean)


337
338
339
# File 'lib/mina/helpers.rb', line 337

def simulate_mode?
  !! ENV['simulate']
end

#to(name, &blk) ⇒ Object

### to Defines instructions on how to do a certain thing. This makes the commands that are ‘queue`d go into a different bucket in commands.

Returns nothing.

to :prepare do
  run "bundle install"
end
to :launch do
  run "nginx -s restart"
end

commands(:prepare) == ["bundle install"]
commands(:restart) == ["nginx -s restart"]


250
251
252
253
254
255
# File 'lib/mina/helpers.rb', line 250

def to(name, &blk)
  old, @to = @to, name
  yield
ensure
  @to = old
end

#unindent(code) ⇒ Object

### unindent Internal: Normalizes indentation on a given string.

Returns the normalized string without extraneous indentation.

puts unindent %{
  Hello
    There
}
# Output:
# Hello
#   There


363
364
365
366
367
368
369
# File 'lib/mina/helpers.rb', line 363

def unindent(code)
  if code =~ /^\n([ \t]+)/
    code = code.gsub(/^#{$1}/, '')
  end

  code.strip
end

#verbose_mode?Boolean

### verbose_mode? Checks if Rake was invoked with –verbose.

Returns true or false.

if verbose_mode?
  queue %[echo "-----> Starting a new process"]
end

Returns:

  • (Boolean)


322
323
324
325
326
327
328
329
330
# File 'lib/mina/helpers.rb', line 322

def verbose_mode?
  if Rake.respond_to?(:verbose)
    #- Rake 0.9.x
    Rake.verbose == true
  else
    #- Rake 0.8.x
    RakeFileUtils.verbose_flag != :default
  end
end