Apply suggestion to rel/files/bin/pleroma_ctl
[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="$(arch)"
6 if [ "$arch" = "x86_64" ]; then
7 arch="amd64"
8 elif [ "$arch" = "armv7l" ]; then
9 arch="arm"
10 elif [ "$arch" = "aarch64" ]; then
11 arch="arm64"
12 else
13 echo "Unsupported arch: $arch" >&2
14 exit 1
15 fi
16
17 if getconf GNU_LIBC_VERSION >/dev/null; then
18 libc_postfix=""
19 elif [ "$(ldd 2>&1 | head -c 9)" = "musl libc" ]; then
20 libc_postfix="-musl"
21 elif [ "$(find /lib/libc.musl* | wc -l)" ]; then
22 libc_postfix="-musl"
23 else
24 echo "Unsupported libc" >&2
25 exit 1
26 fi
27
28 echo "$arch$libc_postfix"
29 }
30
31 detect_branch() {
32 version="$(cut -d' ' -f2 <"$RELEASE_ROOT"/releases/start_erl.data)"
33 branch="$(echo "$version" | cut -d'-' -f 4)"
34 if [ "$branch" = "develop" ]; then
35 echo "develop"
36 elif [ "$branch" = "" ]; then
37 echo "master"
38 else
39 echo "Releases are built only for master and develop branches" >&2
40 exit 1
41 fi
42 }
43 update() {
44 set -e
45 RELEASE_ROOT=$(dirname "$SCRIPTPATH")
46 uri="${PLEROMA_CTL_URI:-https://git.pleroma.social}"
47 project_id="${PLEROMA_CTL_PROJECT_ID:-2}"
48 project_branch="$(detect_branch)"
49 flavour="${PLEROMA_CTL_FLAVOUR:-$(detect_flavour)}"
50 echo "Detected flavour: $flavour"
51 tmp="${PLEROMA_CTL_TMP_DIR:-/tmp}"
52 artifact="$tmp/pleroma.zip"
53 full_uri="${uri}/api/v4/projects/${project_id}/jobs/artifacts/${project_branch}/download?job=${flavour}"
54 echo "Downloading the artifact from ${full_uri} to ${artifact}"
55 curl "$full_uri" -o "${artifact}"
56 echo "Unpacking ${artifact} to ${tmp}"
57 unzip -q "$artifact" -d "$tmp"
58 echo "Copying files over to $RELEASE_ROOT"
59 if [ "$1" != "--no-rm" ]; then
60 rm -r "${RELEASE_ROOT:-?}"/*
61 fi
62 cp -rf "$tmp/release"/* "$RELEASE_ROOT"
63 echo "Removing temporary files"
64 rm -r "$tmp/release"
65 rm "$artifact"
66 echo "Done! Please refer to the changelog/release notes for changes and update instructions"
67 set +e
68 }
69
70 if [ -z "$1" ] || [ "$1" = "help" ]; then
71 # TODO: Just list the commands on `pleroma_ctl help` and output help for the individual command on `pleroma_ctl help $COMMAND`
72 echo "Usage: $(basename "$0") COMMAND [ARGS]
73
74 The known commands are:
75
76 create
77 Create database schema (needs to be executed only once)
78
79 migrate
80 Execute database migrations (needs to be done after updates)
81
82 rollback [VERSION]
83 Rollback database migrations (needs to be done before downgrading)
84
85 update [OPTIONS]
86 Update the instance using the latest CI artifact for the current branch.
87
88 The only supported option is --no-rm, when set the script won't delete the whole directory, but
89 just force copy over files from the new release. This wastes more space, but may be useful if
90 some files are stored inside of the release directories (although you really shouldn't store them
91 there), or if you want to be able to quickly revert a broken update.
92
93 The script will try to detect your architecture and ABI and set a flavour automatically,
94 but if it is wrong, you can overwrite it by setting PLEROMA_CTL_FLAVOUR to the desired flavour.
95
96 By default the artifact will be downloaded from https://git.pleroma.social for pleroma/pleroma (project id: 2)
97 to /tmp/, you can overwrite these settings by setting PLEROMA_CTL_URI, PLEROMA_CTL_PROJECT_ID and PLEROMA_CTL_TMP_DIR
98 respectively.
99
100
101 and any mix tasks under Pleroma namespace, for example \`mix pleroma.user COMMAND\` is
102 equivalent to \`$(basename "$0") user COMMAND\`
103
104 By default pleroma_ctl will try calling into a running instance to execute non migration-related commands,
105 if for some reason this is undesired, set PLEROMA_CTL_RPC_DISABLED environment variable
106 "
107 else
108 SCRIPT=$(readlink -f "$0")
109 SCRIPTPATH=$(dirname "$SCRIPT")
110
111 if [ "$1" = "update" ]; then
112 update "$2"
113 elif [ "$1" = "migrate" ] || [ "$1" = "rollback" ] || [ "$1" = "create" ] || [ -n "$PLEROMA_CTL_RPC_DISABLED" ]; then
114 "$SCRIPTPATH"/pleroma eval 'Pleroma.ReleaseTasks.run("'"$*"'")'
115 else
116 "$SCRIPTPATH"/pleroma rpc 'Pleroma.ReleaseTasks.run("'"$*"'")'
117 fi
118 fi