docker_core 0.0.22 → 0.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/docker_core.rb +135 -40
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e24b7a9fd2ff938ceda59f6846768c1e414dd54b9b99ee22d8384a3044d1978
4
- data.tar.gz: 4b15e835e7b275c3b8dbbb5e82db512870a07d551f3064f7f0ce2a8e9606e625
3
+ metadata.gz: 59611c639cd8210b57b49f5c538f05b31c840c693c174ad97a0910ae0910713a
4
+ data.tar.gz: 267162407a1497ef11607b92328b17d8896465fa309bcec0ade8d29e41fabde2
5
5
  SHA512:
6
- metadata.gz: 4b35e8577e6893fd84518b7eeaa8bb364684b3d632806b1c33ad0a884a3ac3457e2dbbc3c5697355a1785722f11f0b8d6076d62185b5d56a0c0f8438bbf6201d
7
- data.tar.gz: 2b73c1c304b64e38ffb065c948a5856753c7b33100b436816f9da02219ca1f6c73931b1db2b021ac40044572085e1f5b38a537c781555ff868ddbe7b3e0302c0
6
+ metadata.gz: 28ab87b38c6cd69fa0bf13ad9187058b48b3d461cef229c4c2c4d24a2f3abb33fabe5a8c7b7383f6b0ba72a2f138b4a52ceaab15ba9d4932debfa4e065e8272a
7
+ data.tar.gz: 2ad3d345775b85deae503d88f860194aead2db16a3fea44f9e70d7c7e2571f6bc0ce4234bf6ce5bf8beff5dd0c14a04909527ffa8907d34fe3cb4fcdba946a52
data/lib/docker_core.rb CHANGED
@@ -7,6 +7,7 @@ require('thor')
7
7
  require('zlib')
8
8
 
9
9
  module DockerCore
10
+ SUDO = false
10
11
  USER = 'core'
11
12
  GROUP = 'core'
12
13
 
@@ -37,6 +38,63 @@ module DockerCore
37
38
  end
38
39
 
39
40
  module Command
41
+ module Swarm
42
+ def self.swarm_path
43
+ return File.expand_path('~/.swarm')
44
+ end
45
+
46
+ # @param [String] service
47
+ def self.service_actived(service)
48
+ return 'active' == Process.capture("systemctl is-active #{service}").downcase
49
+ end
50
+
51
+ # @param [String] name
52
+ def self.write_swarm(name = '')
53
+ return File.write(self.swarm_path, "#{name}\n")
54
+ end
55
+
56
+ def self.read_swarm
57
+ file = self.swarm_path
58
+ return File.exists?(file) ? File.read(file).strip : ''
59
+ end
60
+
61
+ # @param [String] name
62
+ def self.update_swarm(name = '')
63
+ if false == name.empty?
64
+ self.write_swarm(name)
65
+ end
66
+
67
+ name = self.detect_swarm
68
+ self.write_swarm(name)
69
+
70
+ if name.empty?
71
+ name = 'none'
72
+ end
73
+
74
+ Color.echo("Swarm: #{name}", Color::GREEN)
75
+ end
76
+
77
+ def self.detect_swarm
78
+ swarm = self.read_swarm
79
+ detect = self.detect_services
80
+
81
+ if detect.has_key?(swarm) && detect[swarm.to_sym]
82
+ return swarm
83
+ end
84
+
85
+ index = detect.key(true)
86
+
87
+ if index.nil?
88
+ return ''
89
+ end
90
+
91
+ return index.to_s
92
+ end
93
+
94
+ def self.detect_services
95
+ return { docker: self.service_actived('docker') }
96
+ end
97
+ end
40
98
  end
41
99
 
42
100
  module Paser
@@ -56,15 +114,20 @@ module DockerCore
56
114
 
57
115
  if hash.is_a?(Hash)
58
116
  hash.each do |key, value|
59
- name = '--' + self.kebab(key)
117
+ short = 1 == key.length
118
+ prefix = short ? '-' : '--'
119
+ separator = short ? ' ' : '='
120
+ name = prefix + self.kebab(key)
60
121
 
61
122
  if true == value
62
123
  items << name
63
124
  next
64
125
  end
65
126
 
66
- value = "#{value}".gsub(/\\/, '\&\&').gsub('"', '\"')
67
- items << %(#{name}="#{value}")
127
+ [value].flatten.each do |item|
128
+ item = "#{item}".gsub(/\\/, '\&\&').gsub('"', '\"')
129
+ items << %(#{name}#{separator}"#{item}")
130
+ end
68
131
  next
69
132
  end
70
133
  end
@@ -96,51 +159,82 @@ module DockerCore
96
159
  end
97
160
 
98
161
  module Process
162
+ # @param [Numeric] second
163
+ def self.wait(second = 0)
164
+ if second.is_a?(Numeric)
165
+ if 0 > second
166
+ return sleep
167
+ end
168
+ if 0 < second
169
+ return sleep(second)
170
+ end
171
+ end
172
+ end
173
+
99
174
  # @param [Array] arguments
100
- # @param [Boolean] substitute
101
- def self.command(*arguments, substitute: false)
102
- if substitute
103
- arguments.unshift('su-exec', USER)
175
+ # @param [Boolean, String] sudo
176
+ def self.command(*arguments, sudo: SUDO)
177
+ if true == sudo
178
+ arguments.unshift('sudo')
179
+ end
180
+
181
+ if sudo.is_a?(String) && false == "#{sudo}".empty?
182
+ arguments.unshift('su-exec', sudo)
104
183
  end
105
184
 
106
185
  return Paser.arguments(*arguments).join(' ')
107
186
  end
108
187
 
109
188
  # @param [Array] arguments
110
- # @param [Boolean] substitute
111
- # @param [Boolean] exception
189
+ # @param [Boolean, String] sudo
190
+ # @param [Boolean] throw
191
+ # @param [Numeric] wait
112
192
  # @param [Boolean] strip
113
- # @param [Boolean] verbose
114
- def self.capture(*arguments, substitute: false, exception: false, strip: true, verbose: false)
193
+ # @param [Boolean] echo
194
+ def self.capture(*arguments, sudo: SUDO, throw: false, wait: 0, strip: true, echo: false)
115
195
  begin
116
- command = self.command(*arguments, substitute: substitute)
196
+ command = self.command(*arguments, sudo: sudo)
117
197
 
118
- if verbose
119
- Color.echo("+ #{command}", Color::GREEN)
198
+ if echo
199
+ Color.echo(": #{command}", Color::YELLOW)
120
200
  end
121
201
 
122
202
  data = `#{command}`
123
- return strip ? "#{data}".strip : data
203
+ result = strip ? "#{data}".strip : data
204
+ self.wait(wait)
205
+ return result
124
206
  rescue
125
- raise unless exception
207
+ raise unless throw
126
208
  return ''
127
209
  end
128
210
  end
129
211
 
130
212
  # @param [Array] arguments
131
- # @param [Boolean] substitute
132
- # @param [Boolean] exception
133
- def self.run(*arguments, substitute: false, exception: true)
134
- command = self.command(*arguments, substitute: substitute)
135
- Color.echo("+ #{command}", Color::GREEN)
136
- return system(command, exception: exception)
213
+ # @param [Boolean, String] sudo
214
+ # @param [Boolean] throw
215
+ # @param [Numeric] wait
216
+ # @param [Boolean] echo
217
+ def self.run(*arguments, sudo: SUDO, throw: true, wait: 0, echo: true)
218
+ command = self.command(*arguments, sudo: sudo)
219
+
220
+ if echo
221
+ Color.echo("+ #{command}", Color::GREEN)
222
+ end
223
+
224
+ result = system(command, exception: throw)
225
+ self.wait(wait)
226
+ return result
137
227
  end
138
228
 
139
229
  # @param [Array] arguments
140
- # @param [Boolean] substitute
141
- def self.execute(*arguments, substitute: false)
142
- command = self.command(*arguments, substitute: substitute)
143
- Color.echo("= #{command}", Color::CYAN)
230
+ # @param [Boolean, String] sudo
231
+ def self.execute(*arguments, sudo: SUDO, echo: true)
232
+ command = self.command(*arguments, sudo: sudo)
233
+
234
+ if echo
235
+ Color.echo("= #{command}", Color::CYAN)
236
+ end
237
+
144
238
  exec(command)
145
239
  end
146
240
 
@@ -256,8 +350,8 @@ module DockerCore
256
350
 
257
351
  # @param [Object] io
258
352
  # @param [String] path
259
- # @param [Boolean] verbose
260
- def self.unpack_archive(io, path, verbose: false)
353
+ # @param [Boolean] echo
354
+ def self.unpack_archive(io, path, echo: false)
261
355
  Gem::Package::TarReader.new(io) do
262
356
 
263
357
  # @type [Gem::Package::TarReader] reader
@@ -269,7 +363,7 @@ module DockerCore
269
363
  mode = header.mode
270
364
  file = File.join(path, entry.full_name)
271
365
 
272
- if verbose
366
+ if echo
273
367
  Color.echo(": #{file}", Color::GREEN)
274
368
  end
275
369
 
@@ -290,19 +384,19 @@ module DockerCore
290
384
 
291
385
  # @param [String] file
292
386
  # @param [String] path
293
- # @param [Boolean] verbose
294
- def self.inflate_file(file, path, verbose: false)
387
+ # @param [Boolean] echo
388
+ def self.inflate_file(file, path, echo: false)
295
389
  Zlib::GzipReader.open(file) do
296
390
 
297
391
  # @type [Zlib::GzipReader] reader
298
392
  |reader|
299
- self.unpack_archive(reader, path, verbose: verbose)
393
+ self.unpack_archive(reader, path, echo: echo)
300
394
  end
301
395
  end
302
396
 
303
397
  # @param [String] path
304
- # @param [Boolean] verbose
305
- def self.tape_archive(path, verbose: false)
398
+ # @param [Boolean] echo
399
+ def self.tape_archive(path, echo: false)
306
400
  io = StringIO.new('')
307
401
  offset = path.size + 1
308
402
 
@@ -314,7 +408,7 @@ module DockerCore
314
408
  mode = File.stat(name).mode
315
409
  file = name.slice(offset ..)
316
410
 
317
- if verbose
411
+ if echo
318
412
  Color.echo(": #{file}", Color::GREEN)
319
413
  end
320
414
 
@@ -338,13 +432,13 @@ module DockerCore
338
432
 
339
433
  # @param [String] file
340
434
  # @param [String] path
341
- # @param [Boolean] verbose
342
- def self.deflate_file(file, path, verbose: false)
435
+ # @param [Boolean] echo
436
+ def self.deflate_file(file, path, echo: false)
343
437
  Zlib::GzipWriter.open(file, Zlib::BEST_COMPRESSION) do
344
438
 
345
439
  # @type [Zlib::GzipWriter] writer
346
440
  |writer|
347
- writer.write(self.tape_archive(path, verbose: verbose))
441
+ writer.write(self.tape_archive(path, echo: echo))
348
442
  end
349
443
  end
350
444
 
@@ -353,8 +447,9 @@ module DockerCore
353
447
  def self.update_user(uid: 500, gid: 500)
354
448
  uid = ENV.fetch('DOCKER_CORE_UID', uid)
355
449
  gid = ENV.fetch('DOCKER_CORE_GID', gid)
356
- Process.run('deluser', USER, exception: false)
357
- Process.run('delgroup', GROUP, exception: false)
450
+
451
+ Process.run('deluser', USER, throw: false)
452
+ Process.run('delgroup', GROUP, throw: false)
358
453
  Process.run('addgroup', { system: true, gid: gid }, GROUP)
359
454
  Process.run('adduser', { system: true, 'disabled-password': true, 'no-create-home': true, ingroup: GROUP, gecos: USER, shell: '/bin/bash', uid: uid }, USER)
360
455
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.0.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - agrozyme
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-27 00:00:00.000000000 Z
11
+ date: 2021-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fileutils