def set_value(key, value): encode = ENTRIES[key]["encode"] settings = QSettings( os.path.join(get_default_config_dir(os.environ), "parsec_gui.cfg"), QSettings.IniFormat) settings.setValue(key, encode(value)) settings.sync()
async def amain( backend_address, backend_port, db, blockstore, password, administration_token, email_host, add_random_users, add_random_devices, empty, source_file, ): # Set up the temporary environment click.echo() await new_environment(source_file) # Configure MIME types locally await configure_mime_types() # Keep the environment empty if empty: await generate_gui_config(backend_address) return # Start a local backend if backend_address is None: backend_address = await restart_local_backend(administration_token, backend_port, email_host, db, blockstore) click.echo(f"""\ A fresh backend server is now running: {backend_address} """) else: click.echo(f"""\ Using existing backend: {backend_address} """) # Generate dummy config file for gui await generate_gui_config(backend_address) # Initialize the test organization config_dir = get_default_config_dir(os.environ) alice_device, other_alice_device, bob_device = await initialize_test_organization( config_dir=config_dir, backend_address=backend_address, password=password, administration_token=administration_token, additional_users_number=add_random_users, additional_devices_number=add_random_devices, ) # Report click.echo(f"""\ Mount alice and bob drives using: $ parsec core run -P {password} -D {alice_device.slughash[:3]} # Alice $ parsec core run -P {password} -D {other_alice_device.slughash[:3]} # Alice 2nd device $ parsec core run -P {password} -D {bob_device.slughash[:3]} # Bob """)
def wrapper(config_dir, *args, **kwargs): assert "config" not in kwargs ssl_keyfile = kwargs["ssl_keyfile"] ssl_certfile = kwargs["ssl_certfile"] if ssl_certfile or ssl_keyfile: ssl_context = trio.ssl.create_default_context( trio.ssl.Purpose.SERVER_CLIENT) if ssl_certfile: ssl_context.load_cert_chain(ssl_certfile, ssl_keyfile) else: ssl_context.load_default_certs() kwargs["ssl_context"] = ssl_context configure_logging(kwargs["log_level"], kwargs["log_format"], kwargs["log_file"], kwargs["log_filter"]) config_dir = Path( config_dir) if config_dir else get_default_config_dir(os.environ) config = load_config(config_dir, debug="DEBUG" in os.environ) if config.sentry_url: configure_sentry_logging(config.sentry_url) kwargs["config"] = config return fn(**kwargs)
def get_value(key, default=None): decoder = ENTRIES[key]["decode"] value = QSettings( os.path.join(get_default_config_dir(os.environ), "parsec_gui.cfg"), QSettings.IniFormat).value(key, default) if value is not None: value = decoder(value) return value
def list_devices(config_dir): 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 list_devices(config_dir): 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") click.echo(f"Found {num_devices_display} device(s):") for org, device, cipher in devices: device_display = click.style(f"{org}:{device}", fg="yellow") click.echo(f"{device_display} (cipher: {cipher})")
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 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 wrapper(config_dir, *args, **kwargs): assert "config" not in kwargs configure_logging(kwargs["log_level"], kwargs["log_format"], kwargs["log_file"], kwargs["log_filter"]) config_dir = Path( config_dir) if config_dir else get_default_config_dir(os.environ) config = load_config(config_dir, debug="DEBUG" in os.environ) if config.telemetry_enabled and config.sentry_url: configure_sentry_logging(config.sentry_url) kwargs["config"] = config return fn(**kwargs)
async def main(): # Config config_dir = get_default_config_dir(os.environ) config = load_config(config_dir) devices = list_available_devices(config_dir) key_file = next(key_file for _, device_id, _, key_file in devices if device_id == DEVICE_ID) device = load_device_with_password(key_file, PASSWORD) # Log in async with logged_core_factory(config, device) as core: # Get workspace user_manifest = core.user_fs.get_user_manifest() workspace_entry = user_manifest.workspaces[0] workspace = core.user_fs.get_workspace(workspace_entry.id) # await make_workspace_dir_inconsistent(device, workspace, "/bar") await make_workspace_dir_simple_versions(device, workspace, "/foo")
async def amain( backend_address="ws://*****:*****@laptop", bob_device_id="bob@laptop", other_device_name="pc", alice_workspace="alicews", bob_workspace="bobws", password="******", administrator_token=DEFAULT_ADMINISTRATOR_TOKEN, force=False, ): configure_logging("WARNING") config_dir = get_default_config_dir(os.environ) organization_id = OrganizationID(organization_id) backend_address = BackendAddr(backend_address) alice_device_id = DeviceID(alice_device_id) bob_device_id = DeviceID(bob_device_id) alice_slugid = f"{organization_id}:{alice_device_id}" bob_slugid = f"{organization_id}:{bob_device_id}" # Create organization async with backend_administrator_cmds_factory(backend_address, administrator_token) as cmds: bootstrap_token = await cmds.organization_create(organization_id) organization_bootstrap_addr = BackendOrganizationBootstrapAddr.build( backend_address, organization_id, bootstrap_token ) # Bootstrap organization and Alice user async with backend_anonymous_cmds_factory(organization_bootstrap_addr) as cmds: root_signing_key = SigningKey.generate() root_verify_key = root_signing_key.verify_key organization_addr = organization_bootstrap_addr.generate_organization_addr(root_verify_key) alice_device = generate_new_device(alice_device_id, organization_addr) save_device_with_password(config_dir, alice_device, password, force=force) now = pendulum.now() certified_user = certify_user( None, root_signing_key, alice_device.user_id, alice_device.public_key, now ) certified_device = certify_device( None, root_signing_key, alice_device_id, alice_device.verify_key, now ) await cmds.organization_bootstrap( organization_bootstrap_addr.organization_id, organization_bootstrap_addr.bootstrap_token, root_verify_key, certified_user, certified_device, ) # Create a workspace for Alice config = load_config(config_dir, debug="DEBUG" in os.environ) async with logged_core_factory(config, alice_device) as core: await core.fs.workspace_create(f"/{alice_workspace}") # Register a new device for Alice token = generate_invitation_token() other_alice_device_id = DeviceID("@".join((alice_device.user_id, other_device_name))) other_alice_slugid = f"{organization_id}:{other_alice_device_id}" async def invite_task(): async with backend_cmds_factory( alice_device.organization_addr, alice_device.device_id, alice_device.signing_key ) as cmds: await invite_and_create_device(alice_device, cmds, other_device_name, token) async def claim_task(): async with backend_anonymous_cmds_factory(alice_device.organization_addr) as cmds: other_alice_device = await retry(claim_device, cmds, other_alice_device_id, token) save_device_with_password(config_dir, other_alice_device, password, force=force) async with trio.open_nursery() as nursery: nursery.start_soon(invite_task) nursery.start_soon(claim_task) # Invite Bob in token = generate_invitation_token() async def invite_task(): async with backend_cmds_factory( alice_device.organization_addr, alice_device.device_id, alice_device.signing_key ) as cmds: await invite_and_create_user( alice_device, cmds, bob_device_id.user_id, token, is_admin=True ) async def claim_task(): async with backend_anonymous_cmds_factory(alice_device.organization_addr) as cmds: bob_device = await retry(claim_user, cmds, bob_device_id, token) save_device_with_password(config_dir, bob_device, password, force=force) async with trio.open_nursery() as nursery: nursery.start_soon(invite_task) nursery.start_soon(claim_task) # Create bob workspace and share with Alice bob_device = load_device_with_password( config.config_dir, organization_id, bob_device_id, password ) async with logged_core_factory(config, bob_device) as core: await core.fs.workspace_create(f"/{bob_workspace}") await core.fs.share(f"/{bob_workspace}", alice_device_id.user_id) # Share Alice workspace with bob async with logged_core_factory(config, alice_device) as core: await core.fs.share(f"/{alice_workspace}", bob_device_id.user_id) # Print out click.echo( f""" Mount alice and bob drives using: $ parsec core run -P {password} -D {alice_slugid} $ parsec core run -P {password} -D {other_alice_slugid} $ parsec core run -P {password} -D {bob_slugid} """ )
async def initialize_test_organization( backend_address, organization_id, alice_device_id, bob_device_id, other_device_name, alice_workspace, bob_workspace, password, administration_token, force, ): configure_logging("WARNING") config_dir = get_default_config_dir(os.environ) alice_slugid = f"{organization_id}:{alice_device_id}" bob_slugid = f"{organization_id}:{bob_device_id}" # Create organization async with backend_administration_cmds_factory( backend_address, administration_token) as cmds: rep = await cmds.organization_create(organization_id) assert rep["status"] == "ok" bootstrap_token = rep["bootstrap_token"] organization_bootstrap_addr = BackendOrganizationBootstrapAddr.build( backend_address, organization_id, bootstrap_token) # Bootstrap organization and Alice user async with backend_anonymous_cmds_factory( organization_bootstrap_addr) as cmds: root_signing_key = SigningKey.generate() root_verify_key = root_signing_key.verify_key organization_addr = organization_bootstrap_addr.generate_organization_addr( root_verify_key) alice_device = generate_new_device(alice_device_id, organization_addr, True) save_device_with_password(config_dir, alice_device, password, force=force) now = pendulum.now() user_certificate = UserCertificateContent( author=None, timestamp=now, user_id=alice_device.user_id, public_key=alice_device.public_key, is_admin=True, ).dump_and_sign(author_signkey=root_signing_key) device_certificate = DeviceCertificateContent( author=None, timestamp=now, device_id=alice_device.device_id, verify_key=alice_device.verify_key, ).dump_and_sign(author_signkey=root_signing_key) rep = await cmds.organization_bootstrap( organization_bootstrap_addr.organization_id, organization_bootstrap_addr.token, root_verify_key, user_certificate, device_certificate, ) assert rep["status"] == "ok" # Create a workspace for Alice config = load_config(config_dir, debug="DEBUG" in os.environ) async with logged_core_factory(config, alice_device) as core: alice_ws_id = await core.user_fs.workspace_create(f"{alice_workspace}") await core.user_fs.sync() # Register a new device for Alice token = generate_invitation_token() other_alice_device_id = DeviceID( f"{alice_device.user_id}@{other_device_name}") other_alice_slugid = f"{organization_id}:{other_alice_device_id}" async def invite_task(): await invite_and_create_device(alice_device, other_device_name, token) other_alice_device = None async def claim_task(): nonlocal other_alice_device other_alice_device = await retry_claim(claim_device, alice_device.organization_addr, other_alice_device_id, token) save_device_with_password(config_dir, other_alice_device, password, force=force) async with trio.open_service_nursery() as nursery: nursery.start_soon(invite_task) nursery.start_soon(claim_task) # Invite Bob in token = generate_invitation_token() bob_device = None async def invite_task(): await invite_and_create_user(alice_device, bob_device_id.user_id, token, is_admin=False) async def claim_task(): nonlocal bob_device bob_device = await retry_claim(claim_user, alice_device.organization_addr, bob_device_id, token) save_device_with_password(config_dir, bob_device, password, force=force) async with trio.open_service_nursery() as nursery: nursery.start_soon(invite_task) nursery.start_soon(claim_task) # Create bob workspace and share with Alice async with logged_core_factory(config, bob_device) as core: bob_ws_id = await core.user_fs.workspace_create(f"{bob_workspace}") await core.user_fs.workspace_share(bob_ws_id, alice_device_id.user_id, WorkspaceRole.MANAGER) # Share Alice workspace with bob async with logged_core_factory(config, alice_device) as core: await core.user_fs.workspace_share(alice_ws_id, bob_device_id.user_id, WorkspaceRole.MANAGER) # Synchronize every device for device in (alice_device, other_alice_device, bob_device): async with logged_core_factory(config, device) as core: await core.user_fs.process_last_messages() await core.user_fs.sync() return alice_slugid, other_alice_slugid, bob_slugid