summaryrefslogtreecommitdiffhomepage
path: root/tools/setup-unit
blob: 2e10e54de172a1521da19b2e9440b877c12354a2 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/sh

#####################################################################
#
# Copyright (C) NGINX, Inc.
#
# Author:  NGINX Unit Team, F5 Inc.
# Version: 0.0.1
# Date:    2022-05-05
#
# This script will configure the repositories for NGINX Unit on Ubuntu,
# Debian, RedHat, CentOS, Oracle Linux, Amazon Linux, Fedora.
# It must be run as root.
#
# Note: curl and awk are required by this script, so the script checks to make
# sure they are installed.
#
#####################################################################

export LC_ALL=C

checkOSPrereqs () {

    if ! command -v curl > /dev/null 2>&1
    then
        echo "Error: curl not found in PATH.  It must be installed to run this script."
        exit 1
    fi

    if ! command -v awk > /dev/null 2>&1
    then
        echo "Error: awk not found in PATH.  It must be installed to run this script."
        exit 1
    fi

    return 0
}

#####################################################################
# Function getOS
#
# Getting the OS is not the same on all distributions.  First, we use
# uname to find out if we are running on Linux or FreeBSD. For all the
# supported versions of Debian and Ubuntu, we expect to find the
# /etc/os-release file which has multiple lines with name-value pairs
# from which we can get the OS name and version. For RedHat and its
# variants, the os-release file may or may not exist, depending on the
# version.  If it doesn't, then we look for the release package and
# get the OS and version from the package name. For FreeBSD, we use the
# "uname -rs" command.
#
# A string is written to stdout with three values separated by ":":
#    OS
#    OS Name
#    OS Version
#
# If none of these files was found, an empty string is written.
#
# Return: 0 for success, 1 for error
#####################################################################
getOS () {

    os=""
    osName=""
    osVersion=""

    LC_ALL=C

    os=$(uname | tr '[:upper:]' '[:lower:]')

    if [ "$os" != "linux" ] && [ "$os" != "freebsd" ]; then
        echoErr "Error: Operating system is not Linux or FreeBSD, can't proceed"
        echo "On macOS, try 'brew install nginx/unit/unit'"
        echo
        return 1
    fi

    if [ "$os" = "linux" ]; then
        if [ -f "$osRelease" ]; then
            # The value for the ID and VERSION_ID may or may not be in quotes
            osName=$( grep "^ID=" "$osRelease" | sed s/\"//g | awk -F= '{ print $2 }')
            osVersion=$(grep "^VERSION_ID=" "$osRelease" | sed s/\"//g | awk -F= '{ print $2 }')
        else
            # rhel or centos 6.*
            if rpm -q redhat-release-server >/dev/null 2>&1; then
                osName=rhel
                osVersion=$(rpm -q redhat-release-server |sed 's/.*-//' | awk -F. '{print $1"."$2;}')
            elif rpm -q centos-release >/dev/null 2>&1; then
                osName=centos
                osVersion=$(rpm -q centos-release | sed 's/centos-release-//' | sed 's/\..*//' | awk -F- '{print $1"."$2;}')
            else
                echoErr "Error: Unable to determine the operating system and version, or the OS is not supported"
                echo
                return 1
            fi
        fi
    else
        osName=$os
        osVersion=$(uname -rs | awk -F '[ -]' '{print $2}')
        if [ -z "$osVersion" ]; then
            echoErr "Unable to get the FreeBSD version"
            echo
            return 1
        fi
    fi

    # Force osName to lowercase
    osName=$(echo "$osName" | tr '[:upper:]' '[:lower:]')
    echoDebug "getOS: os=$os osName=$osName osVersion=$osVersion"
    echo "$os:$osName:$osVersion"

    return 0
}


installDebian () {

    echoDebug "Install on Debian"

    curl --output /usr/share/keyrings/nginx-keyring.gpg https://unit.nginx.org/keys/nginx-keyring.gpg

    apt install -y apt-transport-https lsb-release ca-certificates

    printf "deb [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ %s unit\n" "$(lsb_release -cs)" | tee /etc/apt/sources.list.d/unit.list
    printf "deb-src [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ %s unit\n" "$(lsb_release -cs)" | tee -a /etc/apt/sources.list.d/unit.list

    apt update

    return 0
}

installUbuntu () {

    echoDebug "Install on Ubuntu"

    curl --output /usr/share/keyrings/nginx-keyring.gpg https://unit.nginx.org/keys/nginx-keyring.gpg

    apt install -y apt-transport-https lsb-release ca-certificates

    printf "deb [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/ubuntu/ %s unit\n" "$(lsb_release -cs)" | tee /etc/apt/sources.list.d/unit.list
    printf "deb-src [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/ubuntu/ %s unit\n" "$(lsb_release -cs)" | tee -a /etc/apt/sources.list.d/unit.list

    apt update

    return 0
}

installRedHat () {

    echoDebug "Install on RedHat/CentOS/Oracle"

    case "$osVersion" in
        6|6.*|7|7.*|8|8.*)
            cat << __EOF__ > /etc/yum.repos.d/unit.repo
[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/rhel/\$releasever/\$basearch/
gpgcheck=0
enabled=1
__EOF__
            ;;
        *)
            echo "Unsupported $osName version: $osVersion"
            exit 1
            ;;
    esac

    yum makecache

    return 0
}

installAmazon () {

    echoDebug "Install on Amazon"

    case "$osVersion" in
        2)
            cat << __EOF__ > /etc/yum.repos.d/unit.repo
[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/amzn2/\$releasever/\$basearch/
gpgcheck=0
enabled=1
__EOF__
         ;;
        *)
            cat << __EOF__ > /etc/yum.repos.d/unit.repo
[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/amzn/\$releasever/\$basearch/
gpgcheck=0
enabled=1
__EOF__
         ;;
    esac

    yum makecache

    return 0
}

installFedora () {

    echoDebug "Install on Fedora"

    cat << __EOF__ > /etc/yum.repos.d/unit.repo
[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/fedora/\$releasever/\$basearch/
gpgcheck=0
enabled=1
__EOF__

    dnf makecache

    return 0
}


am_i_root() {

    USERID=$(id -u)
    if [ 0 -ne "$USERID" ]; then
        echoErr "This script requires root privileges to run; now exiting."
        exit 1
    fi

    return 0
}

echoErr () {

    echo "$*" 1>&2;
}

echoDebug () {

    if [ "$debug" -eq 1 ]; then
        echo "$@" 1>&2;
    fi
}

main() {
debug=0 # If set to 1, debug message will be displayed

checkOSPrereqs

# The name and location of the files that will be used to get Linux
# release info
osRelease="/etc/os-release"

os="" # Will be "linux" or "freebsd"
osName="" # Will be "ubuntu", "debian", "rhel",
          # "centos", "suse", "amzn", or "freebsd"
osVersion=""

am_i_root

echo "This script will setup repositories for NGINX Unit"

# Check the OS
osNameVersion=$(getOS)
if [ -z "$osNameVersion" ]; then
    echoErr "Error getting the operating system information"
    exit 1
fi

# Break out the OS, name, and version
os=$(echo "$osNameVersion" | awk -F: '{print $1}')
osName=$(echo "$osNameVersion" | awk -F: '{print $2}')
osVersion=$(echo "$osNameVersion" | awk -F: '{print $3}')

# Call the appropriate installation function
case "$osName" in
    debian)
        installDebian
        ;;
    ubuntu)
        installUbuntu
        ;;
    rhel)
        installRedHat
        ;;
    centos)
        installRedHat
        ;;
    ol)
        installRedHat
        ;;
    amzn)
        installAmazon
        ;;
    fedora)
        installFedora
        ;;
    *)
        echo "$osName is not supported"
        exit 1
        ;;
esac

echo
echo "All done - NGINX Unit repositories for "$osName" "$osVersion" are set up"
echo "Further steps: https://unit.nginx.org/installation/#official-packages"

}

main

exit 0