def greet_invitation(config, device, token, **kwargs): """ Greet a new device or user into the organization """ with cli_exception_handler(config.debug): # Disable task monitoring given user prompt will block the coroutine trio_run(_greet_invitation, config, device, token)
def export_recovery_device(config: CoreConfig, device: LocalDevice, output: Path, **kwargs) -> None: """ Create a new recovery device for the user. """ with cli_exception_handler(config.debug): trio_run(_export_recovery_device, config, device, output)
def migrate(db, debug, dry_run): """ Updates the database schema """ result_colors = { "error": "red", "new_apply": "green", "to_apply": "white", "already_applied": "white", } with cli_exception_handler(debug): migrations = _sorted_file_migrations() async def _migrate(db): async with spinner("Migrate"): result = await migrate_db(db, migrations, dry_run) for key, values in zip(result._fields, result): color = result_colors.get(key, "white") if key == "error": if values: _echo_migration(values[0], color) raise click.ClickException(values[1]) else: is_done = False for value in values: if key in ["already_applied", "new_apply"]: is_done = True else: is_done = False _echo_migration(value, color, is_done) trio_run(_migrate, db, use_asyncio=True)
def create_workspace(config: CoreConfig, device: LocalDevice, name: str, **kwargs) -> None: """ Create a new workspace for the given device. """ with cli_exception_handler(config.debug): trio_run(_create_workspace, config, device, EntryName(name))
def claim_invitation(config, addr, password, **kwargs): """ Claim a device or user from a invitation """ with cli_exception_handler(config.debug): # Disable task monitoring given user prompt will block the coroutine trio_run(_claim_invitation, config, addr, password)
def bootstrap_organization(config, addr, password, force, **kwargs): """ Configure the organization and register it first user&device. """ with cli_exception_handler(config.debug): # Disable task monitoring given user prompt will block the coroutine trio_run(_bootstrap_organization, config, addr, password, force)
def run_gui( config: CoreConfig, url: str, diagnose: bool, sentry_dsn: Optional[str], sentry_environment: str, **kwargs, ) -> None: """ Run parsec GUI """ # Necessary for DialogInProcess since it's not the default on windows # This method should only be called once which is why we do it here. multiprocessing.set_start_method("spawn") with cli_exception_handler(config.debug): if config.telemetry_enabled and sentry_dsn: configure_sentry_logging(dsn=sentry_dsn, environment=sentry_environment) config = config.evolve(mountpoint_enabled=True) try: _run_gui(config, start_arg=url, diagnose=diagnose) except KeyboardInterrupt: click.echo("bye ;-)")
def claim_user(config, addr, device, token, password, pkcs11, **kwargs): if password and pkcs11: raise SystemExit("Password are PKCS11 options are exclusives.") debug = "DEBUG" in os.environ with cli_exception_handler(debug): trio.run(_claim_user, config, addr, token, device, password, pkcs11)
def invite_user( config: CoreConfig, device: LocalDevice, email: str, send_email: bool, **kwargs ) -> None: """ Create new user invitation """ with cli_exception_handler(config.debug): trio_run(_invite_user, config, device, email, send_email)
def share_workspace(config, device, workspace_name, user_id, role, **kwargs): """ Create a new workspace for the given device. """ role = WORKSPACE_ROLE_CHOICES[role] with cli_exception_handler(config.debug): trio_run(_share_workspace, config, device, workspace_name, user_id, role)
def run_mountpoint(config, device, mountpoint, **kwargs): """ Expose device's parsec drive on the given mountpoint. """ if mountpoint: mountpoint = Path(mountpoint) with cli_exception_handler(config.debug): trio.run(_run_mountpoint, config, device, mountpoint)
def run_mountpoint(config, device, mountpoint, timestamp, **kwargs): """ Expose device's parsec drive on the given mountpoint. """ config = config.evolve(mountpoint_enabled=True) if mountpoint: config = config.evolve(mountpoint_base_dir=Path(mountpoint)) with cli_exception_handler(config.debug): trio_run(_run_mountpoint, config, device, timestamp)
def status_organization( organization_id: OrganizationID, addr: BackendAddr, administration_token: str, debug: bool, **kwargs, ) -> None: with cli_exception_handler(debug): trio_run(_status_organization, organization_id, addr, administration_token)
def list_devices(config_dir: Path, debug: bool, **kwargs) -> None: with cli_exception_handler(debug): config_dir = Path( config_dir) if config_dir else get_default_config_dir(os.environ) devices = list_available_devices(config_dir) num_devices_display = click.style(str(len(devices)), fg="green") config_dir_display = click.style(str(config_dir), fg="yellow") click.echo( f"Found {num_devices_display} device(s) in {config_dir_display}:") click.echo(format_available_devices(devices))
def pki_enrollment_submit( config: CoreConfig, enrollment_address: BackendPkiEnrollmentAddr, device_label: DeviceLabel, force: bool, **kwargs, ): """Submit a new PKI enrollment""" with cli_exception_handler(config.debug): _ensure_pki_enrollment_available() trio_run(_pki_enrollment_submit, config, enrollment_address, device_label, force)
def bootstrap_organization(device, addr, config_dir, force, password): """ Configure the organization and register it first user&device. """ config_dir = Path(config_dir) if config_dir else get_default_config_dir(os.environ) debug = "DEBUG" in os.environ configure_logging(log_level="DEBUG" if debug else "WARNING") with cli_exception_handler(debug): trio_run(_bootstrap_organization, debug, device, addr, config_dir, force, password)
def claim_device(config, addr, token, password, **kwargs): if token and addr.token: raise SystemExit("token already specified in the addr option") token = token or addr.token if not token: raise SystemExit("Missing token value") debug = "DEBUG" in os.environ with cli_exception_handler(debug): trio_run(_claim_device, config, addr.to_organization_addr(), addr.device_id, token, password)
def cancel_invitation( config: CoreConfig, device: LocalDevice, token_or_url: Union[BackendInvitationAddr, InvitationToken], **kwargs, ) -> None: """ Cancel invitation """ with cli_exception_handler(config.debug): token = extract_token_from_token_or_url(token_or_url, device) trio_run(_cancel_invitation, config, device, token)
def claim_invitation( config: CoreConfig, addr: BackendInvitationAddr, save_device_with_selected_auth: Callable, **kwargs, ) -> None: """ Claim a device or user from a invitation """ with cli_exception_handler(config.debug): # Disable task monitoring given user prompt will block the coroutine trio_run(_claim_invitation, config, addr, save_device_with_selected_auth)
def greet_invitation( config: CoreConfig, device: LocalDevice, token_or_url: Union[BackendInvitationAddr, InvitationToken], **kwargs, ) -> None: """ Greet a new device or user into the organization """ with cli_exception_handler(config.debug): token = extract_token_from_token_or_url(token_or_url, device) # Disable task monitoring given user prompt will block the coroutine trio_run(_greet_invitation, config, device, token)
def init_cmd(db, debug): """ Initialize a new backend's PostgreSQL database. """ with cli_exception_handler(debug): async def _init_db(db): async with spinner("Initializing database"): already_initialized = await init_db(db) if already_initialized: click.echo("Database already initialized, nothing to do.") trio_run(_init_db, db, use_asyncio=True)
def share_workspace( config: CoreConfig, device: LocalDevice, workspace_name: str, user_id: Optional[UserID], recipiant: Optional[str], role: RealmRole, **kwargs, ) -> None: """ Share a workspace with someone """ role = WORKSPACE_ROLE_CHOICES[role] with cli_exception_handler(config.debug): trio_run(_share_workspace, config, device, workspace_name, user_id, recipiant, role)
def init_cmd(db, force): """ Initialize a new backend's PostgreSQL database. """ if not (db.startswith("postgresql://") or db.startswith("postgres://")): raise SystemExit("Can only initialize a PostgreSQL database.") debug = "DEBUG" in os.environ with cli_exception_handler(debug): async def _init_db(db, force): async with spinner("Initializing database"): already_initialized = await init_db(db, force) if already_initialized: click.echo("Database already initialized, nothing to do.") trio_asyncio.run(_init_db, db, force)
def run_mountpoint( config: CoreConfig, device: LocalDevice, mountpoint: Path, timestamp: Optional[DateTime], **kwargs, ) -> None: """ Expose device's parsec drive on the given mountpoint. """ config = config.evolve(mountpoint_enabled=True) if mountpoint: config = config.evolve(mountpoint_base_dir=Path(mountpoint)) with cli_exception_handler(config.debug): try: trio_run(_run_mountpoint, config, device, timestamp) except KeyboardInterrupt: click.echo("bye ;-)")
def pki_enrollment_poll( config: CoreConfig, enrollment_id: Optional[str], dry_run: bool, save_device_with_selected_auth: Callable, finalize: Sequence[str], **kwargs, ): """Check status of the pending PKI enrollments locally available""" with cli_exception_handler(config.debug): _ensure_pki_enrollment_available() trio_run( _pki_enrollment_poll, config, enrollment_id, dry_run, save_device_with_selected_auth, finalize, )
def import_recovery_device( config: CoreConfig, file: Path, passphrase: str, device_label: DeviceLabel, save_device_with_selected_auth: Callable, **kwargs, ) -> None: """ Create a new device from a .psrk recovery device file. """ with cli_exception_handler(config.debug): trio_run( _import_recovery_device, config, file, passphrase, device_label, save_device_with_selected_auth, )
def pki_enrollment_review_pendings( config: CoreConfig, device: LocalDevice, list_only: bool, accept: Sequence[str], reject: Sequence[str], **kwargs, ): """ Show the pending PKI enrollments and accept/reject them """ if list_only and (accept or reject): raise RuntimeError("Cannot use --list-only together with --accept/--reject") if set(accept) & set(reject): raise RuntimeError("An enrollment ID cannot be both marked as --accept and --reject") with cli_exception_handler(config.debug): _ensure_pki_enrollment_available() trio_run(_pki_enrollment_review_pendings, config, device, list_only, accept, reject)
def bootstrap_organization(config: CoreConfig, addr: BackendOrganizationBootstrapAddr, device_label: Optional[DeviceLabel], human_label: Optional[str], human_email: Optional[str], save_device_with_selected_auth: Callable, **kwargs) -> None: """ Configure the organization and register it first user&device. """ with cli_exception_handler(config.debug): # Disable task monitoring given user prompt will block the coroutine trio_run( _bootstrap_organization, config, addr, device_label, human_label, human_email, save_device_with_selected_auth, )
def migrate(db, debug, dry_run): """ Updates the database schema """ with cli_exception_handler(debug): migrations = retrieve_migrations() async def _migrate(db): async with spinner("Migrate"): result = await apply_migrations(db, migrations, dry_run) for migration in result.already_applied: click.secho(f"{migration.file_name} (already applied)", fg="white") for migration in result.new_apply: click.secho(f"{migration.file_name} {ok}", fg="green") if result.error: migration, msg = result.error click.secho(f"{migration.file_name} {ko}: {msg}", fg="red") trio_run(_migrate, db, use_asyncio=True)
def status_organization(name, addr, administration_token): debug = "DEBUG" in os.environ configure_logging(log_level="DEBUG" if debug else "WARNING") with cli_exception_handler(debug): trio_run(_status_organization, name, addr, administration_token)