OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 28 Amazon Glacier tool built on top of boto. Look at the usage method to see |
| 29 how to use it. |
| 30 |
| 31 Author: Miguel Olivares <miguel@moliware.com> |
| 32 """ |
| 33 import sys |
| 34 |
| 35 from boto.glacier import connect_to_region |
| 36 from getopt import getopt, GetoptError |
| 37 from os.path import isfile |
| 38 |
| 39 |
| 40 COMMANDS = ('vaults', 'jobs', 'upload') |
| 41 |
| 42 |
| 43 def usage(): |
| 44 print """ |
| 45 glacier <command> [args] |
| 46 |
| 47 Commands |
| 48 vaults - Operations with vaults |
| 49 jobs - Operations with jobs |
| 50 upload - Upload files to a vault. If the vault doesn't exits, it is |
| 51 created |
| 52 |
| 53 Common args: |
| 54 --access_key - Your AWS Access Key ID. If not supplied, boto will |
| 55 use the value of the environment variable |
| 56 AWS_ACCESS_KEY_ID |
| 57 --secret_key - Your AWS Secret Access Key. If not supplied, boto |
| 58 will use the value of the environment variable |
| 59 AWS_SECRET_ACCESS_KEY |
| 60 --region - AWS region to use. Possible values: us-east-1, us-west-1, |
| 61 us-west-2, ap-northeast-1, eu-west-1. |
| 62 Default: us-east-1 |
| 63 |
| 64 Vaults operations: |
| 65 |
| 66 List vaults: |
| 67 glacier vaults |
| 68 |
| 69 Jobs operations: |
| 70 |
| 71 List jobs: |
| 72 glacier jobs <vault name> |
| 73 |
| 74 Uploading files: |
| 75 |
| 76 glacier upload <vault name> <files> |
| 77 |
| 78 Examples : |
| 79 glacier upload pics *.jpg |
| 80 glacier upload pics a.jpg b.jpg |
| 81 """ |
| 82 sys.exit() |
| 83 |
| 84 |
| 85 def connect(region, debug_level=0, access_key=None, secret_key=None): |
| 86 """ Connect to a specific region """ |
| 87 return connect_to_region(region, |
| 88 aws_access_key_id=access_key, |
| 89 aws_secret_access_key=secret_key, |
| 90 debug=debug_level) |
| 91 |
| 92 |
| 93 def list_vaults(region, access_key=None, secret_key=None): |
| 94 layer2 = connect(region, access_key = access_key, secret_key = secret_key) |
| 95 for vault in layer2.list_vaults(): |
| 96 print vault.arn |
| 97 |
| 98 |
| 99 def list_jobs(vault_name, region, access_key=None, secret_key=None): |
| 100 layer2 = connect(region, access_key = access_key, secret_key = secret_key) |
| 101 print layer2.layer1.list_jobs(vault_name) |
| 102 |
| 103 |
| 104 def upload_files(vault_name, filenames, region, access_key=None, secret_key=None
): |
| 105 layer2 = connect(region, access_key = access_key, secret_key = secret_key) |
| 106 layer2.create_vault(vault_name) |
| 107 glacier_vault = layer2.get_vault(vault_name) |
| 108 for filename in filenames: |
| 109 if isfile(filename): |
| 110 print 'Uploading %s to %s' % (filename, vault_name) |
| 111 glacier_vault.upload_archive(filename) |
| 112 |
| 113 |
| 114 def main(): |
| 115 if len(sys.argv) < 2: |
| 116 usage() |
| 117 |
| 118 command = sys.argv[1] |
| 119 if command not in COMMANDS: |
| 120 usage() |
| 121 |
| 122 argv = sys.argv[2:] |
| 123 options = 'a:s:r:' |
| 124 long_options = ['access_key=', 'secret_key=', 'region='] |
| 125 try: |
| 126 opts, args = getopt(argv, options, long_options) |
| 127 except GetoptError, e: |
| 128 usage() |
| 129 |
| 130 # Parse agument |
| 131 access_key = secret_key = None |
| 132 region = 'us-east-1' |
| 133 for option, value in opts: |
| 134 if option in ('-a', '--access_key'): |
| 135 access_key = value |
| 136 elif option in ('-s', '--secret_key'): |
| 137 secret_key = value |
| 138 elif option in ('-r', '--region'): |
| 139 region = value |
| 140 # handle each command |
| 141 if command == 'vaults': |
| 142 list_vaults(region, access_key, secret_key) |
| 143 elif command == 'jobs': |
| 144 if len(args) != 1: |
| 145 usage() |
| 146 list_jobs(args[0], region, access_key, secret_key) |
| 147 elif command == 'upload': |
| 148 if len(args) < 2: |
| 149 usage() |
| 150 upload_files(args[0], args[1:], region, access_key, secret_key) |
| 151 |
| 152 |
| 153 if __name__ == '__main__': |
| 154 main() |
OLD | NEW |