askrobots / dbbasic-deploy
Public
Deploy dbbasic-web apps to Ubuntu servers over SSH: nginx, Let's Encrypt and systemd, no Docker or Ansible.
cli.py · 3.8 KB · 108 lines
| 1 | """Command-line interface for dbbasic-deploy.""" |
| 2 | |
| 3 | import click |
| 4 | from .setup import setup_server |
| 5 | from .deploy import deploy_app |
| 6 | |
| 7 | |
| 8 | @click.group() |
| 9 | @click.version_option(version="0.1.0") |
| 10 | def main(): |
| 11 | """Simple SSH deployment tool for dbbasic-web apps to Ubuntu servers.""" |
| 12 | pass |
| 13 | |
| 14 | |
| 15 | @main.command() |
| 16 | @click.option("--host", required=True, help="Server hostname or IP") |
| 17 | @click.option("--domain", required=True, help="Domain name for the app") |
| 18 | @click.option("--app-name", default="app", help="Application name (default: app)") |
| 19 | @click.option("--port", default=3000, help="Port for uvicorn (default: 3000)") |
| 20 | @click.option("--user", default="root", help="SSH user (default: root)") |
| 21 | @click.option("--key", "key_path", help="Path to SSH private key") |
| 22 | def setup(host, domain, app_name, port, user, key_path): |
| 23 | """ |
| 24 | Set up a fresh Ubuntu server for hosting dbbasic-web apps. |
| 25 | |
| 26 | This will install Python, nginx, certbot, and configure systemd service. |
| 27 | |
| 28 | Example: |
| 29 | dbbasic-deploy setup --host myserver.com --domain demo.dbbasic.com |
| 30 | """ |
| 31 | try: |
| 32 | setup_server(host, domain, app_name, port, user, key_path) |
| 33 | except Exception as e: |
| 34 | click.echo(f"Error during setup: {e}", err=True) |
| 35 | raise click.Abort() |
| 36 | |
| 37 | |
| 38 | @main.command() |
| 39 | @click.option("--host", required=True, help="Server hostname or IP") |
| 40 | @click.option("--domain", required=True, help="Domain name for the app") |
| 41 | @click.option("--app-name", default="app", help="Application name (default: app)") |
| 42 | @click.option("--port", default=3000, help="Port for uvicorn (default: 3000)") |
| 43 | @click.option("--user", default="root", help="SSH user (default: root)") |
| 44 | @click.option("--key", "key_path", help="Path to SSH private key") |
| 45 | @click.option("--path", "local_path", default=".", help="Local path to deploy (default: .)") |
| 46 | def push(host, domain, app_name, port, user, key_path, local_path): |
| 47 | """ |
| 48 | Deploy your app to the server. |
| 49 | |
| 50 | This will upload your code, install dependencies, and restart the service. |
| 51 | |
| 52 | Example: |
| 53 | dbbasic-deploy push --host myserver.com --domain demo.dbbasic.com |
| 54 | """ |
| 55 | try: |
| 56 | deploy_app(host, domain, app_name, port, user, key_path, local_path) |
| 57 | except Exception as e: |
| 58 | click.echo(f"Error during deployment: {e}", err=True) |
| 59 | raise click.Abort() |
| 60 | |
| 61 | |
| 62 | @main.command() |
| 63 | @click.option("--host", required=True, help="Server hostname or IP") |
| 64 | @click.option("--domain", required=True, help="Domain name for SSL certificate") |
| 65 | @click.option("--email", help="Email for Let's Encrypt notifications") |
| 66 | @click.option("--user", default="root", help="SSH user (default: root)") |
| 67 | @click.option("--key", "key_path", help="Path to SSH private key") |
| 68 | def ssl(host, domain, email, user, key_path): |
| 69 | """ |
| 70 | Set up SSL certificate with Let's Encrypt. |
| 71 | |
| 72 | Example: |
| 73 | dbbasic-deploy ssl --host myserver.com --domain demo.dbbasic.com --email you@example.com |
| 74 | """ |
| 75 | import paramiko |
| 76 | |
| 77 | ssh = paramiko.SSHClient() |
| 78 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 79 | |
| 80 | connect_kwargs = {"hostname": host, "username": user} |
| 81 | if key_path: |
| 82 | connect_kwargs["key_filename"] = key_path |
| 83 | |
| 84 | ssh.connect(**connect_kwargs) |
| 85 | |
| 86 | email_flag = f"--email {email}" if email else "--register-unsafely-without-email" |
| 87 | command = f"certbot --nginx -d {domain} {email_flag} --non-interactive --agree-tos" |
| 88 | |
| 89 | click.echo(f"Setting up SSL for {domain}...") |
| 90 | stdin, stdout, stderr = ssh.exec_command(command) |
| 91 | exit_status = stdout.channel.recv_exit_status() |
| 92 | |
| 93 | output = stdout.read().decode() |
| 94 | error = stderr.read().decode() |
| 95 | |
| 96 | if exit_status == 0: |
| 97 | click.echo(output) |
| 98 | click.echo(f"\n✓ SSL certificate installed successfully!") |
| 99 | click.echo(f"Your app is now available at: https://{domain}") |
| 100 | else: |
| 101 | click.echo(f"Error: {error}", err=True) |
| 102 | raise click.Abort() |
| 103 | |
| 104 | ssh.close() |
| 105 | |
| 106 | |
| 107 | if __name__ == "__main__": |
| 108 | main() |