class DockerFlow::Utils

Public Class Methods

get_branch() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/utils.rb, line 36
def self.is_ci
  return (ENV.include? 'CI' and ENV['CI'] == 'true')
end
print_br() click to toggle source
put_build_information(info) click to toggle source
# 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) click to toggle source
# File lib/utils.rb, line 54
def self.put_project_title(title)
  puts " # Project: #{title}"
end
put_system_info() click to toggle source
# 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

Private Class Methods

clean_dirs(dirs) click to toggle source
# 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) click to toggle source
# 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
system_command(command) click to toggle source
# 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