ubuntu_unused_kernels 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7f6c6be4a207dbcac06fb28e99713849a3db1c2
4
+ data.tar.gz: a2390b68d68f1702fde29f9df87249e1f013cc86
5
+ SHA512:
6
+ metadata.gz: 569cc0e35313ec492d1f5eef8fe50bbc01eb1528ed96f4e21ec47de842e46521059b15f4f0334eb5b40cfc5a63053dee6df7cfbe1cb0ce43f2a489cebb2ae54c
7
+ data.tar.gz: c26896d6bd2f4a67ca844a8663506d8eadf895d5ecfc8c6661487607155f6eea22f6895b3281a2a9f4e9c500284c5a59f0594b3dabb310b58a736b28b572536f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ 2015-04-01 Release 0.1.0
2
+ - Initial release ported from internal repository
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ubuntu_unused_kernels.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Dan Carley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # ubuntu-unused-kernels
2
+
3
+ Identify old kernel packages that can be deleted from Ubuntu machines. It
4
+ will omit the currently running kernel and the latest kernel that would be
5
+ used if the machine was rebooted.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'ubuntu_unused_kernels'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ubuntu_unused_kernels
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ sudo apt-get purge $(ubuntu-unused-kernels)
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( http://github.com/gds-operations/ubuntu_unused_kernels/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require "rubocop/rake_task"
7
+ RuboCop::RakeTask.new(:rubocop) do |task|
8
+ task.options = ['--lint']
9
+ end
10
+
11
+ require "gem_publisher"
12
+ task :publish_gem do
13
+ gem = GemPublisher.publish_if_updated("ubuntu_unused_kernels.gemspec", :rubygems)
14
+ puts "Published #{gem}" if gem
15
+ end
16
+
17
+ task :default => [:rubocop, :spec]
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ubuntu_unused_kernels'
4
+ puts UbuntuUnusedKernels.to_remove.join("\n")
data/jenkins.sh ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ export RBENV_VERSION=2.2
5
+ bundle
6
+ bundle exec rake
7
+ bundle exec rake publish_gem
@@ -0,0 +1,3 @@
1
+ module UbuntuUnusedKernels
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,53 @@
1
+ require "ubuntu_unused_kernels/version"
2
+ require "open3"
3
+
4
+ module UbuntuUnusedKernels
5
+ DPKG_PATTERN = %w{linux-image-* linux-headers-*}
6
+ VERSION_REGEX = %r{\d+\.\d+\.\d+-\d+}
7
+
8
+ class << self
9
+ def to_remove
10
+ current = get_current
11
+ packages = get_installed
12
+
13
+ latest = packages.map { |package|
14
+ package.match(VERSION_REGEX)[0]
15
+ }.sort.last
16
+
17
+ packages.reject! { |package|
18
+ package =~ /\b#{Regexp.escape(current)}\b/ ||
19
+ package =~ /\b#{Regexp.escape(latest)}\b/
20
+ }
21
+
22
+ return packages
23
+ end
24
+
25
+ def get_current
26
+ uname = Open3.capture2('uname', '-r')
27
+ raise "Unable to determine current kernel" unless uname.last.success?
28
+
29
+ match = uname.first.chomp.match(/^(#{VERSION_REGEX})-[[:alpha:]]+$/)
30
+ raise "Unable to determine current kernel" unless match
31
+
32
+ return match[1]
33
+ end
34
+
35
+ def get_installed
36
+ dpkg = Open3.capture2(
37
+ 'dpkg-query', '--show',
38
+ '--showformat', '${Package}\t${Version}\n',
39
+ *DPKG_PATTERN
40
+ )
41
+ raise "Unable to get list of packages" unless dpkg.last.success?
42
+
43
+ packages = dpkg.first.split("\n")
44
+ packages.map! { |p| p.split("\t") }
45
+ packages.reject! { |p| p[1].nil? }
46
+ packages.map! { |p| p[0] }
47
+ packages.reject! { |p| p !~ VERSION_REGEX }
48
+ raise "No kernel packages found" if packages.empty?
49
+
50
+ return packages
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'ubuntu_unused_kernels'
@@ -0,0 +1,219 @@
1
+ require 'spec_helper'
2
+
3
+ describe UbuntuUnusedKernels do
4
+ it 'should have a version number' do
5
+ expect(UbuntuUnusedKernels::VERSION).to_not be_nil
6
+ end
7
+
8
+ describe 'to_remove' do
9
+ describe 'one kernel installed, is current and latest' do
10
+ it 'should return nothing' do
11
+ allow(subject).to receive(:get_current).with(no_args).and_return('3.13.0-43')
12
+ allow(subject).to receive(:get_installed).with(no_args).and_return(%w{
13
+ linux-headers-3.13.0-43
14
+ linux-headers-3.13.0-43-generic
15
+ linux-image-3.13.0-43-generic
16
+ })
17
+
18
+ expect(subject.to_remove).to eq([])
19
+ end
20
+ end
21
+
22
+ describe 'five kernels installed' do
23
+ let(:installed) {
24
+ %w{
25
+ linux-headers-3.13.0-39
26
+ linux-headers-3.13.0-39-generic
27
+ linux-headers-3.13.0-40
28
+ linux-headers-3.13.0-40-generic
29
+ linux-headers-3.13.0-41
30
+ linux-headers-3.13.0-41-generic
31
+ linux-headers-3.13.0-42
32
+ linux-headers-3.13.0-42-generic
33
+ linux-headers-3.13.0-43
34
+ linux-headers-3.13.0-43-generic
35
+ linux-image-3.13.0-39-generic
36
+ linux-image-3.13.0-40-generic
37
+ linux-image-3.13.0-41-generic
38
+ linux-image-3.13.0-42-generic
39
+ linux-image-3.13.0-43-generic
40
+ }
41
+ }
42
+
43
+ describe 'current is latest' do
44
+ it 'should return everything except current/latest' do
45
+ allow(subject).to receive(:get_current).with(no_args).and_return('3.13.0-43')
46
+ allow(subject).to receive(:get_installed).with(no_args).and_return(installed)
47
+
48
+ expect(subject.to_remove).to match_array(%w{
49
+ linux-headers-3.13.0-39
50
+ linux-headers-3.13.0-39-generic
51
+ linux-headers-3.13.0-40
52
+ linux-headers-3.13.0-40-generic
53
+ linux-headers-3.13.0-41
54
+ linux-headers-3.13.0-41-generic
55
+ linux-headers-3.13.0-42
56
+ linux-headers-3.13.0-42-generic
57
+ linux-image-3.13.0-39-generic
58
+ linux-image-3.13.0-40-generic
59
+ linux-image-3.13.0-41-generic
60
+ linux-image-3.13.0-42-generic
61
+ })
62
+ end
63
+ end
64
+
65
+ describe 'current is not latest' do
66
+ it 'should return everything except current and latest' do
67
+ allow(subject).to receive(:get_current).with(no_args).and_return('3.13.0-41')
68
+ allow(subject).to receive(:get_installed).with(no_args).and_return(installed)
69
+
70
+ expect(subject.to_remove).to match_array(%w{
71
+ linux-headers-3.13.0-39
72
+ linux-headers-3.13.0-39-generic
73
+ linux-headers-3.13.0-40
74
+ linux-headers-3.13.0-40-generic
75
+ linux-headers-3.13.0-42
76
+ linux-headers-3.13.0-42-generic
77
+ linux-image-3.13.0-39-generic
78
+ linux-image-3.13.0-40-generic
79
+ linux-image-3.13.0-42-generic
80
+ })
81
+ end
82
+ end
83
+
84
+ describe 'unsorted list of kernels' do
85
+ it 'should return everything except current and latest' do
86
+ allow(subject).to receive(:get_current).with(no_args).and_return('3.13.0-41')
87
+ allow(subject).to receive(:get_installed).with(no_args).and_return(installed.shuffle)
88
+
89
+ expect(subject.to_remove).to match_array(%w{
90
+ linux-headers-3.13.0-39
91
+ linux-headers-3.13.0-39-generic
92
+ linux-headers-3.13.0-40
93
+ linux-headers-3.13.0-40-generic
94
+ linux-headers-3.13.0-42
95
+ linux-headers-3.13.0-42-generic
96
+ linux-image-3.13.0-39-generic
97
+ linux-image-3.13.0-40-generic
98
+ linux-image-3.13.0-42-generic
99
+ })
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ describe 'get_current' do
106
+ describe 'normal operation' do
107
+ it 'should return version without suffix' do
108
+ allow(Open3).to receive(:capture2).with('uname', '-r').and_return(
109
+ Open3.capture2('echo', '3.13.0-43-generic')
110
+ )
111
+
112
+ expect(subject.get_current).to eq('3.13.0-43')
113
+ end
114
+ end
115
+
116
+ describe 'unable to parse version' do
117
+ it 'should raise exception' do
118
+ allow(Open3).to receive(:capture2).with('uname', '-r').and_return(
119
+ Open3.capture2('echo', '123-generic')
120
+ )
121
+
122
+ expect { subject.get_current }.to raise_error(
123
+ RuntimeError, "Unable to determine current kernel"
124
+ )
125
+ end
126
+ end
127
+
128
+ describe 'unable to parse suffix' do
129
+ it 'should raise an exception' do
130
+ allow(Open3).to receive(:capture2).with('uname', '-r').and_return(
131
+ Open3.capture2('echo', '3.13.0-43')
132
+ )
133
+
134
+ expect { subject.get_current }.to raise_error(
135
+ RuntimeError, "Unable to determine current kernel"
136
+ )
137
+ end
138
+ end
139
+
140
+ describe 'command returns correct output but non-zero exit code' do
141
+ it 'should raise exception' do
142
+ allow(Open3).to receive(:capture2).with('uname', '-r').and_return(
143
+ Open3.capture2('bash', '-c', 'echo 3.13.0-43-generic; exit 1')
144
+ )
145
+
146
+ expect { subject.get_current }.to raise_error(
147
+ RuntimeError, "Unable to determine current kernel"
148
+ )
149
+ end
150
+ end
151
+ end
152
+
153
+ describe 'get_installed' do
154
+ describe 'three kernels fully and one partially installed' do
155
+ # Beware, hardtabs, including trailing.
156
+ let(:installed) { <<EOS
157
+ linux-headers-3.0
158
+ linux-headers-3.13.0-29 3.13.0-29.53
159
+ linux-headers-3.13.0-29-generic 3.13.0-29.53
160
+ linux-headers-3.13.0-45
161
+ linux-headers-3.13.0-45-generic
162
+ linux-headers-3.13.0-46 3.13.0-46.79
163
+ linux-headers-3.13.0-46-generic 3.13.0-46.79
164
+ linux-headers-generic 3.13.0.46.53
165
+ linux-headers-virtual 3.13.0.46.53
166
+ linux-image-3.0
167
+ linux-image-3.13.0-29-generic 3.13.0-29.53
168
+ linux-image-3.13.0-45-generic 3.13.0-45.74
169
+ linux-image-3.13.0-46-generic 3.13.0-46.79
170
+ linux-image-virtual 3.13.0.46.53
171
+ EOS
172
+ }
173
+
174
+ it 'should return non-meta packages that have an installed version' do
175
+ allow(Open3).to receive(:capture2).with(
176
+ 'dpkg-query', '--show',
177
+ '--showformat', '${Package}\t${Version}\n',
178
+ 'linux-image-*', 'linux-headers-*',
179
+ ).and_return(
180
+ Open3.capture2('echo', installed)
181
+ )
182
+
183
+ expect(subject.get_installed()).to match_array(%w{
184
+ linux-headers-3.13.0-29
185
+ linux-headers-3.13.0-29-generic
186
+ linux-headers-3.13.0-46
187
+ linux-headers-3.13.0-46-generic
188
+ linux-image-3.13.0-29-generic
189
+ linux-image-3.13.0-45-generic
190
+ linux-image-3.13.0-46-generic
191
+ })
192
+ end
193
+ end
194
+
195
+ describe 'no kernels installed' do
196
+ it 'should raise an exception because current or latest should be present' do
197
+ allow(Open3).to receive(:capture2).with(any_args).and_return(
198
+ Open3.capture2('echo')
199
+ )
200
+
201
+ expect { subject.get_installed() }.to raise_error(
202
+ RuntimeError, "No kernel packages found"
203
+ )
204
+ end
205
+ end
206
+
207
+ describe 'command returns non-zero exit code' do
208
+ it 'should raise an exception' do
209
+ allow(Open3).to receive(:capture2).with(any_args).and_return(
210
+ Open3.capture2('bash', '-c', 'echo foo; exit 1')
211
+ )
212
+
213
+ expect { subject.get_installed() }.to raise_error(
214
+ RuntimeError, "Unable to get list of packages"
215
+ )
216
+ end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ubuntu_unused_kernels/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ubuntu_unused_kernels"
8
+ spec.version = UbuntuUnusedKernels::VERSION
9
+ spec.authors = ["Dan Carley"]
10
+ spec.email = ["dan.carley@gmail.com"]
11
+ spec.summary = %q{Identify unused Ubuntu kernels}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/gds-operations/ubuntu_unused_kernels"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rubocop"
25
+ spec.add_development_dependency "gem_publisher"
26
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ubuntu_unused_kernels
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Carley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: gem_publisher
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Identify unused Ubuntu kernels
84
+ email:
85
+ - dan.carley@gmail.com
86
+ executables:
87
+ - ubuntu-unused-kernels
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CHANGELOG
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/ubuntu-unused-kernels
100
+ - jenkins.sh
101
+ - lib/ubuntu_unused_kernels.rb
102
+ - lib/ubuntu_unused_kernels/version.rb
103
+ - spec/spec_helper.rb
104
+ - spec/ubuntu_unused_kernels_spec.rb
105
+ - ubuntu_unused_kernels.gemspec
106
+ homepage: https://github.com/gds-operations/ubuntu_unused_kernels
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.5
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Identify unused Ubuntu kernels
130
+ test_files:
131
+ - spec/spec_helper.rb
132
+ - spec/ubuntu_unused_kernels_spec.rb