def clone(context, instance, user=get_local_user(), branch=BRANCH): """ Clone the project repository into a host instance. """ local = False no_stack = None no_compose = False env_path = f"{HOST_PATH}/{instance}/.envs" env_file = f"{instance}.tar.gz" command = f"tar czvf .envs/{env_file} .envs/.{instance}" run_command(context, user, local, instance, no_stack, command, no_compose) with get_connection(user, HOST) as c: with c.cd(f"{HOST_PATH}"): c.run(f"mkdir -p {HOST_PATH}/{instance}") remote = True command = f"git clone {REPOSITORY} . && git checkout {branch}" run_command(context, user, remote, instance, no_stack, command, no_compose) with get_connection(user, HOST) as c: c.put(f".envs/{env_file}", env_path) command = f"tar zxvf {env_path}/{env_file} && rm {env_path}/{env_file}" run_command(context, user, remote, instance, no_stack, command, no_compose) command = f"mkdir -p {PROJECT}/media" run_command(context, user, remote, instance, no_stack, command, no_compose)
def compose( context, command, user=get_local_user(), remote=False, instance=None, stack=None ): """ Run a raw compose command. """ run_command(context, user, remote, instance, stack, command)
def submits_connect_timeout(self, client): Connection('host', connect_timeout=27).open() client.connect.assert_called_with( username=get_local_user(), hostname='host', port=22, timeout=27, )
def calls_SSHClient_connect(self, client): "calls paramiko.SSHClient.connect() with correct args" Connection('host').open() client.connect.assert_called_with( username=get_local_user(), hostname='host', port=22, )
def calls_SSHClient_connect(self, client): "calls paramiko.SSHClient.connect() with correct args" Connection('host').open() client.connect.assert_called_with( username=get_local_user(), hostname='host', port=22, )
def passes_through_connect_kwargs(self, client): Connection('host', connect_kwargs={'foobar': 'bizbaz'}).open() client.connect.assert_called_with( username=get_local_user(), hostname='host', port=22, foobar='bizbaz', )
def has_various_Fabric_specific_default_keys(self): c = Config() assert c.port == 22 assert c.user == get_local_user() assert c.forward_agent is False assert c.connect_kwargs == {} assert c.timeouts.connect is None assert c.ssh_config_path is None
def connect_kwargs_protection_not_tripped_by_defaults(self, client): Connection('host', connect_kwargs={'timeout': 300}).open() client.connect.assert_called_with( username=get_local_user(), hostname='host', port=22, timeout=300, )
def has_various_Fabric_specific_default_keys(self): c = Config() assert c.port == 22 assert c.user == get_local_user() assert c.forward_agent is False assert c.connect_kwargs == {} assert c.timeouts.connect is None assert c.ssh_config_path is None
def passes_through_connect_kwargs(self, client): Connection("host", connect_kwargs={"foobar": "bizbaz"}).open() client.connect.assert_called_with( username=get_local_user(), hostname="host", port=22, foobar="bizbaz", )
def django( context, command, user=get_local_user(), remote=False, instance=None, stack=None ): """ Run a Django management command. """ command = f"run --rm django python manage.py {command}" run_command(context, user, remote, instance, stack, command)
def connect_kwargs_protection_not_tripped_by_defaults(self, client): Connection("host", connect_kwargs={"timeout": 300}).open() client.connect.assert_called_with( username=get_local_user(), hostname="host", port=22, timeout=300, )
def passes_through_connect_kwargs(self, client): Connection('host', connect_kwargs={'foobar': 'bizbaz'}).open() client.connect.assert_called_with( username=get_local_user(), hostname='host', port=22, foobar='bizbaz', )
def submits_connect_timeout(self, client): Connection('host', connect_timeout=27).open() client.connect.assert_called_with( username=get_local_user(), hostname='host', port=22, timeout=27, )
def hello(c, path='参数值'): init(autoreset=True) print('*' * 50) print(Fore.CYAN + ' Fabric 使用指南\n') print(Fore.GREEN + ' 查看所有命令: fab -l') print(Fore.GREEN + ' 查看命令: fab -d 命令') print(Fore.YELLOW + ' 带参数命令: fab 命令 --参数 值\n') print(Fore.GREEN + ' Hello ~ ' + get_local_user()) print('*' * 50)
def ipv6_addresses_work_ok_but_avoid_port_shorthand(self): for addr in ("2001:DB8:0:0:0:0:0:1", "2001:DB8::1", "::1"): c = Connection(addr, port=123) assert c.user == get_local_user() assert c.host == addr assert c.port == 123 c2 = Connection("somebody@{}".format(addr), port=123) assert c2.user == "somebody" assert c2.host == addr assert c2.port == 123
def backup(context, user=get_local_user(), remote=False, instance=None, stack=None): """ Create a database backup. """ command = "run --rm postgres backup" run_command(context, user, remote, instance, stack, command)
def requirements(context): """ Run piptools pip-compile to update requirements. """ pip_compile = "pip-compile --annotate --quiet" command = (f"{pip_compile} requirements/base.in " f"&& {pip_compile} requirements/local.in " f"&& {pip_compile} requirements/production.in") command = f"run --rm django bash -c '{command}'" run_command(context, get_local_user(), False, None, None, command)
def restore( context, backup, user=get_local_user(), remote=False, instance=None, stack=None, ): """ Restore a database backup. """ command = f"exec postgres pkill -f {PROJECT}" run_command(context, user, remote, instance, stack, command) command = f"run --rm postgres restore {backup}" run_command(context, user, remote, instance, stack, command)
def update(context, user=get_local_user(), remote=False, instance=None, branch=BRANCH): """ Update the host instance from source control. """ no_stack = None no_compose = False command = f"git checkout {branch} || git pull && git checkout {branch}" run_command(context, user, remote, instance, no_stack, command, no_compose) command = f"git pull" run_command(context, user, remote, instance, no_stack, command, no_compose)
def restart( context, user=get_local_user(), remote=False, instance=None, stack=None, services=None, ): """ Restart one or more services. """ command = f"restart" run_command_with_services(context, user, remote, instance, stack, command, services)
def shell( context, user=get_local_user(), remote=False, instance=None, stack=None, service="django", ): """ Connect to a running service. """ command = f"run --rm {service} bash" run_command(context, user, remote, instance, stack, command)
def stop( context, user=get_local_user(), remote=False, instance=None, stack=None, services=None, ): """ Stop one or more services. """ command = f"stop" run_command_with_services(context, user, remote, instance, stack, command, services)
def ipv6_addresses_work_ok_but_avoid_port_shorthand(self): for addr in ( '2001:DB8:0:0:0:0:0:1', '2001:DB8::1', '::1', ): c = Connection(addr, port=123) assert c.user == get_local_user() assert c.host == addr assert c.port == 123 c2 = Connection("somebody@{}".format(addr), port=123) assert c2.user == "somebody" assert c2.host == addr assert c2.port == 123
def deploy( context, instance, user=get_local_user(), initial=False, stack=None, branch=BRANCH, ): """ Deploy the project. By default it creates a database backup before updating from source control and rebuilding the docker stack. """ remote = True if initial: clone(context, instance, user, branch) else: backup(context, user, remote, instance, stack) update(context, user, remote, instance, branch) up(context, user, remote, instance, stack)
def up( context, user=get_local_user(), remote=False, instance=None, stack=None, services=None, ): """ Build the stack for the host instance. """ command = f"up --build" if remote: command = f"{command} --detach" run_command_with_services(context, user, remote, instance, stack, command, services)
def test( context, user=get_local_user(), remote=False, instance=None, stack=None, app=None, coverage=False, ): """ Run tests with pytest. """ command = "pytest" if app: command = f"{command} {app}" if coverage: command = f"coverage run -m {command}" command = f"run --rm django {command}" run_command(context, user, remote, instance, stack, command)
def down( context, user=get_local_user(), remote=False, instance=None, stack=None, images="all", volumes=True, orphans=False, ): """ Stop and remove stack components. """ command = f"down --rmi {images}" if volumes: command = f"{command} --volumes" if orphans: command = f"{command} --remove-orphans" run_command(context, user, remote, instance, stack, command)
def defaults_to_local_user_with_no_config(self): # Tautology-tastic! assert Connection('host').user == get_local_user()
def KeyError_means_SaaS_and_thus_None(self, getuser): assert get_local_user() is None
def defaults_to_local_user_with_no_config(self): # Tautology-tastic! assert Connection("host").user == get_local_user()
def defaults_to_getpass_getuser(self, getuser): "defaults to getpass.getuser" get_local_user() getuser.assert_called_once_with()
"""General program configurations""" from fabric.util import get_local_user from rich.theme import Theme _PROG = 'vem' _VERSION = '0.5.0-dev' _DEFAULT_SSH_PORT = 22 _DEFAULT_SSH_USER = get_local_user() # custom 'rich' theme to use for rich output formatting. rich_theme = Theme( { 'h1': 'bold red', 'h2': 'cyan', 'info': 'cyan', 'keyword': 'bold bright_white', 'var': 'cornflower_blue', 'example': 'italic grey58', 'path': 'grey58', 'error': 'red', 'warning': 'gold3', 'todo': 'bold bright_magenta on purple4', }, inherit=True)