diff --git a/CHANGELOG.md b/CHANGELOG.md index 63a37171..bedc2114 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [v4.2.9-2] - 2023-02-21 +### Changed +- Extract admin creation and deletion scripts (#111) + ## [v4.2.9-1] - 2023-02-14 ### Changed - Upgrade to Redmine 4.2.9; #109 diff --git a/Dockerfile b/Dockerfile index 1ce61277..5fcf52e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM registry.cloudogu.com/official/base:3.14.3-1 LABEL NAME="official/redmine" \ - VERSION="4.2.9-1" \ + VERSION="4.2.9-2" \ maintainer="hello@cloudogu.com" # This Dockerfile is based on https://github.com/docker-library/redmine/blob/master/4.0/alpine/Dockerfile diff --git a/Jenkinsfile b/Jenkinsfile index 84d0d309..f4d134eb 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -43,7 +43,7 @@ node('vagrant') { } stage('Shell-Check') { - shellCheck("./resources/startup.sh ./resources/post-upgrade.sh ./resources/pre-upgrade.sh ./resources/util.sh ./resources/upgrade-notification.sh ./resources/create-admin.sh ./resources/default-config.sh ./resources/remove-user.sh ./resources/util.sh ./resources/delete-plugin.sh") + shellCheck("./resources/startup.sh ./resources/post-upgrade.sh ./resources/pre-upgrade.sh ./resources/util.sh ./resources/upgrade-notification.sh ./resources/default-config.sh ./resources/util.sh ./resources/delete-plugin.sh") } try { diff --git a/dogu.json b/dogu.json index d02c17c0..286bfc15 100644 --- a/dogu.json +++ b/dogu.json @@ -1,6 +1,6 @@ { "Name": "official/redmine", - "Version": "4.2.9-1", + "Version": "4.2.9-2", "DisplayName": "Redmine", "Description": "Redmine is a flexible project management web application", "Category": "Development Apps", diff --git a/resources/create-admin.sh b/resources/create-admin.sh deleted file mode 100755 index 07a1c71b..00000000 --- a/resources/create-admin.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash -set -o errexit -set -o nounset -set -o pipefail - -USERNAME="${1}" -PASSWORD="${2}" - -OUTPUT="$(RAILS_ENV=production bundle exec rails console <<< " -begin - user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option, :admin => true) - user.login = '${USERNAME}' - user.password = '${PASSWORD}' - user.password_confirmation = '${PASSWORD}' - user.lastname = '${USERNAME}' - user.firstname = '${USERNAME}' - user.mail = '${USERNAME}@${USERNAME}.de' - user.save! -rescue => error - puts error.message - raise 'User was not saved' -end -")" - -# There is no way to get an exit code on error. So we check if exception raised text appears and exit manually. -if [[ "${OUTPUT}" == *"RuntimeError (User was not saved)"* ]]; then - echo "Could not create admin ${USERNAME} due to error: " - printf '%s\n' "${OUTPUT#*end}" - exit 1 -else - echo "Created user ${USERNAME} successfully." -fi diff --git a/resources/rails_scripts/create_admin.rb b/resources/rails_scripts/create_admin.rb new file mode 100644 index 00000000..bead90f9 --- /dev/null +++ b/resources/rails_scripts/create_admin.rb @@ -0,0 +1,44 @@ +require 'optparse' + +module CreateAdminHelper + @options = {} + OptionParser.new do |opts| + opts.banner = "Usage: get_setting.rb [options]" + + opts.on("-u", "--username USERNAME", "username") + opts.on("-p", "--password PASSWORD", "password") + end.parse!(into: @options) + + @options[:username] = "" if @options[:username].nil? + + def self.options + @options + end + + class ActiveSupport::HashWithIndifferentAccess + def symbolize_keys! + transform_keys! { |key| key.to_sym rescue key } + end + end +end + +begin + opts = CreateAdminHelper::options + username = opts[:username] + password = opts[:password] + puts "Try to create user '%s'" % username + user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option, :admin => true) + user.login = username + user.password = password + user.password_confirmation = password + user.lastname = username + user.firstname = username + user.mail = "#{username}@#{username}.de" + user.save! + puts 'User saved successfully.' +rescue Exception => error + puts '================================================' + puts error.message + puts '================================================' + raise 'User was not saved.' +end diff --git a/resources/rails_scripts/remove_user.rb b/resources/rails_scripts/remove_user.rb new file mode 100644 index 00000000..53233db8 --- /dev/null +++ b/resources/rails_scripts/remove_user.rb @@ -0,0 +1,36 @@ +require 'optparse' + +module RemoveUserHelper +@options = {} +OptionParser.new do |opts| +opts.banner = "Usage: get_setting.rb [options]" + +opts.on("-u", "--username USERNAME", "username") +end.parse!(into: @options) + +@options[:username] = "" if @options[:username].nil? + + def self.options +@options +end + +class ActiveSupport::HashWithIndifferentAccess +def symbolize_keys! +transform_keys! { |key| key.to_sym rescue key } +end +end +end + +begin +opts = RemoveUserHelper::options +puts "Try to remove user %s" % opts[:username] +user = User.where(login: opts[:username]).first +puts 'User did not exist' if user.nil? + user.destroy unless user.nil? + puts 'User removed successfully.' unless user.nil? + rescue Exception => error +puts '=====================================================' +puts error.message +puts '=====================================================' +puts 'User remove failed.' +end \ No newline at end of file diff --git a/resources/util.sh b/resources/util.sh index 24d56797..cf91955f 100644 --- a/resources/util.sh +++ b/resources/util.sh @@ -157,7 +157,7 @@ function start_redmine_in_background(){ wait_for_redmine_to_get_healthy 300 } -# Creates an admin user by using the create-admin.sh script +# Creates an admin user by using the create_admin.rb script function create_temporary_admin() { echo "Creating temporary admin..." # The Password must contain a special character, a lowercase letter, a capital letter and a number... @@ -170,8 +170,7 @@ function create_temporary_admin() { # In case we are in restart loop to prevent infinite admin users... remove_last_temporary_admin - # shellcheck disable=SC1091 - source "/create-admin.sh" "${TMP_ADMIN_NAME}" "${TMP_ADMIN_PASSWORD}" + railsConsole "/rails_scripts/create_admin.rb" --username "${TMP_ADMIN_NAME}" --password "${TMP_ADMIN_PASSWORD}" || exit 1 doguctl config -e "last_tmp_admin" "${TMP_ADMIN_NAME}" } @@ -188,7 +187,7 @@ function remove_last_temporary_admin() { then echo "Removing last temporary admin..." # shellcheck disable=SC1091 - source "/remove-user.sh" "${LAST_TMP_ADMIN}" + railsConsole "/rails_scripts/remove_user.rb" --username "${LAST_TMP_ADMIN}" || exit 1 doguctl config --rm last_tmp_admin fi }