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