PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Python | 97 lines | 68 code | 8 blank | 21 comment | 13 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. #
  22. import boto
  23. from boto.pyami.scriptbase import ScriptBase
  24. import os, StringIO
  25. class CopyBot(ScriptBase):
  26. def __init__(self):
  27. ScriptBase.__init__(self)
  28. self.wdir = boto.config.get('Pyami', 'working_dir')
  29. self.log_file = '%s.log' % self.instance_id
  30. self.log_path = os.path.join(self.wdir, self.log_file)
  31. boto.set_file_logger(self.name, self.log_path)
  32. self.src_name = boto.config.get(self.name, 'src_bucket')
  33. self.dst_name = boto.config.get(self.name, 'dst_bucket')
  34. self.replace = boto.config.getbool(self.name, 'replace_dst', True)
  35. s3 = boto.connect_s3()
  36. self.src = s3.lookup(self.src_name)
  37. if not self.src:
  38. boto.log.error('Source bucket does not exist: %s' % self.src_name)
  39. dest_access_key = boto.config.get(self.name, 'dest_aws_access_key_id', None)
  40. if dest_access_key:
  41. dest_secret_key = boto.config.get(self.name, 'dest_aws_secret_access_key', None)
  42. s3 = boto.connect(dest_access_key, dest_secret_key)
  43. self.dst = s3.lookup(self.dst_name)
  44. if not self.dst:
  45. self.dst = s3.create_bucket(self.dst_name)
  46. def copy_bucket_acl(self):
  47. if boto.config.get(self.name, 'copy_acls', True):
  48. acl = self.src.get_xml_acl()
  49. self.dst.set_xml_acl(acl)
  50. def copy_key_acl(self, src, dst):
  51. if boto.config.get(self.name, 'copy_acls', True):
  52. acl = src.get_xml_acl()
  53. dst.set_xml_acl(acl)
  54. def copy_keys(self):
  55. boto.log.info('src=%s' % self.src.name)
  56. boto.log.info('dst=%s' % self.dst.name)
  57. try:
  58. for key in self.src:
  59. if not self.replace:
  60. exists = self.dst.lookup(key.name)
  61. if exists:
  62. boto.log.info('key=%s already exists in %s, skipping' % (key.name, self.dst.name))
  63. continue
  64. boto.log.info('copying %d bytes from key=%s' % (key.size, key.name))
  65. prefix, base = os.path.split(key.name)
  66. path = os.path.join(self.wdir, base)
  67. key.get_contents_to_filename(path)
  68. new_key = self.dst.new_key(key.name)
  69. new_key.set_contents_from_filename(path)
  70. self.copy_key_acl(key, new_key)
  71. os.unlink(path)
  72. except:
  73. boto.log.exception('Error copying key: %s' % key.name)
  74. def copy_log(self):
  75. key = self.dst.new_key(self.log_file)
  76. key.set_contents_from_filename(self.log_path)
  77. def main(self):
  78. fp = StringIO.StringIO()
  79. boto.config.dump_safe(fp)
  80. self.notify('%s (%s) Starting' % (self.name, self.instance_id), fp.getvalue())
  81. if self.src and self.dst:
  82. self.copy_keys()
  83. if self.dst:
  84. self.copy_log()
  85. self.notify('%s (%s) Stopping' % (self.name, self.instance_id),
  86. 'Copy Operation Complete')
  87. if boto.config.getbool(self.name, 'exit_on_completion', True):
  88. ec2 = boto.connect_ec2()
  89. ec2.terminate_instances([self.instance_id])