Class: Inprovise::ExecutionContext
- Inherits:
-
Object
- Object
- Inprovise::ExecutionContext
- Defined in:
- lib/inprovise/execution_context.rb
Direct Known Subclasses
Defined Under Namespace
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#script ⇒ Object
Returns the value of attribute script.
Instance Method Summary collapse
- #as(user, &blk) ⇒ Object
- #binary_exists?(binary) ⇒ Boolean
- #copy(from, to) ⇒ Object
- #download(from, to) ⇒ Object
- #env(var) ⇒ Object
- #exec(blk, *args) ⇒ Object
- #exec_config(blk) ⇒ Object
- #for_user(user) ⇒ Object
- #in_dir(path, &blk) ⇒ Object
-
#initialize(node, log, index, config = nil) ⇒ ExecutionContext
constructor
A new instance of ExecutionContext.
- #local(path) ⇒ Object
- #log(msg = nil, color = nil) ⇒ Object
- #mkdir(path) ⇒ Object
- #move(from, to) ⇒ Object
- #remote(path) ⇒ Object
- #remove(path) ⇒ Object
- #resolve_action_ref(action_ref) ⇒ Object
- #run(cmd, opts = {}) ⇒ Object
- #run_local(cmd) ⇒ Object
- #set_owner(path, user, group = nil) ⇒ Object
- #set_permissions(path, mask) ⇒ Object
- #setup_for_node!(node, log) ⇒ Object
- #sudo(cmd, opts = {}) ⇒ Object
- #template(path) ⇒ Object
- #trigger(scr, action_name, *args) ⇒ Object
- #upload(from, to) ⇒ Object
Constructor Details
#initialize(node, log, index, config = nil) ⇒ ExecutionContext
Returns a new instance of ExecutionContext.
101 102 103 104 105 106 107 108 |
# File 'lib/inprovise/execution_context.rb', line 101 def initialize(node, log, index, config=nil) @node = node @log = log @node.log_to(@log) @config = Inprovise::Config.new(config || @node.config) @index = index @script = nil end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
98 99 100 |
# File 'lib/inprovise/execution_context.rb', line 98 def config @config end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
98 99 100 |
# File 'lib/inprovise/execution_context.rb', line 98 def node @node end |
#script ⇒ Object
Returns the value of attribute script.
99 100 101 |
# File 'lib/inprovise/execution_context.rb', line 99 def script @script end |
Instance Method Details
#as(user, &blk) ⇒ Object
118 119 120 |
# File 'lib/inprovise/execution_context.rb', line 118 def as(user, &blk) for_user(user).exec(blk) end |
#binary_exists?(binary) ⇒ Boolean
235 236 237 |
# File 'lib/inprovise/execution_context.rb', line 235 def binary_exists?(binary) @node.binary_exists?(binary) end |
#copy(from, to) ⇒ Object
186 187 188 |
# File 'lib/inprovise/execution_context.rb', line 186 def copy(from, to) @node.copy(from, to) end |
#download(from, to) ⇒ Object
174 175 176 |
# File 'lib/inprovise/execution_context.rb', line 174 def download(from, to) @node.download(from, to) end |
#env(var) ⇒ Object
161 162 163 |
# File 'lib/inprovise/execution_context.rb', line 161 def env(var) @node.env(var) end |
#exec(blk, *args) ⇒ Object
114 115 116 |
# File 'lib/inprovise/execution_context.rb', line 114 def exec(blk, *args) DSL.new(self).instance_exec(*args, &blk) end |
#exec_config(blk) ⇒ Object
110 111 112 |
# File 'lib/inprovise/execution_context.rb', line 110 def exec_config(blk) ConfigDSL.new(self).instance_eval(&blk) end |
#for_user(user) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/inprovise/execution_context.rb', line 133 def for_user(user) return self if user.nil? || user == node.user new_node = @node.for_user(user) new_log = @log.clone_for_node(new_node) self.dup.setup_for_node!(new_node, new_log) end |
#in_dir(path, &blk) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/inprovise/execution_context.rb', line 122 def in_dir(path, &blk) rc = nil old_cwd = @node.helper.set_cwd(path) begin rc = exec(blk) ensure @node.helper.set_cwd(old_cwd) end rc end |
#local(path) ⇒ Object
194 195 196 |
# File 'lib/inprovise/execution_context.rb', line 194 def local(path) Inprovise::LocalFile.new(self, path) end |
#log(msg = nil, color = nil) ⇒ Object
165 166 167 168 |
# File 'lib/inprovise/execution_context.rb', line 165 def log(msg=nil, color=nil) @log.log(msg, color) if msg @log end |
#mkdir(path) ⇒ Object
178 179 180 |
# File 'lib/inprovise/execution_context.rb', line 178 def mkdir(path) @node.mkdir(path) end |
#move(from, to) ⇒ Object
190 191 192 |
# File 'lib/inprovise/execution_context.rb', line 190 def move(from, to) @node.move(from, to) end |
#remote(path) ⇒ Object
198 199 200 |
# File 'lib/inprovise/execution_context.rb', line 198 def remote(path) Inprovise::RemoteFile.new(self, path) end |
#remove(path) ⇒ Object
182 183 184 |
# File 'lib/inprovise/execution_context.rb', line 182 def remove(path) @node.delete(path) end |
#resolve_action_ref(action_ref) ⇒ Object
214 215 216 217 218 |
# File 'lib/inprovise/execution_context.rb', line 214 def resolve_action_ref(action_ref) action_name, scr_name = *action_ref.split(':', 2).reverse scr = scr_name ? @index.get(scr_name) : nil [scr, action_name] end |
#run(cmd, opts = {}) ⇒ Object
153 154 155 |
# File 'lib/inprovise/execution_context.rb', line 153 def run(cmd, opts={}) @node.run(cmd, opts) end |
#run_local(cmd) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/inprovise/execution_context.rb', line 146 def run_local(cmd) @log.local(cmd) stdout, stderr, status = Open3.capture3(cmd) @log.stdout(stdout) @log.stderr(stderr) end |
#set_owner(path, user, group = nil) ⇒ Object
206 207 208 |
# File 'lib/inprovise/execution_context.rb', line 206 def set_owner(path, user, group=nil) @node.set_owner(path, user, group) end |
#set_permissions(path, mask) ⇒ Object
202 203 204 |
# File 'lib/inprovise/execution_context.rb', line 202 def (path, mask) @node.(path, mask) end |
#setup_for_node!(node, log) ⇒ Object
140 141 142 143 144 |
# File 'lib/inprovise/execution_context.rb', line 140 def setup_for_node!(node, log) @node = node @log = log self end |
#sudo(cmd, opts = {}) ⇒ Object
157 158 159 |
# File 'lib/inprovise/execution_context.rb', line 157 def sudo(cmd, opts={}) @node.sudo(cmd, opts) end |
#template(path) ⇒ Object
210 211 212 |
# File 'lib/inprovise/execution_context.rb', line 210 def template(path) Inprovise::Template.new(path, self) end |
#trigger(scr, action_name, *args) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/inprovise/execution_context.rb', line 220 def trigger(scr, action_name, *args) scr ||= @script action = scr ? scr.actions[action_name] : nil raise Inprovise::MissingActionError.new("#{scr ? scr.name+':' : ''}#{action_name}") unless action curtask = @node.log.set_task("#{scr.name}:#{action_name}") curscript = @script @script = scr begin exec(action, *args) ensure @script = curscript @node.log.set_task(curtask) end end |
#upload(from, to) ⇒ Object
170 171 172 |
# File 'lib/inprovise/execution_context.rb', line 170 def upload(from, to) @node.upload(from, to) end |