Class: Inprovise::ExecutionContext
- Inherits:
-
Object
- Object
- Inprovise::ExecutionContext
- Defined in:
- lib/inprovise/execution_context.rb
Direct Known Subclasses
Defined Under Namespace
Classes: DSL
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
- #for_user(user) ⇒ Object
- #in_dir(path, &blk) ⇒ Object
- #init_config(hash) ⇒ Object
-
#initialize(node, log, index, config = nil) ⇒ ExecutionContext
constructor
A new instance of ExecutionContext.
- #local(path) ⇒ Object
- #log(msg = nil) ⇒ Object
- #mkdir(path) ⇒ Object
- #remote(path) ⇒ Object
- #remove(path) ⇒ Object
- #run(cmd, opts = {}) ⇒ Object
- #run_local(cmd) ⇒ Object
- #set_owner(path, user, group = nil) ⇒ Object
- #set_permissions(path, mask) ⇒ Object
- #sudo(cmd, opts = {}) ⇒ Object
- #template(path) ⇒ Object
- #trigger(action_ref, *args) ⇒ Object
- #upload(from, to) ⇒ Object
Constructor Details
#initialize(node, log, index, config = nil) ⇒ ExecutionContext
Returns a new instance of ExecutionContext.
96 97 98 99 100 101 102 103 |
# File 'lib/inprovise/execution_context.rb', line 96 def initialize(node, log, index, config=nil) @node = node @log = log @node.log_to(@log) @config = init_config(config || @node.config) @index = index @script = nil end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
93 94 95 |
# File 'lib/inprovise/execution_context.rb', line 93 def config @config end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
93 94 95 |
# File 'lib/inprovise/execution_context.rb', line 93 def node @node end |
#script ⇒ Object
Returns the value of attribute script.
94 95 96 |
# File 'lib/inprovise/execution_context.rb', line 94 def script @script end |
Instance Method Details
#as(user, &blk) ⇒ Object
120 121 122 |
# File 'lib/inprovise/execution_context.rb', line 120 def as(user, &blk) for_user(user).exec(blk) end |
#binary_exists?(binary) ⇒ Boolean
224 225 226 |
# File 'lib/inprovise/execution_context.rb', line 224 def binary_exists?(binary) @node.binary_exists?(binary) end |
#copy(from, to) ⇒ Object
182 183 184 |
# File 'lib/inprovise/execution_context.rb', line 182 def copy(from, to) @node.copy(from, to) end |
#download(from, to) ⇒ Object
170 171 172 |
# File 'lib/inprovise/execution_context.rb', line 170 def download(from, to) @node.download(from, to) end |
#env(var) ⇒ Object
157 158 159 |
# File 'lib/inprovise/execution_context.rb', line 157 def env(var) @node.env(var) end |
#exec(blk, *args) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/inprovise/execution_context.rb', line 112 def exec(blk, *args) if args.empty? DSL.new(self).instance_eval(&blk) else DSL.new(self).instance_exec(*args, &blk) end end |
#for_user(user) ⇒ Object
135 136 137 138 139 140 |
# File 'lib/inprovise/execution_context.rb', line 135 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.class.new(new_node, new_log, @index, @config) end |
#in_dir(path, &blk) ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/inprovise/execution_context.rb', line 124 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 |
#init_config(hash) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/inprovise/execution_context.rb', line 105 def init_config(hash) hash.to_h.reduce(OpenStruct.new(hash)) do |os,(k,v)| os[k] = init_config(v) if Hash === v os end end |
#local(path) ⇒ Object
186 187 188 |
# File 'lib/inprovise/execution_context.rb', line 186 def local(path) Inprovise::LocalFile.new(path) end |
#log(msg = nil) ⇒ Object
161 162 163 164 |
# File 'lib/inprovise/execution_context.rb', line 161 def log(msg=nil) @log.log(msg) if msg @log end |
#mkdir(path) ⇒ Object
174 175 176 |
# File 'lib/inprovise/execution_context.rb', line 174 def mkdir(path) @node.mkdir(path) end |
#remote(path) ⇒ Object
190 191 192 |
# File 'lib/inprovise/execution_context.rb', line 190 def remote(path) Inprovise::RemoteFile.new(self, path) end |
#remove(path) ⇒ Object
178 179 180 |
# File 'lib/inprovise/execution_context.rb', line 178 def remove(path) @node.delete(path) end |
#run(cmd, opts = {}) ⇒ Object
149 150 151 |
# File 'lib/inprovise/execution_context.rb', line 149 def run(cmd, opts={}) @node.run(cmd, opts) end |
#run_local(cmd) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/inprovise/execution_context.rb', line 142 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
198 199 200 |
# File 'lib/inprovise/execution_context.rb', line 198 def set_owner(path, user, group=nil) @node.set_owner(path, user, group) end |
#set_permissions(path, mask) ⇒ Object
194 195 196 |
# File 'lib/inprovise/execution_context.rb', line 194 def (path, mask) @node.(path, mask) end |
#sudo(cmd, opts = {}) ⇒ Object
153 154 155 |
# File 'lib/inprovise/execution_context.rb', line 153 def sudo(cmd, opts={}) @node.sudo(cmd, opts) end |
#template(path) ⇒ Object
202 203 204 |
# File 'lib/inprovise/execution_context.rb', line 202 def template(path) Inprovise::Template.new(path, self) end |
#trigger(action_ref, *args) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/inprovise/execution_context.rb', line 206 def trigger(action_ref, *args) action_name, pkg_name = *action_ref.split(':', 2).reverse pkg = @script pkg = @index.get(pkg_name) if pkg_name action = pkg.actions[action_name] if pkg raise Inprovise::MissingActionError.new(action_ref) unless action curtask = @node.log.set_task(action_ref) curscript = @script @script = pkg @script.merge_configuration(self.config) begin exec(action, *args) ensure @script = curscript @node.log.set_task(curtask) end end |
#upload(from, to) ⇒ Object
166 167 168 |
# File 'lib/inprovise/execution_context.rb', line 166 def upload(from, to) @node.upload(from, to) end |