Class: Wire::Resource::FigAdapter
- Inherits:
-
ResourceBase
- Object
- ResourceBase
- Wire::Resource::FigAdapter
- Defined in:
- lib/wire/resource/fig_adapter.rb
Overview
Fig Adapter resource, runs fig script to control containers
Instance Attribute Summary collapse
-
#executables ⇒ Object
executables[Hash] of binaries needed to control the resource. -
#figfile ⇒ Object
executables[Hash] of binaries needed to control the resource.
Attributes inherited from ResourceBase
Instance Method Summary collapse
-
#down ⇒ Object
takes containers down.
-
#down? ⇒ Boolean
checks if the bridge is down.
-
#exist? ⇒ Boolean
checks if containers exist.
-
#initialize(name, figfile) ⇒ FigAdapter
constructor
initialize the object with given
nameand path to fig file params: - name fig filename name, i.e. -
#to_s ⇒ Object
Returns a string representation.
-
#up ⇒ Object
brings containers up.
-
#up? ⇒ Boolean
checks if containers are up (using fig ps).
-
#up_ids ⇒ Object
returns the container ids of currently running containers as [Array] of ids.
-
#with_fig(command_arr) ⇒ Object
calls fig with correct -p and -f options and given
command_arrarray.
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
#executables ⇒ Object
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 |
#figfile ⇒ Object
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
#down ⇒ Object
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
100 101 102 |
# File 'lib/wire/resource/fig_adapter.rb', line 100 def down? !(up?) end |
#exist? ⇒ Boolean
checks if containers exist
35 36 37 |
# File 'lib/wire/resource/fig_adapter.rb', line 35 def exist? up? end |
#to_s ⇒ Object
Returns a string representation
122 123 124 |
# File 'lib/wire/resource/fig_adapter.rb', line 122 def to_s "FigAdapter:[#{name},file=#{figfile}]" end |
#up ⇒ Object
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)
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_ids ⇒ Object
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 |