Class: DockerBoss::Module::Templates::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_boss/modules/templates.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ Instance

Returns a new instance of Instance.



36
37
38
39
40
# File 'lib/docker_boss/modules/templates.rb', line 36

def initialize(name, config)
  @name = name
  @config = config
  DockerBoss.logger.debug "templates: Instance `#{@name}`: created"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/docker_boss/modules/templates.rb', line 34

def name
  @name
end

Instance Method Details

#do_actionsObject



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/docker_boss/modules/templates.rb', line 56

def do_actions
  err = false

  if @config.has_key? 'action'
    err ||= !system(@config['action'])
  end

  if @config.has_key? 'linked_container' and @config['linked_container'].has_key? 'action'
    args = @config['linked_container']['action'].split(':', 2)
    case args.first
    when 'shell'
      raise ArgumentError, "action `shell` needs at least one more argument" if args.size < 2
      command = ["sh", "-c", args[1]]
      linked_container.exec(command)
    when 'shell_bg'
      raise ArgumentError, "action `shell_bg` needs at least one more argument" if args.size < 2
      command = ["sh", "-c", args[1]]
      linked_container.exec(command, detach: true)
    when 'exec'
      raise ArgumentError, "action `exec` needs at least one more argument" if args.size < 2
      linked_container.exec(Shellwords.split(args[1]))
    when 'exec_bg'
      raise ArgumentError, "action `exec_bg` needs at least one more argument" if args.size < 2
      linked_container.exec(Shellwords.split(args[1]), detach: true)
    when 'restart'
      linked_container.restart
    when 'start'
      linked_container.start
    when 'stop'
      linked_container.stop
    when 'pause'
      linked_container.pause
    when 'unpause'
      linked_container.unpause
    when 'kill'
      if args.size == 2
        linked_container.kill(:signal => args[1])
      else
        linked_container.kill
      end
    else
      raise ArgumentError, "unknown action `#{args.first}`"
    end
  end
end

#do_file(f, containers) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/docker_boss/modules/templates.rb', line 42

def do_file(f, containers)
  tmpl_path = DockerBoss::Helpers.render_erb(f['template'], :container => linked_container.json)
  file_path = DockerBoss::Helpers.render_erb(f['file'], :container => linked_container.json)

  file_contents = DockerBoss::Helpers.render_erb_file(tmpl_path, :containers => containers)

  new_digest = Digest::SHA256.hexdigest file_contents
  old_digest = (f.has_key? 'checksum') ? f['checksum'] : ""
  f['checksum'] = new_digest

  File.write(file_path, file_contents) if new_digest != old_digest
  new_digest != old_digest
end

#has_link?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/docker_boss/modules/templates.rb', line 115

def has_link?
  @config.has_key? 'linked_container'
end

#linked_containerObject



119
120
121
122
123
124
125
# File 'lib/docker_boss/modules/templates.rb', line 119

def linked_container
  if has_link?
    (Docker::Container.all(:all => true).find { |c| c.json['Name'] == "/#{@config['linked_container']['name']}" })
  else
    nil
  end
end

#linked_container_propsObject



127
128
129
# File 'lib/docker_boss/modules/templates.rb', line 127

def linked_container_props
  data = linked_container.json
end

#trigger(containers, trigger_id = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/docker_boss/modules/templates.rb', line 102

def trigger(containers, trigger_id = nil)
  if trigger_id.nil? or
      not has_link? or
      linked_container.id != trigger_id
    # Only do something if the linked container is not also the triggering container
    changed = @config['files'].inject (false) { |changed,f| do_file(f, containers) || changed }
    DockerBoss.logger.info "templates: Instance `#{@name}`: triggered; changed=#{changed}"
    do_actions if changed
  else
    DockerBoss.logger.info "templates: Instance `#{@name}`: ignored event"
  end
end