Skip to content

Commit

Permalink
Fix rbenv and add chruby #59 (#64)
Browse files Browse the repository at this point in the history
Fix rbenv and add chruby #59
  • Loading branch information
alvagante committed Mar 21, 2018
1 parent d4ddae5 commit 344c217
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 15 deletions.
32 changes: 32 additions & 0 deletions bin/base_profile.generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
repo_dir="$(dirname $0)/.."
. "${repo_dir}/bin/functions"
app=${1:-undef}
repo=${2:-https://github.com/example42/pdk-module-template-psick-base-profile}

show_help () {
cat << EOF
This script wraps pdk (Puppet Development Kit) to create a new base profile.
It requires the pdk command from Puppet Development Kit (https://docs.puppet.com/pdk/)
You must specify the name of the base profile name and an optional source git repo.
The default template used is: https://github.com/example42/pdk-module-template-psick-base-profile
Usage:
$0 <app> [repo]
EOF
}

if [ "x${app}" == "xundef" ]; then
show_help
exit 1
fi

if [ ! -z $(which pdk) ]; then
echo_title "Creating a new psick profile class with pdk"
cd $repo_dir
pdk new class --template-url=$repo $app
else
show_help
fi

2 changes: 1 addition & 1 deletion lib/puppet/functions/gitlab_get_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# This function converts a gitlab ID to the relevant name
# of the relevant object type.
# This requires profile::gitlab::cli to be installed and properly configured
# This requires psick::gitlab::cli to be installed and properly configured
# on every Puppet master
#
require 'gitlab'
Expand Down
77 changes: 77 additions & 0 deletions manifests/chruby.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# psick::chruby
#
# @summary This psick profile manages chruby.
#
# @example Include it to install chruby
# include psick::chruby
#
# @example Include in PSICK via hiera (yaml)
# psick::profiles::linux_classes:
# chruby: psick::chruby
#
# @example Set no-noop mode and enforce changes even if noop is set for the agent
# psick::chruby::no_noop: true
#
# @param manage If to actually manage any resource in this profile or not
# @param auto_prereq If to automatically install eventual dependencies.
# Set to false if you have problems with duplicated resources, being sure that you
# provide the needed prerequistes.
# @param no_noop Set noop metaparameter to false to all the resources of this class.
# This overrides any noop setting which might be in place.
class psick::chruby (
Psick::Ensure $ensure = 'present',
String $version = '0.3.7',
String $default_ruby_version = '2.4.2',
StdLib::AbsolutePath $ruby_prefix = '/opt/rubies',
String $user = 'puppet',
Optional[String] $group = undef,
Optional[String] $sources_root = undef,
Optional[String] $download_root = undef,

Boolean $manage = $::psick::manage,
Boolean $auto_prereq = $::psick::auto_prereq,
Boolean $no_noop = false,

) {

# We declare resources only if $manage = true
if $manage {

# If no_noop is set it's enforced, unless psick::noop_mode is
if ! $::psick::noop_mode and $no_noop {
info('Forced no-noop mode in psick::chruby')
noop(false)
}

$sources_dest = $sources_root ? {
undef => "${staging_root}/sources",
default => $sources_root
}
$download_dest = $download_root ? {
undef => "${staging_root}/downloads",
default => $download_root
}
psick::netinstall { "chruby-v${version}.tar.gz":
destination_dir => $sources_dest,
url => "https://github.com/postmodern/chruby/archive/v${version}.tar.gz",
extract_command => 'tar -zxf',
owner => $user,
group => $group,
creates => "${sources_dest}/chruby-${version}",
before => Exec['install chruby'],
}

exec { 'install chruby':
cwd => "${sources_dest}/chruby-${version}",
command => 'make install',
creates => '/usr/local/share/chruby',
path => [ '/sbin', '/usr/sbin', '/bin', '/usr/bin' ],
}

file { '/etc/profile.d/chruby.sh':
ensure => 'file',
content => '. "/usr/local/share/chruby/chruby.sh"',
require => Exec['install chruby'],
}
}
}
26 changes: 26 additions & 0 deletions manifests/chruby/gem.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Derived from justinstoller/chruby module
#
define psick::chruby::gem (
String $gem = $title,
Optional[String] $gem_version = undef,
String $ruby_version = '1.9'
) {

if $gem_version {
$version_check = "| grep ${gem_version}"
$version_string = "-v${gem_version}"
} else {
$version_check = ''
$version_string = ''
}

$chruby = '/usr/local/bin/chruby-exec'
$gem_cmd = "gem install ${gem} ${version_string} --no-ri --no-rdoc"
$grep = "grep '^${gem}' ${version_check}"

exec { "install ${gem} on ${ruby_version}":
command => "${chruby} ${ruby_version} -- ${gem_cmd}",
unless => "${chruby} ${ruby_version} -- gem list | ${grep}",
}
}

12 changes: 9 additions & 3 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
# @param manage If to actually manage the resources of a class. This allows
# to skip management of resources even if classes are included. Used
# to avoid to manage some resources when building Docker images.
# @auto_prereq If to automtically manage prerequisites. Set to false here to
# @param auto_prereq If to automtically manage prerequisites. Set to false here to
# apply this value for all the PSICK profiles that honour this global
# setting. Use when you have duplicated resources.
# @auto_conf Which autoconfiguration layout to use. Default is 'none', if you
# @param auto_conf Which autoconfiguration layout to use. Default is 'none', if you
# set 'hardened' some hardened configurations are enforced by default
# @enable_firstrun If to enable firstrun mode, a special one-time only, Puppet
# @param enable_firstrun If to enable firstrun mode, a special one-time only, Puppet
# run where some specific, prerequisites, classes are applied.
# @param noop_mode Psick's noop mode. Looks for the hiera key noop_mode to check
# if to enable noop mode in the module itself. The same is done on the default
# psick-control repo. Note that if noop_mode is set to true here (or in
# Hiera's noop_mode key) the no-noop params in the psick profiles are not
# valid: If noop_mode is true, noop is enforced also where no-noop is true.
# @param is_cluster Defines if the server is a cluster member. Some PSICK profiles
# may use this value.
# @param primary_ip The server primary IP address. Default value is
Expand Down Expand Up @@ -62,6 +67,7 @@
Boolean $auto_prereq = true,
Psick::Autoconf $auto_conf = 'none',
Boolean $enable_firstrun = false,
Boolean $noop_mode = lookup('noop_mode', Boolean,'first',false),

# General network settings
Boolean $is_cluster = false,
Expand Down
10 changes: 8 additions & 2 deletions manifests/netinstall.pp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
$preextract_command = '',
$postextract_command = '',
$postextract_cwd = '',
$exec_env = []
$exec_env = [],
$creates = undef,
) {

$source_filename = url_parse($url,'filename')
Expand Down Expand Up @@ -111,6 +112,11 @@
default => $postextract_cwd,
}

$real_creates = $creates ? {
undef => "${destination_dir}/${real_extracted_dir}",
default => $creates,
}

if $preextract_command and $preextract_command != '' {
exec { "PreExtract ${source_filename} in ${destination_dir} - ${title}":
command => $preextract_command,
Expand All @@ -134,7 +140,7 @@
exec { "Extract ${source_filename} from ${work_dir} - ${title}":
command => "mkdir -p ${destination_dir} && cd ${destination_dir} && ${real_extract_command} ${work_dir}/${source_filename} ${extract_command_second_arg}",
unless => "ls ${destination_dir}/${real_extracted_dir}",
creates => "${destination_dir}/${real_extracted_dir}",
creates => $real_creates,
timeout => $timeout,
require => Exec["Retrieve ${url} in ${work_dir} - ${title}"],
path => $path,
Expand Down
35 changes: 26 additions & 9 deletions manifests/puppet/gems.pp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@
# @param install_puppetserver_gems Manage installation of gems using Puppetserver's gem
# @param install_rbenv_gems Manage installation of gems under rbenv (requires
# jdowning/rbenv or compatible module)
# @param install_chruby_gems Manage installation of gems under chruby (uses
# psick::chruby profile
# @param additional_system_gems Array of additional gems to install using system's gem
# @param additional_puppet_gems Array of additional gems to install using Puppet's gem
# @param additional_puppetserver_gems Array of additional gems to install using
# Puppetserver's gem
# @param additional_rbenv_gems Array of additional gems to install under rbenv
# @param additional_chruby_gems Array of additional gems to install under chruby
# @param rbenv_ruby_version Ruby version to use under rbenv. Default is from
# $::psick::rbenv::default_ruby_version
# @param chruby_ruby_version Ruby version to use under chruby. Default is from
# $::psick::chruby::default_ruby_version
# @param manage If to actually manage ANY resource from this class.
# When set to false, no resource from this class is managed whatever are
# the other parameters.
Expand All @@ -54,12 +59,15 @@
Boolean $install_system_gems = false,
Boolean $install_puppet_gems = true,
Boolean $install_puppetserver_gems = false,
Boolean $install_rbenv_gems = false,
Boolean $install_rbenv_gems = false,
Boolean $install_chruby_gems = false,
Array $additional_system_gems = [],
Array $additional_puppet_gems = [],
Array $additional_puppetserver_gems = [],
Array $additional_rbenv_gems = [],
Optional[String] $rbenv_ruby_version = undef,
Array $additional_chruby_gems = [],
Optional[String] $rbenv_ruby_version = undef,
Optional[String] $chruby_ruby_version = undef,
Boolean $manage = $::psick::manage,
Boolean $auto_prereq = $::psick::auto_prereq,
Boolean $no_noop = false,
Expand All @@ -70,10 +78,8 @@
info('Forced no-noop mode.')
noop(false)
}

$minimal_gems = ['r10k','hiera-eyaml','deep_merge']
$minimal_test_gems = ['puppet-lint','rspec-puppet','rake','bundler','simplecov','minitest','rspec-puppet-facts','puppetlabs_spec_helper','yaml-lint']

$default_gems = $default_set ? {
'none' => [],
'client' => [],
Expand All @@ -83,9 +89,7 @@
'integration' => $minimal_gems + $minimal_test_gems + ['beaker','beaker-rspec','beaker-puppet_install_helper'],
'developer' => $minimal_gems + $minimal_test_gems + ['puppet-debug','puppet-blacksmith'],
}

$all_gems = $default_gems + $install_gems

if $install_system_gems {
if $auto_prereq {
include ::psick::ruby
Expand All @@ -100,7 +104,6 @@
}
}
}

if $install_puppet_gems {
$puppet_gems = $all_gems + $additional_puppet_gems
$puppet_gems.each | $gem | {
Expand All @@ -114,7 +117,6 @@
}
}
}

if $install_puppetserver_gems {
$puppetserver_gems = $all_gems + $additional_puppetserver_gems
$puppetserver_gems.each | $gem | {
Expand All @@ -126,7 +128,6 @@
}
}
}

if $install_rbenv_gems {
if $auto_prereq {
include ::psick::rbenv
Expand All @@ -147,5 +148,21 @@
}
}
}
if $install_chruby_gems {
if $auto_prereq {
include ::psick::chruby
}
$chruby_require = $auto_prereq ? {
true => Class['psick::chruby'],
false => undef,
}
$chruby_gems = $all_gems + $additional_chruby_gems
$chruby_gems.each | $gem | {
psick::chruby::gem { $gem:
ruby_version => pick($chruby_ruby_version,$::psick::chruby::default_ruby_version),
require => $chruby_require,
}
}
}
}
}
30 changes: 30 additions & 0 deletions spec/classes/chruby_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'
require 'yaml'
facts_yaml = File.dirname(__FILE__) + '/../../fixtures/facts/spec.yaml'
facts = YAML.load_file(facts_yaml)

describe 'psick::chruby' do
on_supported_os(facterversion: '2.4').select { |k, _v| k == 'redhat-7-x86_64' || k == 'ubuntu-16.04-x86_64' }.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts.merge(facts) }
let(:pre_condition) { 'include psick' }

describe 'with default params' do
it { is_expected.to compile }
it { is_expected.to contain_class('psick::chruby') }
end

describe 'with manage => false' do
let(:params) { { 'manage' => false } }

it { is_expected.to have_resource_count(0) }
end

describe 'with no_noop => true' do
let(:params) { { 'no_noop' => true } }

# it { is_expected.to contain_package('chruby').with('noop': false) }
end
end
end
end

0 comments on commit 344c217

Please sign in to comment.