forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_parcel.sh
281 lines (246 loc) · 8.9 KB
/
create_parcel.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
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
#!/bin/bash
# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script creates a Cloudera Parcel which contains Google Cloud Storage connector which
# enables connections between CDH cluster and Cloud Storage. Using the parcel, CDH can distribute
# the connector to all the nodes to save time and efforts of distributing and installing the
# connector.
# User must run the script in a separate folder where only the files to be packaged are present
# -f: name of the parcel in a single string format without any spaces or special characters.
# -v: version of the parcel in the format x.x.x (ex: 1.0.0)
# -o: name of the operating system distribution
# -d: flag is to be used if you want to deploy the parcel to the Cloudera manager parcel repo
# folder, this flag is optional and if not provided then the parcel file will be created in the
# same directory where script run.
#######################################
# Capture logs in create_parcel.log and exit
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#######################################
function graceful_exit() {
echo "-------------------------------">>${LOGDIR}/create_parcel.log
echo "From User: "$(whoami)>>${LOGDIR}/create_parcel.log
cat ${LOGDIR}/cparcel_output.log >> ${LOGDIR}/create_parcel.log
exit 1
}
#######################################
# User Help function for irregular input parameters
# Globals:
# None
# Arguments:
# -f: Parcel name
# -v: Parcel version
# -o: Operating system
# -d: (Optional)Parcel deployment through Clodera Manager value : true
# Returns:
# None
#######################################
function help {
echo 'Usage: create_parcel.sh -f <parcel_name> -v <version> -o <os_distro_suffix> [-d true]'
graceful_exit
}
#######################################
# Main entry of the script
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#######################################
function main() {
BASEDIR=${HOME}/GCS-CDH-PARCEL
rm -rf ${BASEDIR}
mkdir -p ${BASEDIR}
LOGDIR=${HOME}/parcel-logs
mkdir -p ${LOGDIR}/
cp * ${BASEDIR}/
cd ${BASEDIR}
touch ${LOGDIR}/cparcel_error.log
touch ${LOGDIR}/cparcel_output.log
# Allowed OS parameter values and flag to check valid input
OS_FLAG=0
OS_VALUE_ARRAY=(el5 el6 el7 sles11 sles12 lucid precise trusty squeeze wheezy)
# Link to GCS connector jar file and flag to validate the file existence
GCSJAR_LINK="https://storage.googleapis.com/hadoop-lib/gcs/gcs-connector-hadoop2-latest.jar"
GCSJAR_FLAG="1"
NUM_ARG=$#
exec > >(tee -i ${LOGDIR}/cparcel_output.log)
exec 2>&1
echo $(date)
# Validating The PARAMETERS
# Regex variable for Parcel and version validity
PARCEL_FLAG='^[A-Za-z0-9]+$'
VERSION_FLAG='^[0-9]\.[0-9]\.[0-9]$'
# Getting the parameter values from command line
while getopts 'f:v:o:d:' OPTION; do
case "${OPTION}"
in
f) FILE_NAME=${OPTARG} ;;
v) VERSION=${OPTARG} ;;
o) OSTYPE=${OPTARG} ;;
d) PLACE_FILE=${OPTARG} ;;
:) help ;;
\?) help ;;
*) help ;;
esac
done
shift "$(($OPTIND -1))"
if [[ NUM_ARG -lt 6 ]] || [[ NUM_ARG -gt 8 ]]; then
echo 'Error: number of parameters is not correct.'
help
fi
if [[ ${FILE_NAME} =~ ${PARCEL_FLAG} && ${VERSION} =~ ${VERSION_FLAG} ]]; then
:
else
echo 'Error: invalid parcel name or version'
graceful_exit
fi
# Throw error if folder doesnt exists (if not using -d parameter then script fails)
if [[ ! -d /opt/cloudera/parcel-repo/ ]] && [[ "${PLACE_FILE}" == "true" ]] ; then
echo "Error: /opt/cloudera/parcel-repo/ folder doesn't exist"
graceful_exit
elif [[ ! $(getent group cloudera-scm) ]] && [[ "${PLACE_FILE}" == "true" ]]; then
echo "Error: cloudera-scm group doesn't exist"
graceful_exit
fi
# Check the OS parameter's validity
OS=${OSTYPE}
for item in ${OS_VALUE_ARRAY[*]}; do
if [[ $item == ${OSTYPE} ]]; then
OS_FLAG=1
fi
done
if [[ $OS_FLAG == 0 ]]; then
echo 'Error: below are the allowed os type'
printf '%s\n' "${OS_VALUE_ARRAY[@]}"
graceful_exit
fi
# Display variable information input by user
echo "Version number:${VERSION}"
echo "OS type:${OS}"
echo "Filename:${FILE_NAME}"
PARCEL_FULLNAME=${FILE_NAME^^}-${VERSION}
# Path is changed to ${HOME} for further execution
# Create the file structure for parcel building
mkdir -p ${PARCEL_FULLNAME}/lib/hadoop/lib && mkdir -p ${PARCEL_FULLNAME}/meta
if [[ $? -ne 0 ]]; then
echo "Error: failed to create directory, check for folder permissions"
graceful_exit
fi
touch ${PARCEL_FULLNAME}/meta/parcel.json
if [[ $? -ne 0 ]]; then
echo "Error: failed to create parcel.json file, check for file/folder permissions"
graceful_exit
fi
# Download gcs connector jar and copy all folder content to parcel location
# Set flag for further parcel file existence validation
curl -o gcs-connector-hadoop2-latest.jar --fail ${GCSJAR_LINK} || GCSJAR_FLAG="0"
# Validate if package downloaded properly
if [[ ${GCSJAR_FLAG} = "0" ]]; then
echo "Error: hadoop connector failed to download, check network connectivity or file/folder permissions"
graceful_exit
fi
# Copy GCS connector jar
cp gcs-connector-hadoop2-latest.jar ${PARCEL_FULLNAME}/lib/hadoop/lib/
# Copy service account key file
cp *.json ${PARCEL_FULLNAME}/lib/hadoop/lib/
# Check the existence of the GCS jar file in lib folder
[[ -f ${PARCEL_FULLNAME}/lib/hadoop/lib/gcs-connector-hadoop2-latest.jar ]] || GCSJAR_FLAG="0"
if [[ ${GCSJAR_FLAG} = "0" ]]; then
echo "Error: hadoop connector is missing from "$(pwd)/${PARCEL_FULLNAME}/lib/hadoop/lib/
graceful_exit
fi
# Create parcel.json file required for parcel packaging
cat >>${PARCEL_FULLNAME}/meta/parcel.json<<EOL
{
"schema_version": 1,
"name": "${FILE_NAME^^}",
"version": "${VERSION}",
"extraVersionInfo": {
"fullVersion": "${VERSION}-0-${OS}",
"baseVersion": "${FILE_NAME^^}${VERSION}",
"patchCount": ""
},
"conflicts": "",
"setActiveSymlink": true,
"scripts": {
"defines": "${FILE_NAME^^}.sh"
},
"packages": [ ],
"components": [ ],
"provides": [
"cdh-plugin"
],
"users": { },
"groups": [ ]
}
EOL
if [[ $? -ne 0 ]]; then
echo "Error: failed to write in parcel.json "$(pwd)/${PARCEL_FULLNAME}/meta/parcel.json
graceful_exit
fi
# export HADOOP_CLASSPATH to enable command line to use the connector
cat >>${PARCEL_FULLNAME}/meta/${FILE_NAME}.sh<<EOL
#!/bin/bash
export HADOOP_CLASSPATH=\$HADOOP_CLASSPATH:/opt/cloudera/parcels/${FILE_NAME}-${VERSION}/lib/hadoop/lib/gcs-connector-latest-hadoop2.jar
EOL
if [[ $? -ne 0 ]]; then
echo "Error: failed to write in file "$(pwd)/${PARCEL_FULLNAME}/meta/${FILE_NAME}.sh
graceful_exit
fi
# Create parcel file, checksum file and move parcel files to
# Cloudera parcel directory and change ownership to cloudera-scm user
if [[ $(getent group cloudera-scm) ]]; then
echo "clouder-scm group exists, proceeding further"
sudo tar zcvf ${PARCEL_FULLNAME}-${OS}.parcel ${PARCEL_FULLNAME}/ --owner=cloudera-scm --group=cloudera-scm
else
echo "clouder-scm group does not exist, creating parcle without group permission"
sudo tar zcvf ${PARCEL_FULLNAME}-${OS}.parcel ${PARCEL_FULLNAME}/
fi
if [[ $? -ne 0 ]]; then
echo "Error: failed to create parcel, check cloudera-scm owner/group exists or not"
graceful_exit
fi
# Create checksum file for parcel
sudo sha1sum ${PARCEL_FULLNAME}-${OS}.parcel | awk '{ print $1 }' > ${PARCEL_FULLNAME}-${OS}.parcel.sha
if [[ $? -ne 0 ]]; then
echo "Error: failed to create checksum(.sha) file, check for valid parcel file "$(pwd)/${PARCEL_FULLNAME}-${OS}.parcel
graceful_exit
fi
# Check if this machine has /opt/cloudera/parcel-repo directory
if [[ "${PLACE_FILE}" == "true" ]]; then
sudo cp ${PARCEL_FULLNAME}-${OS}.parcel* /opt/cloudera/parcel-repo/
if [[ $(getent group cloudera-scm) ]]; then
sudo chown cloudera-scm:cloudera-scm /opt/cloudera/parcel-repo/*
else
echo "Error: not a Cloudera Manager machine, exiting"
graceful_exit
fi
rm -rf *
rm -rf ${BASEDIR}/cparcel
echo "Parcel created successfully in /opt/cloudera/parcel-repo/"${PARCEL_FULLNAME}-${OS}.parcel
else
echo "Parcel created locally in "${BASEDIR}
fi
if [[ $? -ne 0 ]]; then
echo "Error: check cloudera-scm owner/group or /opt/cloudera/parcel-repo/"
graceful_exit
fi
}
main "$@"