1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
3
# Permission is hereby granted, free of charge, to any person obtaining a
4
# copy of this software and associated documentation files (the
5
# "Software"), to deal in the Software without restriction, including
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
8
# persons to whom the Software is furnished to do so, subject to the fol-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
from boto.pyami.scriptbase import ScriptBase
26
class CopyBot(ScriptBase):
29
ScriptBase.__init__(self)
30
self.wdir = boto.config.get('Pyami', 'working_dir')
31
self.log_file = '%s.log' % self.instance_id
32
self.log_path = os.path.join(self.wdir, self.log_file)
33
boto.set_file_logger(self.name, self.log_path)
34
self.src_name = boto.config.get(self.name, 'src_bucket')
35
self.dst_name = boto.config.get(self.name, 'dst_bucket')
36
self.replace = boto.config.getbool(self.name, 'replace_dst', True)
37
s3 = boto.connect_s3()
38
self.src = s3.lookup(self.src_name)
40
boto.log.error('Source bucket does not exist: %s' % self.src_name)
41
dest_access_key = boto.config.get(self.name, 'dest_aws_access_key_id', None)
43
dest_secret_key = boto.config.get(self.name, 'dest_aws_secret_access_key', None)
44
s3 = boto.connect(dest_access_key, dest_secret_key)
45
self.dst = s3.lookup(self.dst_name)
47
self.dst = s3.create_bucket(self.dst_name)
49
def copy_bucket_acl(self):
50
if boto.config.get(self.name, 'copy_acls', True):
51
acl = self.src.get_xml_acl()
52
self.dst.set_xml_acl(acl)
54
def copy_key_acl(self, src, dst):
55
if boto.config.get(self.name, 'copy_acls', True):
56
acl = src.get_xml_acl()
60
boto.log.info('src=%s' % self.src.name)
61
boto.log.info('dst=%s' % self.dst.name)
65
exists = self.dst.lookup(key.name)
67
boto.log.info('key=%s already exists in %s, skipping' % (key.name, self.dst.name))
69
boto.log.info('copying %d bytes from key=%s' % (key.size, key.name))
70
prefix, base = os.path.split(key.name)
71
path = os.path.join(self.wdir, base)
72
key.get_contents_to_filename(path)
73
new_key = self.dst.new_key(key.name)
74
new_key.set_contents_from_filename(path)
75
self.copy_key_acl(key, new_key)
78
boto.log.exception('Error copying key: %s' % key.name)
81
key = self.dst.new_key(self.log_file)
82
key.set_contents_from_filename(self.log_path)
85
fp = StringIO.StringIO()
86
boto.config.dump_safe(fp)
87
self.notify('%s (%s) Starting' % (self.name, self.instance_id), fp.getvalue())
88
if self.src and self.dst:
92
self.notify('%s (%s) Stopping' % (self.name, self.instance_id),
93
'Copy Operation Complete')
94
if boto.config.getbool(self.name, 'exit_on_completion', True):
95
ec2 = boto.connect_ec2()
96
ec2.terminate_instances([self.instance_id])