def release_lab(service_url=None, username=None, lab_name=None, force_release=False, **kwargs): """ Releases given lab in lab state service. """ log.info(("CMD line params: username:'******'," + " labName:'{}'").format(username, lab_name)) exit_code = 0 message = '' try: status_ok, lab_id, reservation_id, reservation_etag, err_message, previously_reverted_state = _check_lab_reservation_status(username, lab_name, service_url) if status_ok: rest.delete_item('reservations', reservation_id, None, reservation_etag, service_url, force_flag = force_release) message = "Released lab: '{}' ({})".format(lab_name, lab_id) else: exit_code = 1 message = err_message except requests.exceptions.HTTPError as e: exit_code = 1 log.exception(e) if e.response.status_code == 422: message = " ### ERROR: Precondition failure in removing 'reservations/{}'! Check the 'status' of the lab. ".format(reservation_id) else: message = " ### ERROR: '{}'".format(e.message) raise e except requests.exceptions.RequestException as e: exit_code = 1 log.debug('''Exception e='%s' is %s''' % (e, type(e))) message = " ### ERROR: '{}'".format(e.message) log.exception(e) raise e finally: _print_out_and_log(exit_code, message, kwargs['output_file']) return exit_code
def remove_server_config_entry(name, service_url=None, **kwargs): ''' remove the value of 'name' on LSS server ''' log.debug('remove_server_config_entry(name = %s, kwargs = %s)' % (name, kwargs)) try: item = rest.get_item('config', name, service_url) etag = item.get('_etag') log.info('REMOVE name: %s [%s]' % (name, etag)) rest.delete_item('config', name, None, etag, service_url) return True except exceptions.HTTPError as e: log.error('No such config item: %s' % (name)) return False
def _remove_reservation_when_needed(reservation_id, reservation_etag, force_reservation_flag, lab_pool_url): status_ok = False status_message = "" if force_reservation_flag: try: rest_client.delete_item('reservations', reservation_id, None, reservation_etag, lab_pool_url, True, force_reservation_flag) status_ok = True except Exception as e: log.exception(e) status_message = " ### ERROR: '{}'".format(e.message) else: status_ok = True return status_ok, status_message
def _reserve_lab_and_delete_snapshots(lab_name, snapshot_ids, username, force_removal_flag, lab_pool_url): status = 0 status_dict = {} status_ok, lab_id, lab_etag, snaps_dict, status_message = _check_lab_status_for_removal(lab_name, force_removal_flag, lab_pool_url, ERROR_TXT) if status_ok: try: if force_removal_flag is False: reserve_lab_data = {"lab": lab_id, "username": username, "message": RESERVE_LAB_FOR_SNAPSHOT_DELETION_MESSAGE} response = rest_client.post_resource('reservations', reserve_lab_data, lab_pool_url, True) status_ok, status_message = resolve_status_from_response(response) if status_ok is True or force_removal_flag is True: index = 1000 for snapshot_id in snapshot_ids: snap_status_ok = False snap_status_message = "" if snapshot_id in snaps_dict: snap_id = snaps_dict[snapshot_id]['_id'] snap_etag = snaps_dict[snapshot_id]['_etag'] try: response = rest_client.delete_item('states', snap_id, None, snap_etag, lab_pool_url, True, force_removal_flag) snap_status_ok = True except requests.exceptions.HTTPError as e: status = 1 if e.response.status_code == 404: log.exception(e) snap_status_message = ERROR_TXT_SNAPSHOT_DOES_NOT_EXIST_IN_LAB elif e.response.status_code == 412: log.exception(e) snap_status_message = str(e) + ' ' + e.response.text except Exception as e: log.exception(e) snap_status_message = ERROR_TXT + str(e) else: status = 1 snap_status_message = ERROR_TXT_SNAPSHOT_DOES_NOT_EXIST_IN_LAB status_dict[index] = {} status_dict[index][snapshot_id] = {} status_dict[index][snapshot_id]['status_ok'] = snap_status_ok status_dict[index][snapshot_id]['msg'] = snap_status_message index += 1 status_message = _formulate_status_dict_for_printing('admin-remove-snapshots', status_dict, REMOVED_TXT, lab_name) except Exception as e: log.exception(e) status_message = ERROR_TXT + "'{}'".format(e.message) status = 1 else: status = 1 return status, status_message
def _remove_additional_snapshots(existing_snapshots, base_url): message = "" for snap in existing_snapshots: _id = existing_snapshots[snap]['_id'] _etag = existing_snapshots[snap]['_etag'] build_id = existing_snapshots[snap]['build_id'] log.debug("_remove_additional_snapshots(): snapshot_id: '{}' build_id: '{}' _id: '{}' _etag: '{}' base_url: '{}'".format(snap, build_id, _id, _etag, base_url)) try: rest_client.delete_item("states", _id, None, _etag, base_url) message += SNAPSHOT_FORMAT % (build_id, snap, _format_string(REMOVED_TXT, AsciiColors.OKBLACK, True)) except requests.HTTPError as e: if e.response.status_code == 404: debug_message = " ### ERROR: Unable to delete snapshot: snapshot_id: '{}' err_msg: '{}'".format(snap, e.message) log.debug(debug_message) message += debug_message else: log.exception(e) raise e except Exception as e: log.exception(e) raise e return message
def _reserve_lab_and_delete(lab_name, username, force_removal_flag, lab_pool_url): status_ok = False removal_status_ok, lab_id, lab_etag, snaps_dict, status_message = _check_lab_status_for_removal(lab_name, force_removal_flag, lab_pool_url) log.debug('_check_lab_status_for_removal returned: removal_status_ok=%s, lab_id=%s, lab_etag=%s, snaps_dict=%s, status_message=%s', removal_status_ok, lab_id, lab_etag, snaps_dict, status_message) if removal_status_ok: try: if force_removal_flag is False: reserve_lab_data = {"lab": lab_id, "username": username, "message": RESERVE_LAB_FOR_DELETION_MESSAGE} response = rest_client.post_resource('reservations', reserve_lab_data, lab_pool_url, True) status_ok, status_message = resolve_status_from_response(response) if status_ok is True or force_removal_flag is True: response = rest_client.delete_item('labs', lab_id, None, lab_etag, lab_pool_url, force_flag = force_removal_flag) status_ok = True except Exception as e: log.exception(e) status_message = " ### ERROR: '{}'".format(e.message) status_ok = False return status_ok, status_message