Class: Wire::Resource::FigAdapter

Inherits:
ResourceBase show all
Defined in:
lib/wire/resource/fig_adapter.rb

Overview

Fig Adapter resource, runs fig script to control containers

Instance Attribute Summary collapse

Attributes inherited from ResourceBase

#name

Instance Method Summary collapse

Constructor Details

#initialize(name, figfile) ⇒ FigAdapter

initialize the object with given name and path to fig file params:

  • name fig filename name, i.e. “backend.yaml”



24
25
26
27
28
29
30
31
32
# File 'lib/wire/resource/fig_adapter.rb', line 24

def initialize(name, figfile)
  super(name.tr('_-', ''))
  @figfile = figfile

  # TODO: make configurable
  @executables = {
    :fig => '/usr/local/bin/fig'
  }
end

Instance Attribute Details

#executablesObject

executables [Hash] of binaries needed to control the resource



18
19
20
# File 'lib/wire/resource/fig_adapter.rb', line 18

def executables
  @executables
end

#figfileObject

executables [Hash] of binaries needed to control the resource



18
19
20
# File 'lib/wire/resource/fig_adapter.rb', line 18

def figfile
  @figfile
end

Instance Method Details

#downObject

takes containers down



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wire/resource/fig_adapter.rb', line 105

def down
  $log.debug 'Taking down fig containers ...'
  with_fig(['stop']) do |exec_obj|
    exec_obj.run

    return false if (exec_obj.exitstatus != 0)
  end
  $log.debug 'Removing fig containers ...'
  with_fig(%w(rm --force)) do |exec_obj|
    exec_obj.run

    return false if (exec_obj.exitstatus != 0)
  end
  true
end

#down?Boolean

checks if the bridge is down

Returns:

  • (Boolean)


100
101
102
# File 'lib/wire/resource/fig_adapter.rb', line 100

def down?
  !(up?)
end

#exist?Boolean

checks if containers exist

Returns:

  • (Boolean)


35
36
37
# File 'lib/wire/resource/fig_adapter.rb', line 35

def exist?
  up?
end

#to_sObject

Returns a string representation



122
123
124
# File 'lib/wire/resource/fig_adapter.rb', line 122

def to_s
  "FigAdapter:[#{name},file=#{figfile}]"
end

#upObject

brings containers up



90
91
92
93
94
95
96
97
# File 'lib/wire/resource/fig_adapter.rb', line 90

def up
  $log.debug 'Bringing up fig containers ...'
  with_fig(%w(up -d --no-recreate)) do |exec_obj|
    exec_obj.run

    return (exec_obj.exitstatus == 0)
  end
end

#up?Boolean

checks if containers are up (using fig ps)

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wire/resource/fig_adapter.rb', line 49

def up?
  with_fig(%w(ps)) do |exec_obj|
    exec_obj.run

    # parse stdout..
    re = Regexp.new "^#{@name}.*Up.*"
    num_up = 0
    exec_obj.stdout.split("\n").each do |line|
      next unless line.match(re)

      $log.debug "Matching fig ps output: #{line}"

      num_up += 1
    end
    $log.debug 'No containers found in fig ps output' if num_up == 0

    return (exec_obj.exitstatus == 0 && num_up > 0)
  end
end

#up_idsObject

returns the container ids of currently running containers as [Array] of ids



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wire/resource/fig_adapter.rb', line 71

def up_ids
  with_fig(%w(ps -q)) do |exec_obj|
    exec_obj.run

    # parse stdout..
    re = Regexp.new '^[0-9a-zA-Z]+'
    res = []

    exec_obj.stdout.split("\n").each do |line|
      next unless line.match(re)
      res << line.chomp.strip
    end

    return res
  end
  nil
end

#with_fig(command_arr) ⇒ Object

calls fig with correct -p and -f options and given command_arr array



41
42
43
44
45
46
# File 'lib/wire/resource/fig_adapter.rb', line 41

def with_fig(command_arr)
  LocalExecution.with(@executables[:fig],
                      ['-p', @name, '-f', @figfile, command_arr].flatten) do |exec_obj|
    yield exec_obj
  end
end