-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·200 lines (198 loc) · 9.84 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env bash
# shellcheck shell=bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
##@Version : 202308271918-git
# @@Author : Jason Hempstead
# @@Contact : [email protected]
# @@License : LICENSE.md
# @@ReadME : install.sh --help
# @@Copyright : Copyright: (c) 2023 Jason Hempstead, Casjays Developments
# @@Created : Sunday, Aug 27, 2023 19:18 EDT
# @@File : install.sh
# @@Description :
# @@Changelog : New script
# @@TODO : Better documentation
# @@Other :
# @@Resource :
# @@Terminal App : no
# @@sudo/root : no
# @@Template : shell/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# shellcheck disable=SC2016
# shellcheck disable=SC2031
# shellcheck disable=SC2120
# shellcheck disable=SC2155
# shellcheck disable=SC2199
# shellcheck disable=SC2317
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# setup debugging - https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
[ -f "/config/.debug" ] && [ -z "$DEBUGGER_OPTIONS" ] && export DEBUGGER_OPTIONS="$(cat "/config/.debug")" || DEBUGGER_OPTIONS="${DEBUGGER_OPTIONS:-}"
{ [ "$DEBUGGER" = "on" ] || [ -f "/config/.debug" ]; } && echo "Enabling debugging" && set -xo pipefail -x$DEBUGGER_OPTIONS && export DEBUGGER="on" || set -o pipefail
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# BASH_SET_SAVED_OPTIONS=$(set +o)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Set default exit code
INSTALL_SH_EXIT_STATUS=0
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# check for command
__cmd_exists() { command "$1" >/dev/null 2>&1 || return 1; }
__function_exists() { command -v "$1" 2>&1 | grep -q "is a function" || return 1; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Relative find functions
__find_file_relative() { find "$1"/* -not -path '*env/*' -not -path '.git*' -type f 2>/dev/null | sed 's|'$1'/||g' | sort -u | grep -v '^$' | grep '^' || false; }
__find_directory_relative() { find "$1"/* -not -path '*env/*' -not -path '.git*' -type d 2>/dev/null | sed 's|'$1'/||g' | sort -u | grep -v '^$' | grep '^' || false; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# get and compare versions
__get_version() { printf '%s\n' "${1:-$(cat "/dev/stdin")}" | awk -F. '{ printf("%d%03d\n", $1,$2); }'; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# custom functions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Define Variables
EXPECTED_OS="alpine"
TEMPLATE_NAME="forgejo"
CONFIG_CHECK_FILE="forgejo/app.ini"
OVER_WRITE_INIT="yes"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TMP_DIR="/tmp/config-$TEMPLATE_NAME"
CONFIG_DIR="/usr/local/share/template-files/config"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WWW_ROOT_DIR="${WWW_ROOT_DIR:-/usr/share/httpd/default}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
INIT_DIR="/usr/local/etc/docker/init.d"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
GIT_REPO="https://github.com/templatemgr/$TEMPLATE_NAME"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
OS_RELEASE="$(grep -si "$EXPECTED_OS" /etc/*-release* | sed 's|.*=||g;s|"||g' | head -n1)"
[ -n "$OS_RELEASE" ] || { echo "Unexpected OS: ${OS_RELEASE:-N/A}" && exit 1; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ "$TEMPLATE_NAME" != "sample-template" ] || { echo "Please set TEMPLATE_NAME" && exit 1; }
git clone -q "$GIT_REPO" "$TMP_DIR" || { echo "Failed to clone the repo" exit 1; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Main application
mkdir -p "$CONFIG_DIR" "$INIT_DIR"
find "$TMP_DIR/" -iname '.gitkeep' -exec rm -f {} \;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# custom pre execution commands
[ -d "/etc/ssmtp" ] && rm -Rf /etc/ssmtp || mkdir -p "/etc/ssmtp"
[ -d "/etc/nginx" ] && rm -Rf /etc/nginx/* || mkdir -p "/etc/nginx"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_dir_list="$(__find_directory_relative "$TMP_DIR/config" || false)"
if [ -n "$get_dir_list" ]; then
for dir in $get_dir_list; do
mkdir -p "$CONFIG_DIR/$dir" /etc/$dir
done
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_file_list="$(__find_file_relative "$TMP_DIR/config" || false)"
if [ -n "$get_file_list" ]; then
for conf in $get_file_list; do
if [ -f "/etc/$conf" ]; then
rm -Rf /etc/${conf:?}
fi
if [ -d "$TMP_DIR/config/$conf" ]; then
cp -Rf "$TMP_DIR/config/$conf/." "/etc/$conf/"
cp -Rf "$TMP_DIR/config/$conf/." "$CONFIG_DIR/$conf/"
elif [ -e "$TMP_DIR/config/$config" ]; then
cp -Rf "$TMP_DIR/config/$conf" "/etc/$conf"
cp -Rf "$TMP_DIR/config/$conf" "$CONFIG_DIR/$conf"
fi
done
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -d "$TMP_DIR/init-scripts" ]; then
init_scripts="$(ls -A "$TMP_DIR/init-scripts/" | grep '^' || false)"
if [ -n "$init_scripts" ]; then
mkdir -p "$INIT_DIR"
for init_script in $init_scripts; do
if [ "$OVER_WRITE_INIT" = "yes" ] || [ ! -f "$INIT_DIR/$init_script" ]; then
echo "Installing $INIT_DIR/$init_script"
cp -Rf "$TMP_DIR/init-scripts/$init_script" "$INIT_DIR/$init_script" &&
chmod -Rf 755 "$INIT_DIR/$init_script" || true
fi
done
fi
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -d "$TMP_DIR/www" ]; then
rm -Rf "$WWW_ROOT_DIR"
mkdir -p "$WWW_ROOT_DIR"
cp -Rf "$TMP_DIR/www/." "$WWW_ROOT_DIR/"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ -d "$INIT_DIR" ] && chmod -Rf 755 "$INIT_DIR"/*.sh || true
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ -d "$TMP_DIR/functions" ] && cp -Rf "$TMP_DIR/functions/." "/usr/local/etc/docker/functions/" || true
[ -d "$TMP_DIR/bin" ] && chmod -Rf 755 "$TMP_DIR/bin/" && cp -Rf "$TMP_DIR/bin/." "/usr/local/bin/" || true
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Other files to copy to system
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# custom operations
ARCH="$(uname -m | tr '[:upper]' '[:lower]')"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FORGEJO_BIN_FILE="/usr/local/bin/forgejo"
FORGEJO_VERSION="${FORGEJO_VERSION:-latest}"
API_URL="$(curl -s https://codeberg.org/api/v1/repos/forgejo/forgejo/releases/latest | jq -r '.assets[] | select(.name|match("linux.*'${ARCH}'$")) | .browser_download_url')"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ACT_BIN_FILE="/usr/local/bin/act_runner"
ACT_VERSIONS="$(curl -q -LSsf -X 'GET' 'https://gitea.com/api/v1/repos/gitea/act_runner/releases' -H 'accept: application/json' | jq -r '.[].tag_name' | sort -Vr | head -n1)"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
case "$ARCH" in
x86_64) ARCH="amd64" ;;
aarch64) ARCH="arm64" ;;
*) echo "$ARCH is not supported by this script" && exit 1 ;;
esac
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -n "$FORGEJO_VERSION" ]; then
API_URL="https://codeberg.org/forgejo/forgejo/releases/download/v${FORGEJO_VERSION}forgejo-${FORGEJO_VERSION}-linux-$ARCH"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#ACT_URL="https://dl.gitea.com/act_runner/nightly/act_runner-nightly-linux-$ARCH"
ACT_URL="$(curl -q -LSsf -X 'GET' "https://gitea.com/api/v1/repos/gitea/act_runner/releases/tags/$ACT_VERSIONS" -H 'accept: application/json' | jq -rc '.assets|.[]|.browser_download_url' | grep "linux.*$ARCH$")"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo "Dowloading from $API_URL"
curl -q -LSsf "$API_URL" -o "$FORGEJO_BIN_FILE" && chmod +x "$FORGEJO_BIN_FILE"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo "Downloading act_runner from $ACT_URL"
curl -q -LSsf "$ACT_URL" -o "$ACT_BIN_FILE" && chmod +x "$ACT_BIN_FILE"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -x "$FORGEJO_BIN_FILE" ]; then
echo "forgejo has been installed to: $FORGEJO_BIN_FILE"
if ! grep -q 'forgejo:' /etc/group; then
echo "Adding group: forgejo"
addgroup -S -g 1000 forgejo
fi
if ! grep -q 'forgejo:' /etc/passwd; then
echo "Adding user: forgejo"
adduser -S -H -D -h /home/forgejo -s /bin/bash -u 1000 -G forgejo forgejo && echo "forgejo:*" | chpasswd -e
fi
if grep -qs "docker" "/etc/group"; then
usermod -aG docker forgejo
fi
[ -d "/etc/sudoers.d" ] && echo "forgejo ALL=(ALL) NOPASSWD: ALL" >"/etc/sudoers.d/forgejo"
exitCode=0
else
exitCode=2
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ -x "$ACT_BIN_FILE" ] && echo "Downloaded act to $ACT_BIN_FILE" || exitCode=2
INSTALL_SH_EXIT_STATUS="$exitCode"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -n "$CONFIG_CHECK_FILE" ]; then
if [ ! -f "$CONFIG_DIR/$CONFIG_CHECK_FILE" ]; then
echo "Can not find a config file: $CONFIG_DIR$CONFIG_CHECK_FILE"
INSTALL_SH_EXIT_STATUS=1
fi
else
echo "CONFIG_CHECK_FILE not enabled"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ -d "$TMP_DIR" ] && rm -Rf "$TMP_DIR"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# End application
# eval "$BASH_SET_SAVED_OPTIONS"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# lets exit with code
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit $INSTALL_SH_EXIT_STATUS
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ex: ts=2 sw=2 et filetype=sh