docker_core 0.0.13 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/docker_core.rb +281 -185
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ec6404e35be53e394d3ec7d1b46f01a3f657b753502b78a2614207e393bfc7b
4
- data.tar.gz: 73ba6004a05843c13882bec32dc3d959e53f0f740f04cd49a51994c5a8154574
3
+ metadata.gz: 85249b5bf70ef8b313ad2ff28898889db66e92c5b96af40a024e61131453ec4c
4
+ data.tar.gz: e4dafbe9bfdcc3fd40fcc3d93b76b8557b599ba07201fdfddff0bcb08ff9c65f
5
5
  SHA512:
6
- metadata.gz: 42065ac773e44fc3133149faabf835a339e4f71640db12d696b6f2ed57356cd77ffa3d73db2b3a4c41fea3ca46a59d3e1251990567bea9cefd061d7342163b20
7
- data.tar.gz: f64554684ae49cfd29ac58c345a12f291d3b467e1596d6ef9bb74b766a2bb00a18b180ecdc201dcff60872d57632e823fa52d32686b890b0269910728a08cfe1
6
+ metadata.gz: '0489e8827a51d214072b11f7bc4d32bfb8624484670da293839727de7586c408863728cf84a1e5f83df9be8b1d14df844debf129570f587a377496dbb5aadee6'
7
+ data.tar.gz: e3d7a871d1ac19786b3cc06bd8525d33000e76b70208d61e44eaaa794bc7c4e9749ff507ec6ba93a09a636961d81c8cb539555714bb56b6053fd1cdb2351a518
data/lib/docker_core.rb CHANGED
@@ -1,250 +1,346 @@
1
+ require('etc')
1
2
  require('fileutils')
2
3
  require('json')
3
4
  require('net/http')
4
5
  require('rubygems/package')
6
+ require('thor')
5
7
  require('zlib')
6
8
 
7
9
  module DockerCore
8
- COLOR_CODE = { error: 31, info: 32, warn: 33 }
9
- ARCH_SUFFIX = { x86: '386', x86_64: 'amd64', armhf: 'armv6', aarch64: 'arm64' }
10
-
11
- # @param [String] text
12
- # @param [Integer] code
13
- def self.color(text, code)
14
- escape = "\033"
15
- prefix = "#{escape}[#{code}m"
16
- suffix = "#{escape}[0m"
17
- return "#{prefix}#{text}#{suffix}"
10
+ module Image
18
11
  end
19
12
 
20
- # @param [String] text
21
- # @param [Integer] code
22
- def self.echo(text, code: self::COLOR_CODE[:info])
23
- puts(self.color(text, code))
13
+ module Command
24
14
  end
25
15
 
26
- # @param [String] title
27
- # @param [Method] run
28
- # @param [Array] argumets
29
- def self.execute(title, run, *argumets)
30
- code = self::COLOR_CODE[:warn]
31
- self.echo("> #{title}", code: code)
32
- run.call(*argumets)
33
- self.echo("< #{title}", code: code)
34
- end
16
+ module Paser
17
+ # @param [String] value
18
+ def self.boolean(value)
19
+ return %w[true yes on].include?("#{value}".strip.downcase)
20
+ end
35
21
 
36
- # @param [String] command
37
- # @param [Boolean] force
38
- def self.run(command, force: false)
39
- begin
40
- self.echo("+ #{command}")
41
- print(`#{command}`)
42
- rescue
43
- raise unless force
22
+ # @param [String] text
23
+ def self.kebab(text)
24
+ return "#{text}".gsub(/_/, '-')
44
25
  end
45
- end
46
26
 
47
- # @param [Integer] uid
48
- # @param [Integer] gid
49
- def self.update_user(uid: 500, gid: 500)
50
- uid = ENV.fetch('DOCKER_CORE_UID', uid)
51
- gid = ENV.fetch('DOCKER_CORE_GID', gid)
52
- self.run('deluser core', force: true)
53
- self.run('delgroup core', force: true)
54
- self.run("addgroup --system --gid=#{gid} core")
55
- self.run("adduser --system --disabled-password --no-create-home --ingroup=core --gecos=core --shell=/bin/bash --uid=#{uid} core")
56
- end
27
+ # @param [Hash] hash
28
+ def self.options(hash = {})
29
+ items = []
57
30
 
58
- # @param [Array<String>] arguments
59
- def self.path(*arguments)
60
- return Dir.glob(arguments.flatten, File::FNM_DOTMATCH).map { |item| File.path(item) }.uniq
61
- end
31
+ hash.each do |key, value|
32
+ name = '--' + self.kebab(key)
33
+ item = (true == value) ? name : %Q(#{name}="#{value}")
34
+ items << item
35
+ end
62
36
 
63
- # @param [Array<String>] arguments
64
- def self.chown(*arguments)
65
- self.path(*arguments).each do |path|
66
- FileUtils.chown_R('core', 'core', path, force: true)
37
+ return items
67
38
  end
68
- end
69
39
 
70
- # @param [Array<String>] arguments
71
- def self.chmod(mode, *arguments)
72
- self.path(*arguments).each do |path|
73
- FileUtils.chmod_R(mode, path, force: true)
40
+ def self.make(*arguments)
41
+ items = []
42
+
43
+ arguments.each do |item|
44
+ if item.is_a?(Array)
45
+ items.concat(item.map { |value| "#{value}" })
46
+ next
47
+ end
48
+
49
+ if item.is_a?(Hash)
50
+ items.concat(self.options(item))
51
+ next
52
+ end
53
+
54
+ items << "#{item}"
55
+ next
56
+ end
57
+
58
+ return items
59
+ end
60
+
61
+ def self.command(*arguments)
62
+ return self.make(*arguments).join(' ')
74
63
  end
75
64
  end
76
65
 
77
- # @param [Array<String>] arguments
78
- def self.mkdir(*arguments, mode: nil)
79
- stack = []
80
- paths = arguments.map { |path| File.path(path) }.uniq
66
+ module Shell
67
+ include(Thor::Shell)
68
+
69
+ USER = 'core'
70
+ GROUP = 'core'
81
71
 
82
- paths.each do |path|
83
- until path == stack.last
84
- stack << path
85
- path = File.dirname(path)
72
+ # @param [String] text
73
+ # @param [Array<String>] colors
74
+ def self.echo(text, *colors)
75
+ if 0 == colors.size
76
+ return puts(text)
86
77
  end
78
+
79
+ color = colors.map do |color|
80
+ if color.is_a?(Symbol)
81
+ next Color.const_get(color.to_s.upcase)
82
+ end
83
+
84
+ next "#{color}"
85
+ end.join
86
+
87
+ return puts("#{color}#{text}#{Color::CLEAR}")
88
+ end
89
+
90
+ # @param [Boolean] strip
91
+ def self.capture(*arguments, strip: true)
92
+ command = Paser.command(*arguments)
93
+ data = `#{command}`
94
+ return strip ? "#{data}".strip : data
87
95
  end
88
96
 
89
- stack.uniq.each do |path|
90
- if File.exist?(path) && false == File.directory?(path)
91
- File.delete(path)
97
+ # @param [Boolean] force
98
+ def self.execute(*arguments, force: false)
99
+ begin
100
+ command = Paser.command(*arguments)
101
+ self.echo("+ #{command}", Color::GREEN)
102
+ print(`#{command}`)
103
+ rescue
104
+ raise unless force
92
105
  end
93
106
  end
94
107
 
95
- FileUtils.mkdir_p(paths, mode: mode)
96
- end
108
+ # @param [Boolean] force
109
+ # @param [String] user
110
+ def self.sudo(*arguments, force: false, user: USER)
111
+ arguments.unshift('su-exec', user)
112
+ self.execute(*arguments, force: force)
113
+ end
97
114
 
98
- # @param [Array<String>] arguments
99
- def self.rm(*arguments)
100
- self.path(*arguments).each do |path|
101
- FileUtils.rm_rf(path)
115
+ # @param [String] title
116
+ # @param [Method] delegate
117
+ # @param [Array] arguments
118
+ def self.invoke(title, delegate, *arguments)
119
+ color = Color::YELLOW
120
+ self.echo("> #{title}", color)
121
+ delegate.call(*arguments)
122
+ self.echo("< #{title}", color)
102
123
  end
103
- end
104
124
 
105
- # @param [String] value
106
- def self.to_boolean(value)
107
- return %w[true yes on].include?("#{value}".strip.downcase)
108
- end
125
+ # @param [Array<String>] arguments
126
+ # @return [Array<String>]
127
+ def self.find_files(*arguments)
128
+ return Dir.glob(arguments.flatten, File::FNM_DOTMATCH).map do |item|
129
+ next File.path(item)
130
+ end.filter do |item|
131
+ next false == %w[. ..].include?(File.basename(item))
132
+ end.uniq
133
+ end
109
134
 
110
- # @param [String] stdout
111
- # @param [String] stderr
112
- def self.link_log(stdout: '', stderr: '')
113
- if '' != stdout
114
- File.symlink('/dev/stdout', File.path(stdout))
135
+ # @param [Array<String>] arguments
136
+ # @param [String] user
137
+ # @param [String] group
138
+ def self.change_owner(*arguments, user: USER, group: GROUP)
139
+ self.find_files(*arguments).each do |path|
140
+ FileUtils.chown_R(user, group, path, force: true)
141
+ end
115
142
  end
116
143
 
117
- if '' != stderr
118
- File.symlink('/dev/stderr', File.path(stderr))
144
+ # @param [Array<String>] arguments
145
+ def self.change_mode(mode, *arguments)
146
+ self.find_files(*arguments).each do |path|
147
+ FileUtils.chmod_R(mode, path, force: true)
148
+ end
119
149
  end
120
- end
121
150
 
122
- # @param [Array<String>] arguments
123
- def self.clear_path(*arguments, mode: nil)
124
- self.rm(*arguments)
125
- self.mkdir(*arguments, mode: mode)
126
- end
151
+ # @param [Array<String>] arguments
152
+ def self.make_folders(*arguments, mode: nil)
153
+ stack = []
154
+ paths = arguments.map do |path|
155
+ next File.path(path)
156
+ end.uniq
157
+
158
+ paths.each do |path|
159
+ until path == stack.last
160
+ stack << path
161
+ path = File.dirname(path)
162
+ end
163
+ end
127
164
 
128
- def self.arch_suffix
129
- arch = "#{`apk --print-arch`}".strip
130
- return ARCH_SUFFIX.fetch(arch.to_sym, arch)
131
- end
165
+ stack.uniq.each do |path|
166
+ if File.exist?(path) && false == File.directory?(path)
167
+ File.delete(path)
168
+ end
169
+ end
132
170
 
133
- # @param [String] repository
134
- def self.github_latest_version(repository)
135
- uri = URI("https://api.github.com/repos/#{repository}/releases/latest")
136
- return JSON.parse(Net::HTTP.get(uri))['tag_name']
137
- end
171
+ FileUtils.mkdir_p(paths, mode: mode)
172
+ end
138
173
 
139
- # @param [String] uri
140
- # @param [Integer] max_redirect
141
- def self.uri_resolve(uri, max_redirect = 10)
142
- response = Net::HTTP.get_response(URI.parse(uri))
174
+ # @param [Array<String>] arguments
175
+ def self.remove_folders(*arguments)
176
+ self.find_files(*arguments).each do |path|
177
+ FileUtils.rm_rf(path)
178
+ end
179
+ end
143
180
 
144
- if false == response.is_a?(Net::HTTPRedirection)
145
- return uri
181
+ # @param [String, Array<String>] source
182
+ # @param [String] target
183
+ def self.move_files(source, target)
184
+ files = [source].flatten
185
+ FileUtils.mv(self.find_files(*files), target)
146
186
  end
147
187
 
148
- max_redirect = max_redirect - 1
188
+ # @param [String] uri
189
+ # @param [Integer] max_redirect
190
+ def self.resolve_link(uri, max_redirect = 10)
191
+ response = Net::HTTP.get_response(URI.parse(uri))
149
192
 
150
- if 0 == max_redirect
151
- raise('maximum redirect reached')
152
- end
193
+ if false == response.is_a?(Net::HTTPRedirection)
194
+ return uri
195
+ end
153
196
 
154
- return self.uri_resolve(response['location'], max_redirect)
155
- end
197
+ max_redirect = max_redirect - 1
156
198
 
157
- # @param [String] uri
158
- # @param [String] file
159
- def self.download_file(uri, file)
160
- uri = self.uri_resolve(uri)
161
- content = Net::HTTP.get(URI(uri))
162
- return File.binwrite(file, content)
163
- end
199
+ if 0 == max_redirect
200
+ raise('maximum redirect reached')
201
+ end
164
202
 
165
- # @param [String] path
166
- # @param [Boolean] verbose
167
- def self.untar(io, path, verbose: false)
168
- # @type [Gem::Package::TarReader] reader
169
- Gem::Package::TarReader.new(io) do |reader|
170
- reader.rewind
171
- reader.each do |entry|
172
- # @type [Gem::Package::TarHeader] header
173
- header = entry.header
174
- mode = header.mode
175
- file = File.join(path, entry.full_name)
176
-
177
- if verbose
178
- self.echo(": #{file}")
179
- end
203
+ return self.resolve_link(response['location'], max_redirect)
204
+ end
180
205
 
181
- if entry.directory?
182
- self.mkdir(file, mode: mode)
183
- next
184
- end
206
+ # @param [String] uri
207
+ # @param [String] file
208
+ def self.download_file(uri, file)
209
+ uri = self.resolve_link(uri)
210
+ content = Net::HTTP.get(URI(uri))
211
+ return File.binwrite(file, content)
212
+ end
185
213
 
186
- if entry.file?
187
- self.mkdir(File.dirname(file))
188
- File.binwrite(file, entry.read)
189
- self.chmod(mode, file)
190
- next
214
+ # @param [String] path
215
+ # @param [Boolean] verbose
216
+ def self.untar(io, path, verbose: false)
217
+ Gem::Package::TarReader.new(io) do
218
+
219
+ # @type [Gem::Package::TarReader] reader
220
+ |reader|
221
+ reader.rewind
222
+ reader.each do |entry|
223
+ # @type [Gem::Package::TarHeader] header
224
+ header = entry.header
225
+ mode = header.mode
226
+ file = File.join(path, entry.full_name)
227
+
228
+ if verbose
229
+ self.echo(": #{file}", Color::GREEN)
230
+ end
231
+
232
+ if entry.directory?
233
+ self.make_folders(file, mode: mode)
234
+ next
235
+ end
236
+
237
+ if entry.file?
238
+ self.make_folders(File.dirname(file))
239
+ File.binwrite(file, entry.read)
240
+ self.change_mode(mode, file)
241
+ next
242
+ end
191
243
  end
192
244
  end
193
245
  end
194
- end
195
246
 
196
- # @param [String] file
197
- # @param [String] path
198
- # @param [Boolean] verbose
199
- def self.ungzip(file, path, verbose: false)
200
- # @type [Zlib::GzipReader] reader
201
- Zlib::GzipReader.open(file) do |reader|
202
- self.untar(reader, path, verbose: verbose)
247
+ # @param [String] file
248
+ # @param [String] path
249
+ # @param [Boolean] verbose
250
+ def self.ungzip(file, path, verbose: false)
251
+ Zlib::GzipReader.open(file) do
252
+
253
+ # @type [Zlib::GzipReader] reader
254
+ |reader|
255
+ self.untar(reader, path, verbose: verbose)
256
+ end
203
257
  end
204
- end
205
258
 
206
- # @param [String] path
207
- # @param [Boolean] verbose
208
- def self.tar(path, verbose: false)
209
- io = StringIO.new('')
210
- offset = path.size + 1
211
-
212
- # @type [Gem::Package::TarWriter] writer
213
- Gem::Package::TarWriter.new(io) do |writer|
214
- # @type [String] file
215
- self.path(File.join(path, '**/*')).each do |name|
216
- mode = File.stat(name).mode
217
- file = name.slice(offset ..)
218
-
219
- if verbose
220
- self.echo(": #{file}")
221
- end
259
+ # @param [String] path
260
+ # @param [Boolean] verbose
261
+ def self.tar(path, verbose: false)
262
+ io = StringIO.new('')
263
+ offset = path.size + 1
222
264
 
223
- if File.directory?(name)
224
- writer.mkdir(file, mode)
225
- next
226
- end
265
+ Gem::Package::TarWriter.new(io) do
227
266
 
228
- if File.file?(name)
229
- writer.add_file(file, mode) do |stream|
230
- stream.write(File.binread(name))
267
+ # @type [Gem::Package::TarWriter] writer
268
+ |writer|
269
+ self.find_files(File.join(path, '**/*')).each do |name|
270
+ mode = File.stat(name).mode
271
+ file = name.slice(offset ..)
272
+
273
+ if verbose
274
+ self.echo(": #{file}", Color::GREEN)
275
+ end
276
+
277
+ if File.directory?(name)
278
+ writer.mkdir(file, mode)
279
+ next
280
+ end
281
+
282
+ if File.file?(name)
283
+ writer.add_file(file, mode) do |stream|
284
+ stream.write(File.binread(name))
285
+ end
286
+ next
231
287
  end
232
- next
233
288
  end
234
289
  end
290
+
291
+ io.rewind
292
+ return io.string
235
293
  end
236
294
 
237
- io.rewind
238
- return io.string
239
- end
295
+ # @param [String] file
296
+ # @param [String] path
297
+ # @param [Boolean] verbose
298
+ def self.gzip(file, path, verbose: false)
299
+ Zlib::GzipWriter.open(file, Zlib::BEST_COMPRESSION) do
300
+
301
+ # @type [Zlib::GzipWriter] writer
302
+ |writer|
303
+ writer.write(self.tar(path, verbose: verbose))
304
+ end
305
+ end
306
+
307
+ # @param [Integer] uid
308
+ # @param [Integer] gid
309
+ def self.update_user(uid: 500, gid: 500)
310
+ self.execute('deluser', USER, force: true)
311
+ self.execute('delgroup', GROUP, force: true)
312
+ self.execute('addgroup', { system: true, gid: ENV.fetch('DOCKER_CORE_GID', gid) }, GROUP)
313
+ self.execute('adduser', { system: true, 'disabled-password': true, 'no-create-home': true, ingroup: GROUP, gecos: USER, shell: '/bin/bash', uid: ENV.fetch('DOCKER_CORE_UID', uid) }, USER)
314
+ end
315
+
316
+ # @param [String] stdout
317
+ # @param [String] stderr
318
+ def self.link_logs(stdout: '', stderr: '')
319
+ if '' != stdout
320
+ File.symlink('/dev/stdout', File.path(stdout))
321
+ end
322
+
323
+ if '' != stderr
324
+ File.symlink('/dev/stderr', File.path(stderr))
325
+ end
326
+ end
327
+
328
+ # @param [Array<String>] arguments
329
+ def self.clear_folders(*arguments, mode: nil)
330
+ self.remove_folders(*arguments)
331
+ self.make_folders(*arguments, mode: mode)
332
+ end
333
+
334
+ def self.architecture
335
+ hash = { x86: '386', x86_64: 'amd64', armhf: 'armv6', aarch64: 'arm64' }
336
+ machine = "#{Etc.uname[:machine]}"
337
+ return hash.fetch(machine.to_sym, machine)
338
+ end
240
339
 
241
- # @param [String] file
242
- # @param [String] path
243
- # @param [Boolean] verbose
244
- def self.gzip(file, path, verbose: false)
245
- # @type [Zlib::GzipWriter] writer
246
- Zlib::GzipWriter.open(file, Zlib::BEST_COMPRESSION) do |writer|
247
- writer.write(self.tar(path, verbose: verbose))
340
+ # @param [String] repository
341
+ def self.github_latest_version(repository)
342
+ uri = URI("https://api.github.com/repos/#{repository}/releases/latest")
343
+ return JSON.parse(Net::HTTP.get(uri))['tag_name']
248
344
  end
249
345
  end
250
346
 
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.13
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - agrozyme
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-13 00:00:00.000000000 Z
11
+ date: 2021-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fileutils
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: zlib
43
57
  requirement: !ruby/object:Gem::Requirement