Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
4542 dpurdie 1
#!/bin/bash
2
#   This script will quarantine a package-vesrion into S3 storage
3
#   The target bucket is: auawsaddp001
4
#   
5
#   The package vesrion will be tar-zipped
6
#   without the dpkg_archive prefix
7
#
8
#   The resulatant tar-zip will be transferred to S3
9
#
10
#       Reduced redundancy is used
11
#
12
#       Credentials: s3_dpkg user 
13
#
14
#       Usage:  ./savePkg /PathTo/pkgName/pkgVersion
15
#
16
function doHelp {
17
cat <<endOfHelp
18
    Command: savePkgToS3
19
 
20
    This program will tar-zip a dpkg_archive Package/Version and store it
21
    into an Amazon S3 bucket in a subdirectory called 'Quarantine'
22
 
23
    Options
24
    -h, --help              - Display this message
25
    -v, --verbose           - Increase verbosity
26
    -b, --bucket=name       - Specifies the name of the target bucket
27
    -p, --path=PVPath       - Specifies the path to the Package-Version to save
28
    -k, --key=keyVar        - Name of the EnvVar that conains the AWS key
29
                              Default is AWSKEY
30
    -s, --secret=secretVar  - Name of the EnvVar that conains the AWS secret
31
                              Default is AWSSECRET
32
endOfHelp
33
}
34
 
35
#
36
#   Init defaults
37
#
38
ProgName=savePkgtoS3
39
awsKeyVar=AWSKEY
40
awsSecretVar=AWSSECRET
41
verbose=0
42
 
43
# Note that we use `"$@"' to let each command-line parameter expand to a 
44
# separate word. The quotes around `$@' are essential!
45
# We need TEMP as the `eval set --' would nuke the return value of getopt.
46
TEMP=$( getopt -n ${ProgName} -o vhb:p:k:s: --long verbose,help,bucket:,path:,key:,secret: -- "$@" )
47
 
48
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
49
 
50
# Note the quotes around `$TEMP': they are essential!
51
eval set -- "$TEMP"
52
 
53
while true ; do
54
	case "$1" in
55
		-h|--help)    doHelp; exit 0;;
56
		-v|--verbose) let verbose++; shift 1;;
57
		-b|--bucket)  bucket="$2" ; shift 2 ;;
58
		-p|--path)    dpkgPath="$2" ; shift 2 ;;
59
		-k|--key)     awsKeyVar="$2" ; shift 2 ;;
60
		-s|--secret)  awsSecretVar="$2" ; shift 2 ;;
61
		--) shift ; break ;;
62
		*) echo "Internal error!" ; exit 1 ;;
63
	esac
64
done
65
 
66
if [ $verbose -gt 1 ] ; then
67
    echo bucket      :$bucket
68
    echo dpkgPath    :$dpkgPath
69
    echo awsKeyVar   :$awsKeyVar
70
    echo awsSecretVar:$awsSecretVar
71
 
72
    echo "Remaining arguments:"
73
    for arg do echo '--> '"\`$arg'" ; done
74
fi
75
 
76
: ${bucket:?No Bucket Specified}
77
: ${dpkgPath:?No Package Version Path}
78
: ${awsKeyVar:?No AWS Key specified}
79
: ${awsSecretVar:?No AWS Secret specified}
80
 
81
#
82
#   The KEY and the Secret are passed via EnvVars
83
#   The name of the vars are passed on the command line
84
#
85
aws_access_key_id=${!awsKeyVar}
86
aws_secret_access_key=${!awsSecretVar}
87
 
88
: ${aws_access_key_id:?No AWS Key found}
89
: ${aws_secret_access_key:?No AWS Secret found}
90
 
91
if [ $verbose -gt 1 ] ; then
92
    echo aws_access_key_id:$aws_access_key_id
93
    echo aws_secret_access_key:$aws_secret_access_key
94
fi
95
 
96
#
97
#   Determine
98
#       dpkgPath    - Cleanup the user arg
99
#       pkgBase     - Base of the package
100
#       pkgName     - Package Name
101
#       pkgVer      - Package Version
102
#
103
 
104
dpkgPath=${dpkgPath%/}
105
 
106
pkgBase=${dpkgPath%/*}
107
pkgBase=${pkgBase%/*}
108
 
109
pkgVer=${dpkgPath##*/}
110
 
111
pkgName=${dpkgPath%/*}
112
pkgName=${pkgName##*/}
113
 
114
if [ $verbose -gt 1 ] ; then
115
    echo pkgBase:$pkgBase
116
    echo pkgName:pkgName
117
    echo pkgVer:$pkgVer
118
fi
119
 
120
if [ ! -d $dpkgPath ] ; then
121
    echo "Error: PVPath does not address a directory"
122
    exit 1
123
fi
124
 
125
#
126
#   Create the output file name
127
#   Format: Quarantined/PkgName_PkgVersion.tgz
128
 
129
file="Quarantined/${pkgName}_${pkgVer}.tgz"
130
 
131
# Basic transfer requirements
132
resource="/${bucket}/${file}"
133
contentType="application/x-compressed-tar"
134
 
135
# Added bits
136
acl="authenticated-read"
137
metaData="Quarantined Package"
138
storageType="REDUCED_REDUNDANCY"
139
 
140
 
141
# Calculate the signature.
142
dateValue=$(date -R)
143
stringToSign="PUT\n\n${contentType}\n${dateValue}\nx-amz-acl:${acl}\nx-amz-meta-reason:${metaData}\nx-amz-storage-class:${storageType}\n${resource}"
144
signature=$(
145
    echo -en "${stringToSign}" |
146
    openssl sha1 -hmac "${aws_secret_access_key}" -binary |
147
    base64
148
)
149
 
150
#echo dateValue: ${dateValue} 
151
#echo stringToSign: ${stringToSign}
152
#echo signature: ${signature}
153
#exit 1
154
 
155
# PUT!
156
if [ 1 ] ; then
157
#set -x
158
tar -czf - -C "$pkgBase" "$pkgName/$pkgVer" |
4547 dpurdie 159
    curl -s \
4542 dpurdie 160
        -X PUT \
161
        --data-binary @- \
162
         --insecure \
163
        -H "Host: ${bucket}.s3.amazonaws.com" \
164
        -H "Date: ${dateValue}" \
165
        -H "Content-Type: ${contentType}" \
166
        -H "Authorization: AWS ${aws_access_key_id}:${signature}" \
167
        -H "x-amz-acl: ${acl}" \
168
        -H "x-amz-meta-reason: ${metaData}" \
169
        -H "x-amz-storage-class: ${storageType}" \
170
        "https://${bucket}.s3.amazonaws.com/${file}"
171
fi
172
 
173
#############################################################
174
#   Fetch file info, just to be sure that the file got there
175
#   Get data about the file
176
#
177
# Calculate the HEAD signature.
178
#   Note the need for a triple \n
179
#   Is that because there is no contentType ?
180
#
181
stringToSign="HEAD\n\n\n${dateValue}\n${resource}"
182
signature=$(
183
    echo -en "${stringToSign}" |
184
    openssl sha1 -hmac "${aws_secret_access_key}" -binary |
185
    base64
186
)
187
 
188
#set -x
189
results=$(curl -I -X HEAD \
190
        -s \
191
         --insecure \
192
        -H "Host: ${bucket}.s3.amazonaws.com" \
193
        -H "Date: ${dateValue}" \
194
        -H "Authorization: AWS ${aws_access_key_id}:${signature}" \
195
        "https://${bucket}.s3.amazonaws.com/${file}" \
196
        )
197
 
198
#echo RV: $?
199
#echo Results: $results
200
if [[ "$results" =~ "HTTP/1.1 200 OK" ]]; then
201
    if [ $verbose -gt 0 ] ; then
202
        echo "${ProgName}: Transferred:$pkgName/$pkgVer" 
203
    fi
204
    exit 0
205
else
206
    echo "${ProgName}: Error cannot access $pkgName/$pkgVer in S3 bucket ${bucket}"
207
    exit 1
208
fi
209
 
210
 
211