This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
install.sh
206 lines (159 loc) · 5.25 KB
/
install.sh
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
#!/bin/bash
export TEMP_FOLDER="temp_nitro_extract"
export FINAL_DIR_LOCATION="/usr/local/bin"
export DOWNLOAD_SUFFIX=""
export DOWNLOAD_ARCH=""
export DOWNLOAD_ZIP_EXTENSION=".tar.gz"
export EXECUTABLE_NAME="nitro"
function checkPlatform {
uname=$(uname)
case $uname in
"Darwin")
DOWNLOAD_SUFFIX="_darwin"
;;
"Linux")
DOWNLOAD_SUFFIX="_linux"
;;
*)
;;
esac
arch=$(uname -m)
case $arch in
"x86_64")
DOWNLOAD_ARCH="_x86_64"
;;
"aarch64")
DOWNLOAD_ARCH="_aarch64"
;;
"arm64")
DOWNLOAD_ARCH="_arm64"
;;
*)
;;
esac
}
checkPlatform
version=$(curl -s https://api.github.com/repos/craftcms/nitro/releases | grep -i -m 1 tag_name | head -1 | sed 's/\("tag_name": "\(.*\)",\)/\2/' | tr -d '[:space:]')
if [ ! "$version" ]; then
echo "There was a problem trying to automatically install Nitro. You can try to install manually:"
echo
echo "1. Open your web browser and go to https://github.com/craftcms/nitro/releases"
echo "2. Download the latest release for your platform and unzip it."
echo "3. Run 'chmod +x ./$EXECUTABLE_NAME' on the unzipped \"$EXECUTABLE_NAME\" executable."
echo "4. Run 'mv ./$EXECUTABLE_NAME $FINAL_DIR_LOCATION'"
echo "5. Run 'nitro init' to create your first machine."
exit 1
fi
function hasCurl {
result=$(command -v curl)
if [ "$?" = "1" ]; then
echo "You need curl to install Nitro."
exit 1
fi
}
function hasDocker {
result=$(command -v docker)
if [ "$?" = "1" ]; then
echo "You need Docker Desktop to use Nitro. Please install it for your platform at https://www.docker.com/products/docker-desktop"
exit 1
fi
}
function checkHash {
sha_cmd="sha256sum"
fileName=nitro_$2_checksums.txt
filePath="$(pwd)/$TEMP_FOLDER/$fileName"
checksumUrl=https://github.com/craftcms/nitro/releases/download/$version/$fileName
targetFile=$3/$fileName
if [ ! -x "$(command -v $sha_cmd)" ]; then
shaCmd="shasum -a 256"
fi
if [ -x "$(command -v $shaCmd)" ]; then
# download the checksum file.
(curl -sLS "$checksumUrl" --output "$targetFile")
# Run the sha command against the zip and grab the hash from the first segment.
zipHash="$($shaCmd "$1" | cut -d' ' -f1 | tr -d '[:space:]')"
# See if the has we calculated matches a result in the checksum file.
checkResultFileName=$(sed -n "s/^$zipHash //p" "$filePath")
# don't need this anymore
rm "$filePath"
# Make sure the file names match up.
if [ "$4" != "$checkResultFileName" ]; then
rm "$1"
echo "It looks like there was an incomplete download. Please try again."
exit 1
fi
fi
}
function getNitro {
targetTempFolder="$(pwd)/$TEMP_FOLDER"
# create our temp folder
if [ ! -d "$targetTempFolder" ]; then
mkdir "$targetTempFolder"
fi
fileName=nitro$DOWNLOAD_SUFFIX$DOWNLOAD_ARCH$DOWNLOAD_ZIP_EXTENSION
packageUrl=https://github.com/craftcms/nitro/releases/download/$version/"$fileName"
targetZipFile="$targetTempFolder/$fileName"
echo "Downloading package $packageUrl to $targetZipFile"
echo
curl -sSL "$packageUrl" --output "$targetZipFile"
if [ "$?" = "0" ]; then
# unzip
tar xzf "$targetZipFile" -C "$targetTempFolder"
# verify
checkHash "$targetZipFile" "$version" "$targetTempFolder" "$fileName"
# move executable up a level
mv "$targetTempFolder"/"$EXECUTABLE_NAME" ./"$EXECUTABLE_NAME"
# make it executable
chmod +x ./$EXECUTABLE_NAME
echo
echo "Download complete."
if [ ! -w "$FINAL_DIR_LOCATION" ]; then
echo
echo "============================================================"
echo " The script was run as a user who is unable to write"
echo " to $FINAL_DIR_LOCATION. To complete the installation the"
echo " following commands may need to be run manually:"
echo "============================================================"
echo
echo " sudo cp ./$EXECUTABLE_NAME $FINAL_DIR_LOCATION/$EXECUTABLE_NAME"
echo " $EXECUTABLE_NAME init"
echo
exit 1
else
echo
echo "Running with sufficient permissions to attempt to move $EXECUTABLE_NAME to $FINAL_DIR_LOCATION"
echo
if [ ! -w "$FINAL_DIR_LOCATION/$EXECUTABLE_NAME" ] && [ -f "$FINAL_DIR_LOCATION/$EXECUTABLE_NAME" ]; then
echo
echo "============================================================"
echo " $FINAL_DIR_LOCATION/$EXECUTABLE_NAME already exists and is"
echo " not writeable by this installer. To complete the installation the"
echo " following commands may need to be run manually:"
echo "============================================================"
echo
echo " sudo cp ./$EXECUTABLE_NAME $FINAL_DIR_LOCATION/$EXECUTABLE_NAME"
echo " $EXECUTABLE_NAME init"
echo
exit 1
fi
# move to final location
mv ./$EXECUTABLE_NAME "$FINAL_DIR_LOCATION"/$EXECUTABLE_NAME
if [ "$?" = "0" ]; then
echo "Nitro $version has been installed to $FINAL_DIR_LOCATION"
fi
if [ -e "$targetTempFolder" ] && [ -w "$targetTempFolder" ]; then
rm -rf "$targetTempFolder"
echo
fi
fi
fi
}
function promptInstall {
nitro init
}
hasCurl
hasDocker
getNitro
if [ "$1" != "update" ]; then
promptInstall
fi