Skip to content
Snippets Groups Projects
Commit 62951cc6 authored by Filip Strömbäck's avatar Filip Strömbäck
Browse files

Removed the update_repo.sh file, not needed anymore.

parent 7da41173
No related branches found
No related tags found
2 merge requests!50Fix: Puppet Defining "data_provider": "hiera" in metadata.json is deprecated....,!40Merge from devel. Commits related to RHEL9 upgrade and pdk cleanup.
Pipeline #102013 passed
#!/bin/bash
# Keep a Git repository updated. Optionally execute a command whenever it has been updated in order
# to trigger further processing of some kind (e.g. restarting services).
# The command is called as follows:
# update_repo.sh <destination path> <source repo> <source branch>
# If REPO_ON_UPDATE is set, then that file will be executed whenever the repo is created and/or
# updated to a new revision. This can be used to trigger further processing of some sort, such as
# compiling the contained source and/or restarting system services as appropriate.
# This script is designed to be executed as a privillegied user so that the update command could do
# privillegied operations. Thus, the git commands will be isolated to a user as described by
# REPO_USER and REPO_GROUP, if they are is set. It is, of course, also possible to just run this
# script as a regular user without setting REPO_USER.
# "function" that keeps the repo updated. Returns 0 if nothing was done, 100 if the repo was updated,
# and something else on some kind of error. This function will be executed as the user indicated in
# the environment variables.
# I'm sorry for this thing... Turns out that sudo does not allow passing bash functions as environment
# variables, so I simply pass the entire "function" to Bash as a string.
update_repo_fn=$(cat <<'EOF'
if [[ ! -d "$repo_path" ]]
then
# Does not exist. We need to checkout the repository.
git clone --single-branch --branch "$repo_branch" "$repo_source" "$repo_path" || exit 1
exit 100
else
# It does exist. Make sure it is updated.
cd "$repo_path"
old_sha=$(git rev-parse HEAD)
git fetch -f "$repo_source" "$repo_branch":remotes/origin/"$repo_branch" || exit 1
new_sha=$(git rev-parse remotes/origin/"$repo_branch")
if [[ "$old_sha" == "$new_sha" ]]
then
# They are the same, we don't need to do anything.
exit 0
else
# They differ. Check out the new revision.
git checkout -f "$new_sha" || exit 1
git branch -f "$repo_branch" "$new_sha" || exit 1
# This is not strictly necessary, but it makes it look like we have the correct branch
# checked out. Good if someone inspects the repo at a later time.
git checkout -f "$repo_branch" || exit 1
exit 100
fi
fi
EOF
)
# Check for enough parameters.
if [[ "$#" < 3 ]]
then
echo "Not enough parameters."
echo "Usage: update_repo.sh <path> <source> <branch>"
exit 2
fi
# Check if the REPO_ON_UPDATE script exists. If it doesn't (probably
# because Puppet has not created it yet), then we don't want to run
# yet. If we run without the script being present, then the script
# will not be executed until the repository is updated, thus breaking
# our promises.
if [[ ! -z "$REPO_ON_UPDATE" ]]
then
if [[ ! -f "$REPO_ON_UPDATE" ]]
then
echo "The script $REPO_ON_UPDATE does not exist."
exit 4
fi
fi
# Setup variables for calling "update_repo"...
repo_path="$1"
repo_source="$2"
repo_branch="$3"
export repo_path repo_source repo_branch
if [[ -z "$REPO_USER" ]]
then
# Just run it in a subshell
bash -c "$update_repo_fn"
else
# Perhaps group was not supplied.
if [[ -z "$REPO_GROUP" ]]
then
REPO_GROUP="$REPO_USER"
fi
sudo --preserve-env=repo_path,repo_source,repo_branch,update_repo --set-home --user="$REPO_USER" --group="$REPO_GROUP" -- bash -c "$update_repo_fn"
fi
# Note: We cannot put any commands between the if-statement and here. We need the result code from
# invoking bash, which is the last command in both the if- and else- branches.
result="$?"
if [[ $result == 0 ]]
then
# All is well, nothing needed to be done.
exit 0
elif [[ $result == 100 ]]
then
# All is well, but we need to tell the environment that we updated the repo.
if [[ ! -z "$REPO_ON_UPDATE" ]]
then
# Run it if it is there.
eval "$REPO_ON_UPDATE"
fi
exit 0
else
# Something went awry, forward the exit code.
exit $result
fi
......@@ -45,19 +45,19 @@ class aes {
}
# File for updating repositories.
file { '/opt/utils':
ensure => directory,
mode => '0755',
owner => root,
group => root,
}
file { '/opt/utils/update_repo.sh':
ensure => file,
mode => '0755',
owner => root,
group => root,
content => file("${module_name}/update_repo.sh"),
}
# file { '/opt/utils':
# ensure => directory,
# mode => '0755',
# owner => root,
# group => root,
# }
# file { '/opt/utils/update_repo.sh':
# ensure => file,
# mode => '0755',
# owner => root,
# group => root,
# content => file("${module_name}/update_repo.sh"),
# }
# File to easily see when Puppet was last executed.
# Ideally, we would like to know if it is devel or production as well.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment