General refactoring

This commit is contained in:
2020-05-30 16:00:49 +02:00
parent 1e3f6f05ec
commit 07cf718ad9
17 changed files with 162 additions and 145 deletions
+6 -7
View File
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Creates a project directory, bootstraps it with a basic directory
# structure and optionally prepares it for deployments.
#
# Usage: project_add <project> [<origin>]
#
# <project> Path to the project
# <origin> Repository URL and branch name (<url>[#<branch>])
# <project> Path to the project
# <origin> Repository URL and branch name (<url>[#<branch>])
########################################################################
project="$1"
@@ -29,7 +30,7 @@ fi
# Create project directory
echo -e "\033[1mCreating project at \033[34m$project\033[0;1m...\033[0m"
if ! (mkdir "$project" && mkdir "$project/data" && touch "$project/.project"); then
echo -e "\033[31mCould not create directory structure at \033[34m$project\033[31m.\033[0m" >&2
echo -e " => \033[31mCould not create directory structure at \033[34m$project\033[31m.\033[0m" >&2
exit 1
fi
echo -e " => \033[32mSuccess.\033[0m"
@@ -49,10 +50,8 @@ if [[ -n "$origin" ]]; then
else
echo -e "\033[1mInitializing local project directory at \033[34m$project/current\033[0;1m...\033[0m"
if ! mkdir "$project/current"; then
echo -e "\033[31mCould not create local project directory.\033[0m" >&2
echo -e " => \033[31mCould not create local project directory.\033[0m" >&2
exit 1
fi
echo -e " => \033[32mSuccess.\033[0m"
exit 0
fi
+23 -27
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Clones the repository of a project from the server, runs the script
# .postdeploy.sh in the repository (if existing) and links the version
@@ -8,9 +9,9 @@
#
# Usage: project_deploy <project> [<revision>]
#
# <project> Path to the project
# <revision> SHA-1 commit hash of the revision to checkout
# Defaults to the latest commit on the set branch
# <project> Path to the project
# <revision> SHA-1 commit hash of the revision to checkout
# Defaults to the latest commit on the set branch.
########################################################################
# Functions
@@ -36,7 +37,7 @@ function project_deploy::reverse() {
# Builds the paths to a version
declare directoryCount versionNumber versionName destinationRelative destination
function buildVersionPath() {
function project_deploy::buildVersionPath() {
destinationRelative="versions/$versionName"
destination="$(cd "$project" && echo -n "$(pwd)/$destinationRelative")"
}
@@ -44,7 +45,7 @@ function buildVersionPath() {
########################################################################
project="$1"
revision="$2"
revision="${2:-latest}"
# Load configuration
CONFIG_PRESERVE_VERSIONS=5
@@ -62,14 +63,11 @@ if [[ -z "$project" ]]; then
echo -e "\033[1mUsage:\033[0m \033[34mproject_deploy\033[0m <project> [<revision>]"
exit 1
fi
if [[ -z "$revision" ]]; then
revision="latest"
# Check if the default value for $CONFIG_HASH_LENGTH has been changed
if [[ $CONFIG_HASH_LENGTH != 40 ]]; then
echo -e "\033[31mYou changed the default value of \033[34m\$CONFIG_HASH_LENGTH\033[31m. To make sure the deployed revisions are unique and never duplicated, deployments of the latest revision aren't supported with a custom value of this option. Please give me the exact revision string in your custom length.\033[0m" >&2
exit 1
fi
# Check if the default value for $CONFIG_HASH_LENGTH has been changed
if [[ "$revision" == "latest" ]] && [[ $CONFIG_HASH_LENGTH != 40 ]]; then
echo -e "\033[31mYou have changed the default value of \033[34m\$CONFIG_HASH_LENGTH\033[31m. To make sure the deployed revisions are unique and never duplicated, deployments of the latest revision aren't supported with a custom value of this option. Please give me the exact revision string in your custom length.\033[0m" >&2
exit 1
fi
# Check if the project exists
@@ -98,7 +96,7 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n---
# We only need to do this if the revision was not deployed yet
if ! ls "$project/versions"/*-$revision &> /dev/null; then
# Check if the repository has already been cloned
url=$(cat "$project/.origin")
url="$(cat "$project/.origin")"
if ! [[ -d "$project/repo" ]]; then
# No, clone it now
echo -e "\033[1mCloning from \033[34m$url\033[0;1m for the first time...\033[0m"
@@ -110,7 +108,7 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n---
else
# Yes, only fetch from the remote repository
echo -e "\033[1mFetching new commits from \033[34m$url\033[0;1m...\033[0m"
if ! (cd "$project/repo"; git fetch); then
if ! (git -C "$project/repo" fetch); then
echo -e " => \033[31mCould not fetch from \033[34m$url\033[31m.\033[0m" >&2
exit 1
fi
@@ -127,15 +125,15 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n---
fi
else
# Check if the set branch exists
branch=$(cat "$project/.branch")
branch="$(cat "$project/.branch")"
echo -e "\033[1mDetermining current revision for branch \033[34m$branch\033[0;1m...\033[0m"
if ! (cd "$project/repo"; git show-branch "$branch" &> /dev/null); then
echo -e "\033[31mThe set branch \033[34m$branch\033[31m does not exist.\033[0m" >&2
if ! (git -C "$project/repo" show-branch "$branch" &> /dev/null); then
echo -e " => \033[31mThe set branch \033[34m$branch\033[31m does not exist.\033[0m" >&2
exit 1
fi
# Get the commit hash from the set branch
revision="$(cd "$project/repo"; git rev-list --max-count=1 "origin/$branch")"
revision="$(git -C "$project/repo" rev-list --max-count=1 "origin/$branch")"
if [[ $? != 0 ]]; then
echo -e " => \033[31mCould not determine current revision.\033[0m" >&2
exit 1
@@ -149,8 +147,8 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n---
# Yes, use that
# Build the pathname of the destination directory
versionName=$(basename $(echo "$project/versions"/*-$revision))
buildVersionPath
versionName="$(basename "$(echo "$project/versions"/*-$revision)")"
project_deploy::buildVersionPath
echo -e "\033[1mThis revision has already been installed, using previous installation \033[34m$versionName\033[0;1m.\033[0m"
else
@@ -161,7 +159,7 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n---
latestVersion=$(echo "$(ls "$project/versions" | tail -1)" | cut -f1 -d- | sed 's/^0*//')
versionNumber=$(($latestVersion + 1)) # Next is +1
versionName="$(printf "%05d" "$versionNumber")-$revision"
buildVersionPath
project_deploy::buildVersionPath
# Copy repository to new destination
echo -e "\033[1mCopying repository to deployment destination...\033[0m"
@@ -174,7 +172,7 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n---
# Checkout correct revision
echo -e "\033[1mChecking out revision \033[34m$revision\033[0;1m...\033[0m"
if ! cd "$destination" || ! git checkout "$revision"; then
if ! git -C "$destination" checkout "$revision"; then
echo -e " => \033[31mCould not checkout revision \033[34m$revision\033[31m of project \033[34m$project\033[31m.\033[0m" >&2
project_deploy::reverse "$project" "$destination"
exit 1
@@ -183,7 +181,7 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n---
# Update submodules
echo -e "\033[1mUpdating submodules...\033[0m"
if ! cd "$destination" || ! git submodule update --init --recursive; then
if ! git -C "$destination" submodule update --init --recursive; then
echo -e " => \033[31mCould not update submodules of project \033[34m$project\033[31m.\033[0m" >&2
project_deploy::reverse "$project" "$destination"
exit 1
@@ -256,5 +254,3 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n---
echo -e " => \033[32mSuccess.\033[0m"
} 2>&1 | tee -a "$log" # Duplicate output to log file
exit 0
+2 -1
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Lists all known project directories.
#
+8 -7
View File
@@ -1,17 +1,18 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Prepares a project for deployment and sets the origin repository and
# branch the project is connected to.
#
# Usage: project_origin <project> [<origin>]
#
# <project> Path to the project
# <origin> Repository URL and branch name (<url>[#<branch>])
# If omitted, the origin gets deleted.
# If the string does not contain a "#", the branch is
# set to "master" ($CONFIG_DEFAULT_BRANCH).
# <project> Path to the project
# <origin> Repository URL and branch name (<url>[#<branch>])
# If omitted, the origin gets deleted.
# If the string does not contain a "#", the branch is
# set to "master" ($CONFIG_DEFAULT_BRANCH).
########################################################################
project="$1"
@@ -58,7 +59,7 @@ if [[ -n "$origin" ]]; then
else
# No branch given, set to master
url="$origin"
branch=$CONFIG_DEFAULT_BRANCH
branch="$CONFIG_DEFAULT_BRANCH"
fi
# Check if the required directory structure already exists
+3 -4
View File
@@ -1,12 +1,13 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Removes a project directory.
#
# Usage: project_remove <project>
#
# <project> Path to the project
# <project> Path to the project
########################################################################
project="$1"
@@ -39,5 +40,3 @@ if ! sed -i "/${realpath//\//\/}/d" "$HOME/.projects"; then # Replace / with \/
exit 1
fi
echo -e " => \033[32mSuccess.\033[0m"
exit 0
+4 -4
View File
@@ -1,12 +1,13 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Reverses a deployable project to the last version.
#
# Usage: project_rollback <project>
#
# <project> Path to the project
# <project> Path to the project
########################################################################
project="$1"
@@ -32,14 +33,13 @@ fi
# Check if the project currently has a last version
if [[ ! -e "$project/last" ]]; then
echo -e "\033[31mThe project \033[34m$project\033[31m currently does not have a last version.\033[0m" >&2
echo -e "\033[31mProbably, it has already been rolled back since the latest deployment.\033[0m" >&2
echo -e "\033[31mIt has probably already been rolled back since the latest deployment.\033[0m" >&2
exit 1
fi
echo -e "\033[1mRolling project \033[34m$project\033[0;1m back to last version...\033[0m"
if rm -f "$project/current" && mv "$project/last" "$project/current"; then
echo -e " => \033[32mSuccess.\033[0m"
exit 0
else
echo -e " => \033[31mSomething went wrong.\033[0m" >&2
exit 1
+4 -5
View File
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Creates a site directory in ~/web/sites/, bootstraps it with a basic
# directory structure and optionally prepares it for deployments.
#
# Usage: site_add <site> [<origin>]
#
# <site> Name of the site (directory in ~/web/sites/)
# <origin> Repository URL and branch name (<url>[#<branch>])
# <site> Name of the site (directory in ~/web/sites/)
# <origin> Repository URL and branch name (<url>[#<branch>])
########################################################################
site="$1"
@@ -42,5 +43,3 @@ if ! touch "$HOME/web/sites/$site/.domains"; then
exit 1
fi
echo -e " => \033[32mSuccess.\033[0m"
exit 0
+5 -4
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Clones the repository of a site from the server, runs the script
# .postdeploy.sh in the repository (if existing) and links the version
@@ -8,9 +9,9 @@
#
# Usage: site_deploy <site> [<revision>]
#
# <site> Name of the site (directory in ~/web/sites/)
# <revision> SHA-1 commit hash of the revision to checkout
# Defaults to the latest commit on the set branch
# <site> Name of the site (directory in ~/web/sites/)
# <revision> SHA-1 commit hash of the revision to checkout
# Defaults to the latest commit on the set branch.
########################################################################
site="$1"
+5 -5
View File
@@ -1,13 +1,14 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Creates a link for the webserver pointing a domain to a site.
#
# Usage: site_link <site> <domain>
#
# <site> Name of the site (directory in ~/web/sites/)
# <domain> FQDN of the domain (like "example.com")
# <site> Name of the site (directory in ~/web/sites/)
# <domain> FQDN of the domain (like "example.com")
########################################################################
site="$1"
@@ -40,7 +41,7 @@ fi
# Create link
echo -e "\033[1mCreating link from site \033[34m$site\033[0;1m to domain \033[34m$domain\033[0;1m...\033[0m"
if ! ln -s "sites/$site/current" "$HOME/web/$domain"; then
echo -e "\033[31mCould not create link at \033[34m$HOME/web/$domain\033[31m.\033[0m" >&2
echo -e " => \033[31mCould not create link at \033[34m$HOME/web/$domain\033[31m.\033[0m" >&2
exit 1
fi
echo -e " => \033[32mSuccess.\033[0m"
@@ -50,7 +51,6 @@ echo -e "\033[1mAdding domain to site's domains (\033[34m.domains\033[0;1m file)
echo "$domain" >> "$HOME/web/sites/$site/.domains"
if [[ $? == 0 ]]; then
echo -e " => \033[32mSuccess.\033[0m"
exit 0
else
echo -e " => \033[31mSomething went wrong.\033[0m" >&2
exit 1
+2 -1
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Lists all known sites.
#
+7 -6
View File
@@ -1,17 +1,18 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Prepares a site for deployment and sets the origin repository and
# branch the site is connected to.
#
# Usage: site_origin <site> [<origin>]
#
# <site> Name of the site (directory in ~/web/sites/)
# <origin> Repository URL and branch name (<url>[#<branch>])
# If omitted, the origin gets deleted.
# If the string does not contain a "#", the branch is
# set to "master".
# <site> Name of the site (directory in ~/web/sites/)
# <origin> Repository URL and branch name (<url>[#<branch>])
# If omitted, the origin gets deleted.
# If the string does not contain a "#", the branch is
# set to "master".
########################################################################
site="$1"
+3 -2
View File
@@ -1,12 +1,13 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Unlinks and removes a site directory in ~/web/sites/.
#
# Usage: site_remove <site>
#
# <site> Name of the site (directory in ~/web/sites/)
# <site> Name of the site (directory in ~/web/sites/)
########################################################################
site="$1"
+3 -2
View File
@@ -1,12 +1,13 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Reverses a deployable site to the last version.
#
# Usage: site_rollback <site>
#
# <site> Name of the site (directory in ~/web/sites/)
# <site> Name of the site (directory in ~/web/sites/)
########################################################################
site="$1"
+7 -7
View File
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
########################################################################
# 2014-09-07 Lukas Bestle mail@lukasbestle.com
# Copyright 2014 Lukas Bestle <project-projectr@lukasbestle.com>
# License MIT
########################################################################
# Removes a link for the webserver pointing a domain to a site.
#
# Usage: site_unlink <site> [<domain>]
#
# <site> Name of the site (directory in ~/web/sites/)
# <domain> FQDN of the domain (like "example.com")
# If not given, the site is completely unlinked
# <site> Name of the site (directory in ~/web/sites/)
# <domain> FQDN of the domain (like "example.com")
# If not given, the site is completely unlinked.
########################################################################
site="$1"
@@ -65,17 +66,16 @@ fi
# Remove link
echo -e "\033[1mRemoving link from site \033[34m$site\033[0;1m to domain \033[34m$domain\033[0;1m...\033[0m"
if ! rm -f "$HOME/web/$domain"; then
echo -e "\033[31mCould not remove link at \033[34m$HOME/web/$domain\033[31m.\033[0m" >&2
echo -e " => \033[31mCould not remove link at \033[34m$HOME/web/$domain\033[31m.\033[0m" >&2
exit 1
fi
echo -e " => \033[32mSuccess.\033[0m"
# Remove from site's domains
echo -e "\033[1mRemoving domain from site's domains (\033[34m.domains\033[0;1m file)...\033[0m"
sed -i "/$domain/d" "$HOME/web/sites/$site/.domains"
sed -i "/${domain//\//\/}/d" "$HOME/web/sites/$site/.domains" # Replace / with \/ because of the sed separators
if [[ $? == 0 ]]; then
echo -e " => \033[32mSuccess.\033[0m"
exit 0
else
echo -e " => \033[31mSomething went wrong.\033[0m" >&2
exit 1