Class: DockerSystem
Overview
Copyright © 2013-2015 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
-
#image ⇒ Object
Returns the value of attribute image.
Attributes inherited from System
Instance Method Summary collapse
- #check_create_archive_dependencies ⇒ Object
- #check_retrieve_files_dependencies ⇒ Object
-
#create_archive(file_list, archive, exclude = []) ⇒ Object
Retrieves files specified in file_list from the container and creates an archive.
-
#initialize(image) ⇒ DockerSystem
constructor
A new instance of DockerSystem.
-
#inject_file(source, destination) ⇒ Object
Copies a file to the system.
-
#read_file(file) ⇒ Object
Reads a file from the System.
-
#remove_file(file) ⇒ Object
Removes a file from the System.
- #requires_root? ⇒ Boolean
-
#retrieve_files(file_list, destination) ⇒ Object
Retrieves files specified in file_list from the container.
- #run_command(*args) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #type ⇒ Object
Methods inherited from System
#arch, #check_requirement, for, #has_command?, #rpm_database, #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
#image ⇒ Object
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_dependencies ⇒ Object
51 52 53 54 |
# File 'lib/docker_system.rb', line 51 def check_create_archive_dependencies # Archives are created using the machinery-helper binary, so there are no additional # dependencies end |
#check_retrieve_files_dependencies ⇒ Object
47 48 49 |
# File 'lib/docker_system.rb', line 47 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/docker_system.rb', line 94 def create_archive(file_list, archive, exclude = []) created = !File.exists?(archive) out = File.open(archive, "w") begin run_command( File.join( Machinery::HELPER_REMOTE_PATH, "machinery-helper" ), "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
78 79 80 |
# File 'lib/docker_system.rb', line 78 def inject_file(source, destination) LoggedCheetah.run("docker", "cp", source, "#{@container}:#{destination}") end |
#read_file(file) ⇒ Object
Reads a file from the System. Returns nil if it does not exist.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/docker_system.rb', line 57 def read_file(file) 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
69 70 71 72 73 74 75 |
# File 'lib/docker_system.rb', line 69 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
120 121 122 |
# File 'lib/docker_system.rb', line 120 def requires_root? false end |
#retrieve_files(file_list, destination) ⇒ Object
Retrieves files specified in file_list from the container
83 84 85 86 87 88 89 90 91 |
# File 'lib/docker_system.rb', line 83 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) LoggedCheetah.run("docker", "cp", "#{@container}:#{file}", "#{destination_path}") LoggedCheetah.run("chmod", "go-rwx", destination_path) end end |
#run_command(*args) ⇒ Object
42 43 44 45 |
# File 'lib/docker_system.rb', line 42 def run_command(*args) Machinery.logger.info("Running '#{args}'") LoggedCheetah.run("docker", "exec", "--user=root", "-i", @container, *args) end |
#start ⇒ Object
31 32 33 34 35 36 |
# File 'lib/docker_system.rb', line 31 def start @container = 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 |
#stop ⇒ Object
38 39 40 |
# File 'lib/docker_system.rb', line 38 def stop LoggedCheetah.run("docker", "rm", "-f", @container) if @container end |
#type ⇒ Object
21 22 23 |
# File 'lib/docker_system.rb', line 21 def type "docker" end |