Subversion Repositories DevTools

Rev

Rev 5805 | Rev 6128 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5608 dpurdie 1
#!/bin/bash
2
#   This script will restore a package from S3 into dpkg_archive
3
#   See savePkgToS3.sh
4
#
5
#   The source bucket is: aupera-dpkg-quarantine
6
#   
7
#   The package version is expeced to be tar-zipped
8
#
9
#   The resulatant tar-zip will be transferred to S3
10
#
11
#       Reduced redundancy is used
12
#
13
#       Credentials: s3_dpkg user 
14
#
15
#       Usage:  getPkgFromS3.sh /PathTo/pkgName/pkgVersion
16
#
17
function doHelp {
18
cat <<endOfHelp
19
    Command: getPkgFromS3
20
 
21
    This program will retrieve tar-zip Package/Version from an Amazon S3 bucket
22
    and store it into a subdirectory specified
23
 
24
    Options
25
    -h, --help              - Display this message
26
    -v, --verbose           - Increase verbosity
27
    -q, --quiet             - No status messages
28
    -b, --bucket=name       - Specifies the name of the target bucket
29
    -p, --path=PVPath       - Specifies the path to the Package-Version to restore
30
    -f, --force             - Delete target and force package download
31
    -k, --key=keyVar        - Name of the EnvVar that conains the AWS key
32
                              Default is AWSKEY
33
    -s, --secret=secretVar  - Name of the EnvVar that conains the AWS secret
34
                              Default is AWSSECRET
35
 
36
   Example:
37
    getPkgFromS3.sh -p \$GBE_DPKG/AcceptanceTestFramework/1.0.10000.cr
38
endOfHelp
39
}
40
 
41
#
42
#   Init defaults
43
#
44
ProgName=getPkgFromS3
45
awsKeyVar=AWSKEY
46
awsSecretVar=AWSSECRET
47
bucket=aupera-dpkg-quarantine
48
verbose=1
49
forceDelete=0
50
 
51
# Note that we use `"$@"' to let each command-line parameter expand to a 
52
# separate word. The quotes around `$@' are essential!
53
# We need TEMP as the `eval set --' would nuke the return value of getopt.
54
TEMP=$( getopt -n ${ProgName} -o qfvhb:p:k:s: --long quiet,force,verbose,help,bucket:,path:,key:,secret: -- "$@" )
55
 
56
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
57
 
58
# Note the quotes around `$TEMP': they are essential!
59
eval set -- "$TEMP"
60
 
61
while true ; do
62
    case "$1" in
63
        -h|--help)    doHelp; exit 0;;
64
        -f|--force)   let forceDelete++; shift 1;;
65
        -q|--quiet)   verbose=0; shift 1;;
66
        -v|--verbose) let verbose++; shift 1;;
67
        -b|--bucket)  bucket="$2" ; shift 2 ;;
68
        -p|--path)    dpkgPath="$2" ; shift 2 ;;
69
        -k|--key)     awsKeyVar="$2" ; shift 2 ;;
70
        -s|--secret)  awsSecretVar="$2" ; shift 2 ;;
71
        --) shift ; break ;;
72
        *) echo "Internal error!" ; exit 1 ;;
73
    esac
74
done
75
 
76
if [ $verbose -gt 1 ] ; then
77
    echo bucket      :$bucket
78
    echo dpkgPath    :$dpkgPath
79
    echo awsKeyVar   :$awsKeyVar
80
    echo awsSecretVar:$awsSecretVar
81
 
82
    echo "Remaining arguments:"
83
    for arg do echo '--> '"\`$arg'" ; done
84
fi
85
 
86
: ${bucket:?No Bucket Specified}
87
: ${dpkgPath:?No Package Version Path}
88
: ${awsKeyVar:?No AWS Key specified}
89
: ${awsSecretVar:?No AWS Secret specified}
90
 
91
#
92
#   The KEY and the Secret are passed via EnvVars
93
#   The name of the vars are passed on the command line
94
#
95
aws_access_key_id=${!awsKeyVar}
96
aws_secret_access_key=${!awsSecretVar}
97
 
98
: ${aws_access_key_id:?No AWS Key found}
99
: ${aws_secret_access_key:?No AWS Secret found}
100
 
101
if [ $verbose -gt 1 ] ; then
102
    echo aws_access_key_id:$aws_access_key_id
103
    echo aws_secret_access_key:$aws_secret_access_key
104
fi
105
 
106
#
107
#   Determine
108
#       dpkgPath    - Cleanup the user arg
109
#       pkgBase     - Base of the package
110
#       pkgName     - Package Name
111
#       pkgVer      - Package Version
112
#
113
 
114
dpkgPath=${dpkgPath%/}
115
 
116
pkgBase=${dpkgPath%/*}
117
pkgBase=${pkgBase%/*}
118
 
119
pkgVer=${dpkgPath##*/}
120
 
121
pkgName=${dpkgPath%/*}
122
pkgName=${pkgName##*/}
123
 
124
if [ $verbose -gt 0 ] ; then
125
    echo Bucket  : $bucket
126
    echo pkgBase : $pkgBase
127
    echo pkgName : $pkgName
128
    echo pkgVer  : $pkgVer
129
fi
130
 
131
if [  ! -d $pkgBase ] ; then
132
    echo "Error: Package archive base does not exist: $pkgBase"
133
    exit 1
134
fi
135
 
136
# Force version deletion - more for test.
137
if [ $forceDelete -gt 0 ] ; then 
138
    [ $verbose -gt 0 ] && echo "Force removal of $pkgName/$pkgVer"
139
    if [ -d $dpkgPath ] ; then
140
        chmod -R +w $dpkgPath
141
        rm -rf $dpkgPath
142
    fi
143
fi
144
 
145
if [  -d $dpkgPath ] ; then
146
    echo "Error: Target PV Path already exists: $dpkgPath"
147
    exit 1
148
fi
149
 
150
#
151
#   Create the source file name
152
#   Format: Quarantined/PkgName_PkgVersion.tgz
153
file="Quarantined/${pkgName}_${pkgVer}.tgz"
154
 
155
# Basic transfer requirements
156
resource="/${bucket}/${file}"
157
dateValue=$(date -R)
158
 
159
 
160
#############################################################
161
#   Fetch file info, just to be sure that the file is there
162
#   Get data about the file
163
#
164
# Calculate the HEAD signature.
165
#   Note the need for a triple \n
166
#   Is that because there is no contentType ?
167
#
168
stringToSign="HEAD\n\n\n${dateValue}\n${resource}"
169
signature=$(
170
    echo -en "${stringToSign}" |
171
    openssl sha1 -hmac "${aws_secret_access_key}" -binary |
172
    base64
173
)
174
 
175
#set -x
176
fileTest=0
177
[ $verbose -gt 1 ] && echo "Testing file presence: ${file}"
178
results=$(curl -I -X HEAD \
179
        -s \
180
         --insecure \
181
        -H "Host: ${bucket}.s3.amazonaws.com" \
182
        -H "Date: ${dateValue}" \
183
        -H "Authorization: AWS ${aws_access_key_id}:${signature}" \
184
        "https://${bucket}.s3.amazonaws.com/${file}" \
185
        )
186
if [[ "$results" =~ "HTTP/1.1 200 OK" ]]; then
187
    fileTest=1
188
fi
189
 
190
# Display results
191
if [ $fileTest -gt 0 ]; then
192
    if [ $verbose -gt 1 ] ; then
193
        echo "${ProgName}: Package Version Exists: $pkgName/$pkgVer" 
194
    fi
195
else
196
    echo "${ProgName}: Error cannot access $pkgName/$pkgVer in S3 bucket ${bucket}"
197
    exit 1
198
fi
199
 
200
# Ensure correct storage class
201
storageClass=$( echo $results | tr '\r' '\n' | awk -F: '/x-amz-storage-class/{print $2}')
202
[ $verbose -gt 0 ] && echo storageClass : $storageClass
5805 dpurdie 203
if [[ "$storageClass" != " REDUCED_REDUNDANCY" &&  "$storageClass" != " STANDARD_IA" ]]; then
5608 dpurdie 204
    echo "${ProgName}: Error cannot access $pkgName/$pkgVer. Incorrect storage class: '$storageClass'"
6023 dpurdie 205
    echo "Resource: $resource"
5608 dpurdie 206
    exit 1
207
fi
208
 
209
#############################################################
210
# GET!
211
 
212
# Calculate the signature.
213
#   Note the need for a triple \n
214
#   Needed becase there is no content type
215
stringToSign="GET\n\n\n${dateValue}\n${resource}"
216
signature=$(
217
    echo -en "${stringToSign}" |
218
    openssl sha1 -hmac "${aws_secret_access_key}" -binary |
219
    base64
220
)
221
 
222
#echo dateValue: ${dateValue} 
223
#echo stringToSign: ${stringToSign}
224
#echo signature: ${signature}
225
#exit 1
226
 
227
mkdir -p $dpkgPath
228
if [  ! -d $dpkgPath ] ; then
229
    echo "Error: Could not create: $dpkgPath"
230
    exit 1
231
fi
232
 
233
if [ 1 ] ; then
234
[ $verbose -gt 0 ] && echo "Transfer $pkgName/$pkgVer from bucket $bucket"
235
#set -x
236
    curl -s \
237
        -X GET \
238
         --insecure \
239
        -H "Host: ${bucket}.s3.amazonaws.com" \
240
        -H "Date: ${dateValue}" \
241
        -H "Authorization: AWS ${aws_access_key_id}:${signature}" \
242
        "https://${bucket}.s3.amazonaws.com/${file}" \
243
        | tar -xz --strip=2 -C $dpkgPath
244
fi
245
 
246
if [ ! -f "$dpkgPath/descpkg" ] ; then
247
    echo "Error: Target PV did not populate as expected: $dpkgPath"
248
 
249
    # Remove if not correctly formatted
250
    chmod -R +w $dpkgPath
251
    rm -rf $dpkgPath
252
    exit 1
253
fi
254
 
255
[ $verbose -gt 0 ] && echo "Transfer complete"
256
 
257
 
258
 
259