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
|
# File 'lib/cide/cli.rb', line 46
def build
containers = []
setup_docker
banner 'Config'
build = Build::Config.load_file CONFIG_FILE
exit 1 if build.nil?
export_dir = options.export_dir || File.dirname(build.export_dir)
build.run = options.run if options.run
name = CIDE::Docker.id options.name
tag = "cide/#{name}"
say_status :config, build.inspect
banner 'Build'
if build.use_ssh
unless File.exist?(options.ssh_key)
fail ArgumentError, "SSH key #{options.ssh_key} not found"
end
create_tmp_file SSH_CONFIG_FILE, File.read(SSH_CONFIG_PATH)
create_tmp_file TEMP_SSH_KEY, File.read(build.ssh_key)
end
create_tmp_file DOCKERFILE, build.to_dockerfile
docker :build, '--force-rm', '--pull', '-f', DOCKERFILE, '-t', tag, '.'
banner 'Run'
build.links.each do |link|
args = ['--detach']
link.env.each_pair do |key, value|
args.push('--env', [key, value].join('='))
end
args << link.image
args << link.run if link.run
link.id = docker(:run, *args, capture: true).strip
containers << link.id
end
run_options = ['--detach']
build.forward_env.each do |env|
run_options.push '--env', [env, ENV[env]].join('=')
end
build.links.each do |link|
run_options.push '--link', [link.id, link.name].join(':')
end
run_options.push tag
run_options.push build.run
id = docker(:run, *run_options, capture: true).strip
containers << id
docker(:attach, id)
return unless options.export
banner 'Export'
fail 'export flag set but no export_dir given' if build.export_dir.nil?
guest_export_dir = File.expand_path(build.export_dir, CIDE_SRC_DIR)
host_export_dir = File.expand_path(export_dir, Dir.pwd)
docker :cp, [id, guest_export_dir].join(':'), host_export_dir
rescue Docker::Error => ex
exit ex.exitstatus
ensure
unless containers.empty?
docker :rm, '--force', *containers.reverse,
verbose: false,
capture: true
end
end
|