Class: DockerFlow::Utils
- Inherits:
-
Object
- Object
- DockerFlow::Utils
- Defined in:
- lib/utils.rb
Class Method Summary collapse
- .clean_dirs(dirs) ⇒ Object
- .clean_images(repository) ⇒ Object
- .get_branch ⇒ Object
- .get_commit_version ⇒ Object
- .get_system_info ⇒ Object
- .is_ci ⇒ Object
- .print_br ⇒ Object
- .put_build_information(info) ⇒ Object
- .put_project_title(title) ⇒ Object
- .put_system_info ⇒ Object
- .system_command(command) ⇒ Object
Class Method Details
.clean_dirs(dirs) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/utils.rb', line 97 def self.clean_dirs(dirs) Docker::Image.create('fromImage' => 'alpine:latest') do |chunk| puts JSON.parse(chunk) end container = Docker::Container.create({ 'Image' => 'alpine:latest', 'Volumes' => { '/app' => {} }, 'Binds' => [ "#{Dir.getwd}:/app" ], 'WorkingDir' => '/app', 'Cmd': [ '/bin/sh', '-c', "rm -rf #{dirs.join(' ')}" ] }) container.tap(&:start).attach { |stream, chunk| puts "#{stream}: #{chunk}" } container.stop container.delete end |
.clean_images(repository) ⇒ Object
120 121 122 123 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 149 150 151 152 153 |
# File 'lib/utils.rb', line 120 def self.clean_images(repository) puts "Cleaning up Docker" # Clean exited containers for container in Docker::Container.all(all: true, filters: { status: ["exited"] }.to_json) puts container.id container.stop container.delete(:force => true) end # Remove dangling images for image in Docker::Image.all(all: true, filters: { dangling: ["true"] }.to_json) puts image.id begin image.remove(:force => true) rescue Docker::Error::NotFoundError # Image already removed end end # Clean tagged images for image in Docker::Image.all # puts image.info["RepoTags"] for repoTag in image.info["RepoTags"] if repoTag.include? repository puts repoTag begin image.remove(:force => true) rescue Docker::Error::NotFoundError # Image already removed end end end end end |
.get_branch ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/utils.rb', line 8 def self.get_branch if self.is_ci if ENV.include? 'GIT_BRANCH' branch = ENV['GIT_BRANCH'].gsub('origin/', '').strip else # Jenkins 2 with Jenkinsfile branch = ENV['BRANCH_NAME'].strip end else branch = self.system_command('git rev-parse --abbrev-ref HEAD').lines.first.strip end # make branch name tag-safe by replacing further slashes with . branch.gsub('/', '.') end |
.get_commit_version ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/utils.rb', line 25 def self.get_commit_version begin version = self.system_command 'git describe --tags' rescue version = self.system_command 'git rev-parse --short HEAD' end version.lines.first.strip end |
.get_system_info ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/utils.rb', line 40 def self.get_system_info system = Ohai::System.new system.load_plugins system.run_plugins(true, %w(cpu platform memory)) data = system.data { :os => "#{data[:platform]} #{data[:os]} #{data[:platform_version]}", :cpu => "#{data[:cpu]['0'][:model_name]}", :ram => "#{(data[:memory][:total].chomp('kB').to_f / 1000 / 1000).round(2)} GB (#{(data[:memory][:free].chomp('kB').to_f / 1000 / 1000).round(2)} GB free)" } end |
.is_ci ⇒ Object
36 37 38 |
# File 'lib/utils.rb', line 36 def self.is_ci return (ENV.include? 'CI' and ENV['CI'] == 'true') end |
.print_br ⇒ Object
75 76 77 |
# File 'lib/utils.rb', line 75 def self.print_br puts '-' * DockerFlow.[:line_length] end |
.put_build_information(info) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/utils.rb', line 58 def self.put_build_information(info) puts ' # Build information' puts " Environment: #{info[:host_type]}" puts " Git branch: #{info[:branch]} @ #{info[:commit_version]}" puts " Container repository: #{info[:repository]}" puts " Build container tag: #{info[:build_container_tag]}" puts " Branch container tag: #{info[:branch_container_tag]}" end |
.put_project_title(title) ⇒ Object
54 55 56 |
# File 'lib/utils.rb', line 54 def self.put_project_title(title) puts " # Project: #{title}" end |
.put_system_info ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/utils.rb', line 67 def self.put_system_info puts ' # System information' info = self.get_system_info puts " OS: #{info[:os]}" puts " CPU: #{info[:cpu]}" puts " RAM: #{info[:ram]}" end |
.system_command(command) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/utils.rb', line 80 def self.system_command(command) output = [] Open3.popen3(command) do |stdin, stdout, stderr, thread| pid = thread.pid output << stdout.readline exit_status = thread.value if exit_status.to_i != 0 raise stderr end end output.join("\n") end |