Module: DO::Commands

Extended by:
Commands
Includes:
Tasks, Utils
Included in:
Commands
Defined in:
lib/do/commands.rb

Instance Method Summary collapse

Methods included from Utils

#ask, #wait, #yes?

Methods included from Tasks

#desc, #namespace, #task, #task_find, #task_run, #tasks

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



123
124
125
# File 'lib/do/commands.rb', line 123

def method_missing(method, *args,  &block)
  current_server && current_server.respond_to?(method) ? current_server.send(method, *args) : super(method, *args,  &block)
end

Instance Method Details

#current_serverObject

Returns the current server



22
23
24
# File 'lib/do/commands.rb', line 22

def current_server
  @_current_server
end

#load_recipe(path) ⇒ Object Also known as: load



55
56
57
# File 'lib/do/commands.rb', line 55

def load_recipe(path)
  instance_eval(File.read(path), __FILE__, __LINE__)
end

#load_recipesObject



51
52
53
# File 'lib/do/commands.rb', line 51

def load_recipes
  recipes.each { |f| load_recipe(f) }
end

#log(text = "", new_line = true) ⇒ Object

Log text under current_server if available



112
113
114
# File 'lib/do/commands.rb', line 112

def log(text="", new_line=true)
  current_server ? current_server.log(text, new_line) : super(text, new_line)
end

#plugin(name, repo) ⇒ Object

Install in your DO_PATH a remote task

Examples

# You can install/update task with
# rake plugin:configuration
plugin "configuration", "https://gist.github.com/raw/xys.rake"


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/do/commands.rb', line 96

def plugin(name, repo)
  desc "install #{name} plugin"
  namespace :plugin do
    task(name => 'setup') do
      log "\e[36m## Installing plugin %s\e[0m" % name
      Dir.mkdir(DO_PATH) unless File.exist?(DO_PATH)
      path = File.join(DO_PATH, '%s.rake' % name)
      sh :curl, '--location', '--progress-bar', repo, '>', path
      load_recipe(path)
    end
  end
end

#recipesObject

DO loads rakefiles in these locations:

~/do/dorc
~/do/*.rake
./Do
./Dofile

DO_PATH, default is ~/do.



43
44
45
46
47
48
49
# File 'lib/do/commands.rb', line 43

def recipes
  @_recipes ||= (
    %w[dorc **/*.rake].map { |f| Dir[File.join(DO_PATH, f)] }.flatten +
    %w[./Do ./Dofile].map { |f| File.expand_path(f) } <<
    File.expand_path('../common.rb', __FILE__)
  ).reject { |f| !File.exist?(f) }
end

#remoteObject



15
16
17
# File 'lib/do/commands.rb', line 15

def remote
  servers.map(&:name)
end

#run(*args) ⇒ Object

Run commands on current_server if available



119
120
121
# File 'lib/do/commands.rb', line 119

def run(*args)
  current_server ? current_server.run(*args) : super(*args)
end

#server(name, host, user, options = {}) ⇒ Object

This method define our servers

Examples:

keys = %w[key1.pem key2.pem key3.pem key4.pem]
server :srv1, 's1.domain.local', 'user', :keys => keys
server :srv2, 's2.domain.local', 'user', :keys => keys
server :srv3, 's3.domain.local', 'user', :keys => keys
server :srv4, 's4.domain.local', 'user', :keys => keys


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/do/commands.rb', line 70

def server(name, host, user, options={})
  servers.push(DO::Server.new(name, host, user, options))
  task name do |opts, b|
    allowed  = opts.map { |k,v| k if remote.include?(k) && v }.compact
    denied   = opts.map { |k,v| k if remote.include?(k) && v == false }.compact
    if (allowed.empty? && denied.empty?) ||
       (!allowed.empty? && allowed.include?(name)) ||
       (!denied.empty? && !denied.include?(name))
      @_current_server = servers.find { |s| s.name == name }
      begin
        b.arity == 1 ? b.call(opts) : b.call
      ensure
        @_current_server = nil
      end
    end
  end
end

#serversObject

Array of DO::Server defined in our tasks



11
12
13
# File 'lib/do/commands.rb', line 11

def servers
  @_servers ||= []
end

#set(option, value) ⇒ Object

Set an option to the given value



29
30
31
# File 'lib/do/commands.rb', line 29

def set(option, value)
  define_method(option) { value }
end