def _create_supervisor_state_file(run_dir): try: i, t = _load_in_json(run_dir) except Exception as e: if pykern.pkio.exception_is_not_found(e): return raise u = simulation_db.uid_from_dir_name(run_dir) sirepo.auth.cfg.logged_in_user = u c = sim_data.get_class(i.simulationType) d = PKDict( computeJid=c.parse_jid(i, u), computeJobHash=c.compute_job_hash( i), # TODO(e-carlin): Another user cookie problem computeJobSerial=t, computeJobStart=t, error=None, history=[], isParallel=c.is_parallel(i), simulationId=i.simulationId, simulationType=i.simulationType, uid=u, ) d.pkupdate( jobRunMode=job.PARALLEL if d.isParallel else job.SEQUENTIAL, nextRequestSeconds=c.poll_seconds(i), ) _add_compute_status(run_dir, d) if d.status not in (job.COMPLETED, job.CANCELED): return if d.isParallel: _add_parallel_status(i, c, run_dir, d) util.json_dump(d, path=_db_file(d.computeJid))
def purge_users(days=180, confirm=False): """Remove old users from db which have not registered. Args: days (int): maximum days of untouched files (old is mtime > days) confirm (bool): delete the directories if True (else don't delete) [False] Returns: list: directories removed (or to remove if confirm) """ days = int(days) assert days >= 1, \ '{}: days must be a positive integer' server.init() uids = auth_db.all_uids() now = datetime.datetime.utcnow() to_remove = [] for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; if simulation_db.uid_from_dir_name(d) in uids: continue for f in pkio.walk_tree(d): if (now - now.fromtimestamp(f.mtime())).days <= days: break else: to_remove.append(d) if confirm: pkio.unchecked_remove(*to_remove) return to_remove
def create_examples(): """Adds missing app examples to all users. """ from pykern import pkio from sirepo import feature_config from sirepo import server from sirepo import simulation_db from sirepo import cookie server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; uid = simulation_db.uid_from_dir_name(d) cookie.init_mock(uid) for sim_type in feature_config.cfg.sim_types: simulation_db.verify_app_directory(sim_type) names = map( lambda x: x['name'], simulation_db.iterate_simulation_datafiles(sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, })) for s in simulation_db.examples(sim_type): if s.models.simulation.name not in names: simulation_db.save_new_example(s)
def api_blueskyAuth(): req = http_request.parse_json() auth_hash(req, verify=True) sid = req.simulationId sim_type = req.simulationType path = simulation_db.find_global_simulation( sim_type, sid, checked=True, ) cookie.set_user(simulation_db.uid_from_dir_name(path)) return http_reply.gen_json_ok(dict( data=simulation_db.open_json_file(req.simulationType, sid=req.simulationId), schema=simulation_db.get_schema(req.simulationType), ))
def auth_login(req): from sirepo import server if cfg.auth_secret: auth_hash(req, verify=True) # DEPRECATED elif not server.cfg.enable_bluesky: util.raise_not_found('bluesky is not enabled') sid = req.simulationId sim_type = req.simulationType path = simulation_db.find_global_simulation( sim_type, sid, checked=True, ) server.session_user(simulation_db.uid_from_dir_name(path))
def api_blueskyAuth(): req = http_request.parse_json() auth_hash(req, verify=True) sid = req.simulationId sim_type = req.simulationType path = simulation_db.find_global_simulation( sim_type, sid, checked=True, ) cookie.set_user(simulation_db.uid_from_dir_name(path)) return http_reply.gen_json_ok( dict( data=simulation_db.open_json_file(req.simulationType, sid=req.simulationId), schema=simulation_db.get_schema(req.simulationType), ))
def api_authBlueskyLogin(): req = sirepo.http_request.parse_post(id=True) auth_hash(req.req_data, verify=True) path = simulation_db.find_global_simulation( req.type, req.id, checked=True, ) sirepo.auth.login( this_module, uid=simulation_db.uid_from_dir_name(path), # do not supply sim_type (see auth.login) ) return sirepo.http_reply.gen_json_ok( PKDict( data=simulation_db.open_json_file(req.type, sid=req.id), schema=simulation_db.get_schema(req.type), ), )
def create_examples(): """Adds missing app examples to all users. """ server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; uid = simulation_db.uid_from_dir_name(d) auth.init_mock(uid) for sim_type in feature_config.cfg.sim_types: simulation_db.verify_app_directory(sim_type) names = map( lambda x: x['name'], simulation_db.iterate_simulation_datafiles(sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, })) for example in simulation_db.examples(sim_type): if example.models.simulation.name not in names: _create_example(example)
def api_authBlueskyLogin(): req = http_request.parse_json() auth_hash(req, verify=True) sid = req.simulationId sim_type = req.simulationType path = simulation_db.find_global_simulation( sim_type, sid, checked=True, ) r = auth.login( this_module, uid=simulation_db.uid_from_dir_name(path), ) if r: return r return http_reply.gen_json_ok(dict( data=simulation_db.open_json_file(req.simulationType, sid=req.simulationId), schema=simulation_db.get_schema(req.simulationType), ))
def create_examples(): """Adds missing app examples to all users. """ server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name().join('*')): if _is_src_dir(d): continue uid = simulation_db.uid_from_dir_name(d) auth.set_user_for_utils(uid) for sim_type in feature_config.cfg().sim_types: simulation_db.verify_app_directory(sim_type) names = [ x.name for x in simulation_db.iterate_simulation_datafiles( sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, }) ] for example in simulation_db.examples(sim_type): if example.models.simulation.name not in names: _create_example(example)
def create_examples(): """Adds missing app examples to all users""" import sirepo.auth_db import sirepo.server sirepo.server.init() for d in pkio.sorted_glob(simulation_db.user_path().join('*')): if _is_src_dir(d): continue; uid = simulation_db.uid_from_dir_name(d) with sirepo.auth_db.session_and_lock(), \ auth.set_user_outside_of_http_request(uid): for sim_type in feature_config.cfg().sim_types: simulation_db.verify_app_directory(sim_type) names = [x.name for x in simulation_db.iterate_simulation_datafiles( sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, })] for example in simulation_db.examples(sim_type): if example.models.simulation.name not in names: _create_example(example)
def purge_guest_users(days=180, confirm=False): """Remove old users from db which have not registered. Args: days (int): maximum days of untouched files (old is mtime > days) confirm (bool): delete the directories if True (else don't delete) [False] Returns: (list, list): dirs and uids of removed guest users (or to remove if confirm) """ days = int(days) assert days >= 1, \ '{}: days must be a positive integer' server.init() from sirepo import srtime guest_uids = auth.guest_uids() now = srtime.utc_now() dirs_and_uids = {} for d in pkio.sorted_glob(simulation_db.user_dir_name().join('*')): uid = simulation_db.uid_from_dir_name(d) if _is_src_dir(d): continue if uid not in guest_uids: continue for f in pkio.walk_tree(d): if (now - now.fromtimestamp(f.mtime())).days <= days: break else: dirs_and_uids[d] = uid if confirm: pkio.unchecked_remove(*dirs_and_uids.keys()) auth_db.UserRegistration.delete_all_for_column_by_values( 'uid', dirs_and_uids.values()) return dirs_and_uids
def purge_users(days=180, confirm=False): """Remove old users from db which have not registered. Args: days (int): maximum days of untouched files (old is mtime > days) confirm (bool): delete the directories if True (else don't delete) [False] Returns: list: directories removed (or to remove if confirm) """ from pykern import pkio from sirepo import server from sirepo import simulation_db from sirepo import api_auth import datetime days = int(days) assert days >= 1, \ '{}: days must be a positive integer' server.init() uids = api_auth.all_uids() now = datetime.datetime.utcnow() to_remove = [] for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; #TODO(pjm): need to skip special "src" user if simulation_db.uid_from_dir_name(d) in uids: continue for f in pkio.walk_tree(d): if (now - now.fromtimestamp(f.mtime())).days <= days: break else: to_remove.append(d) if confirm: pkio.unchecked_remove(*to_remove) return to_remove