source: src/django_gheat/gheat/management/commands/import_nodes.py@ 9819

Last change on this file since 9819 was 9656, checked in by rick, 13 years ago

Some hosts takes up to 10 seconds to generate all the configs.

  • Property svn:executable set to *
File size: 2.3 KB
Line 
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#
11from django.core.management.base import BaseCommand,CommandError
12from django.db.utils import IntegrityError
13from optparse import OptionParser, make_option
14from gheat.models import *
15import os
16import sys
17import logging
18import yaml
19
20from select import select
21from collections import defaultdict
22
23logger = logging.getLogger(__name__)
24logger.setLevel(logging.INFO)
25
26INPUT_TIMEOUT = 10
27
28
29class 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
Note: See TracBrowser for help on using the repository browser.