Class: DockerSpec

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/docker/spec.rb

Overview

Documentation

Constant Summary collapse

CONTAINER_RUN_WAIT_TIMEOUT =
60
CONFIG_FILE =
'docker_spec.yml'
ROOT_DIR =
'root'
DOCKER_AUTH_FILE =
'~/.docker/config.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



21
22
23
# File 'lib/docker/spec.rb', line 21

def config
  @config
end

#containerObject

Returns the value of attribute container.



21
22
23
# File 'lib/docker/spec.rb', line 21

def container
  @container
end

#test_failedObject

Returns the value of attribute test_failed.



21
22
23
# File 'lib/docker/spec.rb', line 21

def test_failed
  @test_failed
end

Instance Method Details

#build_docker_imageObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/docker/spec.rb', line 143

def build_docker_image
  puts
  # Rebuild the cache filesystem
  build_args = ''
  build_args += ' --no-cache' if @config[:clear_cache]

  # Build the docker image
  build_cmd = "docker build -t #{@config[:image_name]} #{build_args} ."
  status = POpen4.popen4(build_cmd) do |stdout, stderr, _stdin|
    stdout.each { |line| puts line }
    stderr.each { |line| puts line.red }
  end
  if status.exitstatus != 0
    puts("ERROR: #{build_cmd} failed")
    exit 1
  end
end

#build_rootObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/docker/spec.rb', line 122

def build_root
  command = "bash -ec '\nexport WD=$(pwd) \nexport TMPDIR=$(mktemp -d -t docker-spec.XXXXXX)\n\ncp -r root $TMPDIR/root \ncd $TMPDIR\nsudo chown root:root -R root\ncd root \nsudo tar --mtime=\"1970-01-01\" -c -f ../root.tar .\ncd ../\nsudo chown -R `id -u`:`id -g` root.tar\ncp root.tar $WD \ntouch -t 200001010000.00 $WD/root.tar\nsudo rm -rf $TMPDIR\n'\n"
  system command if Dir.exist?(ROOT_DIR)
end

#clean_upObject



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/docker/spec.rb', line 217

def clean_up
  # Keep container running
  @config[:keep_running] = get_config(:keep_running, 'DOCKER_SPEC_KEEP_RUNNING',
                                                 "\nKeep container running? ")
  if @config[:keep_running]
    puts "\nINFO: To connect to a running container: \n\n" \
      "docker exec -ti #{@container.info["Name"][1..-1]} bash"
  else
    delete_container
  end
end

#delete_containerObject



212
213
214
215
# File 'lib/docker/spec.rb', line 212

def delete_container
  @container.kill
  @container.delete
end

#get_config(key, envvar, question) ⇒ Object



229
230
231
232
233
234
# File 'lib/docker/spec.rb', line 229

def get_config(key, envvar, question)
  value = to_boolean(ENV[envvar])
  value = @config[key] if value.nil?
  value = agree(question, 'n') if value.nil?
  value
end

#grab_flockObject



40
41
42
43
44
45
46
47
48
# File 'lib/docker/spec.rb', line 40

def grab_flock
  @config = DockerSpec.instance.config
  @lock = File.join('/tmp', @config[:account] + '-' + @config[:name] + '.lock')
  @f = File.open(@lock, 'w')
  if (not @f.flock(File::LOCK_EX | File::LOCK_NB))
    puts "INFO: Another build is already running #{@lock}"
  end
  @f.flock(File::LOCK_EX)
end

#load_configObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/docker/spec.rb', line 105

def load_config
  File.exist?(CONFIG_FILE) || fail('Could not load docker_spec.yml')
  @config = YAML.load(File.read(CONFIG_FILE)) ||
            fail('docker_spec.yml is not a valid yml file')

  @config[:name] || fail('name is not defined in docker_spec.yml')
  @config[:account] || fail('account is not defined in docker_spec.yml')
  @config[:image_name] = format '%s/%s', @config[:account], @config[:name]
  @config[:build_image] = get_config(:build_image, 'DOCKER_SPEC_BUILD_DOCKER_IMAGE',
                                     'Build docker image? ')
  @config[:build_root] = get_config(:build_root, 'DOCKER_SPEC_BUILD_ROOT',
                                    'Rebuild root filesystem? ') if @config[:build_image]
  @config[:clear_cache] = get_config(:clear_cache, 'DOCKER_SPEC_CLEAR_CACHE',
                                     'Clear docker cache? ') if @config[:build_image]
  @config
end

#pushObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/docker/spec.rb', line 50

def push
  @config[:push_container] = get_config(:push_container, 'DOCKER_SPEC_PUSH_CONTAINER',
                                                   'Push new tag? ')
  if @config[:push_container]

    @config[:tag_db] ||
      fail('tag_db is not defined in docker_spec.yml')

    # Load credentials from config file, or default docker config
    if @config[:registry]
      @config[:registry][:username] ||
        fail('registry->username is not defined in docker_spec.yml')
      @config[:registry][:password] ||
        fail('registry->password is not defined in docker_spec.yml')
      @config[:registry][:email] ||
        fail('registry->email is not defined in docker_spec.yml')
    else
      auth = @config[:auth] || 'https://index.docker.io/v1/'
      @config[:registry] = Hash.new
      docker_auth = JSON.parse(File.read(File.expand_path(DOCKER_AUTH_FILE)))
      auth_base64 = docker_auth['auths'][auth]['auth']
      @config[:registry][:username] = Base64.decode64(auth_base64).split(':').first
      @config[:registry][:password] = Base64.decode64(auth_base64).split(':').last
      @config[:registry][:email] = docker_auth['auths'][auth]['email']
    end

    # Open key value store and get the current tag for this repo
    store = Moneta.new(:YAML, file: File.expand_path(@config[:tag_db]))
    current_tag = store.key?(@config[:image_name]) ? store[@config[:image_name]].to_i : 0
    new_tag = current_tag + 1

    image = Docker::Image.all.detect do |i|
      i.info['RepoTags'].include?(@config[:image_name] + ':latest')
    end

    # Login to docker hub and push latest and new_tag
    Docker.authenticate! username: @config[:registry][:username],
                         password: @config[:registry][:password],
                         email: @config[:registry][:email],
                         serveraddress: @config[:serveraddress] || 'https://index.docker.io'

    image.tag repo: @config[:image_name], tag: new_tag, force: true
    puts "\nINFO: pushing #{@config[:image_name]}:#{new_tag} to DockerHub"
    image.push nil, tag: new_tag

    image.tag repo: @config[:image_name], tag: 'latest', force: true
    puts "INFO: pushing #{@config[:image_name]}:latest to DockerHub"
    image.push nil, tag: 'latest'

    # Store the new tag in the tag_db
    store[@config[:image_name]] = new_tag
    store.close
  end
end

#rspec_configureObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/docker/spec.rb', line 161

def rspec_configure
  set :backend, :docker

  RSpec.configure do |rc|
    rc.fail_fast = true

    rc.after(:each) do |test|
      DockerSpec.instance.test_failed = true if test.exception
    end

    rc.before(:suite) do
      DockerSpec.instance.start_container
    end

    rc.after(:suite) do
      DockerSpec.instance.clean_up
      DockerSpec.instance.push unless DockerSpec.instance.test_failed
    end
  end
  Docker::Spec::docker_tests
end

#runObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/docker/spec.rb', line 29

def run
  @config = nil
  @test_failed = false

  load_config
  grab_flock
  build_root if @config[:build_root]
  build_docker_image if @config[:build_image]
  rspec_configure
end

#start_containerObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/docker/spec.rb', line 183

def start_container
  # Run  the container with options
  opts = {}
  opts['HostConfig'] = { 'NetworkMode' => @config[:network_mode] } \
    unless @config[:network_mode].nil?

  opts['env'] = @config[:env] unless @config[:env].nil?
  opts['Image'] = @config[:image_name]
  @container = Docker::Container.create(opts).start
  Timeout::timeout(10) do
    loop do
      @container.refresh!
      break if @container.info["State"]["Running"]
    end
  end


  # Check the logs, when it stops logging we can assume the container
  # is fully up and running
  log_size_ary = []
  CONTAINER_RUN_WAIT_TIMEOUT.times do
    log_size_ary << @container.logs(stdout: true).size
    break if log_size_ary.last(3).sort.uniq.size == 1 &&
             log_size_ary.last(3).sort.uniq.last > 0
    sleep 1
  end
  set :docker_container, @container.id
end

#to_boolean(str) ⇒ Object



236
237
238
# File 'lib/docker/spec.rb', line 236

def to_boolean(str)
  str == 'true' unless str.nil?
end