-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·126 lines (103 loc) · 2.72 KB
/
run
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
#!/usr/bin/ruby
require 'yaml'
require 'optparse'
$full_command = $*.join(' ')
options = {}
opt_parser = OptionParser.new do |opt|
opt.banner = 'Usage: run bash'
opt.separator ''
opt.separator 'Example:'
opt.separator ' run --start'
opt.separator ' run bash # or every command you want to run in the container'
opt.separator ' run --stop'
opt.separator ' run --remove'
opt.separator ''
opt.separator 'You can set the project and the service in `.run.yml` file at the top level of the project.'
opt.separator 'The file have to be like this: '
opt.separator ' run:'
opt.separator ' service: app'
opt.separator ' project: project'
opt.separator ''
opt.separator 'Options'
opt.on('-p', '--project PROJECT', 'which project you want to run') do |value|
options[:project] = value
end
opt.on('-s', '--service SERVICE', 'which service have to be used') do |value|
options[:service] = value
end
opt.on('--rerun', 'stop the current container and exec the command') do
options[:rerun] = true
end
opt.on('--start', 'start a new container') do
options[:start] = true
end
opt.on('--stop', 'stop the container') do
options[:stop] = true
end
opt.on('--remove', 'stop and remove the container') do
options[:remove] = true
end
end
begin
opt_parser.parse!
rescue OptionParser::InvalidOption => e
end
class Docker
attr_accessor :config
def initialize(config:)
@config = config
end
def exec
command = "docker exec -it #{project}_dev #{$full_command}"
system "docker ps | grep #{project}_dev >/dev/null"
start if $?.exitstatus == 1
system command
raise if $?.exitstatus == 1
end
def start
command = config['start_command'] % [project, service]
system %Q{
if docker ps -a | grep #{project}_dev >/dev/null ; then
docker start #{project}_dev >/dev/null
else
#{command}
fi
}
end
def stop
system "docker stop #{project}_dev"
end
def remove
system "docker rm #{project}_dev"
end
private
def project
config['project']
end
def service
config['service']
end
end
root = `git rev-parse --show-toplevel`.chomp
config = YAML.load_file("#{root}/.run.yml")['run']
config['service'] = options[:service] if options[:service]
config['project'] = options[:project] if options[:project]
config['start_command'] ||= "docker-compose run -d --name %s_dev %s bundle exec spring server"
docker = Docker.new(config: config)
if options[:rerun]
docker.stop
docker.remove
docker.run
docker.exec
elsif options[:stop]
docker.stop
elsif options[:remove]
docker.stop
docker.remove
elsif options[:start]
docker.start
elsif ARGV.empty?
puts opt_parser
else
docker.exec
end