From 8f6dc817580590aa1b5e54c044830cf61e7f2543 Mon Sep 17 00:00:00 2001 From: Stefan Goethals Date: Thu, 7 Dec 2023 15:30:50 +0100 Subject: [PATCH] Add Repo-package install option (#30) * Add Repo-package install option --- data/common.yaml | 4 ++ manifests/init.pp | 12 +++- manifests/package.pp | 126 +++++++++++++++++++++----------------- spec/classes/init_spec.rb | 19 +++++- 4 files changed, 103 insertions(+), 58 deletions(-) diff --git a/data/common.yaml b/data/common.yaml index cb86821..0777a5b 100644 --- a/data/common.yaml +++ b/data/common.yaml @@ -1,4 +1,5 @@ --- +nexus::version: null nexus::download_folder: /opt/sonatype nexus::download_proxy: null nexus::download_site: https://download.sonatype.com/nexus/3 @@ -14,3 +15,6 @@ nexus::purge_installations: true nexus::purge_default_repositories: false nexus::user: nexus nexus::work_dir: "%{lookup('nexus::install_root')}/sonatype-work/nexus3" +nexus::package_type: 'src' +nexus::package_name: 'nexus' +nexus::package_ensure: 'installed' diff --git a/manifests/init.pp b/manifests/init.pp index d6db0e6..353bc7e 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -35,6 +35,13 @@ # Set this option if you want old installations of nexus repository manager to get automatically deleted. # @param purge_default_repositories # Set this option if you want to remove the default created maven and nuget repositories. +# @param package_type +# Select 'src' for Source download & install. 'pkg' will fetch te specified package and version +# from repos you must provide. +# @param package_name +# The name of the package to install. Default 'nexus' +# @param package_ensure +# The version to install. See https://puppet.com/docs/puppet/7/types/package.html#package-attribute-ensure # # @example # class{ 'nexus': @@ -42,7 +49,7 @@ # } # class nexus ( - Pattern[/3.\d+.\d+-\d+/] $version, + Optional[Pattern[/3.\d+.\d+-\d+/]] $version, Stdlib::Absolutepath $download_folder, Stdlib::HTTPUrl $download_site, Optional[Stdlib::HTTPUrl] $download_proxy, @@ -58,6 +65,9 @@ Boolean $manage_work_dir, Boolean $purge_installations, Boolean $purge_default_repositories, + Enum['src', 'pkg'] $package_type, + Optional[String] $package_name, + String $package_ensure, ) { include stdlib diff --git a/manifests/package.pp b/manifests/package.pp index e4c4dee..544da3e 100644 --- a/manifests/package.pp +++ b/manifests/package.pp @@ -6,67 +6,83 @@ class nexus::package { assert_private() - $nexus_archive = "nexus-${nexus::version}-unix.tar.gz" - $download_url = "${nexus::download_site}/${nexus_archive}" - $dl_file = "${nexus::download_folder}/${nexus_archive}" - $install_dir = "${nexus::install_root}/nexus-${nexus::version}" + case $nexus::package_type { + 'src': { + if !$nexus::version { + fail('nexus::version must be set when using package_type => src') + } + $nexus_archive = "nexus-${nexus::version}-unix.tar.gz" + $download_url = "${nexus::download_site}/${nexus_archive}" + $dl_file = "${nexus::download_folder}/${nexus_archive}" + $install_dir = "${nexus::install_root}/nexus-${nexus::version}" - extlib::mkdir_p($nexus::install_root) + extlib::mkdir_p($nexus::install_root) - archive { $dl_file: - source => $download_url, - extract => true, - extract_path => $nexus::install_root, - checksum_url => "${download_url}.sha1", - checksum_type => 'sha1', - proxy_server => $nexus::download_proxy, - creates => $install_dir, - user => 'root', - group => 'root', - } + archive { $dl_file: + source => $download_url, + extract => true, + extract_path => $nexus::install_root, + checksum_url => "${download_url}.sha1", + checksum_type => 'sha1', + proxy_server => $nexus::download_proxy, + creates => $install_dir, + user => 'root', + group => 'root', + } - # Prevent "Couldn't flush user prefs" error - https://issues.sonatype.org/browse/NEXUS-3671 - file { ["${nexus::install_root}/.java", "${nexus::install_root}/.java/.userPrefs"]: - ensure => directory, - owner => $nexus::user, - group => $nexus::group, - mode => '0700', - } + # Prevent "Couldn't flush user prefs" error - https://issues.sonatype.org/browse/NEXUS-3671 + file { ["${nexus::install_root}/.java", "${nexus::install_root}/.java/.userPrefs"]: + ensure => directory, + owner => $nexus::user, + group => $nexus::group, + mode => '0700', + } - if $nexus::purge_installations { - File <| title == $nexus::install_root |> { - ensure => 'directory', - backup => false, - force => true, - purge => true, - recurse => true, - ignore => [ - "nexus-${nexus::version}", - 'sonatype-work', - ], - require => [ - Archive[$dl_file], - ], - before => [ - Class['nexus::service'], - ], - } - } + if $nexus::purge_installations { + File <| title == $nexus::install_root |> { + ensure => 'directory', + backup => false, + force => true, + purge => true, + recurse => true, + ignore => [ + "nexus-${nexus::version}", + 'sonatype-work', + ], + require => [ + Archive[$dl_file], + ], + before => [ + Class['nexus::service'], + ], + } + } - if $nexus::manage_work_dir { - $directories = [ - $nexus::work_dir, - "${nexus::work_dir}/etc", - "${nexus::work_dir}/log", - "${nexus::work_dir}/orient", - "${nexus::work_dir}/tmp", - ] + if $nexus::manage_work_dir { + $directories = [ + $nexus::work_dir, + "${nexus::work_dir}/etc", + "${nexus::work_dir}/log", + "${nexus::work_dir}/orient", + "${nexus::work_dir}/tmp", + ] - file { $directories: - ensure => directory, - owner => $nexus::user, - group => $nexus::group, - require => Archive[$dl_file], + file { $directories: + ensure => directory, + owner => $nexus::user, + group => $nexus::group, + require => Archive[$dl_file], + } + } + } + 'pkg': { + $install_dir = "${nexus::install_root}/nexus" + package { $nexus::package_name: + ensure => $nexus::package_ensure, + } + } + default: { + fail("Unknown value for nexus::package_type: ${nexus::package_type}") } } } diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index 1578db6..4212fc2 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -12,7 +12,7 @@ it 'fails if no version configured' do expect { is_expected.to compile }.to raise_error(RSpec::Expectations::ExpectationNotMetError, - %r{expects a value for parameter 'version'}) + %r{nexus::version must be set when using package_type => src}) end end @@ -25,7 +25,7 @@ it 'fails if no version configured' do expect { is_expected.to compile }.to raise_error(RSpec::Expectations::ExpectationNotMetError, - %r{parameter 'version' expects a match for Pattern}) + %r{parameter 'version' expects an undef value or a match for Pattern}) end end @@ -182,6 +182,21 @@ is_expected.not_to contain_user('nexus') end end + + context 'using pkg package type' do + let(:params) do + { + 'package_type' => 'pkg', + } + end + + it { is_expected.to compile } + it { + is_expected.to contain_package('nexus').with( + 'ensure' => 'installed', + ) + } + end end end end