This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
cloudflare.install.sh
executable file
·241 lines (194 loc) · 7.86 KB
/
cloudflare.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
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
#!/bin/bash
#
# CloudFlare cPanel Install Script
#
INSTALLER="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
usage() {
echo "Usage: ./$INSTALLER -k HOST_KEY -n 'ORG_NAME' [-f /path/to/local/cpanel.tar.gz ]"
echo
echo " -k HOST_KEY - Your host key. If you do not have one contact"
echo " CloudFlare to obtain one before proceeding."
echo " -n ORG_NAME - The name of your organization."
echo " -f LOCAL_FILE - By default the latest version of the cPanel plugin is"
echo " downloaded from Github and installed. If the optional -f flag"
echo " is given the install is done from a local file path."
echo " The local file should be called CloudFlare-CPanel-1.2.3.tar.gz where 1.2.3 is"
echo " the version number."
echo " -v - Verbose"
echo
exit 1
}
# Parse the arguments
HOST_KEY=""
ORG_NAME=""
LOCAL_FILE_PATH=""
VERBOSE=false
while getopts ":k:n:f:v" opt; do
case $opt in
k)
if [ "$HOST_KEY" = "" ]; then
HOST_KEY=$OPTARG
fi
;;
n)
if [ "$ORG_NAME" = "" ]; then
ORG_NAME=$OPTARG
fi
;;
f)
if [ "$LOCAL_FILE_PATH" = "" ]; then
LOCAL_FILE_PATH=$OPTARG
fi
;;
v)
VERBOSE=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument" >&2
exit 1
;;
esac
done
# Check for required arguments
if [ "$HOST_KEY" = "" ]; then
echo "ERROR - Missing HOST_KEY"
echo
usage
exit 1
fi
if [ "$ORG_NAME" = "" ]; then
echo "ERROR - Missing ORG_NAME"
echo
usage
exit 1
fi
if [ "$VERBOSE" = true ]; then
echo "HOST_KEY = '$HOST_KEY'"
echo "ORG_NAME = '$ORG_NAME'"
echo "LOCAL_FILE_PATH = '$LOCAL_FILE_PATH'"
fi
# Check that we're running as root (or "effectively" as root, i.e., euid=0)
if [[ $EUID -ne 0 ]]; then
echo "You must run this installation script as root."
exit 1
fi
if [ "$VERBOSE" = true ]; then
echo "Running as root"
fi
echo "Starting CloudFlare CPanel Installation..."
#
# If not installing from a local file we need to download and untar
#
if [ "$LOCAL_FILE_PATH" = "" ]; then
# Find the proper version to download
LATEST_VERSION=$(curl -s https://api.cloudflare.com/host-gw.html -d "act=cpanel_info" -d "host_key=$HOST_KEY" | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep cpanel_latest | cut -d "\"" -f 6)
if [ "$VERBOSE" = true ]; then
echo "LATEST_VERSION - '$LATEST_VERSION'"
fi
if [ -z "$LATEST_VERSION" ]; then
echo -e "ERROR: Could not find latest version. Please double check your HOST_KEY.\n"
usage
else
echo "Downloading and unpacking latest version v$LATEST_VERSION..."
fi
# Download and extract
DOWNLOAD_URL="https://github.com/cloudflare/CloudFlare-CPanel/archive/v$LATEST_VERSION.tar.gz"
if [ "$VERBOSE" = true ]; then
echo "curl -sL $DOWNLOAD_URL | tar xfz -"
fi
curl -sL $DOWNLOAD_URL | tar xzf -
# We could check for extract errors here, but the directory check outside
# the if statement will take care of this
#
# We're installing from a local file
#
else
# Check to make sure the file exists
if [ ! -f $LOCAL_FILE_PATH ]; then
echo "ERROR - Not found '$LOCAL_FILE_PATH'"
exit 1
fi
# We expect the file to be named like so: 'Cloudflare-CPanel-$LATEST_VERSION.tar.gz'
# for example Cloudflare-CPanel-1.2.3.tar.gz
FNAME=$(basename "$LOCAL_FILE_PATH");
LATEST_VERSION=$(echo -n "$FNAME" | sed 's/^Cloudflare-CPanel-//' | sed 's/.tar.gz//')
if [ "$VERBOSE" = true ]; then
echo "LATEST_VERSION - '$LATEST_VERSION'"
fi
echo "Unpacking from local tar file '$LOCAL_FILE_PATH'"
tar xfz $LOCAL_FILE_PATH
# We could check for extract errors here, but the directory check outside
# the if statement will take care of this
fi
# Make sure that the tar directory got created correctly. We expect a directory
# name something like this: Cloudflare-CPanel-$LATEST_VERSION/cloudflare
if [ ! -d "Cloudflare-CPanel-$LATEST_VERSION" ]; then
echo "ERROR - Unpack failed, directory not found: 'Cloudflare-CPanel-$LATEST_VERSION'"
exit 1
fi
SOURCE_DIR="Cloudflare-CPanel-$LATEST_VERSION"
INSTALL_DIR="/usr/local/cpanel"
if [ "$VERBOSE" = true ]; then
echo "Installing from '$SOURCE_DIR' to '$INSTALL_DIR'"
fi
# Create the cloudflare theme directory if it does not exist, then install files
install -d $INSTALL_DIR/base/frontend/paper_lantern/cloudflare
install $SOURCE_DIR/proxy.live.php $INSTALL_DIR/base/frontend/paper_lantern/cloudflare
install $SOURCE_DIR/index.live.php $INSTALL_DIR/base/frontend/paper_lantern/cloudflare
install $SOURCE_DIR/compiled.js $INSTALL_DIR/base/frontend/paper_lantern/cloudflare
install $SOURCE_DIR/config.json.sample $INSTALL_DIR/base/frontend/paper_lantern/cloudflare
# composer.json is used by cloudflare.update.sh to determine the current version number
install $SOURCE_DIR/composer.json $INSTALL_DIR/base/frontend/paper_lantern/cloudflare
# Install internationalization directory
install -d $INSTALL_DIR/base/frontend/paper_lantern/cloudflare/lang
install $SOURCE_DIR/lang/* $INSTALL_DIR/base/frontend/paper_lantern/cloudflare/lang
# Install assets directory
install -d $INSTALL_DIR/base/frontend/paper_lantern/cloudflare/assets
install $SOURCE_DIR/assets/* $INSTALL_DIR/base/frontend/paper_lantern/cloudflare/assets
# Install fonts directory
install -d $INSTALL_DIR/base/frontend/paper_lantern/cloudflare/fonts
install $SOURCE_DIR/fonts/* $INSTALL_DIR/base/frontend/paper_lantern/cloudflare/fonts
# Install stylesheets directory
install -d $INSTALL_DIR/base/frontend/paper_lantern/cloudflare/stylesheets
install $SOURCE_DIR/stylesheets/* $INSTALL_DIR/base/frontend/paper_lantern/cloudflare/stylesheets
# Install the CloudFlare.pm file
install -d $INSTALL_DIR/Cpanel/API
install $SOURCE_DIR/CloudFlare.pm $INSTALL_DIR/Cpanel/API
# Install host key file
echo $HOST_KEY > /root/.cpanel/datastore/cf_api
chmod 600 /root/.cpanel/datastore/cf_api
# Install the API key CPanel adminbin and config
install -d $INSTALL_DIR/bin/admin/CloudFlare
install $SOURCE_DIR/APIKey $INSTALL_DIR/bin/admin/CloudFlare/APIKey
chmod 0700 $INSTALL_DIR/bin/admin/CloudFlare/APIKey
echo "mode=simple" > $INSTALL_DIR/bin/admin/CloudFlare/APIKey.conf
# Get PHP Version
CPANELSUPPORTEDPHPPATH=`ls -l $INSTALL_DIR/3rdparty/bin/php`
PHPVERSION=`echo $CPANELSUPPORTEDPHPPATH | rev | cut -d '/' -f 3 | rev`
# Install PHP code
install -d $INSTALL_DIR/3rdparty/php/$PHPVERSION/lib/php/cloudflare/vendor
/bin/cp -rf $SOURCE_DIR/vendor/* $INSTALL_DIR/3rdparty/php/$PHPVERSION/lib/php/cloudflare/vendor
install -d $INSTALL_DIR/3rdparty/php/$PHPVERSION/lib/php/cloudflare/src
/bin/cp -rf $SOURCE_DIR/src/* $INSTALL_DIR/3rdparty/php/$PHPVERSION/lib/php/cloudflare/src
# Register the plugin buttons with Cpanel
/usr/local/cpanel/scripts/install_plugin $SOURCE_DIR/installers/cloudflare_simple.tar.bz2
# Copy cloudflare_update.sh to where the cron expects it to be
install $SOURCE_DIR/cloudflare_update.sh $INSTALL_DIR/bin
# Create CPanel hook to update plugin after Cpanel Update
CF_ON_UPGRADE=`grep -F "cloudflare_update" /scripts/postupcp`
if [ "$CF_ON_UPGRADE" == "" ]; then
echo "sh /usr/local/cpanel/bin/cloudflare_update.sh force" >> /scripts/postupcp
fi
# Create cron job to automatically update plugin
iscf=`crontab -l | grep cloudflare`
if [ "$iscf" == "" ]; then
crontab -l > c.cur
echo "12 2 * * 0 /usr/local/cpanel/bin/cloudflare_update.sh >/dev/null 2>&1" >> c.cur
crontab c.cur
fi
echo "Cleaning up"
rm -rf "CloudFlare-CPanel-$LATEST_VERSION"