Module: Bailiff::Core

Included in:
Context
Defined in:
lib/bailiff/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



4
5
6
# File 'lib/bailiff/core.rb', line 4

def commands
  @commands
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



4
5
6
# File 'lib/bailiff/core.rb', line 4

def dependencies
  @dependencies
end

#instrumentsObject (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

#contextObject



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(options = {})
    resolve.each do |command|
      case command[:type]
        when :command
          if options[:ssh]
            puts options[:ssh].exec!(command[:command])
          else
            puts `#{command[:command]}`
          end
        when :write
          if options[:ssh]
            file = Tempfile.new("bailiff")
            file.write(command[:data])
            file.close
            options[: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

Raises:

  • (Exception)


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