def update_resource(resource_id, content): try: LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [update_resource] (1) Updating resource [" + resource_id + "] with content [" + str(content) + "] ... ") # complete map and update resource content.update(common_update_map_fields()) LOG.debug( "[usermgnt.data.mF2C.cimi] [update_resource] (2) Updating resource [" + resource_id + "] with content [" + str(content) + "] ... ") res = requests.put(config.dic['CIMI_URL'] + '/' + resource_id, headers=CIMI_HEADER, verify=False, json=content) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [update_resource] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200: return get_resource_by_id(resource_id) LOG.error( "[usermgnt.data.mF2C.cimi] [update_resource] Request failed: " + str(res.status_code) + "; Returning None ...") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [update_resource] Exception; Returning None ..." ) return None
def __thr_create_sharing_model(data): time.sleep(65) try: LOG.info("[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> Creating SHARING-MODEL [" + str(data) + "] in current device ...") created = False while not created: LOG.log(TRACE, '[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> executing ...') # get device_id device_id = data_adapter.get_current_device_id() if device_id == -1: LOG.log(TRACE, '[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> trying again in 45s ...') time.sleep(45) else: # create sharing-model data['device_id'] = device_id up = um_sharing_model.init_sharing_model(data) if up is not None: LOG.info('[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> sharing-model created! ...') created = True else: LOG.error('[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> sharing-model not created! Trying again in 60s ...') time.sleep(30) LOG.info('[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> thread finishes') except: LOG.exception('[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> Exception: Error while initializing application')
def add_resource(resource_name, content): try: LOG.debug( "[usermgnt.data.mF2C.cimi] [add_resource] Adding new resource to [" + resource_name + "] with content [" + str(content) + "] ... ") # complete map and update resource content.update(common_new_map_fields()) #content.pop("user_id", None) res = requests.post(config.dic['CIMI_URL'] + '/' + resource_name, headers=CIMI_HEADER, verify=False, json=content) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [add_resource] response: " + str(res) + ", " + str(res.json())) if res.status_code == 201: return get_resource_by_id(res.json()['resource-id']) LOG.error("[usermgnt.data.mF2C.cimi] [add_resource] Request failed: " + str(res.status_code) + "; Returning None ...") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [add_resource] Exception; Returning None ..." ) return None
def get_agent_info(): try: res = requests.get(config.dic['CIMI_URL'] + "/agent", headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [get_agent_info] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and res.json()['count'] == 0: LOG.warning( "[usermgnt.data.mF2C.cimi] [get_agent_info] 'agent' not found") return -1 elif res.status_code == 200: return res.json()['agents'][0] LOG.warning( "[usermgnt.data.mF2C.cimi] [get_agent_info] 'agent' not found; Returning -1 ..." ) return -1 except: LOG.error( "[usermgnt.data.mF2C.cimi] [get_agent_info] Exception; Returning None ..." ) return None
def __thr_create_user_profile(data): time.sleep(70) try: LOG.info("[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> Creating USER-PROFILE [" + str(data) + "] in current device ...") created = False while not created: LOG.log(TRACE, '[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> executing ...') # get device_id device_id = data_adapter.get_current_device_id() if device_id == -1: LOG.log(TRACE, '[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> trying again in 45s ...') time.sleep(45) else: # create user-profile data['device_id'] = device_id up = um_profiling.create_user_profile(data) if up is not None: LOG.info('[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> user-profile created! ...') created = True else: LOG.error('[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> user-profile not created! Trying again in 60s ...') time.sleep(30) LOG.info('[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> thread finishes') except: LOG.exception('[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> Exception: Error while initializing application')
def __up_policies(): global message try: LOG.log( TRACE, "[usermgnt.modules.policies] [__up_policies] Checking policies ..." ) # get current profile user_profile = data_adapter.get_current_user_profile() if user_profile is None: LOG.error( '[usermgnt.modules.policies] [__up_policies] user_profile not found / error' ) elif user_profile == -1: LOG.warning( '[usermgnt.modules.policies] [__up_policies] user_profile not found' ) else: LOG.log( TRACE, '[usermgnt.modules.policies] [__up_policies] user_profile found. checking values ...' ) if user_profile['resource_contributor']: message = message + "ALLOWED: resource_contributor is set to TRUE; " return True message = message + "NOT ALLOWED: resource_contributor is set to FALSE; " except: LOG.exception('[usermgnt.modules.policies] [__up_policies] Exception') return False
def read_user_id(): LOG.log(TRACE, "[usermgnt.data.app.volume] [read_user_id] Reading user_id value from local VOLUME [" + config.dic['UM_WORKING_DIR_VOLUME'] + "] ...") try: with open(config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt", "r") as file: return file.readline() except: LOG.error("[usermgnt.data.app.volume] [read_user_id] UM could not save 'user_id' value in " + config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt. Returning None ...") return None
def save_user_id(user_id): try: LOG.log(TRACE, "[usermgnt.app.volume] [save_user_id] Storing user_id [" + user_id + "] value in local VOLUME [" + config.dic['UM_WORKING_DIR_VOLUME'] + "] ...") with open(config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt", "w") as file: file.write(user_id) except: LOG.error("[usermgnt.data.app.volume] [save_user_id] UM could not save 'user_id' value in " + config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt. Returning None ...") return None
def __sm_policies(): global message try: LOG.log( TRACE, "[usermgnt.modules.policies] [__sm_policies] Checking policies ..." ) # get current sharing model sharing_model = data_adapter.get_current_sharing_model() if sharing_model is None: LOG.error( '[usermgnt.modules.policies] [__sm_policies] sharing_model not found / error' ) elif sharing_model == -1: LOG.warning( '[usermgnt.modules.policies] [__sm_policies] sharing_model not found' ) else: LOG.log( TRACE, '[usermgnt.modules.policies] [__sm_policies] sharing_model found. checking values ...' ) # 1. battery_level battery_level = data_adapter.get_power() LOG.log( TRACE, "[usermgnt.modules.policies] [__sm_policies] 1. [battery_level=" + str(battery_level) + "] ... [sharing_model('battery_limit')=" + str(sharing_model['battery_limit']) + "]") if battery_level is None or battery_level == -1 or battery_level > sharing_model[ 'battery_limit']: # 2. total services running apps_running = data_adapter.get_total_services_running() LOG.log( TRACE, "[usermgnt.modules.policies] [__sm_policies] 2. [apps_running=" + str(apps_running) + "] ... [sharing_model('max_apps')=" + str(sharing_model['max_apps']) + "]") if apps_running >= sharing_model['max_apps']: message = message + "NOT ALLOWED: apps_running >= max_apps; " return False return True else: message = message + "NOT ALLOWED: battery_level < battery_limit; " return False except: LOG.exception('[usermgnt.modules.policies] [__sm_policies] Exception') return False
def get_resource_by_id(resource_id): try: res = requests.get(config.dic['CIMI_URL'] + "/" + resource_id, headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [get_resource_by_id] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and 'id' in res.json(): return res.json() LOG.error( "[usermgnt.data.mF2C.cimi] [get_resource_by_id] Request failed: " + res.status_code + "; Returning None ...") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [get_resource_by_id] Exception; Returning None ..." ) return None
def operation(data): LOG.info( "[usermgnt.modules.um_assessment] [operation] Execute operation: " + str(data)) if 'operation' not in data: LOG.error( '[usermgnt.modules.um_assessment] [operation] Exception - parameter not found' ) return common.gen_response(406, 'parameter not found: operation', 'data', str(data)) if data['operation'] == 'start': return __start() elif data['operation'] == 'stop': return __stop() else: LOG.error('[usermgnt.modules.um_assessment] [operation] Operation ' + data['operation'] + ' not defined / implemented') return common.gen_response(500, 'operation not defined / implemented', 'operation', data['operation'])
def __daemon(): global execute try: while execute: LOG.debug( '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> executing ...' ) device_id = None user_id = None # 1. get current profile user_profile = data_adapter.get_current_user_profile() if user_profile is None: LOG.error( '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> user_profile not found / error' ) elif user_profile == -1: LOG.warning( '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> user_profile not found' ) else: user_id = user_profile['user_id'] device_id = user_profile['device_id'] LOG.log( TRACE, '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> user_profile found' ) # 2. get current sharing model sharing_model = data_adapter.get_current_sharing_model() if sharing_model is None: LOG.error( '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> sharing_model not found / error' ) elif sharing_model == -1: LOG.warning( '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> sharing_model not found' ) else: user_id = sharing_model['user_id'] device_id = sharing_model['device_id'] LOG.log( TRACE, '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> sharing_model found' ) if not user_id is None and not device_id is None: LOG.log( TRACE, '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> checking values ...' ) # 3. Get information: # - battery battery_level = data_adapter.get_power() # battery_level = 50 # TODO # - total services running total_services = data_adapter.get_total_services_running() # 4. check information and send warning to Lifecycle if needed result = __check_resources_used(user_profile, sharing_model, battery_level, total_services) if not result: LOG.debug( "[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> no violations: result: " + str(result)) else: LOG.debug( "[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> violations found: result: " + str(result)) LOG.log( TRACE, '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> generating warning / sending notification ...' ) mf2c.send_warning(user_id, device_id, user_profile, sharing_model, result) else: LOG.warning( '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> cannot check values' ) # wait 300 seconds LOG.debug( '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> Waiting 5m (300s) for next execution ...' ) time.sleep(300) except: LOG.exception( '[usermgnt.modules.assessment] [__daemon] << Assessment Process Thread >> Exception' )
app = Flask(__name__) CORS(app) # API DOC api = swagger.docs( Api(app), apiVersion=cfg.dic['VERSION'], api_spec_url=cfg.dic['API_DOC_URL'], produces=["application/json", "text/html"], swaggerVersion="1.2", description='mF2C - User Management REST API - version ' + cfg.dic['VERSION'], basePath='http://*****:*****@app.route('/api/v2', methods=['GET']) @app.route('/api/v2/', methods=['GET']) def default_route():