Class: CM::Plugin::DockerResource

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.options(action) ⇒ Object




12
13
14
15
# File 'lib/core/plugin/docker_resource.rb', line 12

def self.options(action)
  action.register_bool :docker, true, 'cm.action.docker_resource.options.docker'
  action.register_bool :keep_alive, false, 'cm.action.docker_resource.options.keep_alive'
end

Instance Method Details

#action(provider, operation) ⇒ Object




219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/core/plugin/docker_resource.rb', line 219

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)

  command('cm', Nucleon::Util::Data.clean({
    :subcommand => extended_config(:action, {
      :command => 'resource run',
      :data => { :encoded  => Nucleon::Util::CLI.encode(action_settings) },
      :args => [ provider, operation ]
    }),
    :quiet => Nucleon::Util::Console.quiet
  })) do |stream, message|
    yield(stream, message) if block_given?
  end
ensure
  unless keep_alive?
    FileUtils.rm_rf(host_input_directory)
    FileUtils.rm_rf(host_output_directory)
  end
end

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




208
209
210
211
212
213
214
215
# File 'lib/core/plugin/docker_resource.rb', line 208

def command(command, options = {})
  command = Nucleon.command(Nucleon::Config.new({ :command => command }, {}, true, false).import(options), :bash)
  Nucleon.remove_plugin(command)

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

#containerObject




92
93
94
# File 'lib/core/plugin/docker_resource.rb', line 92

def container
  @container
end

#docker_idObject


Property accessors / modifiers



76
77
78
# File 'lib/core/plugin/docker_resource.rb', line 76

def docker_id
  @docker_id ||= "#{id}-#{Time.now.strftime('%Y-%m-%dT%H-%M-%S%z')}"
end

#dockerize?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/core/plugin/docker_resource.rb', line 65

def dockerize?
  action_settings[:docker] || settings[:docker]
end

#exec(command) ⇒ Object


Docker resource operation execution



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/core/plugin/docker_resource.rb', line 180

def exec(command)
  data = {}

  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, :file),
      :path => "#{host_output_directory}/config.json"
    }))
      data = Nucleon::Util::Data.clone(output_config.export)
      Nucleon.remove_plugin(output_config)
    end
  end
  data
ensure
  destroy_container
end

#host_input_directoryObject




108
109
110
# File 'lib/core/plugin/docker_resource.rb', line 108

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

#host_output_directoryObject




118
119
120
# File 'lib/core/plugin/docker_resource.rb', line 118

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

#imageObject



80
81
82
# File 'lib/core/plugin/docker_resource.rb', line 80

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

#initialized?(options = {}) ⇒ Boolean


Checks

Returns:

  • (Boolean)


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

def initialized?(options = {})
  true
end

#input_directoryObject



112
113
114
# File 'lib/core/plugin/docker_resource.rb', line 112

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

#internal?Boolean


Returns:

  • (Boolean)


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

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

#keep_alive?Boolean

Returns:

  • (Boolean)


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

def keep_alive?
  action_settings[:keep_alive] || settings[:keep_alive]
end

#key_directoryObject



102
103
104
# File 'lib/core/plugin/docker_resource.rb', line 102

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

#normalize(reload) ⇒ Object


Plugin interface



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/core/plugin/docker_resource.rb', line 20

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

  Docker.options[:write_timeout] = timeout
  Excon.defaults[:write_timeout] = timeout
  Docker.options[:read_timeout] = timeout
  Excon.defaults[:read_timeout] = timeout

  yield if block_given?
end

#operation_deployObject


Operations



129
130
131
132
133
134
135
# File 'lib/core/plugin/docker_resource.rb', line 129

def operation_deploy
  operation_run(:deploy) do
    data = super
    yield if block_given?
    data
  end
end

#operation_destroyObject



137
138
139
140
141
142
143
# File 'lib/core/plugin/docker_resource.rb', line 137

def operation_destroy
  operation_run(:destroy) do
    data = super
    yield if block_given?
    data
  end
end

#operation_run(operation) ⇒ Object




147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/core/plugin/docker_resource.rb', line 147

def operation_run(operation)
  data = {}

  # A fork in the road!
  if !dockerize? || internal?
    data = yield if block_given?

    if dockerize?
      FileUtils.mkdir_p(output_directory)

      output_config = CM.configuration(extended_config(:resource_results, {
        :provider => get(:resource_output_provider, :file),
        :path => "#{output_directory}/config.json"
      }))
      output_config.import(Nucleon::Config.ensure(data).export)
      output_config.save
      Nucleon.remove_plugin(output_config)
    end

    logger.info("Docker internal data: #{hash(data)}")
  else
    info('cm.resource.docker_resource.info.run_dockerized', { :image => Nucleon.yellow(image), :id => Nucleon.green(id), :op => operation, :time => Nucleon.purple(Time.now.to_s) })
    logger.info("Running #{operation} operation on #{plugin_provider} resource")

    data = action(plugin_provider, operation)
    logger.info("Docker return data: #{hash(data)}")
  end
  data
end

#output_directoryObject



122
123
124
# File 'lib/core/plugin/docker_resource.rb', line 122

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

#plan_directoryObject




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

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

#remove_pluginObject




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

def remove_plugin
  destroy_container
end

#startup_commandsObject




86
87
88
# File 'lib/core/plugin/docker_resource.rb', line 86

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