kanrisuru 0.2.2 → 0.2.3

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: dc638e7e85cf76460cffecc7bcd885a7cc3f30c098c1d2fa0250daeb455e87a2
4
- data.tar.gz: 78d733e6299f3a49ce40a0be6bb73b50a8c6b9f184ceacf920bc55661653c393
3
+ metadata.gz: 8366ff4859b2cf024741e8be0ca834863fabc641c3089a9e73e070b49715209b
4
+ data.tar.gz: d74ba72f68efee3d6228fc23aa01b762aba3ab5a3bc36c40721705027a1d8157
5
5
  SHA512:
6
- metadata.gz: 2706ac5496f456560795adedbe445ecfa1509e7d3d60dde5b5d4e85a59687b1477fdf035aeba0b31a5ade24cc25c962a8cbb559a75ba5899eaf90e4f02ada212
7
- data.tar.gz: 606de07f8ddd04465df2bc0f4bd3ffbfb1e362986613a31f2184076337ba9542ea05ac1874d36c24442d1609f176dfa1bd2cf8104835fa1233d9e4c68cd7f7a4
6
+ metadata.gz: 48617976b38c0b0f230b08220fac0fa79eccc6e5a2b1669f51fcf8709a7f18fec3b6d137dc6add1c26b31979d419f5741d76252ad14a91b0a094e11f70053c00
7
+ data.tar.gz: ca0a3df92bb1dbee0b9c3d46c74ea9287adfded79a548afe41212c936e339135be96ff91cf801e958b29e576405e9ba7c02fa561f3abb7c3aa88602e1e0dbbd2
data/kanrisuru.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.author = 'Ryan Mammina'
9
9
  gem.email = 'ryan@avamia.com'
10
10
  gem.license = 'MIT'
11
- gem.summary = 'Manage remote servers with ruby.'
11
+ gem.summary = 'Manage remote servers over ssh with ruby.'
12
12
  gem.homepage = 'https://github.com/avamia/kanrisuru'
13
13
 
14
14
  gem.required_ruby_version = '>= 2.5.0'
@@ -14,6 +14,7 @@ require_relative 'core/stream'
14
14
  require_relative 'core/transfer'
15
15
  require_relative 'core/ip'
16
16
  require_relative 'core/socket'
17
+ require_relative 'core/apt'
17
18
 
18
19
  module Kanrisuru
19
20
  module Remote
@@ -32,6 +33,7 @@ module Kanrisuru
32
33
  os_include Kanrisuru::Core::Transfer
33
34
  os_include Kanrisuru::Core::IP
34
35
  os_include Kanrisuru::Core::Socket
36
+ os_include Kanrisuru::Core::Apt
35
37
  end
36
38
 
37
39
  class Cluster
@@ -48,6 +50,7 @@ module Kanrisuru
48
50
  os_collection Kanrisuru::Core::Transfer
49
51
  os_collection Kanrisuru::Core::IP
50
52
  os_collection Kanrisuru::Core::Socket
53
+ os_collection Kanrisuru::Core::Apt
51
54
  end
52
55
  end
53
56
  end
@@ -0,0 +1,337 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module Apt
6
+ extend OsPackage::Define
7
+
8
+ os_define :debian, :apt
9
+
10
+ AptSource = Struct.new(:url, :dist, :architecture)
11
+ AptPackageOverview = Struct.new(:package, :version, :suites, :architecture, :installed, :upgradeable, :automatic)
12
+ AptPackageDetail = Struct.new(
13
+ :package,
14
+ :version,
15
+ :priority,
16
+ :section,
17
+ :origin,
18
+ :maintainer,
19
+ :original_maintainer,
20
+ :bugs,
21
+ :install_size,
22
+ :dependencies,
23
+ :recommends,
24
+ :provides,
25
+ :suggests,
26
+ :breaks,
27
+ :conflicts,
28
+ :replaces,
29
+ :homepage,
30
+ :task,
31
+ :supported,
32
+ :download_size,
33
+ :apt_manual_installed,
34
+ :apt_sources,
35
+ :description,
36
+ :summary
37
+ )
38
+
39
+ def apt(action, opts = {})
40
+ case action
41
+ when 'list'
42
+ apt_list(opts)
43
+ when 'update'
44
+ apt_update(opts)
45
+ when 'upgrade'
46
+ apt_upgrade(opts)
47
+ when 'full-upgrade', 'full_upgrade'
48
+ apt_full_upgrade(opts)
49
+ when 'install'
50
+ apt_install(opts)
51
+ when 'remove'
52
+ apt_remove(opts)
53
+ when 'purge'
54
+ apt_purge(opts)
55
+ when 'autoremove'
56
+ apt_autoremove(opts)
57
+ when 'search'
58
+ apt_search(opts)
59
+ when 'show'
60
+ apt_show(opts)
61
+ when 'clean'
62
+ apt_clean(opts)
63
+ when 'autoclean'
64
+ apt_autoclean(opts)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def apt_autoclean(_opts)
71
+ command = Kanrisuru::Command.new('apt-get autoclean')
72
+ execute_shell(command)
73
+ Kanrisuru::Result.new(command)
74
+ end
75
+
76
+ def apt_clean(_opts)
77
+ command = Kanrisuru::Command.new('apt-get clean')
78
+ execute_shell(command)
79
+ Kanrisuru::Result.new(command)
80
+ end
81
+
82
+ def apt_search(opts)
83
+ command = Kanrisuru::Command.new('apt search')
84
+ command << opts[:query]
85
+
86
+ execute_shell(command)
87
+
88
+ Kanrisuru::Result.new(command) do |cmd|
89
+ lines = cmd.to_a
90
+ lines.shift
91
+ lines.shift
92
+
93
+ result = []
94
+
95
+ lines.each do |line|
96
+ next unless line.include?('/')
97
+
98
+ item = parse_apt_line(line)
99
+ next unless item
100
+
101
+ result << item
102
+ end
103
+
104
+ result
105
+ end
106
+ end
107
+
108
+ def apt_list(opts)
109
+ command = Kanrisuru::Command.new('apt list')
110
+ command.append_flag('--installed', opts[:installed])
111
+ command.append_flag('--upgradeable', opts[:upgradeable])
112
+ command.append_flag('--all-versions', opts[:all_versions])
113
+ command.append_arg('-a', opts[:package_name])
114
+
115
+ execute_shell(command)
116
+
117
+ Kanrisuru::Result.new(command) do |cmd|
118
+ lines = cmd.to_a
119
+ lines.shift
120
+
121
+ result = []
122
+ lines.each.with_index do |line, _index|
123
+ item = parse_apt_line(line)
124
+ next unless item
125
+
126
+ result << item
127
+ end
128
+
129
+ result
130
+ end
131
+ end
132
+
133
+ def apt_autoremove(_opts)
134
+ command = Kanrisuru::Command.new('apt-get autoremove')
135
+ command.append_flag('-y')
136
+
137
+ execute_shell(command)
138
+ Kanrisuru::Result.new(command)
139
+ end
140
+
141
+ def apt_purge(opts)
142
+ command = Kanrisuru::Command.new('apt-get purge')
143
+ command.append_flag('-y')
144
+
145
+ packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
146
+ command << packages
147
+
148
+ execute_shell(command)
149
+ Kanrisuru::Result.new(command)
150
+ end
151
+
152
+ def apt_remove(opts)
153
+ command = Kanrisuru::Command.new('apt-get remove')
154
+ command.append_flag('-y')
155
+
156
+ packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
157
+ command << packages
158
+
159
+ execute_shell(command)
160
+ Kanrisuru::Result.new(command)
161
+ end
162
+
163
+ def apt_install(opts)
164
+ command = Kanrisuru::Command.new('apt-get install')
165
+ command.append_flag('-y')
166
+
167
+ command.append_flag('--no-upgrade', opts[:no_upgrade])
168
+ command.append_flag('--only-upgrade', opts[:only_upgrade])
169
+ command.append_flag('--reinstall', opts[:reinstall])
170
+
171
+ packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
172
+ command << packages
173
+
174
+ execute_shell(command)
175
+ Kanrisuru::Result.new(command)
176
+ end
177
+
178
+ def apt_full_upgrade(_opts)
179
+ command = Kanrisuru::Command.new('apt full-upgrade')
180
+ command.append_flag('-y')
181
+ execute_shell(command)
182
+ Kanrisuru::Result.new(command)
183
+ end
184
+
185
+ def apt_upgrade(_opts)
186
+ command = Kanrisuru::Command.new('apt-get upgrade')
187
+ command.append_flag('-y')
188
+ execute_shell(command)
189
+ Kanrisuru::Result.new(command)
190
+ end
191
+
192
+ def apt_update(_opts)
193
+ command = Kanrisuru::Command.new('apt-get update')
194
+ command.append_flag('-y')
195
+ execute_shell(command)
196
+ Kanrisuru::Result.new(command)
197
+ end
198
+
199
+ def apt_show(opts)
200
+ command = Kanrisuru::Command.new('apt show')
201
+ command.append_flag('-a')
202
+
203
+ packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
204
+ command << packages
205
+
206
+ execute_shell(command)
207
+
208
+ Kanrisuru::Result.new(command) do |cmd|
209
+ lines = cmd.to_a
210
+ rows = []
211
+
212
+ current_row = nil
213
+ summary = ''
214
+
215
+ lines.each do |line|
216
+ next if line == 'WARNING: apt does not have a stable CLI interface. Use with caution in scripts.'
217
+ next if [] ['', nil, '.'].include?(line)
218
+
219
+ case line
220
+ when /^Package:/
221
+ unless current_row.nil?
222
+ current_row.summary = summary.strip
223
+ summary = ''
224
+ rows << current_row
225
+ end
226
+
227
+ current_row = AptPackageDetail.new
228
+ current_row.package = extract_single_line(line)
229
+ when /^Version:/
230
+ current_row.version = extract_single_line(line)
231
+ when /^Priority:/
232
+ current_row.priority = extract_single_line(line)
233
+ when /^Section:/
234
+ current_row.section = extract_single_line(line)
235
+ when /^Origin:/
236
+ current_row.origin = extract_single_line(line)
237
+ when /^Maintainer:/
238
+ current_row.maintainer = extract_single_line(line)
239
+ when /^Original-Maintainer:/
240
+ current_row.original_maintainer = extract_single_line(line)
241
+ when /^Bugs:/
242
+ current_row.bugs = extract_single_line(line)
243
+ when /^Installed-Size:/
244
+ current_row.install_size = normalize_size(extract_single_line(line))
245
+ when /^Download-Size:/
246
+ current_row.download_size = normalize_size(extract_single_line(line))
247
+ when /^Depends:/
248
+ current_row.dependencies = parse_comma_values(extract_single_line(line))
249
+ when /^Provides:/
250
+ current_row.provides = parse_comma_values(extract_single_line(line))
251
+ when /^Recommends:/
252
+ current_row.recommends = parse_comma_values(extract_single_line(line))
253
+ when /^Suggests:/
254
+ current_row.suggests = parse_comma_values(extract_single_line(line))
255
+ when /^Breaks:/
256
+ current_row.breaks = parse_comma_values(extract_single_line(line))
257
+ when /^Conflicts:/
258
+ current_row.conflicts = parse_comma_values(extract_single_line(line))
259
+ when /^Replaces:/
260
+ current_row.replaces = parse_comma_values(extract_single_line(line))
261
+ when /^Homepage:/
262
+ current_row.homepage = extract_single_line(line)
263
+ when /^Task:/
264
+ current_row.task = parse_comma_values(extract_single_line(line))
265
+ when /^Supported:/
266
+ current_row.supported = extract_single_line(line)
267
+ when /^APT-Sources:/
268
+ current_row.apt_sources = parse_apt_sources(extract_single_line(line))
269
+ when /^APT-Manual-Installed:/
270
+ current_row.apt_manual_installed = extract_single_line(line) == 'yes'
271
+ when /^Description:/
272
+ current_row.description = extract_single_line(line)
273
+ else
274
+ summary += " #{line.strip}"
275
+ end
276
+ end
277
+
278
+ current_row.summary = summary.strip
279
+ rows << current_row
280
+ rows
281
+ end
282
+ end
283
+
284
+ def extract_single_line(line)
285
+ line.split(': ')[1]
286
+ end
287
+
288
+ def parse_comma_values(string)
289
+ string.split(', ')
290
+ end
291
+
292
+ def parse_apt_sources(string)
293
+ url, dist, architecture, = string.split
294
+ AptSource.new(url, dist, architecture)
295
+ end
296
+
297
+ def normalize_size(string)
298
+ size, unit = string.split
299
+ size = size.to_i
300
+ case unit.downcase
301
+ when 'b'
302
+ Kanrisuru::Util::Bits.convert_bytes(size, :byte, :kilobyte)
303
+ when 'kb'
304
+ size
305
+ when 'mb'
306
+ Kanrisuru::Util::Bits.convert_from_mb(size, :kilobyte).to_i
307
+ when 'gb'
308
+ Kanrisuru::Util::Bits.convert_from_gb(size, :kilobyte).to_i
309
+ end
310
+ end
311
+
312
+ def parse_apt_line(line)
313
+ values = line.split('/')
314
+ return if values.length < 2
315
+
316
+ package = values[0]
317
+
318
+ values = values[1].split
319
+ suites = values[0].split(',')
320
+ version = values[1]
321
+ architecture = values[2]
322
+
323
+ installed = false
324
+ upgradeable = false
325
+ automatic = false
326
+
327
+ if values.length > 3
328
+ installed = values[3].include?('installed')
329
+ upgradeable = values[3].include?('upgradeable')
330
+ automatic = values[3].include?('automatic')
331
+ end
332
+
333
+ AptPackageOverview.new(package, version, suites, architecture, installed, upgradeable, automatic)
334
+ end
335
+ end
336
+ end
337
+ end
@@ -15,13 +15,13 @@ module Kanrisuru
15
15
  !Kanrisuru::Util.blank?(value)
16
16
  end
17
17
 
18
- def self.array_join_string(arg)
19
- arg.instance_of?(Array) ? arg.join(',') : arg
18
+ def self.array_join_string(arg, field = ',')
19
+ arg.instance_of?(Array) ? arg.join(field) : arg
20
20
  end
21
21
 
22
- def self.string_join_array(arg)
22
+ def self.string_join_array(arg, field = ',')
23
23
  array = arg.instance_of?(String) ? [arg] : arg
24
- array.join(',')
24
+ array.join(field)
25
25
  end
26
26
 
27
27
  def self.numeric?(value)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.2.2'
4
+ VERSION = '0.2.3'
5
5
  end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Apt do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+ let(:host) do
10
+ Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+ end
16
+
17
+ after do
18
+ host.disconnect
19
+ end
20
+
21
+ describe 'apt install' do
22
+ it 'installs package' do
23
+ case os_name
24
+ when 'debian', 'ubuntu'
25
+ host.su('root')
26
+ result = host.apt('install', packages: %w[ffmpeg curl])
27
+ expect(result).to be_success
28
+ end
29
+ end
30
+ end
31
+
32
+ describe 'apt remove' do
33
+ it 'removes installed packages' do
34
+ case os_name
35
+ when 'debian', 'ubuntu'
36
+ host.su('root')
37
+ result = host.apt('remove', packages: ['ffmpeg'])
38
+ expect(result).to be_success
39
+ end
40
+ end
41
+
42
+ it 'purges installed packages' do
43
+ case os_name
44
+ when 'debian', 'ubuntu'
45
+ host.su('root')
46
+ result = host.apt('purge', packages: ['ffmpeg'])
47
+ expect(result).to be_success
48
+ end
49
+ end
50
+ end
51
+
52
+ describe 'apt autoremove' do
53
+ it 'removes unused packages' do
54
+ case os_name
55
+ when 'debian', 'ubuntu'
56
+ host.su('root')
57
+ result = host.apt('autoremove')
58
+ expect(result).to be_success
59
+ end
60
+ end
61
+ end
62
+
63
+ describe 'apt clean' do
64
+ it 'cleans packages' do
65
+ case os_name
66
+ when 'debian', 'ubuntu'
67
+ host.su('root')
68
+ result = host.apt('clean')
69
+ expect(result).to be_success
70
+ end
71
+ end
72
+
73
+ it 'autocleans packages' do
74
+ case os_name
75
+ when 'debian', 'ubuntu'
76
+ host.su('root')
77
+ result = host.apt('autoclean')
78
+ expect(result).to be_success
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'apt upgrade' do
84
+ it 'upgrades packages' do
85
+ case os_name
86
+ when 'debian', 'ubuntu'
87
+ host.su('root')
88
+ result = host.apt('upgrade')
89
+ expect(result).to be_success
90
+ end
91
+ end
92
+
93
+ it 'fully upgrades packages' do
94
+ case os_name
95
+ when 'debian', 'ubuntu'
96
+ host.su('root')
97
+ result = host.apt('full-upgrade')
98
+ expect(result).to be_success
99
+ end
100
+ end
101
+ end
102
+
103
+ describe 'apt show' do
104
+ it 'shows details of listed packages' do
105
+ case os_name
106
+ when 'debian', 'ubuntu'
107
+ result = host.apt('show', packages: %w[wget curl git sudo])
108
+ expect(result).to be_success
109
+ end
110
+ end
111
+ end
112
+
113
+ describe 'apt list' do
114
+ it 'lists all packages' do
115
+ case os_name
116
+ when 'debian', 'ubuntu'
117
+ host.su('root')
118
+ result = host.apt('list')
119
+ expect(result).to be_success
120
+ end
121
+ end
122
+
123
+ it 'lists installed packages' do
124
+ case os_name
125
+ when 'debian', 'ubuntu'
126
+ host.su('root')
127
+ result = host.apt('list', installed: true)
128
+ expect(result).to be_success
129
+ end
130
+ end
131
+
132
+ it 'lists upgradable packages' do
133
+ case os_name
134
+ when 'debian', 'ubuntu'
135
+ host.su('root')
136
+ result = host.apt('list', upgradable: true)
137
+ expect(result).to be_success
138
+ end
139
+ end
140
+
141
+ it 'lists all versions for packages' do
142
+ case os_name
143
+ when 'debian', 'ubuntu'
144
+ host.su('root')
145
+ result = host.apt('list', all_versions: true)
146
+ expect(result).to be_success
147
+ end
148
+ end
149
+
150
+ it 'searches packages' do
151
+ case os_name
152
+ when 'debian', 'ubuntu'
153
+ result = host.apt('search', query: 'wget')
154
+ expect(result).to be_success
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -89,7 +89,7 @@ RSpec.describe Kanrisuru::Core::Stream do
89
89
  result = host.read_file_chunk("#{spec_dir}/test-file-chunk.txt", 2, 4)
90
90
  expect(result).to be_success
91
91
  expect(result.data.length).to eq(3)
92
- expect(result.data).to eq(['is', 'is', 'a'])
92
+ expect(result.data).to eq(%w[is is a])
93
93
  end
94
94
 
95
95
  it 'cats a file' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanrisuru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Mammina
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-16 00:00:00.000000000 Z
11
+ date: 2021-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -131,6 +131,7 @@ files:
131
131
  - lib/kanrisuru.rb
132
132
  - lib/kanrisuru/command.rb
133
133
  - lib/kanrisuru/core.rb
134
+ - lib/kanrisuru/core/apt.rb
134
135
  - lib/kanrisuru/core/archive.rb
135
136
  - lib/kanrisuru/core/disk.rb
136
137
  - lib/kanrisuru/core/file.rb
@@ -165,6 +166,7 @@ files:
165
166
  - lib/kanrisuru/util/os_family.rb
166
167
  - lib/kanrisuru/util/signal.rb
167
168
  - lib/kanrisuru/version.rb
169
+ - spec/functional/core/apt_spec.rb
168
170
  - spec/functional/core/archive_spec.rb
169
171
  - spec/functional/core/disk_spec.rb
170
172
  - spec/functional/core/file_spec.rb
@@ -219,5 +221,5 @@ requirements: []
219
221
  rubygems_version: 3.1.2
220
222
  signing_key:
221
223
  specification_version: 4
222
- summary: Manage remote servers with ruby.
224
+ summary: Manage remote servers over ssh with ruby.
223
225
  test_files: []