PageRenderTime 36ms CodeModel.GetById 28ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/boto/pyami/copybot.py

#
Python | 97 lines | 68 code | 8 blank | 21 comment | 16 complexity | 03764f3939a06c4d0796ef426ce1d2a6 MD5 | raw file
 1# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 2#
 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-
 9# lowing conditions:
10#
11# The above copyright notice and this permission notice shall be included
12# in all copies or substantial portions of the Software.
13#
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
20# IN THE SOFTWARE.
21#
22import boto
23from boto.pyami.scriptbase import ScriptBase
24import os, StringIO
25
26class CopyBot(ScriptBase):
27
28    def __init__(self):
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)
39        if not self.src:
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)
42        if dest_access_key:
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)
46        if not self.dst:
47            self.dst = s3.create_bucket(self.dst_name)
48
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)
53
54    def copy_key_acl(self, src, dst):
55        if boto.config.get(self.name, 'copy_acls', True):
56            acl = src.get_xml_acl()
57            dst.set_xml_acl(acl)
58
59    def copy_keys(self):
60        boto.log.info('src=%s' % self.src.name)
61        boto.log.info('dst=%s' % self.dst.name)
62        try:
63            for key in self.src:
64                if not self.replace:
65                    exists = self.dst.lookup(key.name)
66                    if exists:
67                        boto.log.info('key=%s already exists in %s, skipping' % (key.name, self.dst.name))
68                        continue
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)
76                os.unlink(path)
77        except:
78            boto.log.exception('Error copying key: %s' % key.name)
79
80    def copy_log(self):
81        key = self.dst.new_key(self.log_file)
82        key.set_contents_from_filename(self.log_path)
83        
84    def main(self):
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:
89            self.copy_keys()
90        if self.dst:
91            self.copy_log()
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])
97