Skip to content

Commit c8464ec

Browse files
authored
Merge pull request #17 from danielparks/ensure-absent
(#16) Enable uninstalling Go
2 parents 98c48dd + f372e0f commit c8464ec

File tree

6 files changed

+106
-31
lines changed

6 files changed

+106
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
## main branch
66

7+
**Features**
8+
9+
* Added `ensure` parameter to allow uninstalling Go.
10+
711
**Bugfixes**
812

913
* Used pre-release version of Puppet Strings to (mostly) fix parameter default

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class { 'golang':
1818
}
1919
```
2020

21+
To uninstall Go, just do:
22+
23+
``` puppet
24+
class { 'golang':
25+
ensure => absent,
26+
}
27+
```
28+
2129
## Limitations
2230

2331
This does not support Windows.

REFERENCE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@ Most people will not need to change any parameter other than `$version`.
2020

2121
The following parameters are available in the `golang` class:
2222

23+
* [`ensure`](#-golang--ensure)
2324
* [`version`](#-golang--version)
2425
* [`link_binaries`](#-golang--link_binaries)
2526
* [`source_prefix`](#-golang--source_prefix)
2627
* [`os`](#-golang--os)
2728
* [`arch`](#-golang--arch)
2829
* [`source`](#-golang--source)
2930

31+
##### <a name="-golang--ensure"></a>`ensure`
32+
33+
Data type: `Enum[present, absent]`
34+
35+
* `present`: Make sure go is installed.
36+
* `absent`: Make sure go is uninstalled.
37+
38+
Default value: `present`
39+
3040
##### <a name="-golang--version"></a>`version`
3141

3242
Data type: `String[1]`

manifests/init.pp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#
55
# Most people will not need to change any parameter other than `$version`.
66
#
7+
# @param ensure
8+
# * `present`: Make sure go is installed.
9+
# * `absent`: Make sure go is uninstalled.
710
# @param version
811
# The version of Go to install. You can find the latest version number at
912
# https://go.dev/dl/
@@ -18,31 +21,41 @@
1821
# @param source
1922
# URL to actual archive.
2023
class golang (
21-
String[1] $version = '1.19.1',
22-
Array[String[1]] $link_binaries = ['go', 'gofmt'],
23-
String[1] $source_prefix = 'https://go.dev/dl',
24-
String[1] $os = $facts['kernel'] ? {
24+
Enum[present, absent] $ensure = present,
25+
String[1] $version = '1.19.1',
26+
Array[String[1]] $link_binaries = ['go', 'gofmt'],
27+
String[1] $source_prefix = 'https://go.dev/dl',
28+
String[1] $os = $facts['kernel'] ? {
2529
'Linux' => 'linux',
2630
'Darwin' => 'darwin',
2731
default => $facts['kernel'] # lint:ignore:parameter_documentation broken
2832
},
29-
String[1] $arch = $facts['os']['hardware'] ? {
33+
String[1] $arch = $facts['os']['hardware'] ? {
3034
undef => 'amd64', # Assume amd64 if os.hardware is missing.
3135
'aarch64' => 'arm64',
3236
'armv7l' => 'armv6l',
3337
'i686' => '386',
3438
'x86_64' => 'amd64',
3539
default => $facts['os']['hardware'], # lint:ignore:parameter_documentation broken
3640
},
37-
String[1] $source = "${source_prefix}/go${version}.${os}-${arch}.tar.gz",
41+
String[1] $source = "${source_prefix}/go${version}.${os}-${arch}.tar.gz",
3842
) {
3943
$archive_path = '/tmp/puppet-golang.tar.gz'
4044

45+
$file_ensure = $ensure ? {
46+
'present' => file,
47+
default => absent,
48+
}
49+
$link_ensure = $ensure ? {
50+
'present' => link,
51+
default => absent,
52+
}
53+
4154
include archive
4255

4356
# Used to ensure that the installation is updated when $source changes.
4457
file { '/usr/local/share/go-SOURCE':
45-
ensure => file,
58+
ensure => $file_ensure,
4659
owner => 0,
4760
group => 0, # group might be called root or wheel
4861
mode => '0644',
@@ -52,20 +65,27 @@
5265
${source}
5366
| EOF
5467
# lint:endignore
55-
notify => Exec['dp/golang refresh go installation'],
5668
}
5769

58-
# If the /usr/local/go directory exists, archive won't update it.
59-
exec { 'dp/golang refresh go installation':
60-
command => 'rm -rf /usr/local/go',
61-
path => ['/usr/local/bin', '/usr/bin', '/bin'],
62-
user => 'root',
63-
refreshonly => true,
64-
notify => Archive[$archive_path],
70+
if $ensure == present {
71+
# If the /usr/local/go directory exists, archive won't update it.
72+
exec { 'dp/golang refresh go installation':
73+
command => 'rm -rf /usr/local/go',
74+
path => ['/usr/local/bin', '/usr/bin', '/bin'],
75+
user => 'root',
76+
refreshonly => true,
77+
subscribe => File['/usr/local/share/go-SOURCE'],
78+
notify => Archive[$archive_path],
79+
}
80+
} else {
81+
file { '/usr/local/go':
82+
ensure => absent,
83+
force => true,
84+
}
6585
}
6686

6787
archive { $archive_path:
68-
ensure => present,
88+
ensure => $ensure,
6989
extract => true,
7090
extract_path => '/usr/local',
7191
source => $source,
@@ -75,7 +95,7 @@
7595

7696
$link_binaries.each |$binary| {
7797
file { "/usr/local/bin/${binary}":
78-
ensure => link,
98+
ensure => $link_ensure,
7999
target => "/usr/local/go/bin/${binary}",
80100
require => Archive[$archive_path],
81101
}

spec/acceptance/golang_spec.rb

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
require 'spec_helper_acceptance'
44

5-
describe 'Install Go' do
6-
context 'without parameters' do
7-
it { idempotent_apply('include golang') }
5+
describe 'golang' do
6+
context 'install with a specific version' do
7+
it do
8+
idempotent_apply(<<~'END')
9+
class { 'golang':
10+
version => '1.10.4',
11+
}
12+
END
13+
end
814

915
describe file('/usr/local/go') do
1016
it { is_expected.to be_directory }
@@ -23,20 +29,14 @@
2329
end
2430

2531
describe command('/usr/local/bin/go version') do
26-
its(:stdout) { is_expected.to match(%r{\Ago version }) }
32+
its(:stdout) { is_expected.to match(%r{\Ago version go1.10.4 }) }
2733
its(:stderr) { is_expected.to eq '' }
2834
its(:exit_status) { is_expected.to eq 0 }
2935
end
3036
end
3137

32-
context 'with a specific version' do
33-
it do
34-
idempotent_apply(<<~'END')
35-
class { 'golang':
36-
version => '1.10.4',
37-
}
38-
END
39-
end
38+
context 'install without parameters' do
39+
it { idempotent_apply('include golang') }
4040

4141
describe file('/usr/local/go') do
4242
it { is_expected.to be_directory }
@@ -55,9 +55,32 @@ class { 'golang':
5555
end
5656

5757
describe command('/usr/local/bin/go version') do
58-
its(:stdout) { is_expected.to match(%r{\Ago version go1.10.4 }) }
58+
its(:stdout) { is_expected.to match(%r{\Ago version }) }
5959
its(:stderr) { is_expected.to eq '' }
6060
its(:exit_status) { is_expected.to eq 0 }
6161
end
6262
end
63+
64+
describe 'uninstall' do
65+
it do
66+
# These tests are run in order, so this isn’t strictly necessary. However,
67+
# I’d prefer to avoid relying on the previous tests, so here it is. In
68+
# order to avoid unnecessary changes, this should match the last test run
69+
apply_manifest('include golang')
70+
71+
idempotent_apply(<<~'END')
72+
class { 'golang':
73+
ensure => absent,
74+
}
75+
END
76+
end
77+
78+
describe file('/usr/local/go') do
79+
it { is_expected.not_to exist }
80+
end
81+
82+
describe file('/usr/local/bin/go') do
83+
it { is_expected.not_to exist }
84+
end
85+
end
6386
end

spec/classes/golang_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
context "on #{os}" do
88
let(:facts) { os_facts }
99

10-
it { is_expected.to compile }
10+
describe 'default' do
11+
it { is_expected.to compile }
12+
end
13+
14+
describe 'ensure => absent' do
15+
let(:params) do
16+
{ ensure: 'absent' }
17+
end
18+
19+
it { is_expected.to compile }
20+
end
1121
end
1222
end
1323
end

0 commit comments

Comments
 (0)