diff --git a/.fixtures.yml b/.fixtures.yml index 00aeaa8..cbc3711 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -9,3 +9,5 @@ fixtures: yumrepo: "puppetlabs/yumrepo_core" vcsrepo: "puppetlabs/vcsrepo" systemd: "puppet/systemd" + rvm: "puppet/rvm" + gnupg: "golja/gnupg" diff --git a/manifests/init.pp b/manifests/init.pp index ac9dcbb..b7b8322 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -3,10 +3,9 @@ # This class controls the installation of HDM # # @param method Select the installation method. -# Avalable methods: docker -# The puppet-ruby implenentation is not yet working. -# When using puppet-ruby we install bundler gem into -# the puppet-agent ruby installation. +# Avalable methods: docker, rvm +# When using rvm we install rvm into system and add the +# bundler gem. # # @param manage_docker Set to false if this module should NOT # also include the docker class (without any arguments) @@ -19,6 +18,9 @@ # Version is the image tag name when using docker and # the git tag when using puppet-ruby # +# @param ruby_version Select the ruby version when installing using rvm +# Please check [hdm ruby version requirement](https://github.com/betadots/hdm/blob/main/.ruby-version) +# # @param port The port where HDM should run on # # @param bind_ip The ip address to bind the process to @@ -110,9 +112,10 @@ # @example # include hdm class hdm ( - Enum['docker'] $method = 'docker', + Enum['docker', 'rvm'] $method = 'docker', Boolean $manage_docker = true, String[1] $version = 'main', + String[1] $ruby_version = '3.1.2', Stdlib::Port $port = 3000, Stdlib::IP::Address::Nosubnet $bind_ip = '0.0.0.0', String[1] $hostname = $facts['networking']['fqdn'], @@ -133,8 +136,8 @@ 'docker': { include hdm::docker } - 'puppet-ruby': { - include hdm::puppet_ruby + 'rvm': { + include hdm::rvm } default: { fail('Unknown HDM installation method.') diff --git a/manifests/puppet_ruby.pp b/manifests/puppet_ruby.pp deleted file mode 100644 index 254ff67..0000000 --- a/manifests/puppet_ruby.pp +++ /dev/null @@ -1,63 +0,0 @@ -# @summary A short summary of the purpose of this class -# -# A description of what this class does -# -# @api private -# -# @example -# include hdm::puppet_ruby -class hdm::puppet_ruby { - package { 'bundler': - ensure => present, - provider => 'puppet_gem', - } - - group { $hdm::group: - ensure => present, - } - - user { $hdm::user: - ensure => present, - gid => $hdm::group, - shell => '/sbin/nologin', - } - - vcsrepo { $hdm::hdm_path: - ensure => present, - provider => 'git', - source => $hdm::git_url, - revision => $hdm::version, - } - - exec { 'bundle install': - command => 'bundle install --path vendor --jobs $(nproc) && touch .bundle_install_finished', - path => "${facts['path']}:/opt/puppetlabs/puppet/bin", - cwd => $hdm::hdm_path, - creates => "${hdm::hdm_path}/.bundle_install_finished", - } - - file { "${hdm::hdm_path}/config/hdm.yml": - ensure => file, - content => epp('hdm/hdm.yml.epp'), - } - - exec { 'bundle db setup': - command => 'bundle exec rails db:setup && touch .bundle_db_setup_finished', - path => "${facts['path']}:/opt/puppetlabs/puppet/bin", - cwd => $hdm::hdm_path, - creates => "${hdm::hdm_path}/.bundle_db_setup_finished", - } - - exec { 'bundle rails credentials': - command => 'echo "secret" | EDITOR="vim" bundle exec rails credentials:edit', - path => "${facts['path']}:/opt/puppetlabs/puppet/bin", - cwd => $hdm::hdm_path, - creates => "${hdm::hdm_path}/config/credentials.yml.enc", - } - - systemd::unit_file { 'hdm.service': - content => epp('hdm/hdm.service.epp'), - enable => true, - active => true, - } -} diff --git a/manifests/rvm.pp b/manifests/rvm.pp new file mode 100644 index 0000000..5505e52 --- /dev/null +++ b/manifests/rvm.pp @@ -0,0 +1,122 @@ +# class hdm::rvm +class hdm::rvm { + group { $hdm::group: + ensure => present, + } + + user { $hdm::user: + ensure => present, + shell => '/sbin/nologin', + home => $hdm::hdm_path, + gid => $hdm::group, + } + + file { $hdm::hdm_path: + ensure => directory, + owner => $hdm::user, + group => $hdm::group, + } + + include gnupg + + include rvm + + rvm::system_user { 'hdm': + create => false, + } + + rvm_system_ruby { "ruby-${hdm::ruby_version}": + ensure => present, + default_use => false, + } + + rvm_gem { 'bundler': + ensure => present, + ruby_version => "ruby-${hdm::ruby_version}", + require => Rvm_system_ruby["ruby-${hdm::ruby_version}"], + } + + # Fix for old g++ and sqlite3 + case $facts['os']['family'] { + 'RedHat': { + if versioncmp($facts['os']['release']['major'], '8') < 0 { + package { 'centos-release-scl': + ensure => present, + } + + package { 'devtoolset-7': + ensure => present, + } + $exec_prefix = 'scl enable devtoolset-7 ' + exec { 'update sqlite': + command => 'yum install -y https://kojipkgs.fedoraproject.org//packages/sqlite/3.8.11/1.fc21/x86_64/sqlite-devel-3.8.11-1.fc21.x86_64.rpm https://kojipkgs.fedoraproject.org//packages/sqlite/3.8.11/1.fc21/x86_64/sqlite-3.8.11-1.fc21.x86_64.rpm', + path => $facts['path'], + unless => 'rpm -q sqlite | grep 3.8', + } + } else { + $exec_prefix = '' + } + } + default: { + $exec_prefix = '' + } + } + + vcsrepo { $hdm::hdm_path: + ensure => present, + provider => 'git', + source => 'https://github.com/betadots/hdm.git', + revision => $hdm::version, + } + + exec { 'bundle config path': + command => "source $(rvm ${hdm::ruby_version} do rvm env --path) && rvm ${hdm::ruby_version} do bundle config set --local path 'vendor/bundle'", + cwd => $hdm::hdm_path, + path => "/usr/local/rvm/bin:${facts['path']}", + unless => 'grep vendor/bundle .bundle/config', + provider => 'shell', + } + + exec { 'bundle config development': + command => "source $(rvm ${hdm::ruby_version} do rvm env --path) && rvm ${hdm::ruby_version} do bundle config set --local with 'development'", + cwd => $hdm::hdm_path, + path => "/usr/local/rvm/bin:${facts['path']}", + unless => 'grep development .bundle/config', + provider => 'shell', + } + + exec { 'bundle install': + command => "${exec_prefix} 'source $(rvm ${hdm::ruby_version} do rvm env --path) && rvm ${hdm::ruby_version} do bundle install --jobs $(nproc) && touch .bundle_install_finished'", + cwd => $hdm::hdm_path, + path => "/usr/local/rvm/bin:${facts['path']}", + creates => "${hdm::hdm_path}/.bundle_install_finished", + provider => 'shell', + } + + file { "${hdm::hdm_path}/config/hdm.yml": + ensure => file, + content => epp('hdm/hdm.yml.epp'), + } + + exec { 'bundle db:setup': + command => "source $(rvm ${hdm::ruby_version} do rvm env --path) && rvm ${hdm::ruby_version} do bundle exec rails db:setup && touch .bundle_db_setup_finished", + cwd => $hdm::hdm_path, + path => "/usr/local/rvm/bin:${facts['path']}", + creates => "${hdm::hdm_path}/.bundle_db_setup_finished", + provider => 'shell', + } + + exec { 'bundle rails credentials': + command => "source $(rvm ${hdm::ruby_version} do rvm env --path) && rvm ${hdm::ruby_version} do echo 'secret' | EDITOR='vim' bundle exec rails credentials:edit", + cwd => $hdm::hdm_path, + path => "/usr/local/rvm/bin:${facts['path']}", + creates => "${hdm::hdm_path}/config/credentials.yml.enc", + provider => 'shell', + } + + systemd::unit_file { 'hdm.service': + content => epp('hdm/hdm.service.epp'), + enable => true, + active => true, + } +} diff --git a/metadata.json b/metadata.json index 0d24e4c..aacf783 100644 --- a/metadata.json +++ b/metadata.json @@ -14,14 +14,18 @@ "name": "puppetlabs/docker", "version_requirement": ">= 4.4.0 < 5.0.0" }, - { - "name": "puppetlabs/yumrepo_core", - "version_requirement": ">= 1.1.0 < 2.0.0" - }, { "name": "puppetlabs/vcsrepo", "version_requirement": ">= 5.2.0 < 6.0.0" }, + { + "name": "golja/gnupg", + "version_requirement": ">= 1.2.3 < 2.0.0" + }, + { + "name": "puppet/rvm", + "version_requirement": ">= 2.0.0 < 3.0.0" + }, { "name": "puppetlabs/apt", "version_requirement": ">= 8.5.0 < 9.0.0" diff --git a/spec/classes/hdm_spec.rb b/spec/classes/hdm_docker_spec.rb similarity index 60% rename from spec/classes/hdm_spec.rb rename to spec/classes/hdm_docker_spec.rb index c41842d..df45797 100644 --- a/spec/classes/hdm_spec.rb +++ b/spec/classes/hdm_docker_spec.rb @@ -6,8 +6,13 @@ on_supported_os.each do |os, os_facts| context "on #{os} using docker" do let(:facts) { os_facts } + let(:params) do + { + 'method' => 'docker', + } + end - it { is_expected.to compile } + it { is_expected.to compile.with_all_deps } end end end diff --git a/spec/classes/hdm_rvm_spec.rb b/spec/classes/hdm_rvm_spec.rb new file mode 100644 index 0000000..fa3acc6 --- /dev/null +++ b/spec/classes/hdm_rvm_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'hdm' do + on_supported_os.each do |os, os_facts| + context "on #{os} using rvm" do + let(:facts) { os_facts } + let(:params) do + { + 'method' => 'rvm', + } + end + + it { is_expected.to compile.with_all_deps } + end + end +end diff --git a/spec/default_module_facts.yml b/spec/default_module_facts.yml new file mode 100644 index 0000000..0a7db93 --- /dev/null +++ b/spec/default_module_facts.yml @@ -0,0 +1,2 @@ +--- +root_home: '/root' diff --git a/templates/hdm.service.epp b/templates/hdm.service.epp index 5275bbf..dcb8e15 100644 --- a/templates/hdm.service.epp +++ b/templates/hdm.service.epp @@ -4,11 +4,14 @@ Requires=network.target [Service] Type=simple -User=<%= $hdm::user %> -Group=<%= $hdm::group %> +#User=<%= $hdm::user %> +#Group=<%= $hdm::group %> +User=root +Group=root WorkingDirectory=<%= $hdm::hdm_path %> -# ExecStart=/bin/bash -lc 'bundle exec rails server -e production --bind 0.0.0.0 --port 80' -ExecStart=/bin/bash -lc 'bundle exec rails server -b <%= $hdm::bind_ip %> -p <%= $hdm::port %>' +Environment="PATH=/usr/local/rvm/bin:<%= $facts['path'] %>" +Environment="RAILS_DEVELOPMENT_HOSTS=<%= $hdm::hostname %>" +ExecStart=/bin/bash -lc 'source $(rvm <%= $hdm::ruby_version %> do rvm env --path) && bundle exec rails server -b <%= $hdm::bind_ip %> -p <%= $hdm::port %>' TimeoutSec=30 RestartSec=15s Restart=always