1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | #
|
---|
4 | # Script for importing nodes from yaml information generated by gformat.py
|
---|
5 | #
|
---|
6 | # Example: $ ~/wleiden/node-config/genesis/tools/gformat.py full-export |
|
---|
7 | # ./manage.py import_nodes -
|
---|
8 | #
|
---|
9 | # Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
10 | #
|
---|
11 | from django.core.management.base import BaseCommand,CommandError
|
---|
12 | from django.db.utils import IntegrityError
|
---|
13 | from optparse import OptionParser, make_option
|
---|
14 | from gheat.models import *
|
---|
15 | import os
|
---|
16 | import sys
|
---|
17 | import logging
|
---|
18 | import yaml
|
---|
19 |
|
---|
20 | from select import select
|
---|
21 | from collections import defaultdict
|
---|
22 |
|
---|
23 | logger = logging.getLogger(__name__)
|
---|
24 | logger.setLevel(logging.INFO)
|
---|
25 |
|
---|
26 | INPUT_TIMEOUT = 10
|
---|
27 |
|
---|
28 |
|
---|
29 | class Command(BaseCommand):
|
---|
30 | args = '[<filename>]'
|
---|
31 | help = 'Specify - to read from stdin'
|
---|
32 | option_list = BaseCommand.option_list + (
|
---|
33 | make_option('-g', '--debug', dest='debug', default='False', help="Debug", action="store_true"),
|
---|
34 | )
|
---|
35 |
|
---|
36 | def handle(self, *args, **options):
|
---|
37 | if options['debug']:
|
---|
38 | logger.setLevel(logging.DEBUG)
|
---|
39 | if len(args) == 0:
|
---|
40 | self.print_help(sys.argv[0],sys.argv[1])
|
---|
41 | raise CommandError("Not all arguments are provided")
|
---|
42 |
|
---|
43 | filename = args[0]
|
---|
44 | if filename == '-':
|
---|
45 | logger.info("Waiting %s seconds for input to arive", INPUT_TIMEOUT)
|
---|
46 | rlist, _, _ = select([sys.stdin], [], [], INPUT_TIMEOUT)
|
---|
47 | if rlist:
|
---|
48 | fh = sys.stdin
|
---|
49 | else:
|
---|
50 | raise CommandError("No stdin input specified within %s seconds" % timeout)
|
---|
51 | else:
|
---|
52 | if not os.path.isfile(filename):
|
---|
53 | raise CommandError("yaml file '%s' does not exists" % filename)
|
---|
54 | fh = open(filename,'r')
|
---|
55 |
|
---|
56 | hosts = yaml.load(fh)
|
---|
57 | for host,items in hosts.iteritems():
|
---|
58 | for iface_key in [x for x in items.keys() if x.startswith('iface_')]:
|
---|
59 | if items[iface_key].has_key('ssid'):
|
---|
60 | org = Organization.get_by_ssid(items[iface_key]['ssid'])
|
---|
61 | if org: break
|
---|
62 | logger.error("NO Organization for SSID '%s'", items[iface_key]['ssid'])
|
---|
63 |
|
---|
64 | if not org:
|
---|
65 | logger.warning("NO Organization for Node '%s'", host)
|
---|
66 |
|
---|
67 | node, created = Node.objects.get_or_create(name=host)
|
---|
68 | node.latitude = items['latitude']
|
---|
69 | node.longitude = items['longitude']
|
---|
70 | node.organization = org
|
---|
71 | node.save()
|
---|
72 | if created:
|
---|
73 | logger.info("Created Node '%s'", node)
|
---|
74 | logger.info("Processed %s items", len(hosts))
|
---|
75 |
|
---|