Module: RCS::Updater::DSL

Defined in:
lib/rcs-common/updater/dsl.rb

Constant Summary collapse

@@settings =
SafeOpenStruct.new
@@tasks =
{}
@@descriptions =
{}
@@last_description =
nil

Instance Method Summary collapse

Instance Method Details

#address?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rcs-common/updater/dsl.rb', line 36

def address?
  self.respond_to?(:address)
end

#desc(string) ⇒ Object



40
41
42
43
# File 'lib/rcs-common/updater/dsl.rb', line 40

def desc(string)
  raise("You cannot call `desc' in this context") if address?
  @@last_description = string
end

#echo(message) ⇒ Object



127
128
129
130
131
132
# File 'lib/rcs-common/updater/dsl.rb', line 127

def echo(message)
  message = "[echo]#{echo_indent}#{message}"
  message << " (#{self.address})" if self.respond_to?(:address) and echo_indent.empty? and !resolve_to_localhost?(self.address)
  $stdout.puts(message)
  $stdout.flush
end

#echo_error(message) ⇒ Object



121
122
123
124
125
# File 'lib/rcs-common/updater/dsl.rb', line 121

def echo_error(message)
  $stderr.puts("[erro]#{message}")
  $stderr.flush
  raise(message)
end

#echo_indentObject



113
114
115
116
117
118
119
# File 'lib/rcs-common/updater/dsl.rb', line 113

def echo_indent
  obj = self
  str = ""
  str << "--" until !(obj = obj.instance_variable_get('@_parent_task'))
  str << "> " unless str.empty?
  return str
end

#echo_install_failed(node_type, message = nil, addr = nil) ⇒ Object



134
135
136
137
138
139
# File 'lib/rcs-common/updater/dsl.rb', line 134

def echo_install_failed(node_type, message = nil, addr = nil)
  trace(:error, "Install of #{node_type} @ #{addr || address} failed: #{message}") if respond_to?(:trace)

  $stdout.puts("[infa]#{node_type.to_s.capitalize} node on #{addr || address}")
  $stdout.flush
end

#echo_install_success(node_type, addr = nil) ⇒ Object



141
142
143
144
# File 'lib/rcs-common/updater/dsl.rb', line 141

def echo_install_success(node_type, addr = nil)
  $stdout.puts("[insu]#{node_type.to_s.capitalize} node on #{addr || address}")
  $stdout.flush
end

#invoke(args) ⇒ Object

Examples:

invoke :task1 => 'localhost'

task :task2 do
  invoke(:task3)
  rm_rf("/tmp/my_file")
end


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rcs-common/updater/dsl.rb', line 77

def invoke(args)
  if address? and ([String, Symbol].include?(args.class))
    task_name, address = args, self.address
  elsif !address? and args.kind_of?(Hash)
    task_name, address =  *args.to_a.flatten
    return on(address) { invoke(task_name) }
  else
    raise("Invalid use of `invoke'")
  end

  raise("Undefined task `#{task_name}'") unless @@tasks[task_name.to_s]

  trace(:debug, "invoke #{task_name} on #{address}") if respond_to?(:trace)

  echo(@@descriptions[task_name]) if @@descriptions[task_name]

  client = Client.new(address)
  client.singleton_class.__send__(:include, DSL)
  client.instance_variable_set('@_parent_task', self) if address?
  return client.instance_eval(&@@tasks[task_name.to_s])
end

#on(address, &block) ⇒ Object

Define an anonymous task

Examples:

on('172.20.20.152') do
  invoke(:task1)
  rm_rf("/tmp/my_file")
end


106
107
108
109
110
111
# File 'lib/rcs-common/updater/dsl.rb', line 106

def on(address, &block)
  raise("You cannot call `on' in this context") if address?
  client = Client.new(address)
  client.singleton_class.__send__(:include, DSL)
  return client.instance_eval(&block)
end

#paramsObject

Access to parameters passed via command line.

Examples:

Script is called with –first-param “test” –param2

params.first_param #=> "test"
params.param2      #=> true
params.param3      #=> An exception is raised!
params.param3?     #=> nil


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rcs-common/updater/dsl.rb', line 157

def params
  return @@params if defined?(@@params)
  @@params = SafeOpenStruct.new
  i = 0

  loop do
    s1, s2 = ARGV[i], ARGV[i+1]
    break unless s1
    if s1[0] == '-'
      s2 = (s2 and s2[0] != '-') ? s2 : true
      @@params[s1.gsub(/^\-{1,2}/, '').gsub('-', '_')] = s2 unless s2.to_s.strip.empty?
    end
    i += 1
  end

  @@params
end

#resolve_to_localhost?(name) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/rcs-common/updater/dsl.rb', line 146

def resolve_to_localhost?(name)
  Client.resolve_to_localhost?(name)
end

#set(name, value) ⇒ Object



27
28
29
# File 'lib/rcs-common/updater/dsl.rb', line 27

def set(name, value)
  @@settings[name] = value
end

#settingsObject

Access to settings defined using [ set ]



32
33
34
# File 'lib/rcs-common/updater/dsl.rb', line 32

def settings
  @@settings
end

#task(name, &block) ⇒ Object

Define a task or alias an existing task

Examples:

task :task1 do
  rm_rf("/tmp/my_file")
  start_service("RCSDB")
end
# Aliasing task1
task :task3 => :task1


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rcs-common/updater/dsl.rb', line 54

def task(name, &block)
  if name.kind_of?(Hash)
    name.each do |alias_name, task_name|
      raise("Undefined task `#{task_name}'") unless @@tasks[task_name.to_s]
      @@tasks[alias_name.to_s] = @@tasks[task_name.to_s]
      @@descriptions[alias_name.to_s] = @@last_description
    end
  else
    raise("Task `#{name}' is defined more than once") if @@tasks[name.to_s]
    @@tasks[name.to_s] = block
    @@descriptions[name.to_s] = @@last_description
  end

  @@last_description = nil
end