e0e6d1b5af867bfae4b44c4ccf68c4b3a2c5ac82
[akkoma] / rel / files / bin / pleroma_ctl
1 #!/bin/sh
2 # XXX: This should be removed when elixir's releases get custom command support
3
4 detect_flavour() {
5 arch="amd64"
6 # Special cases
7 if grep -qe "VERSION_CODENAME=jammy" /etc/os-release; then
8 echo "$arch-ubuntu-jammy"
9 else
10 if getconf GNU_LIBC_VERSION >/dev/null; then
11 libc_postfix=""
12 elif [ "$(ldd 2>&1 | head -c 9)" = "musl libc" ]; then
13 libc_postfix="-musl"
14 elif [ "$(find /lib/libc.musl* | wc -l)" ]; then
15 libc_postfix="-musl"
16 else
17 echo "Unsupported libc" >&2
18 exit 1
19 fi
20
21 echo "$arch$libc_postfix"
22 fi
23 }
24
25 detect_branch() {
26 version="$(cut -d' ' -f2 <"$RELEASE_ROOT"/releases/start_erl.data)"
27 # Expected format: major.minor.patch_version(-number_of_commits_ahead_of_tag-gcommit_hash).branch
28 branch="$(echo "$version" | cut -d'.' -f 4)"
29 if [ "$branch" = "develop" ]; then
30 echo "develop"
31 elif [ "$branch" = "" ]; then
32 echo "stable"
33 else
34 # Note: branch name in version is of SemVer format and may only contain [0-9a-zA-Z-] symbols —
35 # if supporting releases for more branches, need to ensure they contain only these symbols.
36 echo "Can't detect the branch automatically, please specify it by using the --branch option." >&2
37 exit 1
38 fi
39 }
40 update() {
41 set -e
42 NO_RM=false
43
44 while echo "$1" | grep "^-" >/dev/null; do
45 case "$1" in
46 --zip-url)
47 FULL_URI="$2"
48 shift 2
49 ;;
50 --no-rm)
51 NO_RM=true
52 shift
53 ;;
54 --flavour)
55 FLAVOUR="$2"
56 shift 2
57 ;;
58 --branch)
59 BRANCH="$2"
60 shift 2
61 ;;
62 --tmp-dir)
63 TMP_DIR="$2"
64 shift 2
65 ;;
66 -*)
67 echo "invalid option: $1" 1>&2
68 shift
69 ;;
70 esac
71 done
72
73 RELEASE_ROOT=$(dirname "$SCRIPTPATH")
74 uri="https://akkoma-updates.s3-website.fr-par.scw.cloud"
75 project_id="2"
76 project_branch="${BRANCH:-$(detect_branch)}"
77 flavour="${FLAVOUR:-$(detect_flavour)}"
78 tmp="${TMP_DIR:-/tmp}"
79 artifact="$tmp/pleroma.zip"
80 full_uri="${FULL_URI:-${uri}/${project_branch}/akkoma-${flavour}}.zip"
81 echo "Downloading the artifact from ${full_uri} to ${artifact}"
82 curl "$full_uri" -o "${artifact}"
83 echo "Unpacking ${artifact} to ${tmp}"
84 unzip -q "$artifact" -d "$tmp"
85 echo "Backing up erlang cookie"
86 erlang_cookie=$(cat $RELEASE_ROOT/releases/COOKIE)
87 echo "Cookie: $erlang_cookie"
88 echo "Copying files over to $RELEASE_ROOT"
89 if [ "$NO_RM" = false ]; then
90 echo "Removing files from the previous release"
91 rm -r "${RELEASE_ROOT:-?}"/*
92 fi
93 cp -rf "$tmp/release"/* "$RELEASE_ROOT"
94 echo "Removing temporary files"
95 rm -r "$tmp/release"
96 rm "$artifact"
97 echo "Restoring erlang cookie"
98 echo $erlang_cookie > $RELEASE_ROOT/releases/COOKIE
99 echo "Done! Please refer to the changelog/release notes for changes and update instructions"
100 echo "You probably also want to update your frontend!"
101 set +e
102 }
103
104 if [ -z "$1" ] || [ "$1" = "help" ]; then
105 # TODO: Just list the commands on `pleroma_ctl help` and output help for the individual command on `pleroma_ctl help $COMMAND`
106 echo "Usage: $(basename "$0") COMMAND [ARGS]
107
108 The known commands are:
109
110 create
111 Create database schema (needs to be executed only once)
112
113 migrate
114 Execute database migrations (needs to be done after updates)
115
116 rollback [VERSION]
117 Rollback database migrations (needs to be done before downgrading)
118
119 update [OPTIONS]
120 Update the instance.
121
122 Options:
123 --branch Update to a specified branch, instead of the latest version of the current one.
124 --flavour Update to a specified flavour (CPU architecture+libc), instead of the current one.
125 --zip-url Get the release from a specified url. If set, renders the previous 2 options inactive.
126 --no-rm Do not erase previous release's files.
127 --tmp-dir Download the temporary files to a specified directory.
128
129 and any mix tasks under Pleroma namespace, for example \`mix pleroma.user COMMAND\` is
130 equivalent to \`$(basename "$0") user COMMAND\`
131
132 By default pleroma_ctl will try calling into a running instance to execute non migration-related commands,
133 if for some reason this is undesired, set PLEROMA_CTL_RPC_DISABLED environment variable.
134
135 "
136 else
137 SCRIPT=$(readlink -f "$0")
138 SCRIPTPATH=$(dirname "$SCRIPT")
139
140 FULL_ARGS="$*"
141
142 ACTION="$1"
143 if [ $# -gt 0 ]; then
144 shift
145 fi
146 echo "$1" | grep "^-" >/dev/null
147 if [ $? -eq 1 ]; then
148 SUBACTION="$1"
149 if [ $# -gt 0 ]; then
150 shift
151 fi
152 fi
153
154 if [ "$ACTION" = "update" ]; then
155 update "$@"
156 elif [ "$ACTION" = "migrate" ] || [ "$ACTION" = "rollback" ] || [ "$ACTION" = "create" ] || [ "$ACTION $SUBACTION" = "instance gen" ] || [ "$PLEROMA_CTL_RPC_DISABLED" = true ]; then
157 "$SCRIPTPATH"/pleroma eval 'Pleroma.ReleaseTasks.run("'"$FULL_ARGS"'")'
158 else
159 "$SCRIPTPATH"/pleroma rpc 'Pleroma.ReleaseTasks.run("'"$FULL_ARGS"'")'
160 fi
161 fi