dklet 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/lib/dklet/cli.rb +40 -32
- data/lib/dklet/util.rb +13 -4
- data/lib/dklet/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc26a08b87b0a21b92d09c1b8d13b645b70ce6aad5363b5806fd5eab78f42519
|
4
|
+
data.tar.gz: 3a8a3992d33903a639429882e425cc50f68f2d1775aa30c75f80d8f35b4393b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79e9cdf2077d7797dbfae77fc5950da8c6b70e539f989dbb0f885bd6a08c23d2310803f6fd8192a0a3bf7dc948092d45d1f6e7f7e599b99092fd520eb85c25ee
|
7
|
+
data.tar.gz: '048429c1495c95995b960121929844411ac7ee9e0fea8eef410044101a329295362e0af7398fdd6eb9897406ca6ea7d86f48be47da6c0b82ecd0f2603c48bcaf'
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/dklet/cli.rb
CHANGED
@@ -47,11 +47,11 @@ class DockletCLI < Thor
|
|
47
47
|
end
|
48
48
|
|
49
49
|
desc 'runsh [CONTAINER]', 'run into container'
|
50
|
-
option :
|
51
|
-
option :opts, banner: 'docker run options'
|
50
|
+
option :cid, banner: 'target container id or name'
|
52
51
|
option :tmp, type: :boolean, default: false, banner: 'allow run tmp container'
|
53
|
-
|
54
|
-
|
52
|
+
option :opts, banner: 'docker run options'
|
53
|
+
def runsh(cmds = 'sh')
|
54
|
+
container_run(cmds)
|
55
55
|
end
|
56
56
|
map "sh" => :runsh
|
57
57
|
|
@@ -305,7 +305,19 @@ class DockletCLI < Thor
|
|
305
305
|
klass.new.invoke(task, args, options)
|
306
306
|
end
|
307
307
|
|
308
|
+
# encapsulate run commands behaviors in system
|
309
|
+
def system_run(cmds, opts={})
|
310
|
+
unless options[:quiet]
|
311
|
+
puts cmds
|
312
|
+
end
|
313
|
+
unless options[:dry]
|
314
|
+
system cmds
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
308
318
|
def container_run(cmds, opts = {})
|
319
|
+
opts = options.merge(opts)
|
320
|
+
# get target container
|
309
321
|
cid = opts[:cid] || ops_container
|
310
322
|
tmprun = opts[:tmp]
|
311
323
|
if tmprun
|
@@ -317,25 +329,31 @@ class DockletCLI < Thor
|
|
317
329
|
end
|
318
330
|
abort "No container found!" unless cid
|
319
331
|
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
332
|
+
# how to run
|
333
|
+
new_cmds = if Dklet::Util.single_line?(cmds)
|
334
|
+
<<~Desc
|
335
|
+
docker exec -it #{opts[:opts]} #{cid} #{cmds}
|
336
|
+
Desc
|
337
|
+
else
|
338
|
+
cmds = cmds.join("\n") if cmds.is_a?(Array)
|
339
|
+
tfile = tmpfile_for(cmds)
|
340
|
+
dst_file = "/tmp/dklet-#{File.basename(tfile)}-#{rand(10000)}"
|
341
|
+
<<~Desc
|
342
|
+
docker cp --archive #{tfile} #{cid}:#{dst_file}
|
343
|
+
docker exec -it #{opts[:opts]} #{cid} sh #{'-x' if opts[:debug]} #{dst_file}
|
344
|
+
docker exec #{cid} rm -f #{dst_file}
|
345
|
+
Desc
|
346
|
+
end
|
347
|
+
unless opts[:quiet]
|
348
|
+
puts "==commands to run on #{cid}"
|
349
|
+
puts cmds
|
350
|
+
if opts[:debug]
|
351
|
+
puts "====by run on host"
|
352
|
+
puts new_cmds
|
353
|
+
end
|
354
|
+
puts "==end of print commands"
|
336
355
|
end
|
337
|
-
|
338
|
-
system cmds unless opts[:dry]
|
356
|
+
system new_cmds unless opts[:dry]
|
339
357
|
|
340
358
|
if tmprun
|
341
359
|
system <<~Desc
|
@@ -343,15 +361,5 @@ class DockletCLI < Thor
|
|
343
361
|
Desc
|
344
362
|
end
|
345
363
|
end
|
346
|
-
|
347
|
-
# encapsulate run commands behaviors in system
|
348
|
-
def system_run(cmds, opts={})
|
349
|
-
unless options[:quiet]
|
350
|
-
puts cmds
|
351
|
-
end
|
352
|
-
unless options[:dry]
|
353
|
-
system cmds
|
354
|
-
end
|
355
|
-
end
|
356
364
|
end # of no_commands
|
357
365
|
end
|
data/lib/dklet/util.rb
CHANGED
@@ -4,6 +4,10 @@ require 'socket'
|
|
4
4
|
module Dklet::Util
|
5
5
|
module_function
|
6
6
|
|
7
|
+
def human_timestamp(t = Time.now)
|
8
|
+
t.strftime("%Y%m%d%H%M%S")
|
9
|
+
end
|
10
|
+
|
7
11
|
def tmpfile_for(str, prefix: 'dklet-tmp')
|
8
12
|
file = Tempfile.new(prefix)
|
9
13
|
file.write str
|
@@ -12,11 +16,16 @@ module Dklet::Util
|
|
12
16
|
file.path
|
13
17
|
end
|
14
18
|
|
15
|
-
def human_timestamp(t = Time.now)
|
16
|
-
t.strftime("%Y%m%d%H%M%S")
|
17
|
-
end
|
18
|
-
|
19
19
|
def host_ip
|
20
20
|
Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address
|
21
21
|
end
|
22
|
+
|
23
|
+
def single_line?(cmds)
|
24
|
+
cmds = cmds.chomp
|
25
|
+
return false if cmds.is_a? Array
|
26
|
+
return false if cmds =~ /\n/
|
27
|
+
return true if cmds =~ /^\s*(bash|sh)/
|
28
|
+
return false if cmds =~ /.+;/
|
29
|
+
true
|
30
|
+
end
|
22
31
|
end
|
data/lib/dklet/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dklet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruijian Cao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|