You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.2 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2012 Miguel Olivares http://moliware.com/
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish, dis-
  9. # tribute, sublicense, and/or sell copies of the Software, and to permit
  10. # persons to whom the Software is furnished to do so, subject to the fol-
  11. # lowing conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included
  14. # in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  18. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  19. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. # IN THE SOFTWARE.
  23. #
  24. """
  25. glacier
  26. ~~~~~~~
  27. Amazon Glacier tool built on top of boto. Look at the usage method to see
  28. how to use it.
  29. Author: Miguel Olivares <miguel@moliware.com>
  30. """
  31. import sys
  32. from boto.glacier import connect_to_region
  33. from getopt import getopt, GetoptError
  34. from os.path import isfile, basename
  35. COMMANDS = ('vaults', 'jobs', 'upload')
  36. def usage():
  37. print("""
  38. glacier <command> [args]
  39. Commands
  40. vaults - Operations with vaults
  41. jobs - Operations with jobs
  42. upload - Upload files to a vault. If the vault doesn't exits, it is
  43. created
  44. Common args:
  45. --access_key - Your AWS Access Key ID. If not supplied, boto will
  46. use the value of the environment variable
  47. AWS_ACCESS_KEY_ID
  48. --secret_key - Your AWS Secret Access Key. If not supplied, boto
  49. will use the value of the environment variable
  50. AWS_SECRET_ACCESS_KEY
  51. --region - AWS region to use. Possible values: us-east-1, us-west-1,
  52. us-west-2, ap-northeast-1, eu-west-1.
  53. Default: us-east-1
  54. Vaults operations:
  55. List vaults:
  56. glacier vaults
  57. Jobs operations:
  58. List jobs:
  59. glacier jobs <vault name>
  60. Uploading files:
  61. glacier upload <vault name> <files>
  62. Examples :
  63. glacier upload pics *.jpg
  64. glacier upload pics a.jpg b.jpg
  65. """)
  66. sys.exit()
  67. def connect(region, debug_level=0, access_key=None, secret_key=None):
  68. """ Connect to a specific region """
  69. layer2 = connect_to_region(region,
  70. aws_access_key_id=access_key,
  71. aws_secret_access_key=secret_key,
  72. debug=debug_level)
  73. if layer2 is None:
  74. print('Invalid region (%s)' % region)
  75. sys.exit(1)
  76. return layer2
  77. def list_vaults(region, access_key=None, secret_key=None):
  78. layer2 = connect(region, access_key = access_key, secret_key = secret_key)
  79. for vault in layer2.list_vaults():
  80. print(vault.arn)
  81. def list_jobs(vault_name, region, access_key=None, secret_key=None):
  82. layer2 = connect(region, access_key = access_key, secret_key = secret_key)
  83. print(layer2.layer1.list_jobs(vault_name))
  84. def upload_files(vault_name, filenames, region, access_key=None, secret_key=None):
  85. layer2 = connect(region, access_key = access_key, secret_key = secret_key)
  86. layer2.create_vault(vault_name)
  87. glacier_vault = layer2.get_vault(vault_name)
  88. for filename in filenames:
  89. if isfile(filename):
  90. sys.stdout.write('Uploading %s to %s...' % (filename, vault_name))
  91. sys.stdout.flush()
  92. archive_id = glacier_vault.upload_archive(
  93. filename,
  94. description = basename(filename))
  95. print(' done. Vault returned ArchiveID %s' % archive_id)
  96. def main():
  97. if len(sys.argv) < 2:
  98. usage()
  99. command = sys.argv[1]
  100. if command not in COMMANDS:
  101. usage()
  102. argv = sys.argv[2:]
  103. options = 'a:s:r:'
  104. long_options = ['access_key=', 'secret_key=', 'region=']
  105. try:
  106. opts, args = getopt(argv, options, long_options)
  107. except GetoptError as e:
  108. usage()
  109. # Parse agument
  110. access_key = secret_key = None
  111. region = 'us-east-1'
  112. for option, value in opts:
  113. if option in ('-a', '--access_key'):
  114. access_key = value
  115. elif option in ('-s', '--secret_key'):
  116. secret_key = value
  117. elif option in ('-r', '--region'):
  118. region = value
  119. # handle each command
  120. if command == 'vaults':
  121. list_vaults(region, access_key, secret_key)
  122. elif command == 'jobs':
  123. if len(args) != 1:
  124. usage()
  125. list_jobs(args[0], region, access_key, secret_key)
  126. elif command == 'upload':
  127. if len(args) < 2:
  128. usage()
  129. upload_files(args[0], args[1:], region, access_key, secret_key)
  130. if __name__ == '__main__':
  131. main()