blob: 864f4b9dc43de645569389f28b1c2043940f518b [file] [log] [blame]
Tony Mack09078f02013-04-02 07:53:46 -04001import os
Scott Baker76a840e2015-02-11 21:38:09 -08002#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
Tony Mackbd0cd322013-04-02 09:54:07 -04003import sys
4from optparse import OptionParser
5from getpass import getpass
Tony Mackf593dba2013-03-24 13:17:43 -04006import xmlrpclib
Siobhan Tully30fd4292013-05-10 08:59:56 -04007from plclassic.site_importer import SiteImporter
8from plclassic.user_importer import UserImporter
9from plclassic.slice_importer import SliceImporter
10from plclassic.sliver_importer import SliverImporter
Tony Mackf593dba2013-03-24 13:17:43 -040011
12
13class Call:
14 def __init__(self, callable, auth):
15 self.callable = callable
16 self.auth = auth
17
18 def __call__(self, *args, **kwds):
Tony Mackdd12c682013-04-02 10:15:27 -040019 a = [self.auth] + list(args)
Tony Mackf593dba2013-03-24 13:17:43 -040020 return self.callable(*a)
21
22class API():
Tony Mack59c762a2013-04-02 09:42:10 -040023 def __init__(self, username, password, url):
Tony Mack8eeddf92013-03-26 13:42:59 -040024 self.auth = {'AuthMethod': 'password',
Tony Mack59c762a2013-04-02 09:42:10 -040025 'Username': username,
26 'AuthString': password}
27 self.server = xmlrpclib.ServerProxy(url, allow_none=True)
Tony Mackf593dba2013-03-24 13:17:43 -040028
29 def __getattr__(self, name):
30 return Call(getattr(self.server, name), self.auth)
31
32class Importer:
33
Tony Mack59c762a2013-04-02 09:42:10 -040034 def __init__(self, username, password, url):
35 api = API(username, password, url)
Tony Mack743ddf22013-04-02 07:36:25 -040036 self.sites = SiteImporter(api)
Tony Mackdd12c682013-04-02 10:15:27 -040037 self.slices = SliceImporter(api)
Tony Mack743ddf22013-04-02 07:36:25 -040038 self.users = UserImporter(api)
39 self.slivers = SliverImporter(api)
Tony Mackf593dba2013-03-24 13:17:43 -040040
41 def run(self):
Tony Mackf593dba2013-03-24 13:17:43 -040042 self.sites.run()
43 self.users.run()
Tony Mackdd12c682013-04-02 10:15:27 -040044 self.slices.run(remote_sites=self.sites.remote_sites,
45 local_sites=self.sites.local_sites)
Tony Mackf593dba2013-03-24 13:17:43 -040046 self.slivers.run()
47
48
Tony Mack09078f02013-04-02 07:53:46 -040049
50if __name__ == '__main__':
Tony Mackbd0cd322013-04-02 09:54:07 -040051 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 Mackbd0cd322013-04-02 09:54:07 -040070 Importer(config.username, password, config.url).run()