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
- #address? ⇒ Boolean
- #desc(string) ⇒ Object
- #echo(message) ⇒ Object
- #echo_error(message) ⇒ Object
- #echo_indent ⇒ Object
- #echo_install_failed(node_type, message = nil, addr = nil) ⇒ Object
- #echo_install_success(node_type, addr = nil) ⇒ Object
- #invoke(args) ⇒ Object
-
#on(address, &block) ⇒ Object
Define an anonymous task.
-
#params ⇒ Object
Access to parameters passed via command line.
- #resolve_to_localhost?(name) ⇒ Boolean
- #set(name, value) ⇒ Object
-
#settings ⇒ Object
Access to settings defined using [ set ].
-
#task(name, &block) ⇒ Object
Define a task or alias an existing task.
Instance Method Details
#address? ⇒ 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() = "[echo]#{echo_indent}#{message}" << " (#{self.address})" if self.respond_to?(:address) and echo_indent.empty? and !resolve_to_localhost?(self.address) $stdout.puts() $stdout.flush end |
#echo_error(message) ⇒ Object
121 122 123 124 125 |
# File 'lib/rcs-common/updater/dsl.rb', line 121 def echo_error() $stderr.puts("[erro]#{message}") $stderr.flush raise() end |
#echo_indent ⇒ Object
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, = 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
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
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 |
#params ⇒ Object
Access to parameters passed via command line.
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
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 |
#settings ⇒ Object
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
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 |