Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame^] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import requests |
| 5 | import time |
| 6 | from vcpecommon import * |
| 7 | from datetime import datetime |
| 8 | import soutils |
| 9 | import logging |
| 10 | import preload |
| 11 | import json |
| 12 | |
| 13 | |
| 14 | class CustomService: |
| 15 | def __init__(self, vcpecommon): |
| 16 | self.logger = logging.getLogger(__name__) |
| 17 | self.vcpecommon = vcpecommon |
| 18 | |
| 19 | # delete all vgw stacks |
| 20 | def del_all_vgw_stacks(self, keyword): |
| 21 | param = ' '.join([k + ' ' + v for k, v in self.vcpecommon.cloud.items()]) |
| 22 | openstackcmd = 'openstack ' + param + ' ' |
| 23 | |
| 24 | stacks = os.popen(openstackcmd + 'stack list').read() |
| 25 | found = False |
| 26 | for stack_description in stacks.split('\n'): |
| 27 | if keyword in stack_description: |
| 28 | found = True |
| 29 | stack_name = stack_description.split('|')[2].strip() |
| 30 | cmd = openstackcmd + 'stack delete -y ' + stack_name |
| 31 | self.logger.info('Deleting ' + stack_name) |
| 32 | os.popen(cmd) |
| 33 | |
| 34 | if not found: |
| 35 | self.logger.info('No vGW stack to delete') |
| 36 | |
| 37 | # clean up SDNC |
| 38 | def clean_up_sdnc(self): |
| 39 | items = ['tunnelxconn-allotted-resources', 'brg-allotted-resources'] |
| 40 | for res in items: |
| 41 | self.logger.info('Cleaning up ' + res + ' from SDNC') |
| 42 | requests.delete(self.vcpecommon.sdnc_ar_cleanup_url + res, auth=self.vcpecommon.sdnc_userpass) |
| 43 | |
| 44 | def print_success_info(self, print_instructions=True, nodes=None): |
| 45 | if not nodes: |
| 46 | nodes = ['brg', 'mux', 'gw', 'web'] |
| 47 | ip_dict = self.vcpecommon.get_vm_ip(nodes, self.vcpecommon.external_net_addr, |
| 48 | self.vcpecommon.external_net_prefix_len) |
| 49 | |
| 50 | print(json.dumps(ip_dict, indent=4, sort_keys=True)) |
| 51 | for node in ['brg', 'mux']: |
| 52 | print('VxLAN config in {0}:'.format(node)) |
| 53 | self.vcpecommon.get_vxlan_interfaces(ip_dict[node], print_info=True) |
| 54 | |
| 55 | print(json.dumps(ip_dict, indent=4, sort_keys=True)) |
| 56 | |
| 57 | if print_instructions: |
| 58 | print('----------------------------------------------------------------------------') |
| 59 | print('Custom service created successfully. See above for VxLAN configuration info.') |
| 60 | print('To test data plane connectivity, following the steps below.') |
| 61 | print(' 1. ssh to vGW at {0}'.format(ip_dict['gw'])) |
| 62 | print(' 2. Restart DHCP: systemctl restart isc-dhcp-server') |
| 63 | print(' 3. ssh to vBRG at {0}'.format(ip_dict['brg'])) |
| 64 | print(' 4. Get IP from vGW: dhclient lstack') |
| 65 | print(' 5. Add route to Internet: ip route add 10.2.0.0/24 via 192.168.1.254 dev lstack') |
| 66 | print(' 6. ping the web server: ping {0}'.format('10.2.0.10')) |
| 67 | print(' 7. wget http://{0}'.format('10.2.0.10')) |
| 68 | |
| 69 | def create_custom_service(self, csar_file, vgw_template_file, preload_dict=None): |
| 70 | name_suffix = datetime.now().strftime('%Y%m%d%H%M') |
| 71 | brg_mac = self.vcpecommon.get_brg_mac_from_sdnc() |
| 72 | # preload vGW |
| 73 | if preload_dict: |
| 74 | preloader = preload.Preload(self.vcpecommon) |
| 75 | preloader.preload_vgw(vgw_template_file, brg_mac, preload_dict, name_suffix) |
| 76 | |
| 77 | # create service |
| 78 | so = soutils.SoUtils(self.vcpecommon, 'v5') |
| 79 | if so.create_custom_service(csar_file, brg_mac, name_suffix): |
| 80 | self.print_success_info() |