Puppet Class: r_profile::ruby

Defined in:
manifests/ruby.pp

Overview

R_profile::Ruby

Install a system-level ruby for customer applications to use. While its possible to use Puppet’s vendored ruby for everything, its safter to for customer to install their own ruby for internal apps to use since otherwise messing with the gems could break puppet in such a way that it is unable to run, leaving the system unmanaged

Parameters:

  • version (Any) (defaults to: hiera('r_profile::ruby::version', ['2.4.1', '2.3.4']))

    Array of Ruby versions to install (linux). On windows, only a single version can be installed due to packaging so we will install the first listed version. On linux, the first listed version will be set as the default

  • gems (Any) (defaults to: hiera('r_profile::ruby::gems', {}))

    Hash of default gems to install in a form suitable for create_resources of the ‘rbenv::gem` type (not recommened - please use bundler for day-to-day gem installation). IMPORTANT: Bundler is already installed as part of rbenv::build, attempting installation here as well will cause duplicate resource error @see forge.puppet.com/jdowning/rbenv#full-example

  • rbenv_plugins

    Array of rbenv pluings to install (for building new rubies)

  • install_dir (Any) (defaults to: undef)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'manifests/ruby.pp', line 20

class r_profile::ruby(
    $version        = hiera('r_profile::ruby::version', ['2.4.1', '2.3.4']),
    $gems           = hiera('r_profile::ruby::gems', {}),
    $install_dir    = undef,
) {

  case $facts['os']['family'] {
    'windows': {
      $_version     = any2array($version)[0]
      $_install_dir = pick($install_dir, 'c:\tools')
      $ruby_dir     = regsubst($_version, '(\d)\.(\d)\.(\d)\.?(\d+)?', 'ruby\1\2\3')
      $ruby_home    = "${_install_dir}\\${ruby_dir}\\bin"

      package { 'ruby':
        ensure   => $_version,
        provider => chocolatey,
      }

      # Make sure the current Chocolatey installed Ruby comes first in PATH
      # (as opposed to other Rubies installed by builds, old versions, etc.)
      windows_env { 'PATH':
        ensure    => present,
        value     => $ruby_home,
        mergemode => prepend,
      }

      if $gems {
        $gems.each |$gem, $opts| {
          $gem_version = pick($opts['version'], '>= 0')
          $path = pick($opts["path"], $ruby_home)

          exec { "gem install ${gem} ${version}":
            path    => $path,
            command => "gem.bat install ${gem} --version \"${gem_version}\"",
            unless  => "gem.bat list ${gem} --installed --version \"${gem_version}\"",
          }
        }
      }
    }

    'RedHat', 'Suse': {
      # pick the first defined ruby as the default version
      $ruby_home    = "${install_dir}/version/${version[0]}"

      class { '::rbenv':
        install_dir => $install_dir,
      }

      rbenv::plugin { [ 'rbenv/rbenv-vars', 'rbenv/ruby-build' ]: }

      any2array($version).each |$v| {
        rbenv::build { $v:
          global => false,
        }

        $gems.each |$gem, $opts| {
          rbenv::gem {
            "ruby_${v}_gem_${gem}":
              gem          => $gem,
              ruby_version => $v,
              *            => $opts,
          }
        }
      }

    }
    default: {
      fail("#{name} does not support ${facts['os']['family']}")
    }
  }

  # set a global ruby_home variable
  environment_variable::variable{ "RUBY_HOME=${ruby_home}": }
}