| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2009 Chris Moyer http://kopertop.blogspot.com/ | 2 # Copyright (c) 2009 Chris Moyer http://kopertop.blogspot.com/ |
| 3 # | 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a | 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the | 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including | 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- | 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit | 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- | 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: | 10 # lowing conditions: |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 for line in file.readlines(): | 83 for line in file.readlines(): |
| 84 if line: | 84 if line: |
| 85 data = json.loads(line) | 85 data = json.loads(line) |
| 86 item = domain.new_item(data['name']) | 86 item = domain.new_item(data['name']) |
| 87 item.update(data['attributes']) | 87 item.update(data['attributes']) |
| 88 item.save() | 88 item.save() |
| 89 | 89 |
| 90 else: | 90 else: |
| 91 domain.from_xml(file) | 91 domain.from_xml(file) |
| 92 | 92 |
| 93 def check_valid_region(conn, region): |
| 94 if conn is None: |
| 95 print 'Invalid region (%s)' % region |
| 96 sys.exit(1) |
| 97 |
| 93 def create_db(domain_name, region_name): | 98 def create_db(domain_name, region_name): |
| 94 """Create a new DB | 99 """Create a new DB |
| 95 | 100 |
| 96 :param domain: Name of the domain to create | 101 :param domain: Name of the domain to create |
| 97 :type domain: str | 102 :type domain: str |
| 98 """ | 103 """ |
| 99 sdb = boto.sdb.connect_to_region(region_name) | 104 sdb = boto.sdb.connect_to_region(region_name) |
| 105 check_valid_region(sdb, region_name) |
| 100 return sdb.create_domain(domain_name) | 106 return sdb.create_domain(domain_name) |
| 101 | 107 |
| 102 if __name__ == "__main__": | 108 if __name__ == "__main__": |
| 103 from optparse import OptionParser | 109 from optparse import OptionParser |
| 104 parser = OptionParser(version=VERSION, usage="Usage: %prog [--dump|--load|--
empty|--list|-l] [options]") | 110 parser = OptionParser(version=VERSION, usage="Usage: %prog [--dump|--load|--
empty|--list|-l] [options]") |
| 105 | 111 |
| 106 # Commands | 112 # Commands |
| 107 parser.add_option("--dump", help="Dump domain to file", dest="dump", default
=False, action="store_true") | 113 parser.add_option("--dump", help="Dump domain to file", dest="dump", default
=False, action="store_true") |
| 108 parser.add_option("--load", help="Load domain contents from file", dest="loa
d", default=False, action="store_true") | 114 parser.add_option("--load", help="Load domain contents from file", dest="loa
d", default=False, action="store_true") |
| 109 parser.add_option("--empty", help="Empty all contents of domain", dest="empt
y", default=False, action="store_true") | 115 parser.add_option("--empty", help="Empty all contents of domain", dest="empt
y", default=False, action="store_true") |
| 110 parser.add_option("-l", "--list", help="List All domains", dest="list", defa
ult=False, action="store_true") | 116 parser.add_option("-l", "--list", help="List All domains", dest="list", defa
ult=False, action="store_true") |
| 111 parser.add_option("-c", "--create", help="Create domain", dest="create", def
ault=False, action="store_true") | 117 parser.add_option("-c", "--create", help="Create domain", dest="create", def
ault=False, action="store_true") |
| 112 | 118 |
| 113 parser.add_option("-a", "--all-domains", help="Operate on all domains", acti
on="store_true", default=False, dest="all_domains") | 119 parser.add_option("-a", "--all-domains", help="Operate on all domains", acti
on="store_true", default=False, dest="all_domains") |
| 114 if json: | 120 if json: |
| 115 parser.add_option("-j", "--use-json", help="Load/Store as JSON instead o
f XML", action="store_true", default=False, dest="json") | 121 parser.add_option("-j", "--use-json", help="Load/Store as JSON instead o
f XML", action="store_true", default=False, dest="json") |
| 116 parser.add_option("-s", "--sort-attibutes", help="Sort the element attribute
s", action="store_true", default=False, dest="sort_attributes") | 122 parser.add_option("-s", "--sort-attibutes", help="Sort the element attribute
s", action="store_true", default=False, dest="sort_attributes") |
| 117 parser.add_option("-d", "--domain", help="Do functions on domain (may be mor
e then one)", action="append", dest="domains") | 123 parser.add_option("-d", "--domain", help="Do functions on domain (may be mor
e then one)", action="append", dest="domains") |
| 118 parser.add_option("-f", "--file", help="Input/Output file we're operating on
", dest="file_name") | 124 parser.add_option("-f", "--file", help="Input/Output file we're operating on
", dest="file_name") |
| 119 parser.add_option("-r", "--region", help="Region (e.g. us-east-1[default] or
eu-west-1)", default="us-east-1", dest="region_name") | 125 parser.add_option("-r", "--region", help="Region (e.g. us-east-1[default] or
eu-west-1)", default="us-east-1", dest="region_name") |
| 120 (options, args) = parser.parse_args() | 126 (options, args) = parser.parse_args() |
| 121 | 127 |
| 122 if options.create: | 128 if options.create: |
| 123 for domain_name in options.domains: | 129 for domain_name in options.domains: |
| 124 create_db(domain_name, options.region_name) | 130 create_db(domain_name, options.region_name) |
| 125 exit() | 131 exit() |
| 126 | 132 |
| 127 sdb = boto.sdb.connect_to_region(options.region_name) | 133 sdb = boto.sdb.connect_to_region(options.region_name) |
| 134 check_valid_region(sdb, options.region_name) |
| 128 if options.list: | 135 if options.list: |
| 129 for db in sdb.get_all_domains(): | 136 for db in sdb.get_all_domains(): |
| 130 print db | 137 print db |
| 131 exit() | 138 exit() |
| 132 | 139 |
| 133 if not options.dump and not options.load and not options.empty: | 140 if not options.dump and not options.load and not options.empty: |
| 134 parser.print_help() | 141 parser.print_help() |
| 135 exit() | 142 exit() |
| 136 | 143 |
| 137 | 144 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 print "---------> Loading %s <----------" % domain.name | 185 print "---------> Loading %s <----------" % domain.name |
| 179 if options.file_name: | 186 if options.file_name: |
| 180 file_name = options.file_name | 187 file_name = options.file_name |
| 181 else: | 188 else: |
| 182 file_name = "%s.db" % domain.name | 189 file_name = "%s.db" % domain.name |
| 183 load_db(domain, open(file_name, "rb"), options.json) | 190 load_db(domain, open(file_name, "rb"), options.json) |
| 184 | 191 |
| 185 | 192 |
| 186 total_time = round(time.time() - stime, 2) | 193 total_time = round(time.time() - stime, 2) |
| 187 print "--------> Finished in %s <--------" % total_time | 194 print "--------> Finished in %s <--------" % total_time |
| OLD | NEW |