~virtual-maasers/modev/trunk

« back to all changes in this revision

Viewing changes to modev/commands/cmd_modev.py

  • Committer: pierre-andre.morey at canonical
  • Date: 2014-07-09 22:14:05 UTC
  • Revision ID: pierre-andre.morey@canonical.com-20140709221405-3bh0tbtiy0jtxy5f
rewrote the cird checking part, start to search for free cidr to automate generation of cluster's subnet. Late, cluster part doesn't work. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
import argparse
4
3
import os
5
4
import subprocess
6
5
import sys
7
 
import netaddr  # check python-netaddr package
8
 
import yaml  # need package PyYaml...
 
6
import argparse
 
7
import netaddr      # check python-netaddr package
 
8
import yaml         # need package PyYaml...
9
9
import logging
10
10
 
11
11
from neutronclient.v2_0 import client
43
43
    'python-glanceclient': 'glance',
44
44
    'python-swiftclient': 'swift',
45
45
    'qemu-utils': 'qemu-img',
46
 
    'python-netaddr': 'None',
 
46
    'python-netaddr': 'ls',  # need to mod to be able to specify None
47
47
}
48
48
 
49
49
COMMON_ARGS = []
50
50
 
51
 
# COMMON_FLAGS = {
52
 
#    'config': (('--config',),
53
 
#               {'help': 'modev config to use', 'default': DEFAULT_CONFIG}),
54
 
#    'dry-run': (('-n', '--dry-run'),
55
 
#                {'help': 'only report what would be done',
56
 
#                 'action': 'store_true', 'default': False}),
57
 
#    'ipv6': (('-6', '--ipv6'),
58
 
#                {'help': 'All netwroks are IPv6',
59
 
#                 'action': 'store_true', 'default': False}),
60
 
#
61
 
#}
 
51
'''
 
52
 COMMON_FLAGS = {
 
53
    'config': (('--config',),
 
54
               {'help': 'modev config to use', 'default': DEFAULT_CONFIG}),
 
55
    'dry-run': (('-n', '--dry-run'),
 
56
                {'help': 'only report what would be done',
 
57
                 'action': 'store_true', 'default': False}),
 
58
    'ipv6': (('-6', '--ipv6'),
 
59
                {'help': 'All netwroks are IPv6',
 
60
                 'action': 'store_true', 'default': False}),
 
61
 
 
62
}
 
63
'''
 
64
 
62
65
 
63
66
SUBCOMMANDS = {
64
67
    'check': {
76
79
        'help': 'add a new cluster network in a region',
77
80
        'opts': [
78
81
            #            COMMON_FLAGS['config'],
79
 
            (('--base-cidr','-cidr',),
 
82
            (('--base-cidr', '-cidr',),
80
83
             {'help': 'subnet definition \
81
84
                (default {})'.format(DEFAULT_CLUSTER_SUBNET),
82
85
              'action': 'store', 'default': DEFAULT_CLUSTER_SUBNET}),
83
86
 
84
87
            #            (('--cluster-controller-create'),
85
 
            #             {'help': 'create the maas cluster controller instance \
 
88
            #             {'help': 'create the maas cluster controller instance
86
89
            #                on this cluster',
87
90
            #              'action': 'store', 'default': True}),
88
91
 
166
169
            #             {'help': 'pem file to use (don\'t create one)'}),
167
170
 
168
171
            (('--net-router', '-nr'),
169
 
             {'help': 'if you want to reuse an existing router'}),
 
172
             {'help': 'if you want to reuse an existing router, this ' +
 
173
                      'can be an id or an existing region name'}),
170
174
 
171
175
            (('--force', '-f'),
172
176
             {'help': 'if leftover config files exists',
201
205
             {'help': 'list region on the same cloud than this one'}),
202
206
        ]
203
207
    },
 
208
    'region-print': {
 
209
        'help': 'dump config of region',
 
210
        'opts': [
 
211
            (('--region-name', '-rn'),
 
212
             {'help': 'The name of the region to use/create',
 
213
              'action': 'store', 'default': DEFAULT_REGION}),
 
214
        ]
 
215
    },
204
216
    'region-purge': {
205
217
        'help': 'purge everything in a region',
206
218
        'opts': [
359
371
 
360
372
 
361
373
def is_modev_region(name=None):
362
 
    return(os.path.exists(DEFAULT_CONF_DIR + '/' + name + '/'
363
 
                          + name + '.conf'))
 
374
    return(os.path.exists(DEFAULT_CONF_DIR + '/' + str(name) + '/'
 
375
                          + str(name) + '.conf'))
364
376
 
365
377
 
366
378
def get_modev_config():
367
 
    #failsafe if file no properly configured:
 
379
    # failsafe if file no properly configured:
368
380
    modev_cfg = {'options': {'region': {'default': 'm0d'}}}
369
381
    if os.path.exists(DEFAULT_CONFIG):
370
382
        modev_file = open(DEFAULT_CONFIG, 'r')
377
389
    if not os.path.exists(DEFAULT_CONF_DIR):
378
390
        os.makedirs(DEFAULT_CONF_DIR)
379
391
    modev_file = open(DEFAULT_CONFIG, 'w')
380
 
    modev_file.write(yaml.dump(modev_cfg))
 
392
    modev_file.write(yaml.safe_dump(modev_cfg,
 
393
                     default_flow_style=False))
381
394
    modev_file.close()
382
395
 
383
396
 
389
402
              '{}'.format(DEFAULT_CONFIG))
390
403
        modev_region = DEFAULT_REGION
391
404
        set_modev_region(modev_region)
 
405
 
 
406
    if is_modev_region(modev_region) is False:
 
407
        regions = list_modev_regions()
 
408
        if len(regions) is 0:
 
409
            set_modev_region(DEFAULT_REGION)
 
410
            modev_region = DEFAULT_REGION
 
411
        else:
 
412
            set_modev_region(regions[0])
 
413
            modev_region = regions[0]
392
414
    return(modev_region)
393
415
 
394
416
 
395
 
def update_modev_config(update=None):
 
417
def set_modev_region(name=None):
 
418
    if name is None:
 
419
        name = DEFAULT_REGION
396
420
    if not os.path.exists(DEFAULT_CONF_DIR):
397
421
        os.makedirs(DEFAULT_CONF_DIR)
398
422
    modev_cfg = get_modev_config()
399
 
    modev_cfg.update(update)
 
423
    modev_cfg['options']['region']['default'] = name
400
424
    set_modev_config(modev_cfg)
401
425
 
402
426
 
403
 
def set_modev_region(name=None):
404
 
    if name is None:
405
 
        name = DEFAULT_REGION
406
 
    update_modev_config({'options': {'region': {'default': name}}})
407
 
    
408
 
 
409
427
def select_region(name=None, config=None):
410
428
    modev_region = get_modev_region()
411
429
    if name is None:
412
 
        if modev_region is None:
 
430
        if is_modev_region(modev_region) is False:
413
431
            name = DEFAULT_REGION
414
432
        else:
415
433
            name = modev_region
417
435
        if '_' in name:
418
436
            print('You can\'t put a "_" in a region name')
419
437
            sys.exit()
 
438
    # to reinitialize after deletion
 
439
    set_modev_region(name)
420
440
    return name
421
441
 
422
442
 
447
467
    return region_objects
448
468
 
449
469
 
 
470
def neutron_get_region_router_id(name=None):
 
471
    conf = get_region_config(name)
 
472
    return(conf['router-id'])
 
473
 
 
474
 
 
475
def neutron_get_netname_from_cidr(rc=None, ncidr=None):
 
476
    neutron = neutron_connect(rc)
 
477
    net_name = neutron.list_subnets(cidr=str(ncidr))
 
478
    net_name = net_name['subnets'][0]['network_id']
 
479
    return(neutron.list_networks(id=net_name)['networks'][0][u'name'])
 
480
 
 
481
 
450
482
def neutron_get_cidrs(rc=None):
451
483
    cidr_list = []
452
484
    neutron = neutron_connect(rc)
453
485
    for sub in neutron.list_subnets()['subnets']:
454
486
        cidr_list.append(sub[u'cidr'])
455
 
    return(netaddr.cidr_merge(cidr_list))
 
487
    return(cidr_list)
 
488
 
 
489
 
 
490
def neutron_get_region_cidrs(name=None):
 
491
    name = select_region(name)
 
492
    subs = []
 
493
    for sub in get_region_config(name)['subnets']:
 
494
        neutron = neutron_connect(get_region_rc(name))
 
495
        filter = str('id=' + '\'' + sub['subnet-id'] + '\'')
 
496
        subs.append(neutron.list_subnets(filter)['subnets'][0]['cidr'])
 
497
    return(subs)
456
498
 
457
499
 
458
500
def neutron_check_cidrs(rc=None, cidr=None):
459
501
    neutron = neutron_connect(rc)
460
 
    for ncidr in neutron_get_cidrs(rc):
461
 
        for ip in ncidr.cidr:
462
 
            if ip == cidr.ip:
463
 
                net_name = neutron_get_netname_from_cidr(rc, ncidr.cidr)
464
 
                return(True, str(ncidr), net_name)
 
502
    return(check_cidr_in_cidrs(cidr, neutron_get_cidrs(rc), rc))
 
503
 
 
504
 
 
505
def check_cidr_in_cidrs(cidr=None, cidrs=None, rc=None):
 
506
    cidr = netaddr.IPNetwork(cidr)
 
507
    for ncidr in cidrs:
 
508
        ncidr = netaddr.IPNetwork(ncidr)
 
509
        if cidr.ip in ncidr.cidr or ncidr.ip in cidr.cidr:
 
510
            net_name = neutron_get_netname_from_cidr(rc, ncidr.cidr)
 
511
            return(True, str(ncidr), net_name)
465
512
    return(False, None, None)
466
513
 
467
514
 
468
 
def neutron_get_netname_from_cidr(rc=None, ncidr=None):
469
 
    neutron = neutron_connect(rc)
470
 
    net_name = neutron.list_subnets(cidr=str(ncidr))
471
 
    net_name = net_name['subnets'][0]['network_id']
472
 
    return(neutron.list_networks(id=net_name)['networks'][0][u'name'])
473
 
 
474
 
 
475
 
def neutron_get_region_router_id(name=None):
476
 
    conf = get_region_config(name)
477
 
    return(conf['router-id'])
478
 
 
479
 
 
480
515
def neutron_get_router_cidrs(name=None, rc=None, router_id=None):
 
516
    name = select_region(name)
481
517
    cidrs = []
482
 
    neutron = neutron_connect(rc)
 
518
 
 
519
    if rc is None:
 
520
        neutron = neutron_connect(get_region_rc(name))
 
521
    else:
 
522
        neutron = neutron_connect(rc)
 
523
 
483
524
    if router_id is None:
484
525
        router_id = neutron_get_region_router_id(name)
 
526
 
485
527
    for port in neutron.list_ports(device_id=router_id)['ports']:
486
528
        subnet_ids = []
487
529
        for ip in port['fixed_ips']:
489
531
        for sub_id in subnet_ids:
490
532
            for sub in neutron.list_subnets(id=sub_id)['subnets']:
491
533
                cidrs.append(sub['cidr'])
492
 
    return(netaddr.cidr_merge(cidrs))
493
 
 
494
 
 
495
 
def neutron_check_router_cidrs(name=None, rc=None, cidr=None,
 
534
    return(cidrs)
 
535
 
 
536
 
 
537
def neutron_check_router_cidrs(name=None, cidr=None, rc=None,
496
538
                               router_id=None):
 
539
 
 
540
    if rc is None:
 
541
        neutron = neutron_connect(get_region_rc(name))
 
542
    else:
 
543
        neutron = neutron_connect(rc)
 
544
 
497
545
    if router_id is None:
498
546
        router_id = neutron_get_region_router_id(name)
499
 
    neutron = neutron_connect(rc)
500
 
    for ncidr in neutron_get_router_cidrs(name, rc, router_id):
501
 
        for ip in ncidr.cidr:
502
 
            if ip == cidr.ip:
503
 
                net_name = neutron_get_netname_from_cidr(rc, ncidr.cidr)
504
 
                return(True, str(ncidr), net_name)
505
 
    return(False, None, None)
 
547
 
 
548
    return(check_cidr_in_cidrs(cidr,
 
549
                               neutron_get_router_cidrs(name, rc,
 
550
                                                        router_id), rc))
 
551
 
 
552
 
 
553
def cidr_router_find_free(name=None):
 
554
    name = select_region(name)
 
555
    cidrs = neutron_get_router_cidrs(name)
 
556
    cidrs.sort()
 
557
    test_cidr = DEFAULT_CLUSTER_SUBNET.split('.')
 
558
 
 
559
    counter = int(test_cidr[2])
 
560
    while(counter < 255):
 
561
 
 
562
        counter += 1
 
563
    test_cidr[2] = counter
 
564
    print(test_cidr)
 
565
    sys.exit()
 
566
    found = False
 
567
#    while( base_cidr <= 255 ):
 
568
#        test_cidr(')
 
569
 
 
570
    print(cidrs)
 
571
 
 
572
 
 
573
def cidr_region_find_free(name=None):
 
574
    print('to be done')
 
575
 
 
576
 
 
577
def cidr_region_list(name=None):
 
578
    name = select_region(name)
 
579
    config = get_region_config(name)
 
580
 
 
581
 
 
582
def cidr_check_free(cidr=None, name=None, rc=None, router_id=None):
 
583
 
 
584
    if cidr is None:
 
585
        raise NameError('cidr must be given!')
 
586
 
 
587
    if rc is None and name is not None:
 
588
        rc = get_region_rc(name)
 
589
        neutron = neutron_connect(rc)
 
590
    else:
 
591
        neutron = neutron_connect(rc)
 
592
 
 
593
    if router_id is None and name is not None:
 
594
        router_id = neutron_get_region_router_id(name)
 
595
 
 
596
    exist, ex_cidr, ex_name = \
 
597
        neutron_check_router_cidrs(rc=rc, cidr=cidr, router_id=router_id)
 
598
    if exist:
 
599
        print('This cidr is overlapping another one used on this ' +
 
600
              'environment:\nexisting cidr: {} '.format(ex_cidr) +
 
601
              '- network name: {}'.format(ex_name))
 
602
        full_region = FIXED_PREFIX + name
 
603
        neutron_purge(prefix=full_region, rc=rc)
 
604
        sys.exit(2)
 
605
 
 
606
    return(exist)
 
607
 
 
608
 
 
609
def cidr_validate(cidr=None):
 
610
    #   Validate base cidr
 
611
    try:
 
612
        vcidr = netaddr.IPNetwork(cidr)
 
613
    except:
 
614
        print('Could\'nt validate your cidr address, please verify ' +
 
615
              'it must be something like:\n' +
 
616
              '{}'.format(DEFAULT_ADM_SUBNET))
 
617
        sys.exit(2)
 
618
    return(vcidr)
506
619
 
507
620
 
508
621
def neutron_update_ports(name=None):
519
632
                neutron.update_port(port[u'id'], port_update)
520
633
 
521
634
 
522
 
def neutron_purge(prefix=None, rc=None):
 
635
def neutron_purge(prefix=None, rc=None, verbose=False):
523
636
#   we connect to neutron to retrieve the list of networks
524
637
    neutron = neutron_connect(rc)
525
638
    net_list = neutron.list_networks()['networks']
547
660
 
548
661
#   we can delete networks now
549
662
    for network in region_net_list:
550
 
        print('deleting network:' +
551
 
              'name:"{}", id:"{}"'.format(network[u'name'],
552
 
                                          network[u'id']))
 
663
        if verbose:
 
664
            print('deleting network:' +
 
665
                  'name:"{}", id:"{}"'.format(network[u'name'],
 
666
                                              network[u'id']))
553
667
        neutron.delete_network(network[u'id'])
554
668
 
555
 
    router_list = neutron.list_routers()['routers']
556
 
    for router in router_list:
 
669
#   we delete routers now
 
670
    for router in neutron.list_routers()['routers']:
557
671
        if prefix in router[u'name']:
558
 
            print('deleting router:' +
559
 
                  'name:"{}", id:"{}"'.format(router[u'name'],
560
 
                                              router[u'id']))
561
 
            neutron.delete_router(router[u'id'])
 
672
            if verbose:
 
673
                print('deleting router:' +
 
674
                      'name:"{}", id:"{}"'.format(router[u'name'],
 
675
                                                  router[u'id']))
 
676
            try:
 
677
                neutron.delete_router(router[u'id'])
 
678
            except:
 
679
                print('An other region use this router')
562
680
 
563
681
 
564
682
def main_check(args):
590
708
    # We check if any of the needed package are missing
591
709
    for check in sorted(DEPENDANCIES.keys()):
592
710
 
593
 
        cmd_line = 'dpkg -s ' + check + \
 
711
        cmd_line = 'dpkg -s ' + str(check) + \
594
712
            ' | grep Status | grep installed |wc -l'
595
713
        output = subprocess.check_output(cmd_line, shell=True)
596
714
 
637
755
        print('please provide an existing region, you can create it ' +
638
756
              'with region-create command')
639
757
        sys.exit()
640
 
    set_modev_region(args.region_name)
 
758
 
 
759
    set_modev_region(select_region(args.region_name))
641
760
 
642
761
 
643
762
def main_region_create(args):
 
763
#   Need to rework the all function....
 
764
 
 
765
#   we read data from openrc file
 
766
    if not os.path.isfile(args.openrc):
 
767
        print('openrc file: "{}" don\'t exist!'.format(args.openrc))
 
768
        sys.exit()
 
769
 
644
770
    region_name = select_region(args.region_name)
 
771
    if region_name == DEFAULT_REGION and is_modev_region(region_name):
 
772
        i = 0
 
773
        while(i <= 9):
 
774
            free_region = str('m' + str(i) + 'd')
 
775
            if is_modev_region(free_region) is False:
 
776
                region_name = free_region
 
777
                break
 
778
            i += 1
 
779
        if is_modev_region(region_name):
 
780
            print('please specify a region name, seems like ' +
 
781
                  'you exhausted them all!')
 
782
            sys.exit(2)
645
783
 
646
 
#   Validate base cidr
647
 
    try:
648
 
        cidr = netaddr.IPNetwork(args.base_cidr)
649
 
    except:
650
 
        print('Could\'nt validate your cidr address, please verify ' +
651
 
              'it must be something like:\n' +
652
 
              '{}'.format(DEFAULT_ADM_SUBNET))
653
 
        sys.exit(2)
 
784
    cidr = cidr_validate(args.base_cidr)
654
785
 
655
786
#   we test if region isn't already configured
656
787
#   create directory if necessary
672
803
    openrc_cfg = {}
673
804
    region_cfg = {}
674
805
 
675
 
#   we read data from openrc file
676
 
    if not os.path.isfile(args.openrc):
677
 
        print('openrc file: "{}" don\'t exist!'.format(args.openrc))
678
 
        sys.exit()
679
 
 
680
806
    openrc_cfg.update({k: '' for k in CFG_NAMES})
681
807
    found = load_openrc(openrc=args.openrc,)
682
808
    openrc_cfg.update(found)
743
869
 
744
870
#   checking and using the one provided:
745
871
    else:
746
 
        # using a former one, we chek if it exists:
747
 
        router = neutron.list_routers(id=args.net_router)
 
872
#   checking if we need to get router from an other region
 
873
        if is_modev_region(args.net_router):
 
874
            router_id = neutron_get_region_router_id(args.net_router)
 
875
            print('Using router from region : ' +
 
876
                  '"{}" - id: "{}"'.format(args.net_router, router_id))
 
877
        else:
 
878
            # using a former one, we chek if it exists:
 
879
            router = neutron.list_routers(id=args.net_router)
748
880
 
749
 
        if len(router['routers']) is 0:
750
 
            print('This router Id doesn\'t exist!')
751
 
            sys.exit()
752
 
        else:
753
 
            router_id = router['routers'][0]['id']
754
 
            print(router_id)
 
881
            if len(router['routers']) is 0:
 
882
                print('This router Id doesn\'t exist!')
 
883
                sys.exit()
 
884
            else:
 
885
                router_id = router['routers'][0]['id']
 
886
                print('Using router: "{}"'.format(router_id))
755
887
    region_cfg.update({'router-id': router_id})
756
888
 
757
889
#
764
896
    network = neutron.create_network({'network': network})
765
897
    network_id = network[u'network'][u'id']
766
898
    region_cfg.update({'networks': [{'name': 'net-admin-id',
767
 
                                    'network-id': network_id},]})
 
899
                                    'network-id': network_id}, ]})
768
900
 
769
901
#
770
902
#                       SUBNET CREATION
772
904
 
773
905
#   creating network admin subnet:
774
906
#   First we check that this cidr is available on the tenant network
775
 
 
776
 
    exist, ex_cidr, ex_name = \
777
 
        neutron_check_router_cidrs(rc=openrc_cfg, cidr=cidr,
778
 
                                   router_id=router_id)
779
 
    if exist:
780
 
        print('This range is already used on this environment' +
781
 
              '!:\nexisting cidr: {} '.format(ex_cidr) +
782
 
              '- network name: {}'.format(ex_name))
783
 
        neutron_purge(prefix=full_region, rc=openrc_cfg)
784
 
        sys.exit(2)
 
907
    cidr_check_free(cidr, region_name, openrc_cfg, router_id)
785
908
 
786
909
    if cidr.version == 4:
787
910
        subnet = {'name': full_region + 'adm-snet',
791
914
        subnet_id = subnet[u'subnet'][u'id']
792
915
        region_cfg.update({'subnets': [{'name': 'subnet-adm-id',
793
916
                                       'subnet-id': subnet_id,
794
 
                                       'subnet-cidr': str(cidr)},]})
 
917
                                       'subnet-cidr': str(cidr)}, ]})
795
918
    elif cidr.version == 6:
796
919
        print('IPV6 is not yet implemented, sorry!')
797
920
        neutron_purge(prefix=full_region, rc=openrc_cfg)
824
947
                   'config': region_cfg, }
825
948
 
826
949
#   creating conf file
827
 
    save_region_file(region_name,region_conf)
 
950
    save_region_file(region_name, region_conf)
828
951
 
829
952
#
830
953
#                       Neutron Port 'Post-Conf'
840
963
def main_region_current(args):
841
964
    region = get_modev_region()
842
965
    if is_modev_region(region):
843
 
        state = 'Config file exists, state OK'
 
966
        print('Current selected region:\n{}'.format(region))
844
967
    else:
845
 
        state = 'Config file is destroyed, state KO'
846
 
    print('Current selected region:\n{}, {}'.format(region, state))
 
968
        print('No region is currently configured!')
 
969
        sys.exit(2)
847
970
 
848
971
 
849
972
def main_region_list(args):
856
979
    sys.exit()
857
980
 
858
981
 
 
982
def main_region_print(args):
 
983
    name = select_region(args.region_name)
 
984
    print('Printing configuration for region: "{}"'.format(name))
 
985
    print(yaml.safe_dump(get_region_config(name),
 
986
                         default_flow_style=False))
 
987
 
 
988
 
859
989
def get_openrc_regions(rc=None):
860
990
    net_list = []
861
991
    neutron = neutron_connect(rc)
864
994
        if 'modev' in name[u'name']:
865
995
        #   we retrieve the name of the region from network name
866
996
            net_list.append(name[u'name'].split('_')[1])
 
997
    for name in neutron.list_routers()['routers']:
 
998
        if 'modev' in name[u'name']:
 
999
            name = name[u'name'].split('_')[1]
 
1000
            if name not in net_list:
 
1001
                net_list.append(name)
867
1002
    return(net_list)
868
1003
 
869
1004
 
870
 
def purge_openrc(rc=None):
 
1005
def purge_openrc(rc=None, verbose=False):
871
1006
    for region in get_openrc_regions(rc):
872
 
        neutron_purge(FIXED_PREFIX + region, rc)
 
1007
        neutron_purge(FIXED_PREFIX + region, rc, verbose)
873
1008
        rmdir(DEFAULT_CONF_DIR + region)
874
 
 
875
 
 
876
 
def purge_region(name=None):
 
1009
    #   To be sure to delete all routers (some can be mutualised)
 
1010
    while(get_openrc_regions(rc)):
 
1011
        print('purging leftovers')
 
1012
        purge_openrc(rc, True)
 
1013
 
 
1014
 
 
1015
def purge_region(name=None, verbose=False):
877
1016
    if name is None:
878
1017
        raise NameError('Error, region can\'t be None, please report')
879
1018
    region_rc = get_region_rc(name)
880
 
    neutron_purge(FIXED_PREFIX + name, region_rc)
 
1019
    neutron_purge(FIXED_PREFIX + name, region_rc, verbose)
881
1020
    rmdir(DEFAULT_CONF_DIR + name)
882
1021
 
883
1022
 
886
1025
    if args.all_regions:
887
1026
        for region in list_modev_regions():
888
1027
            region_rc = get_region_rc(region)
889
 
            purge_openrc(region_rc)
 
1028
            purge_openrc(region_rc, verbose=True)
890
1029
    elif args.openrc:
891
1030
        region_rc = load_openrc(openrc=args.openrc)
892
 
        purge_openrc(region_rc)
 
1031
        purge_openrc(region_rc, verbose=True)
893
1032
    elif region:
894
1033
        region = select_region(args.region_name)
895
1034
        if args.same_cloud:
896
1035
            region_rc = get_region_rc(region)
897
 
            purge_openrc(region_rc)
 
1036
            purge_openrc(region_rc, verbose=True)
898
1037
        else:
899
 
            purge_region(region)
 
1038
            purge_region(region, verbose=True)
900
1039
    else:
901
1040
        print('You must specifies either : \nan openrc file (-rc)\n' +
902
1041
              'a region name (-rn)\n--all-regions to remove anything')
938
1077
 
939
1078
 
940
1079
def main_cluster_add(args):
 
1080
#   First checks
941
1081
    name = select_region(args.region_name)
942
1082
    region_cfg = load_region_file(name)
943
1083
    if is_modev_region(name) is False:
945
1085
        print('You can try command region-list')
946
1086
        sys.exit(2)
947
1087
 
948
 
    neutron =  neutron_connect(get_region_rc(name))
949
 
 
950
 
#   Creating Neutron Network
 
1088
    if args.base_cidr:
 
1089
        cidr = cidr_validate(args.base_cidr)
 
1090
    #   checking if given cidr is free on the region router
 
1091
        exist, ex_cidr, ex_name = \
 
1092
            neutron_check_router_cidrs(rc=openrc_cfg, cidr=cidr,
 
1093
                                       router_id=router_id)
 
1094
    else:
 
1095
        cluster_cidr = cidr_find_free(name)
 
1096
 
 
1097
#   Neutron Network Setup
 
1098
    neutron = neutron_connect(get_region_rc(name))
 
1099
 
 
1100
#   Check if it already exists
951
1101
    c_name = FIXED_PREFIX + name + '_' + args.name + '_cluster'
952
1102
    for cluster_net in region_cfg['config']['networks']:
953
1103
        if cluster_net['name'] == c_name:
954
1104
            print('A cluster with this name already exists!')
955
1105
            sys.exit(2)
956
1106
 
 
1107
#   We create the network for this cluster
957
1108
    network = {'name': c_name,
958
1109
               'admin_state_up': True}
959
1110
    network = neutron.create_network({'network': network})
960
1111
    network_id = network[u'network'][u'id']
961
 
 
 
1112
#   We add it to the region configuration
962
1113
    region_cfg['config']['networks'].append({'name': c_name,
963
1114
                                             'network-id': network_id})
964
 
    update_region_file(name,region_cfg)
 
1115
 
 
1116
#   Creation of subnet for the cluster
 
1117
 
 
1118
#   Update the region file
 
1119
    update_region_file(name, region_cfg)
965
1120
 
966
1121
    print('region to use:"{}"'.format(name))
967
 
    print('name:{}\n'.format(args.name) +\
 
1122
    print('name:{}\n'.format(args.name) +
968
1123
          'base-cidr:{}\n'.format(args.base_cidr))
969
1124
 
970
1125
 
977
1132
        print('There seems to be no region named: "{}"'.format(name))
978
1133
        print('You can try command region-list')
979
1134
        sys.exit(2)
980
 
    neutron =  neutron_connect(get_region_rc(name))
 
1135
    neutron = neutron_connect(get_region_rc(name))
981
1136
 
982
1137
#   Deleting Neutron Network
983
1138
    c_name = FIXED_PREFIX + name + '_' + args.name + '_cluster'
988
1143
            print('A cluster with this name doesn\'t exists!')
989
1144
            sys.exit(2)
990
1145
 
991
 
    print('name:{}\n'.format(args.name) +\
992
 
          'nodes-purge:{}\n'.format(args.nodes_purge) +\
 
1146
    print('name:{}\n'.format(args.name) +
 
1147
          'nodes-purge:{}\n'.format(args.nodes_purge) +
993
1148
          'nodes-reallocate:{}\n'.format(args.nodes_reallocate))
994
1149
 
995
1150
 
1000
1155
 
1001
1156
 
1002
1157
def main_td(args):
1003
 
    cfg = load_openrc()
1004
 
    neutron_get_region_router_id(args.rn)
 
1158
    cidr_router_find_free(args.rn)
1005
1159
 
1006
1160
 
1007
1161
def main():