Esempio n. 1
0
def save_launch():
    """
    save launch setting.
    :return: the error list of the edited launch setting.
    """
    launch_config_data = request.json if request.method == "POST" else request.args

    xml_configs = \
        get_xml_configs(launch_config_data['old_launch_name'].startswith('user_defined_'))
    launch_config = xml_configs[3]

    if xml_configs[1] is None or xml_configs[0] is None:
        return {'status': 'fail',
                'error_list': {'error': 'Please select the board info before this operation.'}}

    old_launch_name = launch_config_data['old_launch_name']
    old_launch_path = os.path.join(current_app.config.get('CONFIG_PATH'), xml_configs[1],
                                   old_launch_name + '.xml')
    if launch_config_data['old_launch_name'].startswith('user_defined_'):
        old_launch_name = launch_config_data['old_launch_name'][13:]
        old_launch_path = os.path.join(current_app.config.get('CONFIG_PATH'), xml_configs[1],
                                       'user_defined', old_launch_name + '.xml')
    launch_config.set_curr(old_launch_name)

    for key in launch_config_data:
        if key not in ['old_launch_name', 'new_launch_name', 'board_info_file',
                       'board_info_upload', 'scenario_name']:
            if isinstance(launch_config_data[key], list):
                launch_config.set_curr_list(launch_config_data[key], *tuple(key.split(',')))
            else:
                launch_config.set_curr_value(launch_config_data[key], *tuple(key.split(',')))

    scenario_name = launch_config_data['scenario_name']
    scenario_file_path = os.path.join(current_app.config.get('CONFIG_PATH'),
                                      current_app.config.get('BOARD_TYPE'), scenario_name + '.xml')
    if launch_config_data['scenario_name'].startswith('user_defined_'):
        scenario_name = launch_config_data['scenario_name'][13:]
        scenario_file_path = os.path.join(current_app.config.get('CONFIG_PATH'),
                                          current_app.config.get('BOARD_TYPE'),
                                          'user_defined', scenario_name + '.xml')
    launch_config.set_curr_attr('scenario', scenario_name)

    tmp_launch_file = os.path.join(current_app.config.get('CONFIG_PATH'), xml_configs[1],
                                   'user_defined',
                                   'tmp_' + launch_config_data['new_launch_name'] + '.xml')
    # if os.path.isfile(tmp_launch_file):
    #     os.remove(tmp_launch_file)
    launch_config.save('tmp_' + launch_config_data['new_launch_name'])

    # call validate function
    rename = False
    try:
        (error_list, pthru_sel, dm_value) = validate_launch_setting(
            os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res', xml_configs[0]+'.xml'),
            scenario_file_path,
            tmp_launch_file)
        print(pthru_sel, dm_value)
    except Exception as error:
        return {'status': 'fail', 'file_name': launch_config_data['new_launch_name'],
                'rename': rename, 'error_list': {'launch config error': str(error)}}

    print('error_list: ', error_list)

    if not error_list:
        new_launch_path = os.path.join(current_app.config.get('CONFIG_PATH'),
                                       xml_configs[1], 'user_defined',
                                       launch_config_data['new_launch_name'] + '.xml')
        # check name conflict
        if old_launch_path != new_launch_path and os.path.isfile(new_launch_path):
            launch_config_data['new_launch_name'] = launch_config_data['new_launch_name'] \
                                                    + '_' + datetime.now().strftime('%Y%m%d%H%M%S')
            rename = True

        if os.path.isfile(old_launch_path) \
                and launch_config_data['old_launch_name'].startswith('user_defined_'):
            os.remove(old_launch_path)
        launch_config.save(launch_config_data['new_launch_name'])

    if os.path.isfile(tmp_launch_file):
        os.remove(tmp_launch_file)

    return {'status': 'success', 'file_name': launch_config_data['new_launch_name'],
            'rename': rename, 'error_list': error_list}
Esempio n. 2
0
def save_launch():
    """
    save launch setting.
    :return: the error list of the edited launch setting.
    """
    launch_config_data = request.json if request.method == "POST" else request.args
    xml_configs = get_xml_configs()
    launch_config = xml_configs[3]

    if xml_configs[1] is None or xml_configs[0] is None:
        return {
            'status': 'fail',
            'error_list': {
                'error': 'Please select the board info before this operation.'
            }
        }

    old_launch_name = launch_config_data['old_launch_name']
    launch_config.set_curr(old_launch_name)
    scenario_name = launch_config_data['scenario_name']
    scenario_file_path = os.path.join(current_app.config.get('CONFIG_PATH'),
                                      current_app.config.get('BOARD_TYPE'),
                                      'user_defined', scenario_name + '.xml')

    for key in launch_config_data:
        if key not in [
                'old_launch_name', 'new_launch_name', 'generator',
                'add_launch_type', 'scenario_name'
        ]:
            if isinstance(launch_config_data[key], list):
                launch_config.set_curr_list(launch_config_data[key],
                                            *tuple(key.split(',')))
            else:
                launch_config.set_curr_value(launch_config_data[key],
                                             *tuple(key.split(',')))

    generator = launch_config_data['generator']
    if generator is not None:
        if generator.startswith('add_vm:'):
            vm_list = []
            for vm in launch_config.get_curr_root().getchildren():
                if vm.tag == 'uos':
                    vm_list.append(vm.attrib['id'])
            if len(vm_list) >= MAX_VM_NUM:
                return {
                    'status': 'fail',
                    'error_list': {
                        'error':
                        'Can not add a new VM. Max VM number is {}.'.format(
                            MAX_VM_NUM)
                    }
                }
            curr_vm_id = generator.split(':')[1].strip()
            add_launch_type = launch_config_data['add_launch_type']
            if add_launch_type is None or len(
                    add_launch_type.split('ID :')) < 2:
                return {
                    'status': 'fail',
                    'error_list': {
                        'error':
                        'No VM is selected. '
                        'Please select a scenario with available post launched VMs.'
                    }
                }
            add_vm_id = add_launch_type.split('ID :')[1].replace(')',
                                                                 '').strip()
            add_launch_type = 'LAUNCH_' + add_launch_type.split()[0]
            if add_launch_type == 'LAUNCH_KATA_VM':
                add_launch_type = 'LAUNCH_POST_STD_VM'
            add_launch_id = 1
            post_vm_list = get_post_launch_vm_list(scenario_name)
            for i in range(len(post_vm_list)):
                vm_info = post_vm_list[i]
                if add_vm_id == vm_info[0]:
                    add_launch_id = i + 1
                    break
            generic_scenario_config = get_generic_scenario_config(
                launch_config, add_launch_type)
            generic_scenario_config_root = generic_scenario_config.get_curr_root(
            )
            vm_to_add = []
            if str(curr_vm_id) == '-1':
                curr_vm_index = len(vm_list)
            else:
                curr_vm_index = 0
                for i in range(len(vm_list)):
                    if curr_vm_id == vm_list[i]:
                        curr_vm_index = i + 1
                        break
            for vm in generic_scenario_config_root.getchildren():
                if vm.tag == 'uos':
                    for i in range(1, MAX_VM_NUM):
                        if str(i) not in vm_list:
                            break
                    vm.attrib['id'] = str(add_launch_id)
                    vm_to_add.append(vm)

            for vm in vm_to_add:
                launch_config.insert_curr_elem(curr_vm_index, vm)
        elif generator.startswith('remove_vm:'):
            remove_vm_id = generator.split(':')[1]
            launch_config.delete_curr_key('uos:id=' + remove_vm_id.strip())

    launch_config.set_curr_attr('scenario', scenario_name)
    launch_config.set_curr_attr('board', current_app.config.get('BOARD_TYPE'))

    launch_config.set_curr_attr(
        'uos_launcher', str(len(launch_config.get_curr_root().getchildren())))

    tmp_launch_file = os.path.join(
        current_app.config.get('CONFIG_PATH'), xml_configs[1], 'user_defined',
        'tmp_' + launch_config_data['new_launch_name'] + '.xml')
    # if os.path.isfile(tmp_launch_file):
    #     os.remove(tmp_launch_file)
    launch_config.save('tmp_' + launch_config_data['new_launch_name'])

    # call validate function
    error_list = {}
    rename = False
    try:
        if generator is None or not (generator.startswith('add_vm:')
                                     or generator.startswith('remove_vm:')):
            (error_list, pthru_sel, virtio,
             dm_value) = validate_launch_setting(
                 os.path.join(os.path.dirname(os.path.abspath(__file__)),
                              'res', xml_configs[0] + '.xml'),
                 scenario_file_path, tmp_launch_file)
    except Exception as error:
        if os.path.isfile(tmp_launch_file):
            os.remove(tmp_launch_file)
        return {
            'status': 'fail',
            'file_name': launch_config_data['new_launch_name'],
            'rename': rename,
            'error_list': {
                'launch config error': str(error)
            }
        }

    if not error_list:
        launch_config.save(launch_config_data['new_launch_name'])
        if old_launch_name != launch_config_data['new_launch_name']:
            os.remove(
                os.path.join(current_app.config.get('CONFIG_PATH'),
                             xml_configs[1], old_launch_name + '.xml'))

    if os.path.isfile(tmp_launch_file):
        os.remove(tmp_launch_file)

    return {
        'status': 'success',
        'file_name': launch_config_data['new_launch_name'],
        'rename': rename,
        'error_list': error_list
    }