summarylogtreecommitdiffstats
path: root/misskey.sh
blob: 88e9faea9f8db43ab2e755307de26120def75408 (plain)
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
#!/usr/bin/env sh
##########################################################
######################### License ########################

## The MIT License (MIT)
##
## Copyright © 2022 Fabian Bornschein <fabiscafe/at/mailbox/dot/org>
##
##Permission is hereby granted, free of charge, to any person obtaining a copy
##of this software and associated documentation files (the “Software”), to
##deal in the Software without restriction, including without limitation the
##rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
##sell copies of the Software, and to permit persons to whom the Software is
##furnished to do so, subject to the following conditions:
##
##The above copyright notice and this permission notice shall be included in
##all copies or substantial portions of the Software.
##
##THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
##IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
##AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
##LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
##FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
##IN THE SOFTWARE.

##########################################################
###################### Permissions #######################

# Exit script on failures
set -e

# Exit script if not called by root
if [ $(id -u) -ne 0 ]; then
    printf "You must be root\n"
    exit 1
fi

##########################################################
###################### Dependencies ######################

# Only run this script if all dependencies are available
Dependencylist=(
  "/usr/bin/runuser"
  "/usr/bin/systemctl"
  "/usr/bin/yarn"
)

for dep in ${Dependencylist[@]}; do
    if ! [ -f ${dep} ]; then
        printf "Dependency: ${dep} not found\n"
        DEP_MISSING=1
    fi
done
# Stop here if a dependency is missing
if ! [ -z ${DEP_MISSING+x} ]; then
    exit 1
fi

##########################################################
####################### Functions ########################

HELP()
{
   printf "Misskey helper script.\n"
   printf "\n"
   printf "Syntax: %s -[i|I|m|M|h]\n" $0
   printf "options:\n"
   printf -- "  -i\tInitialize misskey databases for the first run.\n"
   printf -- "  -I\tSame as -i, without DB and Redis check. (For use with external database servers)\n"
   printf -- "  -m\tMigrate the database to the new version.\n"
   printf -- "  -M\tSame as -m, without DB and Redis check. (For use with external database servers)\n"
   printf -- "  -h\tPrint this Help.\n"
   printf "\n"
   printf "https://wiki.archlinux.org/title/Misskey\n"
}

PSQL_REDIS_STATUS()
{
    printf "Current service-status\n"
    printf "   PostgreSQL:\t%s\n" $(/usr/bin/systemctl is-active postgresql.service)
    printf "   Redis:\t%s\n" $(/usr/bin/systemctl is-active redis.service)
}

PSQL_REDIS_DEP()
{
if ! /usr/bin/systemctl is-active postgresql.service redis.service > /dev/null; then
    printf "This functionality requires PostgreSQL and Redis running\n"
    printf "Please start the services and run %s again\n" $0
    printf "\n"
    PSQL_REDIS_STATUS
    exit 1;
fi
}

INIT()
{
    ## It should be impossible that misskey runs before init, but better check anyways
    if /usr/bin/systemctl is-active misskey.service > /dev/null; then
        printf "Shutting down misskey…\n"
        /usr/bin/systemctl stop misskey.service
    fi
    printf "Initialize Misskey databases…\n"
    cd /usr/share/webapps/misskey
    /usr/bin/runuser -u misskey -- env HOME="/usr/share/webapps/misskey" /usr/bin/yarn run init
}

MIGRATE()
{
    # Misskey needs to be stopped before migration.
    if /usr/bin/systemctl is-active misskey.service > /dev/null; then
        printf "Shutting down misskey\n"
        /usr/bin/systemctl stop misskey.service
        MK_ACTIVE=1
    fi
    printf "Migrate data to new version\n"
    cd "/usr/share/webapps/misskey/"
    /usr/bin/runuser -u misskey -- env HOME="/usr/share/webapps/misskey" /usr/bin/yarn migrate
    if ! [ -z ${MK_ACTIVE+x} ] ; then
        printf "starting up misskey\n"
        /usr/bin/systemctl start misskey.service
    else
        printf "Misskey service is *not* started\n"
        printf "Thats your job!\n"
    fi
}

##########################################################
########################## Main ##########################

# Get the options
while getopts "i I m M h" option; do
    case $option in
        i)
            PSQL_REDIS_DEP
            INIT
            exit 0;;
        I)
            INIT
            exit 0;;
        m)
            PSQL_REDIS_DEP
            MIGRATE
            exit 0;;
        M)
            MIGRATE
            exit 0;;
        \?)
            HELP
            exit 0;;
   esac
done

HELP
exit 0