-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.rb
executable file
·100 lines (85 loc) · 1.74 KB
/
cli.rb
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
#!/usr/bin/env ruby
# frozen_string_literal: true
def run(cmdline)
puts cmdline
system cmdline
end
def build_images()
run <<~CMDLINE.strip
cd docker && \
docker build --tag fmstates-script:latest --file FMStates.dockerfile .
CMDLINE
end
def clean_images()
run <<~CMDLINE.strip
docker rmi $(docker images -f dangling=true -q)
CMDLINE
end
def options_users()
uid = `id -u`.strip
gid = `id -g`.strip
"-u #{uid}:#{gid}"
end
def options_logs()
[
"max-size=10m",
"max-file=5",
].map { |s| "--log-opt #{s}" }.join(" ")
end
def options_volume()
"-v `pwd`/project:/project"
end
def options_all()
<<~CMDLINE.strip
-p 8888:8888 \
#{options_logs()} \
#{options_volume()}
CMDLINE
end
def run_bash()
run <<~CMDLINE.strip
docker run -it --rm --name fmstates-bash \
#{options_all()} \
fmstates-script /bin/bash
CMDLINE
end
def run_notebook()
run <<~CMDLINE.strip
docker run -it --rm --name fmstates-notebook \
#{options_all()} \
fmstates-script jupyter-notebook
CMDLINE
end
def stop_all()
run <<~CMDLINE.strip
docker stop fmstates-bash fmstates-notebook
CMDLINE
end
def prepare_directory()
curr_dir = File.expand_path(".", __dir__)
Dir.chdir(curr_dir)
end
def main()
commands = {
"build" => -> { build_images() },
"clean" => -> { clean_images() },
"stop-all" => -> { stop_all() },
"run-bash" => -> { run_bash() },
"run-notebook" => -> { run_notebook() },
}
if ARGV.empty?
puts "Available commands:"
puts commands.keys.join("\n")
return
end
ARGV.each do |name|
prepare_directory()
cmd = commands[name]
if cmd.nil?
puts "unknown command: #{name}"
else
cmd.call()
end
end
end
main() if caller.empty?