forked from Bioconductor/bioc_docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
204 lines (182 loc) · 7.6 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require 'erb'
require 'ostruct'
require 'yaml'
require 'fileutils'
require 'pp'
require 'open3'
require 'rake'
require 'rake/clean'
class ErbBinding < OpenStruct
def get_binding
return binding()
end
end
CONFIG = YAML.load_file "config.yml"
SEP = File::SEPARATOR
REPO = 'bioconductor' # That's the username in the URL https://hub.docker.com/r/bioconductor/release_base/
#REPO = 'sneumann' # That's the username in the URL https://hub.docker.com/r/bioconductor/release_base/
@docker_setup = nil
def setup_docker
return unless @docker_setup.nil?
require 'docker'
# increase read and write timeouts to 2 hours
timeout = (60 * 60) * 2
Excon.defaults[:write_timeout] = timeout
Excon.defaults[:read_timeout] = timeout
if defined? ENV['DOCKER_HOST']
puts "setting docker url"
Docker.url = ENV['DOCKER_HOST']
end
auth = YAML.load_file('auth.yml')
Docker.authenticate!(auth)
@docker_setup = true
end
alldeps = []
mkimg_deps = []
for version_name in CONFIG['versions'].keys
version_hash= CONFIG['versions'][version_name]
for container_name in CONFIG['containers'].keys
taskname = version_name + "_" + container_name
mkimg_deps << taskname
container_hash = CONFIG['containers'][container_name]
parent = container_hash['parent']
parent = container_hash['parent'].sub("bioconductor/", "#{REPO}/#{version_name}_")
if parent =~ /bogus/
parent = version_hash['parent']
end
ptasks = []
if parent.start_with? "#{REPO}/"
ptasks << parent.sub("#{REPO}/", "")
end
task taskname => ptasks do |t|
puts "running task #{t.name}..."
setup_docker
today = Time.now.strftime "%Y%m%d"
puts "checking for existing image #{t.name}:#{today}...."
images = Docker::Image.all
existing = images.find { |i| (i.info['RepoTags'] || []).include? "#{REPO}/#{t.name}:#{today}" }
unless existing.nil?
puts "found an existing image with id #{existing.id}..."
end
puts "building #{t.name} from Dockerfile in out#{SEP}#{t.name}..."
image = Docker::Image.build_from_dir("out" + SEP + t.name) do |ch|
puts ch
end
if (!existing.nil?) and existing.id.start_with? image.id
puts "built id matches pre-existing id, skipping tag and push steps..."
next # exit the block
end
if t.name.start_with? "release"
version_number = CONFIG['versions']['release']['version_number']
else
version_number = CONFIG['versions']['devel']['version_number']
end
wanted_tags = ['latest', today, version_number]
wanted_tags.each do |tag|
puts "tagging #{t.name} with tag #{tag}..."
image.tag("repo" => "#{REPO}/" + t.name, "tag" => tag, "force" => true)
end
puts "image tags are: #{image.info['RepoTags'].join(", ")}"
puts "pushing #{t.name}..."
# see if this fixes pushing issues:
# image_to_push = images.find{|i| i.info['RepoTags'].include? "#{REPO}/#{t.name}:latest"}
# image_to_push.push()
# use docker executable instead of api
# because api pushes seem flaky
cmd = "docker push #{REPO}/#{t.name}"
Open3.popen2e(cmd) do |stdin, stdout_err, wait_thr|
while line = stdout_err.gets
puts line
end
exit_status = wait_thr.value
unless exit_status.success?
abort "FAILED !!! '#{cmd}' returned exit code #{exit_status.exitstatus}"
end
end
puts "push done!"
# confirm that image in repo is properly tagged
output = `docker run --rm rufus/docker-registry-debug -q info #{REPO}/#{t.name}`
lines = output.split("\n")
found_tags = []
puts "inspecting tags in image on hub..."
for line in lines
next if line.start_with? '-'
tag = line.strip.split(" ").first
puts "found tag #{tag}..."
found_tags << tag
end
# if wanted_tags.sort == found_tags.sort
# puts "tags matched!"
# else
# puts "tags did not match, retrying"
# end
end
srcdir = "src" + SEP + container_name
destdir = "out" + SEP + version_name + "_" + container_name
copyfiles = Rake::FileList.new(srcdir + SEP + "**" + SEP + "*") do |fl|
fl.exclude(/\.in$/)
fl.exclude do |f|
File.directory? f
end
end
infiles = Rake::FileList[srcdir + SEP + "**" + SEP + "*.in"]
deps = []
for inputfile in copyfiles + infiles
destfile = inputfile.sub(/#{srcdir}|common/, destdir)
destfile = destfile.sub(/\.in$/, "") if inputfile.end_with? ".in"
deps << destfile
alldeps << destfile
CLOBBER.include(destfile)
file destfile => inputfile do |t|
source = (t.source.nil?) ? t.prerequisites.first : t.source
if source.end_with? ".in"
puts "building #{t.name} from #{source}..."
# in here you can't refer to anything outside, only 't'
vcont_name = t.name.split(SEP)[1]
version, cont_name = vcont_name.split('_')
vhash = CONFIG.dup['versions'][version]
# make a read/only copy to get around some weirdness
rodata = CONFIG.dup['containers'][cont_name]
data = rodata.dup
if data['parent'].start_with? "bioconductor/"
data['parent'] = rodata['parent'].sub("bioconductor/", "#{REPO}/#{version}_")
end
if data['parent'] == 'bogus'
data['parent'] = vhash['parent']
end
data['is_devel'] = (version == 'devel')
data['use_r_devel'] = vhash['use_r_devel']
if vhash.has_key? 'use_numbered_branch'
data['use_numbered_branch'] = vhash['use_numbered_branch']
end
data['image_name'] = "#{REPO}/#{vcont_name}"
data['r_url'] = vhash['r_url']
data['bioc_version'] = vhash['version_number']
vars = ErbBinding.new(data)
erb = ERB.new(File.new(source).read, nil, "%")
vars_binding = vars.send(:get_binding)
destfold = File.dirname(t.name)
mkdir_p destfold
out_fh = File.open(t.name, "w")
out_fh.write(erb.result(vars_binding))
out_fh.close()
else
destfold = File.dirname(t.name)
mkdir_p destfold
cp source, t.name, :preserve => true
end
end
end
desc "merge metadata values into templates (default)"
task :build_files => alldeps
end
end
task :default => :build_files
desc "(re)build all containers that require it"
task build_all_containers: mkimg_deps
# be careful using the following...it could theoretically
# try and build 8 containers at once (all leaf containers
# for release and devel) which could use a lot of memory
# and other resources.
desc "in parallel, (re)build all containers that require it"
multitask parallel_build_all_containers: mkimg_deps