Class: Machinery::DockerSystem

Inherits:
System show all
Defined in:
lib/docker_system.rb

Overview

Copyright © 2013-2016 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Instance Attribute Summary collapse

Attributes inherited from System

#locale

Instance Method Summary collapse

Methods inherited from System

#arch, #check_requirement, for, #has_command?, #managed_files_database, #run_command_with_progress, #run_script, #run_script_with_progress

Constructor Details

#initialize(image) ⇒ DockerSystem

Returns a new instance of DockerSystem.



25
26
27
28
29
# File 'lib/docker_system.rb', line 25

def initialize(image)
  @image = image

  validate_image_name(image)
end

Instance Attribute Details

#imageObject

Returns the value of attribute image.



19
20
21
# File 'lib/docker_system.rb', line 19

def image
  @image
end

Instance Method Details

#check_create_archive_dependenciesObject



53
54
55
56
# File 'lib/docker_system.rb', line 53

def check_create_archive_dependencies
  # Archives are created using the machinery-helper binary, so there are no additional
  # dependencies
end

#check_retrieve_files_dependenciesObject



49
50
51
# File 'lib/docker_system.rb', line 49

def check_retrieve_files_dependencies
  # Files are retrieved using the `docker cp` command, so there are no additional dependencies
end

#create_archive(file_list, archive, exclude = []) ⇒ Object

Retrieves files specified in file_list from the container and creates an archive.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/docker_system.rb', line 98

def create_archive(file_list, archive, exclude = [])
  created = !File.exist?(archive)
  out = File.open(archive, "w")
  begin
    helper = MachineryHelper.new(self)
    helper.run_helper_subcommand(
      "tar", "--create", "--gzip", "--null", "--files-from=-",
      *exclude.flat_map { |f| ["--exclude", f] },
      stdout: out,
      stdin: Array(file_list).join("\0"),
      stderr: STDERR
    )
  rescue Cheetah::ExecutionFailed => e
    if e.status.exitstatus == 1
      # The tarball has been created successfully but some files were changed
      # on disk while being archived, so we just log the warning and go on
      Machinery.logger.info e.stderr
    else
      raise
    end
  end
  out.close
  File.chmod(0600, archive) if created
end

#inject_file(source, destination) ⇒ Object

Copies a file to the system



80
81
82
# File 'lib/docker_system.rb', line 80

def inject_file(source, destination)
  Machinery::LoggedCheetah.run("docker", "cp", source, "#{@container}:#{destination}")
end

#read_file(file, _options = {}) ⇒ Object

Reads a file from the System. Returns nil if it does not exist.



59
60
61
62
63
64
65
66
67
68
# File 'lib/docker_system.rb', line 59

def read_file(file, _options = {})
  run_command("cat", file, stdout: :capture)
rescue Cheetah::ExecutionFailed => e
  if e.status.exitstatus == 1
    # File not found, return nil
    return
  else
    raise
  end
end

#remove_file(file) ⇒ Object

Removes a file from the System



71
72
73
74
75
76
77
# File 'lib/docker_system.rb', line 71

def remove_file(file)
  run_command("rm", file)
rescue => e
  raise Machinery::Errors::RemoveFileFailed.new(
    "Could not remove file '#{file}'.\nError: #{e}"
  )
end

#requires_root?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/docker_system.rb', line 123

def requires_root?
  false
end

#retrieve_files(file_list, destination) ⇒ Object

Retrieves files specified in file_list from the container



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/docker_system.rb', line 85

def retrieve_files(file_list, destination)
  file_list.each do |file|
    destination_path = File.join(destination, file)
    FileUtils.mkdir_p(File.dirname(destination_path), mode: 0700)

    Machinery::LoggedCheetah.run(
      "docker", "cp", "#{@container}:#{file}", destination_path.to_s
    )
    Machinery::LoggedCheetah.run("chmod", "go-rwx", destination_path)
  end
end

#run_command(*args) ⇒ Object



44
45
46
47
# File 'lib/docker_system.rb', line 44

def run_command(*args)
  Machinery.logger.info("Running '#{args}'")
  Machinery::LoggedCheetah.run("docker", "exec", "--user=root", "-i", @container, *args)
end

#startObject



31
32
33
34
35
36
37
38
# File 'lib/docker_system.rb', line 31

def start
  @container = Machinery::LoggedCheetah.run(
    "docker", "run", "-id", @image, "bash", stdout: :capture
  ).chomp
rescue Cheetah::ExecutionFailed => e
  raise Machinery::Errors::MachineryError, "Container could not be started." \
    " The error message was:\n" + e.stderr
end

#stopObject



40
41
42
# File 'lib/docker_system.rb', line 40

def stop
  Machinery::LoggedCheetah.run("docker", "rm", "-f", @container) if @container
end

#typeObject



21
22
23
# File 'lib/docker_system.rb', line 21

def type
  "docker"
end