summaryrefslogtreecommitdiffstats
path: root/install-server.sh
blob: 3724e16a66bc708f9def88acb8aca13c34651061 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/sh
# -*- coding: utf-8 -*-

basedir="$(realpath "$0" | xargs dirname)"

info()
{
    echo "--- $*"
}

error()
{
    echo "=== ERROR: $*" >&2
}

warning()
{
    echo "=== WARNING: $*" >&2
}

die()
{
    error "$*"
    exit 1
}

do_install()
{
    info "install $*"
    install "$@" || die "Failed install $*"
}

do_systemctl()
{
    info "systemctl $*"
    systemctl "$@" || die "Failed to systemctl $*"
}

do_chown()
{
    info "chown $*"
    chown "$@" || die "Failed to chown $*"
}

do_chmod()
{
    info "chmod $*"
    chmod "$@" || die "Failed to chmod $*"
}

try_systemctl()
{
    info "systemctl $*"
    systemctl "$@" 2>/dev/null
}

do_chown()
{
    info "chown $*"
    chown "$@" || die "Failed to chown $*"
}

do_chmod()
{
    info "chmod $*"
    chmod "$@" || die "Failed to chmod $*"
}

entry_checks()
{
    [ -d "$target" ] || die "letmein is not built! Run ./build.sh"

    [ "$(id -u)" = "0" ] || die "Must be root to install letmein."

    if ! grep -qe '^letmeind:' /etc/passwd; then
        die "The system user 'letmeind' does not exist in /etc/passwd. Please run ./create-user.sh"
    fi
    if ! grep -qe '^letmeind:' /etc/group; then
        die "The system group 'letmeind' does not exist in /etc/group. Please run ./create-user.sh"
    fi
}

stop_services()
{
    try_systemctl stop letmeind.socket
    try_systemctl stop letmeind.service
    try_systemctl stop letmeinfwd.socket
    try_systemctl stop letmeinfwd.service
    try_systemctl disable letmeind.service
    try_systemctl disable letmeind.socket
    try_systemctl disable letmeinfwd.service
    try_systemctl disable letmeinfwd.socket
}

start_services()
{
    do_systemctl start letmeinfwd.socket
    do_systemctl start letmeinfwd.service
    do_systemctl start letmeind.socket
    do_systemctl start letmeind.service
}

install_dirs()
{
    do_install \
        -o root -g root -m 0755 \
        -d /opt/letmein/bin

    do_install \
        -o root -g root -m 0755 \
        -d /opt/letmein/etc
}

install_conf()
{
    if [ -e /opt/letmein/etc/letmeind.conf ]; then
        do_chown root:letmeind /opt/letmein/etc/letmeind.conf
        do_chmod 0640 /opt/letmein/etc/letmeind.conf
    else
        do_install \
            -o root -g letmeind -m 0640 \
            "$basedir/letmeind/letmeind.conf" \
            /opt/letmein/etc/letmeind.conf
    fi
}

install_letmeinfwd()
{
    do_install \
        -o root -g root -m 0755 \
        "$target/letmeinfwd" \
        /opt/letmein/bin/

    do_install \
        -o root -g root -m 0644 \
        "$basedir/letmeinfwd/letmeinfwd.service" \
        /etc/systemd/system/

    do_install \
        -o root -g root -m 0644 \
        "$basedir/letmeinfwd/letmeinfwd.socket" \
        /etc/systemd/system/

    do_systemctl enable letmeinfwd.socket
    do_systemctl enable letmeinfwd.service
}

install_letmeind()
{
    do_install \
        -o root -g root -m 0755 \
        "$target/letmeind" \
        /opt/letmein/bin/

    do_install \
        -o root -g root -m 0644 \
        "$basedir/letmeind/letmeind.service" \
        /etc/systemd/system/

    do_install \
        -o root -g root -m 0644 \
        "$basedir/letmeind/letmeind.socket" \
        /etc/systemd/system/

    do_systemctl enable letmeind.socket
    do_systemctl enable letmeind.service
}

release="release"
while [ $# -ge 1 ]; do
    case "$1" in
        --debug|-d)
            release="debug"
            ;;
        --release|-r)
            release="release"
            ;;
        *)
            die "Invalid option: $1"
            ;;
    esac
    shift
done
target="$basedir/target/$release"

entry_checks
stop_services
install_dirs
install_conf
install_letmeinfwd
install_letmeind
start_services

# vim: ts=4 sw=4 expandtab
bues.ch cgit interface