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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cef224e6d8e6c819c7a76c4c71b06632f99f7902205a5b4ad8b199ee53907d4a
4
- data.tar.gz: fd493858e93bcf28cf2f91ac847f4fa0ed9350f860b1309fa931fe653399e4e7
3
+ metadata.gz: fc26a08b87b0a21b92d09c1b8d13b645b70ce6aad5363b5806fd5eab78f42519
4
+ data.tar.gz: 3a8a3992d33903a639429882e425cc50f68f2d1775aa30c75f80d8f35b4393b0
5
5
  SHA512:
6
- metadata.gz: 147dbf5105f07740e17891439964cfdf12fef2626dda6bc97f50390362fa7c68cc21a600c33643adf66da4d5a9dd0cba1002239bb3b42d9648be031089538d71
7
- data.tar.gz: 8d28fbee29f3aacbdf1c0655ac83e591686b807b287fb6776803fd3df67ebd15322be7c3591ae4739355165c93416ae084b908af2ea8df3a13a3eb4aba66f399
6
+ metadata.gz: 79e9cdf2077d7797dbfae77fc5950da8c6b70e539f989dbb0f885bd6a08c23d2310803f6fd8192a0a3bf7dc948092d45d1f6e7f7e599b99092fd520eb85c25ee
7
+ data.tar.gz: '048429c1495c95995b960121929844411ac7ee9e0fea8eef410044101a329295362e0af7398fdd6eb9897406ca6ea7d86f48be47da6c0b82ecd0f2603c48bcaf'
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  pkg
2
+ .rspec_status
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dklet (0.1.0)
4
+ dklet (0.1.5)
5
5
  thor
6
6
 
7
7
  GEM
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 :cmd, banner: 'run command in container'
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
- def runsh(cid = ops_container)
54
- container_run(options[:cmd], options.merge(cid: cid))
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
- cmds = cmds.join("\n") if cmds.is_a?(Array)
321
- cmd = cmds || 'sh'
322
- puts "run : #{cmd}" unless opts[:quiet]
323
-
324
- if cmd == 'sh' # simple case
325
- cmds = <<~Desc
326
- docker exec -it #{opts[:opts]} #{cid} #{cmd}
327
- Desc
328
- else
329
- tfile = tmpfile_for(cmd)
330
- dst_file = "/tmp/dklet-#{File.basename(tfile)}-#{rand(10000)}"
331
- # todo user permissions for pg
332
- cmds = <<~Desc
333
- docker cp --archive #{tfile} #{cid}:#{dst_file}
334
- docker exec -it #{opts[:opts]} #{cid} sh -c 'sh #{dst_file} && rm -f #{dst_file}'
335
- Desc
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
- puts cmds unless opts[:quiet]
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
@@ -1,5 +1,5 @@
1
1
  module Dklet
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
 
4
4
  def self.version
5
5
  VERSION
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
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-25 00:00:00.000000000 Z
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler