Tony Mack | 09078f0 | 2013-04-02 07:53:46 -0400 | [diff] [blame] | 1 | import os |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 2 | #os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
Tony Mack | bd0cd32 | 2013-04-02 09:54:07 -0400 | [diff] [blame] | 3 | import sys |
| 4 | from optparse import OptionParser |
| 5 | from getpass import getpass |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 6 | import xmlrpclib |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 7 | from plclassic.site_importer import SiteImporter |
| 8 | from plclassic.user_importer import UserImporter |
| 9 | from plclassic.slice_importer import SliceImporter |
| 10 | from plclassic.sliver_importer import SliverImporter |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class Call: |
| 14 | def __init__(self, callable, auth): |
| 15 | self.callable = callable |
| 16 | self.auth = auth |
| 17 | |
| 18 | def __call__(self, *args, **kwds): |
Tony Mack | dd12c68 | 2013-04-02 10:15:27 -0400 | [diff] [blame] | 19 | a = [self.auth] + list(args) |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 20 | return self.callable(*a) |
| 21 | |
| 22 | class API(): |
Tony Mack | 59c762a | 2013-04-02 09:42:10 -0400 | [diff] [blame] | 23 | def __init__(self, username, password, url): |
Tony Mack | 8eeddf9 | 2013-03-26 13:42:59 -0400 | [diff] [blame] | 24 | self.auth = {'AuthMethod': 'password', |
Tony Mack | 59c762a | 2013-04-02 09:42:10 -0400 | [diff] [blame] | 25 | 'Username': username, |
| 26 | 'AuthString': password} |
| 27 | self.server = xmlrpclib.ServerProxy(url, allow_none=True) |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 28 | |
| 29 | def __getattr__(self, name): |
| 30 | return Call(getattr(self.server, name), self.auth) |
| 31 | |
| 32 | class Importer: |
| 33 | |
Tony Mack | 59c762a | 2013-04-02 09:42:10 -0400 | [diff] [blame] | 34 | def __init__(self, username, password, url): |
| 35 | api = API(username, password, url) |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 36 | self.sites = SiteImporter(api) |
Tony Mack | dd12c68 | 2013-04-02 10:15:27 -0400 | [diff] [blame] | 37 | self.slices = SliceImporter(api) |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 38 | self.users = UserImporter(api) |
| 39 | self.slivers = SliverImporter(api) |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 40 | |
| 41 | def run(self): |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 42 | self.sites.run() |
| 43 | self.users.run() |
Tony Mack | dd12c68 | 2013-04-02 10:15:27 -0400 | [diff] [blame] | 44 | self.slices.run(remote_sites=self.sites.remote_sites, |
| 45 | local_sites=self.sites.local_sites) |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 46 | self.slivers.run() |
| 47 | |
| 48 | |
Tony Mack | 09078f0 | 2013-04-02 07:53:46 -0400 | [diff] [blame] | 49 | |
| 50 | if __name__ == '__main__': |
Tony Mack | bd0cd32 | 2013-04-02 09:54:07 -0400 | [diff] [blame] | 51 | parser = OptionParser() |
| 52 | |
| 53 | parser.add_option("-u", "--username", dest="username", |
| 54 | help="PLC username with which to authenticate") |
| 55 | parser.add_option("", "--url", dest="url", |
| 56 | help="PLC url to contact") |
| 57 | |
| 58 | (config, args) = parser.parse_args() |
| 59 | if len(sys.argv) == 1: |
| 60 | parser.print_help() |
| 61 | sys.exit(1) |
| 62 | |
| 63 | password = None |
| 64 | try: |
| 65 | password = getpass() |
| 66 | except (EOFError, KeyboardInterrupt): |
| 67 | print |
| 68 | sys.exit(0) |
| 69 | |
Tony Mack | bd0cd32 | 2013-04-02 09:54:07 -0400 | [diff] [blame] | 70 | Importer(config.username, password, config.url).run() |