-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathde
More file actions
executable file
·408 lines (286 loc) · 9.01 KB
/
de
File metadata and controls
executable file
·408 lines (286 loc) · 9.01 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env perl
#
#ver http://stackoverflow.com/a/26547845
#
use strict;
use YAML qw(LoadFile DumpFile);
use Getopt::Std;
use Cwd;
use Data::Dumper;
use File::Spec;
use File::Basename;
our $VERSION=2.1.1;
my $CONFIG_FILE = '.de';
my %DEFAULT_ENV = (
TERM => 'xterm-256color'
);
my @current_path = File::Spec->splitdir(
File::Spec->rel2abs(
File::Spec->curdir()
)
);
my $DEFAULT_PROJECT_NAME = pop @current_path;
my $REAL_GROUP_ID = (split(' ', $())[0];
my $CONFIG = {
container => undef,
image => undef,
working_dir => File::Spec->join(
File::Spec->rootdir,
'home',
$DEFAULT_PROJECT_NAME
),
ports => [],
volumes => [],
links => [],
action => 'exec',
homedir => File::Spec->join(
File::Spec->rootdir(),
'home',
scalar(getpwuid($<))
),
user_id => $<,
group_id => $REAL_GROUP_ID,
env => [],
sysctl => [],
entrypoint => undef,
};
&read_config;
&load_command_line_args;
if($CONFIG->{action} eq 'exec'){
my $sh_cmd = &build_command(@ARGV);
&execute_command(@$sh_cmd);
}
elsif($CONFIG->{action} =~ /^delete|drop|rm|remove$/i){
&delete_image();
}
#########################################################################
sub current_path_relative_to_project_root {
my $current_abs_path = File::Spec->rel2abs(File::Spec->curdir());
my @current_abs_path = File::Spec->splitdir($current_abs_path);
my $rel_path = File::Spec->curdir();
my @slices = ();
while(@current_abs_path > 0){
if(-e File::Spec->join($rel_path, $CONFIG_FILE)){
return File::Spec->join(@slices, File::Spec->curdir());
}
unshift @slices, pop(@current_abs_path);
$rel_path = File::Spec->join(File::Spec->updir(), $rel_path);
}
}
sub abs_root_path {
my $current_abs_path = File::Spec->rel2abs(File::Spec->curdir());
my @current_abs_path = File::Spec->splitdir($current_abs_path);
my $rel_path = File::Spec->curdir();
while(@current_abs_path > 0 ){
if(-e File::Spec->join($rel_path, $CONFIG_FILE)){
return File::Spec->join(@current_abs_path);
}
pop(@current_abs_path);
$rel_path = File::Spec->join(File::Spec->updir(), $rel_path);
}
}
sub build_command {
my @args = @_;
unless(@args){
HELP_MESSAGE();
die("Command (to run in container) missing");
}
my $cmd = join (' ', @args);
return [@args];
}
sub build_env {
my $env = {};
if(ref($CONFIG->{env}) eq 'ARRAY'){
foreach my $env_var (@{$CONFIG->{env}}){
my ($key, $val) = split '=', $env_var;
$env->{$key} = $val;
}
}
elsif(ref($CONFIG->{env}) eq 'HASH'){
$env = $CONFIG->{env};
}
$env = {(%$env, %DEFAULT_ENV)};
$env;
}
sub execute_command{
my (@sh_cmd) = @_;
die("image not specified") unless($CONFIG->{image});
my $container_name = &generate_container_name();
# para evitar retornos de carro (\r) na saida do docker run
# evitamos activa a tty a menos que se indique explicitamente
#(https://github.com/docker/docker/issues/8513)
my @command = qw(docker run -i);
if($CONFIG->{tty}){
push @command, '-t'
}
if($CONFIG->{commit}){
push @command, ('--name', $container_name);
}
else{
push @command, "--rm";
}
#
# seteamos el usuario dentro del container
# equivalente al que lo esta ejecutando fuera
#
unless($CONFIG->{root}){
#push @command, ('-e', 'USER='.$ENV{USER});
#push @command, ('-e', 'USERID='.$<);
#push @command, ('-u', "$<:$REAL_GROUP_ID");
push @command, ('-e', 'USER='.getpwuid($CONFIG->{user_id}));
push @command, ('-e', 'USERID='.$CONFIG->{user_id});
push @command, ('-u', $CONFIG->{user_id}.':'.$CONFIG->{group_id});
}
push @command, ("-v", File::Spec->rel2abs(File::Spec->curdir()).':'.$CONFIG->{working_dir});
push @command, ('-w', $CONFIG->{working_dir});
# volumes extra
foreach my $vol (@{$CONFIG->{volumes}}){
my @part = split ':', $vol;
my $local_path;
# sustituimos variables de entorno, si existen
$part[0] = &interpolate_env_vars($part[0]);
my $access_control = ($part[2])? ':'.$part[2] : '';
# miramos si es ruta relativa o absoluta
# si es relativa, hai que calcular la absoluta
my $root = File::Spec->rootdir();
#
# Pero para poder montar los containers con named_volumes
# si la ruta empieza por @ (de label) no se hace más que seguir la label
#
if($part[0] =~ /^\@([^:]+)/){
push @command, ('-v', $1.':'.$part[1].$access_control);
next;
}
else{
if($part[0] =~ qr!^$root!x ){
$local_path = $part[0]
}
else{
$local_path = File::Spec->rel2abs(&abs_root_path.'/'.$part[0]);
}
}
push @command, ('-v', $local_path.':'.$part[1].$access_control);
}
# env vars definied en config
my $env = &build_env;
while(my ($k,$v) = each(%$env)){
$v = &interpolate_env_vars($v);
push @command, ('-e', "$k=$v");
}
# links
foreach my $link (@{$CONFIG->{links}}){
push @command, ('--link', $link);
}
# ports
if($CONFIG->{connect_ports}){
foreach my $port (@{$CONFIG->{ports}}){
push @command, ("-p", $port);
}
}
# sysctl
foreach my $opt (@{$CONFIG->{sysctl}}){
push @command, ('--sysctl', $opt);
}
# entrypoint
if(defined($CONFIG->{entrypoint})){
push @command, ("--entrypoint" , $CONFIG->{entrypoint});
}
push @command, ($CONFIG->{image}, @sh_cmd);
run_cmd(@command);
if($CONFIG->{commit}){
my $tag = $CONFIG->{tag} || 'latest';
commit_container($container_name, $CONFIG->{image}.":$tag")
}
}
sub interpolate_env_vars{
$_[0] =~ s/\$(\w+)/$ENV{$1}/g;
$_[0];
}
sub generate_container_name {
use Time::HiRes qw(time);
time;
}
sub commit_container{
my ($container, $image) = @_;
run_cmd(
"docker commit $container $image"
);
# limpamos container
run_cmd(
"docker rm -f -v $container"
);
}
sub delete_image{
die("image not specified") unless ($CONFIG->{image});
print "Removing image ".$CONFIG->{image}." ...";
run_cmd("docker rmi $CONFIG->{image}");
}
sub read_config {
my $config_path = File::Spec->join(
&abs_root_path,
$CONFIG_FILE
);
return unless -e $config_path;
my $data = LoadFile($config_path);
while(my ($k, $v) = each(%$data)){
if(exists($CONFIG->{$k})){
$CONFIG->{$k} = $v
}
}
}
sub save_config {
delete($CONFIG->{action});
DumpFile($CONFIG_FILE, $CONFIG)
}
sub load_command_line_args{
my $conf = $CONFIG;
# Mergeamos CONF cas opcions pasadas desde a linea de comandos
my $opts = {};
getopts("i:c:w:a:e:dRCUST:t", $opts);
$conf->{container} = $opts->{c} if($opts->{c});
$conf->{image} = $opts->{i} if($opts->{i});
$conf->{working_dir} = $opts->{w} if($opts->{w});
$conf->{action} = $opts->{a} if($opts->{a});
$conf->{root} = 1 if($opts->{R});
$conf->{debug} = 1 if($opts->{d});
$conf->{commit} = $opts->{C} || $opts->{S};
$conf->{tag} = $opts->{T} if($opts->{T});
$conf->{tty} = $opts->{t} if($opts->{t});
$conf->{connect_ports} = !($opts->{U});
$conf->{entrypoint} = $opts->{e} if($opts->{e});
}
sub HELP_MESSAGE {
print "Run commands related to a code project, inside an associated docker container, from outside\n\n";
print "Usage: $0 <args> <command>\n\n";
print
"VALID ARGS:\n",
" -c <container_name>: Launch container with name specified, by default random name is used \n",
" -i <image_name>: Use image to run container \n",
" -w <dir>: working dir, default to '.'\n",
" -e <entrypoint>: entrypoint, modify image entrypoint\n",
" -R: root mode, run as root\n",
" -d: debug, print docker command executed\n",
" -t: tty allocate, to get echo in terminal\n",
" -U: avoid connect ports to container to avoid colision with another family container in execution (only command line)\n",
" -C,-S: commit after container exit. It will use -T tag, or image name if not tag is specified\n",
" -T: tag to commit to, if commit flag is set\n",
"\n\n";
}
sub run_cmd{
my ($cmd,@rest) = @_;
print join(' ', $cmd, @rest, "\n") if($CONFIG->{debug});
system(&split_command($cmd), @rest);
}
sub split_command {
my ($cmd) = @_;
my @cmd;
#
# regexp que permite partir por espacios y por bloques entrecomillados
#
while($cmd =~ /((\"[^"]+\")|(\'[^']+\')|([^\s]+))\s?/g){
my $token = $1;
$token =~ s/(^["']|["']$)//g;
push @cmd, $token;
}
@cmd;
}