Module: Bailiff::Core
- Included in:
- Context
- Defined in:
- lib/bailiff/core.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#instruments ⇒ Object
readonly
Returns the value of attribute instruments.
Instance Method Summary collapse
- #after(&block) ⇒ Object
- #before(&block) ⇒ Object
- #context ⇒ Object
- #dependency(*instruments) ⇒ Object
- #execute(options = {}) ⇒ Object
- #has_package(package) ⇒ Object
- #instrument(name, context = self, &block) ⇒ Object
- #package(*packages) ⇒ Object
- #resolve(resolve_commands = true) ⇒ Object
- #run(command, type = :command) ⇒ Object
- #write_file(path, data) ⇒ Object
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
4 5 6 |
# File 'lib/bailiff/core.rb', line 4 def commands @commands end |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
4 5 6 |
# File 'lib/bailiff/core.rb', line 4 def dependencies @dependencies end |
#instruments ⇒ Object (readonly)
Returns the value of attribute instruments.
4 5 6 |
# File 'lib/bailiff/core.rb', line 4 def instruments @instruments end |
Instance Method Details
#after(&block) ⇒ Object
26 27 28 |
# File 'lib/bailiff/core.rb', line 26 def after(&block) run block, :call end |
#before(&block) ⇒ Object
22 23 24 |
# File 'lib/bailiff/core.rb', line 22 def before(&block) run block, :call end |
#context ⇒ Object
50 51 52 |
# File 'lib/bailiff/core.rb', line 50 def context @context || self end |
#dependency(*instruments) ⇒ Object
34 35 36 37 |
# File 'lib/bailiff/core.rb', line 34 def dependency(*instruments) @dependencies ||= [] @dependencies += instruments end |
#execute(options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bailiff/core.rb', line 54 def execute( = {}) resolve.each do |command| case command[:type] when :command if [:ssh] puts [:ssh].exec!(command[:command]) else puts `#{command[:command]}` end when :write if [:ssh] file = Tempfile.new("bailiff") file.write(command[:data]) file.close [:ssh].scp.upload!(file.path, command[:path]) file.unlink else File.open(command[:path], "w") do |file| file.write(command[:data]) end end when :call out = command[:command].call puts out if out.kind_of?(String) end end end |
#has_package(package) ⇒ Object
30 31 32 |
# File 'lib/bailiff/core.rb', line 30 def has_package(package) run has_apt(package) end |
#instrument(name, context = self, &block) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/bailiff/core.rb', line 39 def instrument(name, context = self, &block) raise Exception.new("Can not define instrument in instrument") if context.class == Instrument instrument = Instrument.new(context, &block) @instruments ||= {} @instruments[name] = instrument end |
#package(*packages) ⇒ Object
17 18 19 20 |
# File 'lib/bailiff/core.rb', line 17 def package(*packages) cmd = apt(*packages) run cmd end |
#resolve(resolve_commands = true) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bailiff/core.rb', line 82 def resolve(resolve_commands = true) done = [] commands = [] dependencies = [] @dependencies.each do |dependency| dep = context.instruments[dependency] if dep.dependencies dependencies += dep.resolve(false) end dependencies << dependency end if @dependencies return dependencies unless resolve_commands dependencies.each do |dependency| unless done.include?(dependency) dep = context.instruments[dependency] cmds = dep.commands if dep commands += cmds if cmds done << dependency end end if @commands commands += @commands end return commands end |
#run(command, type = :command) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/bailiff/core.rb', line 6 def run(command, type = :command) @commands ||= [] cmd = { :type => type } if command.kind_of?(Hash) cmd.merge!(command) else cmd[:command] = command end @commands << cmd end |
#write_file(path, data) ⇒ Object
46 47 48 |
# File 'lib/bailiff/core.rb', line 46 def write_file(path, data) run({ :path => path, :data => data }, :write) end |