Class: CM::Plugin::DockerResource

Inherits:
Object
  • Object
show all
Defined in:
lib/core/plugin/docker_resource.rb

Instance Method Summary collapse

Instance Method Details

#action(provider, operation) ⇒ Object




171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/core/plugin/docker_resource.rb', line 171

def action(provider, operation)
  FileUtils.mkdir_p(host_input_directory)
  FileUtils.mkdir_p(host_output_directory)

  action_settings = Nucleon::Util::Data.clean(plan.action_settings)
  initialize_remote_config(action_settings)

  encoded_config = Nucleon::Util::CLI.encode(action_settings)
  action_config  = extended_config(:action, {
    :command => 'resource run',
    :data    => { :encoded  => encoded_config },
    :args    => [ provider, operation ]
  })
  action_config[:data][:log_level] = Nucleon.log_level if Nucleon.log_level

  data = command('cm', Nucleon::Util::Data.clean({
    :subcommand => action_config,
    :quiet      => Nucleon::Util::Console.quiet
  })) do |stream, message|
    yield(stream, message) if block_given?
  end

  FileUtils.rm_rf(host_input_directory)
  FileUtils.rm_rf(host_output_directory)
  data
end

#command(command, options = {}) ⇒ Object




152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/core/plugin/docker_resource.rb', line 152

def command(command, options = {})
  config         = Nucleon::Config.ensure(options)
  remove_command = false

  unless command.is_a?(Nucleon::Plugin::Command)
    command        = Nucleon.command(Nucleon::Config.new({ :command => command }, {}, true, false).import(config), :bash)
    remove_command = true
  end

  data = exec(command.to_s.strip) do |stream, message|
    yield(stream, message) if block_given?
  end

  Nucleon.remove_plugin(command) if remove_command
  data
end

#containerObject




68
69
70
# File 'lib/core/plugin/docker_resource.rb', line 68

def container
  @container
end

#exec(command) ⇒ Object


Docker resource operation execution



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/core/plugin/docker_resource.rb', line 124

def exec(command)
  data = nil

  create_container

  results = container.exec(['bash', '-l', '-c', command]) do |stream, message|
    unless message.match(/stdin\:\s+is not a tty/)
      render_docker_message(stream, message)
      yield(stream, message) if block_given?
    end
  end

  if results[2] == 0
    if output_config = CM.configuration(extended_config(:resource_results, {
      :provider => get(:resource_result_provider, :directory),
      :path => host_output_directory
    }))
      data = Nucleon::Util::Data.clone(output_config.export)
      Nucleon.remove_plugin(output_config)
    end
  end

  destroy_container
  data
end

#host_input_directoryObject




84
85
86
# File 'lib/core/plugin/docker_resource.rb', line 84

def host_input_directory
  get(:host_input_directory, "/tmp/cm/input/#{plugin_instance_name}")
end

#host_output_directoryObject




94
95
96
# File 'lib/core/plugin/docker_resource.rb', line 94

def host_output_directory
  get(:host_output_directory, "/tmp/cm/output/#{plugin_instance_name}")
end

#imageObject


Property accessors / modifiers



56
57
58
# File 'lib/core/plugin/docker_resource.rb', line 56

def image
  get(:image, 'awebb/cm').to_s
end

#initialized?(options = {}) ⇒ Boolean


Checks

Returns:

  • (Boolean)


43
44
45
# File 'lib/core/plugin/docker_resource.rb', line 43

def initialized?(options = {})
  true
end

#input_directoryObject



88
89
90
# File 'lib/core/plugin/docker_resource.rb', line 88

def input_directory
  get(:input_directory, '/opt/cm/volumes/input')
end

#internal?Boolean


Returns:

  • (Boolean)


49
50
51
# File 'lib/core/plugin/docker_resource.rb', line 49

def internal?
  File.exist?('/.dockerinit')
end

#key_directoryObject



78
79
80
# File 'lib/core/plugin/docker_resource.rb', line 78

def key_directory
  get(:key_directory, '/opt/cm/volumes/keys')
end

#normalize(reload) ⇒ Object


Plugin interface



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/core/plugin/docker_resource.rb', line 13

def normalize(reload)
  require 'docker'
  super

  codes :docker_exec_failed

  settings[:docker_protocol] ||= 'unix'
  settings[:docker_sock] ||= '/var/run/docker.sock'
  settings[:docker_host] ||= nil
  settings[:docker_port] ||= '127.0.0.1'
  settings[:docker_image] ||= 'awebb/cm'

  if settings[:docker_host].nil?
    Docker.url = "#{settings[:docker_protocol]}://#{settings[:docker_sock]}"
  else
    Docker.url = "#{settings[:docker_protocol]}://#{settings[:docker_host]}:#{settings[:docker_port]}"
  end

  yield if block_given?
end

#operation_deployObject


Operations



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

def operation_deploy
  super do
    data = nil

    # A fork in the road!
    if internal?
      data = yield if block_given?
    else
      data = action(plugin_provider, :deploy)
      myself.status = code.docker_exec_failed unless data
    end
    myself.data = data
    myself.status == code.success
  end
end

#output_directoryObject



98
99
100
# File 'lib/core/plugin/docker_resource.rb', line 98

def output_directory
  get(:output_directory, '/opt/cm/volumes/output')
end

#plan_directoryObject




74
75
76
# File 'lib/core/plugin/docker_resource.rb', line 74

def plan_directory
  get(:plan_directory, '/opt/cm/volumes/plan')
end

#remove_pluginObject




36
37
38
# File 'lib/core/plugin/docker_resource.rb', line 36

def remove_plugin
  destroy_container(plugin_instance_name)
end

#render_docker_message(stream, message) ⇒ Object




306
307
308
309
310
311
312
# File 'lib/core/plugin/docker_resource.rb', line 306

def render_docker_message(stream, message)
  if stream == 'stderr'
    puts message
  else
    puts message
  end
end

#startup_commandsObject




62
63
64
# File 'lib/core/plugin/docker_resource.rb', line 62

def startup_commands
  get(:startup_commands, ['bash'])
end