def _preserve_state_operation(lab_id, previously_reverted_state, wait_time, set_attr_list, delete_attr_list, append_to_list_list, trigger_and_exit, base_url): err_message = "" build_id = "" _id = "" status_ok = False starttime = time.time() sleep_time = 2 state_data = _formulate_new_state(previously_reverted_state, set_attr_list, delete_attr_list, append_to_list_list) response = rest.post_resource('states', state_data, base_url) if trigger_and_exit: status_ok = True while int(time.time() - starttime) < int(wait_time): log.debug(" *** preserve-state post new state waiting '{}'".format(wait_time)) data = rest.get_item('states', response['_id'], base_url) snapshot_status = data.get('snapshot_status') _id = data['_id'] build = data.get('build') if build: build_id = build['id'] log.debug(" *** build_id: '{}', snapshot_status: '{}'".format(build_id, snapshot_status)) if snapshot_status in FINAL_SNAPSHOT_STATUSES: if snapshot_status == 'available': status_ok = True else: status_ok = False err_message = "Snapshot error!" break time.sleep(sleep_time) return (status_ok, build_id, _id, err_message)
def _check_if_lab_is_in_final_state(lab_id, base_url): lab_in_final_state = False data = rest.get_item('labs', lab_id, base_url) lab_name = data.get('lab_name') lab_status = data.get('status') if lab_status in FINAL_LAB_STATES: lab_in_final_state = True return lab_name, lab_status, lab_in_final_state
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 get_server_config_entry(name=None, service_url=None, **kwargs): ''' retrieve the value of 'name' on LSS server and returns it. If name is None then whole configuration is retrieved. 'raw' switch decides whether to return the value (False) or the while item on server ''' log.debug('get_server_config_entry(name = %s, kwargs = %s)' % (name, kwargs)) try: item = rest.get_item('config', name, service_url) log.debug('get_item(url=%s/config, name=%s): %s' % (service_url, name, item)) except exceptions.HTTPError as e: return None return item.get('value')
def change_lab_reservation(service_url=None, username=None, lab_name=None, duration=None, **kwargs): ''' Changing the reservation of the lab: :param duration * first alternative: numeric value with +|- time in seconds for lab reservation time to be added or reduced plain number set the duration of the reservation * second alternative: date or date + hour and minutes time in datetime format when lab reservation should expire ''' log.info('Changing lab reservation: lab=%s duration=%s' % (lab_name, duration)) # fetch lab lab = rest.get_item('labs', lab_name, service_url) reservation_id = lab['reservation'] log.debug('Lab: %s Reservation: %s' % (lab['lab_name'], reservation_id)) reservation = rest.get_item('reservations', lab['reservation'], service_url) log.debug('Reservation: %s' % (reservation)) updates = _parse_duration_input(duration) if updates is None: log.error('Invalid duration value; %s', duration) return 255 log.debug('Updates: %s Duration: %s ExpireAt: %s', updates, updates.get('duration'), updates.get('ExpireAt')) resp = rest.patch_item('reservations', reservation_id, updates, reservation['_etag'], service_url, raise_error=False) log.debug('RESPONSE: %s', resp) if resp['_status'] == 'ERR': error = resp.get('_error') if error is not None: log.error('HTTP %u %s', error['code'], error['message']) return error['code'] else: issues = resp.get('_issues','') for issue, mess in issues.items(): log.error('%s:\t%s', issue, mess) return 255 return 0
def set_server_config_entry(name, value=None, service_url=None, **kwargs): ''' set the value of 'name' on LSS server to 'value' ''' log.debug('set_server_config_entry(name = %s, value = %s, kwargs = %s)' % (name, value, kwargs)) try: item = rest.get_item('config', name, service_url) etag, old_value = item.get('_etag'), item.get('value') log.info('SET name: %s [%s] to "%s" (old value: "%s")' % (name, etag, value, old_value)) rest.patch_item('config', name, {'value': value}, etag, service_url) except exceptions.HTTPError as e: log.info('item %s not retrieved: %s' % (name, e)) rest.post_resource('config', { 'name': name, 'value': value }, service_url)
def admin_patch_item(href, param_list, service_url=None, username=None, **kwargs): """ Patches 'labs|states|reservations' item as JSON or text for given href. """ log.debug('admin_patch_item(href=%s, param_list=%s, service_url=%s, username=%s, kwargs=%s)' % (href, param_list, service_url, username, kwargs)) exit_code = 0 message = "" status = "ERROR" status_str = "" issues = "" resource, _id = href.split("/") try: exit_code, message = _check_admin_operation_credentials(username) if exit_code == 0: response = rest_client.get_item(resource, _id, service_url) _id = response['_id'] _etag = response['_etag'] status, issues = _modify_item_data(resource, _id, _etag, service_url, param_list) if status in 'OK': show_item(href, service_url, **kwargs) else: exit_code = 1 status_str = status + issues message = "\n *** Operation 'admin-patch-item' ({} '{}') status: {}.\n".format(href, param_list, _format_string(status_str, status, True)) except requests.exceptions.HTTPError as e: exit_code = 1 if e.response.status_code == 404: log.exception(e) message = " ### ERROR: item not found '{}'".format(href) else: message = " ### ERROR: '{}'".format(e.message) raise e except Exception as e: log.exception(e) message = " ### ERROR: '{}'".format(e.message) exit_code = 1 raise e finally: message += " Exit code: '{}'".format(exit_code) _print_out(message, kwargs['output_file']) return exit_code
def _get_labrequest_data(labrequest_id, base_url): data = rest.get_item('labrequests', labrequest_id, base_url) labrequest_status = data.get('status') labrequest_etag = data.get('_etag') lab_id = data.get('lab') return labrequest_status, labrequest_etag, lab_id