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