forked from cgbystrom/yum-s3-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.py
221 lines (176 loc) · 6.63 KB
/
s3.py
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
"""
Yum plugin for Amazon S3 access.
This plugin provides access to a protected Amazon S3 bucket using either boto
or Amazon's REST authentication scheme.
On CentOS this file goes into /usr/lib/yum-plugins/s3.py
You will also need two configuration files. See s3.conf and s3test.repo for
examples on how to deploy those.
"""
# Copyright 2011, Robert Mela
#
# 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.
SECRET_KEY = 'my_amazon_secret_key'
KEY_ID = 'my_amazon_key_id'
def createBotoGrabber():
import boto
from urlparse import urlparse
import sys
class BotoGrabber:
def __init__(self, awsAccessKey, awsSecretKey, baseurl):
print "BotoGrabber init BASE_URL=%s" % baseurl
if not baseurl: raise Exception("BotoGrabberInit got blank baseurl")
try: baseurl = baseurl[0]
except: pass
self.s3 = boto.connect_s3(awsAccessKey, awsSecretKey)
self.baseurl = urlparse(baseurl)
self.bucket_name = self.baseurl.netloc.split('.')[0]
self.key_prefix = self.baseurl.path[1:]
def _key_name(self,url):
return "%s%s" % ( self.key_prefix, url )
def _key(self, key_name):
bucket = self.s3.get_bucket(self.bucket_name)
return bucket.get_key(key_name)
def urlgrab(self, url, filename=None, **kwargs):
"""urlgrab(url) copy the file to the local filesystem"""
print "BotoGrabber urlgrab url=%s filename=%s" % ( url, filename )
key_name = self._key_name(url)
print "BotoGrabber urlgrab url=%s key_name=%s filename=%s" % ( url, key_name, filename )
key = self._key(key_name)
if not key: raise Exception("Can not get key for key=%s" % key_name )
if not filename: filename = key.key
key.get_contents_to_filename(filename)
return filename
# zzz - does this return a value or something?
def urlopen(self, url, **kwargs):
"""urlopen(url) open the remote file and return a file object"""
print "BotoGrabber urlopen url=%s" % url
return self._key(url)
def urlread(self, url, limit=None, **kwargs):
"""urlread(url) return the contents of the file as a string"""
print "BotoGrabber urlread url=%s" % url
return self._key(url).read()
return BotoGrabber
def createUrllibGrabber():
import os
import sys
import urllib2
import time, hashlib, hmac, base64
class UrllibGrabber:
@classmethod
def s3sign(cls,request, secret_key, key_id, date=None):
date=time.strftime("%a, %d %b %Y %H:%M:%S +0000", date or time.gmtime() )
host = request.get_host()
bucket = host.split('.')[0]
request.add_header( 'Date', date)
resource = "/%s%s" % ( bucket, request.get_selector() )
sigstring = """%(method)s\n\n\n%(date)s\n%(canon_amzn_resource)s""" % {
'method':request.get_method(),
#'content_md5':'',
#'content_type':'', # only for PUT
'date':request.headers.get('Date'),
#'canon_amzn_headers':'',
'canon_amzn_resource':resource }
digest = hmac.new(secret_key, sigstring, hashlib.sha1 ).digest()
digest = base64.b64encode(digest)
request.add_header('Authorization', "AWS %s:%s" % ( key_id, digest ))
def __init__(self, awsAccessKey, awsSecretKey, baseurl ):
try: baseurl = baseurl[0]
except: pass
self.baseurl = baseurl
self.awsAccessKey = awsAccessKey
self.awsSecretKey = awsSecretKey
def _request(self,url):
req = urllib2.Request("%s%s" % (self.baseurl, url))
UrllibGrabber.s3sign(req, self.awsSecretKey, self.awsAccessKey )
return req
def urlgrab(self, url, filename=None, **kwargs):
"""urlgrab(url) copy the file to the local filesystem"""
print "UrlLibGrabber urlgrab url=%s filename=%s" % ( url, filename )
req = self._request(url)
if not filename:
filename = req.get_selector()
if filename[0] == '/': filename = filename[1:]
out = open(filename, 'w+')
resp = urllib2.urlopen(req)
buff = resp.read(8192)
while buff:
out.write(buff)
buff = resp.read(8192)
return filename
# zzz - does this return a value or something?
def urlopen(self, url, **kwargs):
"""urlopen(url) open the remote file and return a file object"""
return urllib2.urlopen( self._request(url) )
def urlread(self, url, limit=None, **kwargs):
"""urlread(url) return the contents of the file as a string"""
return urllib2.urlopen( self._request(url) ).read()
return UrllibGrabber
def createGrabber():
try:
rv = createBotoGrabber()
print "Created BotoGrabber"
return rv
except:
print "Creating UrllibGrabber"
return createUrllibGrabber()
AmazonS3Grabber = createGrabber()
import os
import sys
import urllib
from yum.plugins import TYPE_CORE
from yum.yumRepo import YumRepository
from yum import config
import yum.Errors
__revision__ = "1.0.0"
requires_api_version = '2.5'
plugin_type = TYPE_CORE
CONDUIT=None
def config_hook(conduit):
config.RepoConf.s3_enabled = config.BoolOption(False)
def init_hook(conduit):
"""
Plugin initialization hook. Setup the S3 repositories.
"""
repos = conduit.getRepos()
for key,repo in repos.repos.iteritems():
print type(repo)
print "s3_enabled=%s" % repo.s3_enabled
if isinstance(repo, YumRepository) and repo.s3_enabled:
new_repo = AmazonS3Repo(key)
new_repo.baseurl = repo.baseurl
new_repo.mirrorlist = repo.mirrorlist
new_repo.basecachedir = repo.basecachedir
new_repo.gpgcheck = repo.gpgcheck
new_repo.proxy = repo.proxy
new_repo.enablegroups = repo.enablegroups
del repos.repos[repo.id]
repos.add(new_repo)
#repos.add(new_repo)
class AmazonS3Repo(YumRepository):
"""
Repository object for Amazon S3.
"""
def __init__(self, repoid):
YumRepository.__init__(self, repoid)
self.enable()
self.grabber = None
def setupGrab(self):
YumRepository.setupGrab(self)
self.grabber = AmazonS3Grabber(KEY_ID, SECRET_KEY )
def _getgrabfunc(self): raise Exception("get grabfunc!")
def _getgrab(self):
if not self.grabber:
self.grabber = AmazonS3Grabber(KEY_ID, SECRET_KEY, baseurl=self.baseurl )
return self.grabber
grabfunc = property(lambda self: self._getgrabfunc())
grab = property(lambda self: self._getgrab())