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