Class: Shipitron::Server::Docker::Configure

Inherits:
Object
  • Object
show all
Includes:
Metaractor, ConsulKeys
Defined in:
lib/shipitron/server/docker/configure.rb

Instance Method Summary collapse

Methods included from ConsulKeys

#configure_consul_client!, #fetch_key, #fetch_key!, #set_key, #set_key!

Instance Method Details

#callObject



19
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
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shipitron/server/docker/configure.rb', line 19

def call
  username = fetch_scoped_key('docker_user')
  password = fetch_scoped_key('docker_password')

  if username && password
    Logger.info `docker login --username #{username} --password #{password}`
    if $? != 0
      fail_with_error!(message: 'Docker login failed.')
    end
  end

  if registry
    case registry
    when /docker\.io/
      # do nothing
    when /\d+\.dkr\.ecr\.us-east-1\.amazonaws\.com/
      # ECR
      config_file = Pathname.new('/home/shipitron/.docker/config.json')
      config_file.parent.mkpath

      config_hash = {}
      if config_file.file?
        config_file.open('rb') do |file|
          json = file.read
          config_hash = JSON.parse(json) rescue {}
        end
      end

      config_hash['credHelpers'] ||= {}
      config_hash['credHelpers'][registry] = 'ecr-login'

      config_file.open('wb') do |file|
        file.puts(JSON.generate(config_hash))
        file.chmod(0600)
      end
    end
  end
end