Skip to content

Commit

Permalink
Refactor the inital code to comply with puppet module standards
Browse files Browse the repository at this point in the history
* split the main class to install, uninstall.pp
* use private variables wherever possible
* introduce unit tests
* improve the latest install to fetch last version
  • Loading branch information
jimirocks committed Apr 14, 2020
1 parent 157b08f commit dd5de7b
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 68 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ group :development do
gem "puppet-module-posix-dev-r#{minor_version}", '~> 0.4', require: false, platforms: [:ruby]
gem "puppet-module-win-default-r#{minor_version}", '~> 0.4', require: false, platforms: [:mswin, :mingw, :x64_mingw]
gem "puppet-module-win-dev-r#{minor_version}", '~> 0.4', require: false, platforms: [:mswin, :mingw, :x64_mingw]
gem "webmock", '~> 3.8.3'
end

puppet_version = ENV['PUPPET_GEM_VERSION']
Expand Down
26 changes: 22 additions & 4 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@

**Classes**

_Public Classes_

* [`rclone`](#rclone): Download and install Rclone

_Private Classes_

* `rclone::install`: Ensures Rclone installed
* `rclone::uninstall`: Removes rclone installed by this module

**Functions**

_Public Functions_


_Private Functions_

* `rclone::last_version`: Fetches the last released Rclone version from internet

## Classes

### rclone
Expand All @@ -25,11 +41,13 @@ include rclone

The following parameters are available in the `rclone` class.

##### `version`
##### `ensure`

Data type: `Pattern[/absent/, /latest/, /\d+\.\d+\.\d+/]`

Data type: `String`
installed version, can be 'latest', 'absent' or valid version string

installed version
Default value: 'latest'

Default value: 'current'
## Functions

17 changes: 17 additions & 0 deletions lib/puppet/functions/rclone/last_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'net/http'

# @summary Fetches the last released Rclone version from internet
#
# @api private
Puppet::Functions.create_function(:'rclone::last_version') do
# @return [String] last released Rclone version
def last_version
uri = URI('https://downloads.rclone.org/version.txt')
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
response = http.request Net::HTTP::Get.new(uri)
response.body.gsub(%r{.*rclone v(\d+\.\d+\.\d+).*}, '\1')
end
end
end
73 changes: 9 additions & 64 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,29 @@
#
# Install rclone binary and man page
#
# @param [String] version
# installed version
#
# @example
# include rclone
#
# @param ensure
# installed version, can be 'latest', 'absent' or valid version string
#
class rclone(
String $version = 'current',
Pattern[/absent/, /latest/, /\d+\.\d+\.\d+/] $ensure = 'latest',
) {

$os = $facts['os']['family'] ? {
/(Debian|Ubuntu)/ => 'linux',
default => 'UnknownOS',
}

$architecture = $facts['os']['architecture'] ? {
/arm.*/ => 'arm',
/(amd64|x86_64)/ => 'amd64',
}

$download_path = '/tmp/rclone.zip'
$install_dir = '/opt/rclone'
$instance = "rclone-v${version}-${os}-${architecture}"
$instance_binary = "${install_dir}/${instance}/rclone"
$instance_man_page = "${install_dir}/${instance}/rclone.1"

$binary = '/usr/bin/rclone'
$man_page_dir = '/usr/local/share/man/man1'
$man_page = "${man_page_dir}/rclone.1"

if !defined(File['/opt']) {
file { '/opt':
ensure => directory,
before => File[$install_dir],
}
}

file { $install_dir:
ensure => directory,
case $ensure {
'absent': { contain rclone::uninstall }
default: { contain rclone::install }
}

if !defined(File[$man_page_dir]) {
file { $man_page_dir:
ensure => directory,
before => File[$man_page],
}
}

archive { 'download rclone':
path => $download_path,
extract_path => $install_dir,
source => "https://downloads.rclone.org/v${version}/${instance}.zip",
extract => true,
cleanup => true,
creates => $instance_binary,
require => File[$install_dir]
}

file { $instance_binary:
owner => 'root',
mode => '0755',
subscribe => Archive['download rclone'],
}

file { $binary:
ensure => link,
target => $instance_binary,
subscribe => Archive['download rclone'],
}

file { $man_page:
ensure => link,
target => $instance_man_page,
subscribe => Archive['download rclone'],
}
~> exec { 'rclone mandb':
exec { 'rclone mandb':
command => 'mandb',
path => '/usr/bin',
refreshonly => true,
unless => 'man rclone',
}
}
72 changes: 72 additions & 0 deletions manifests/install.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# @summary Ensures Rclone installed
#
# @api private
class rclone::install {

$_os = $facts['os']['family'] ? {
/(Debian|Ubuntu)/ => 'linux',
default => fail("Unsupported OS family ${facts['os']['family']}"),
}

$_architecture = $facts['os']['architecture'] ? {
/arm.*/ => 'arm',
/(amd64|x86_64)/ => 'amd64',
}

$_version = $rclone::ensure ? {
/latest/ => rclone::last_version(),
default => $rclone::ensure,
}

$_download_path = '/tmp/rclone.zip'
$_instance = "rclone-v${_version}-${_os}-${_architecture}"
$_instance_binary = "${rclone::install_dir}/${_instance}/rclone"
$_instance_man_page = "${rclone::install_dir}/${_instance}/rclone.1"

if !defined(File['/opt']) {
file { '/opt':
ensure => directory,
before => File[$rclone::install_dir],
}
}

file { $rclone::install_dir:
ensure => directory,
}

if !defined(File[$rclone::man_page_dir]) {
file { $rclone::man_page_dir:
ensure => directory,
before => File[$rclone::man_page],
}
}

archive { 'download rclone':
path => $_download_path,
extract_path => $rclone::install_dir,
source => "https://downloads.rclone.org/v${_version}/${_instance}.zip",
extract => true,
cleanup => true,
creates => $_instance_binary,
require => File[$rclone::install_dir]
}

file { $_instance_binary:
owner => 'root',
mode => '0755',
subscribe => Archive['download rclone'],
}

file { $rclone::binary:
ensure => link,
target => $_instance_binary,
subscribe => Archive['download rclone'],
}

file { $rclone::man_page:
ensure => link,
target => $_instance_man_page,
subscribe => Archive['download rclone'],
notify => Exec['rclone mandb'],
}
}
24 changes: 24 additions & 0 deletions manifests/uninstall.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# @summary Removes rclone installed by this module
#
# @api private
class rclone::uninstall {

file { "remove ${rclone::man_page}":
ensure => absent,
path => $rclone::man_page,
notify => Exec['rclone mandb'],
}

file { "remove ${rclone::binary}":
ensure => absent,
path => $rclone::binary,
}

file { "remove ${rclone::install_dir}":
ensure => absent,
path => $rclone::install_dir,
purge => true,
recurse => true,
force => true,
}
}
Loading

0 comments on commit dd5de7b

Please sign in to comment.