Class: Kitchen::Driver::DockerCli

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/driver/docker_cli.rb

Overview

Docker CLI driver for Kitchen.

Author:

Instance Method Summary collapse

Instance Method Details

#build(state) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/kitchen/driver/docker_cli.rb', line 75

def build(state)
  output = ""
  instance.transport.connection(state) do |conn|
    output = conn.run_docker(docker_build_command, :input => docker_file)
  end
  parse_image_id(output)
end

#create(state) ⇒ Object



60
61
62
63
# File 'lib/kitchen/driver/docker_cli.rb', line 60

def create(state)
  state[:image] = build(state) unless state[:image]
  state[:container_id] = run(state) unless state[:container_id]
end

#default_imageObject



48
49
50
51
52
53
54
# File 'lib/kitchen/driver/docker_cli.rb', line 48

def default_image
  platform, version = instance.platform.name.split('-')
  if platform == 'centos' && version
    version = "centos#{version.split('.').first}"
  end
  version ? [platform, version].join(':') : platform
end

#default_platformObject



56
57
58
# File 'lib/kitchen/driver/docker_cli.rb', line 56

def default_platform
  instance.platform.name.split('-').first
end

#destroy(state) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/kitchen/driver/docker_cli.rb', line 65

def destroy(state)
  instance.transport.connection(state) do |conn|
    begin
      conn.run_docker("rm -f #{state[:container_id]}") if state[:container_id]
    rescue => e
      raise e unless conn.send(:options)[:lxc_driver]
    end
  end
end

#docker_build_commandObject



91
92
93
94
95
96
97
98
99
# File 'lib/kitchen/driver/docker_cli.rb', line 91

def docker_build_command
  cmd = String.new('build')
  cmd << ' --no-cache' if config[:no_cache]
  if config[:build_context]
    cmd << ' .'
  else
    cmd << ' -'
  end
end

#docker_fileObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/kitchen/driver/docker_cli.rb', line 138

def docker_file
  if config[:dockerfile]
    file = ::Kitchen::DockerCli::DockerfileTemplate.new(
      config[:dockerfile_vars],
      config.to_hash
    ).result
  else
    file = ["FROM #{config[:image]}"]
    unless config[:skip_preparation]
      case config[:platform]
      when 'debian', 'ubuntu'
        file << 'RUN apt-get update'
        file << 'RUN apt-get -y install sudo curl tar'
      when 'rhel', 'centos', 'fedora'
        file << 'RUN yum clean all'
        file << 'RUN yum -y install sudo curl tar'
        file << 'RUN echo "Defaults:root !requiretty" >> /etc/sudoers'
      else
        # TODO: Support other distribution
      end
    end
    Array(config[:environment]).each { |env, value| file << "ENV #{env}=#{value}" }
    Array(config[:run_command]).each { |cmd| file << "RUN #{cmd}" }
    file.join("\n")
  end
end

#docker_run_command(image) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kitchen/driver/docker_cli.rb', line 101

def docker_run_command(image)
  cmd = String.new("run -d -t")
  cmd << " --name #{config[:container_name]}" if config[:container_name]
  cmd << ' -P' if config[:publish_all]
  cmd << " -m #{config[:memory_limit]}" if config[:memory_limit]
  cmd << " -c #{config[:cpu_shares]}" if config[:cpu_shares]
  cmd << ' --privileged' if config[:privileged]
  cmd << " --net #{config[:network]}" if config[:network]
  if config[:hostname]
    cmd << " -h #{config[:hostname]}"
  elsif config[:instance_host_name]
    cmd << " -h #{instance.name}"
  end
  Array(config[:publish]).each { |pub| cmd << " -p #{pub}" }
  Array(config[:volume]).each { |vol| cmd << " -v #{vol}" }
  Array(config[:volumes_from]).each { |vf| cmd << " --volumes-from #{vf}" }
  Array(config[:link]).each { |link| cmd << " --link #{link}" }
  Array(config[:expose]).each { |exp| cmd << " --expose #{exp}" }
  Array(config[:dns]).each {|dns| cmd << " --dns #{dns}"}
  Array(config[:add_host]).each {|mapping| cmd << " --add-host #{mapping}"}
  cmd << " #{image} #{config[:command]}"
end

#parse_container_id(output) ⇒ Object



131
132
133
134
135
136
# File 'lib/kitchen/driver/docker_cli.rb', line 131

def parse_container_id(output)
  unless output.chomp.match(/([0-9a-z]{64})$/)
    raise ActionFailed, 'Could not parse CONTAINER ID.'
  end
  $1
end

#parse_image_id(output) ⇒ Object



124
125
126
127
128
129
# File 'lib/kitchen/driver/docker_cli.rb', line 124

def parse_image_id(output)
  unless output.chomp.match(/Successfully built ([0-9a-z]{12})$/)
    raise ActionFailed, 'Could not parse IMAGE ID.'
  end
  $1
end

#run(state) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/kitchen/driver/docker_cli.rb', line 83

def run(state)
  output = ""
  instance.transport.connection(state) do |conn|
    output = conn.run_docker(docker_run_command(state[:image]))
  end
  parse_container_id(output)
end