dashboard/integration/cli.py

308 lines
10 KiB
Python

import argparse
from requests import Session
def gitea_from_args(args, c: Session):
from .gitea import Gitea
return Gitea(
host=args.host,
username=args.username,
password=args.password,
email=args.email,
c=c,
)
class Gitea:
def __init__(self, parser, c: Session):
self.c = c
self.parser = parser
self.subparser = self.parser.add_subparsers()
self.install()
self.register()
self.login()
self.create_repository()
self.install_sso()
self.add_deploy_key()
def __add_credentials_parser(self, parser):
group = parser.add_argument_group("credentials", "User credentials")
group.add_argument("username", type=str, help="Gitea user's username")
group.add_argument("password", type=str, help="Gitea user's password")
group.add_argument("email", type=str, help="Gitea user's email")
group.add_argument("host", type=str, help="URI at which Gitea is running")
def install(self):
def run(args, c: Session):
gitea = gitea_from_args(args, c=c)
gitea.install()
self.install_parser = self.subparser.add_parser(
name="install", description="Install Gitea", help="Install Gitea"
)
self.__add_credentials_parser(self.install_parser)
self.install_parser.set_defaults(func=run)
def register(self):
def run(args, c: Session):
gitea = gitea_from_args(args, c=c)
gitea.register()
self.register_parser = self.subparser.add_parser(
name="register",
description="Gitea user registration",
help="Register a user on Gitea",
)
self.__add_credentials_parser(self.register_parser)
self.register_parser.set_defaults(func=run)
def login(self):
def run(args, c: Session):
gitea = gitea_from_args(args, c=c)
gitea.login()
self.login_parser = self.subparser.add_parser(
name="login", description="Gitea user login", help="Login on Gitea"
)
self.__add_credentials_parser(self.login_parser)
self.login_parser.set_defaults(func=run)
def create_repository(self):
def run(args, c: Session):
gitea = gitea_from_args(args, c=c)
gitea.login()
gitea.create_repository(name=args.repo_name)
self.create_repository_parser = self.subparser.add_parser(
name="create_repo",
description="Create repository on Gitea",
help="Create repository on Gitea",
)
self.__add_credentials_parser(self.create_repository_parser)
self.create_repository_parser.set_defaults(func=run)
self.create_repository_parser.add_argument(
"repo_name", type=str, help="Name of the repository to be created"
)
def install_sso(self):
def run(args, c: Session):
gitea = gitea_from_args(args, c=c)
gitea.login()
print(f"CLIENT ID: {args.client_id}")
gitea.install_sso(
sso_name=args.sso_name,
client_id=args.client_id,
client_secret=args.client_secret,
sso_auto_discovery_url=args.sso_auto_discovery_url,
)
self.install_sso_parser = self.subparser.add_parser(
name="install_sso",
description="Install SSO on Gitea",
help="Install SSO on Gitea",
)
self.__add_credentials_parser(self.install_sso_parser)
self.install_sso_parser.add_argument(
"sso_name", type=str, help="(Human readable)Name of the SSO"
)
self.install_sso_parser.add_argument(
"client_id", type=str, help="Client ID generated by the SSO"
)
self.install_sso_parser.add_argument(
"client_secret", type=str, help="Client secret generated by the SSO"
)
self.install_sso_parser.add_argument(
"sso_auto_discovery_url",
type=str,
help="OIDC Auto Discovery URL of the SSO",
)
self.install_sso_parser.set_defaults(func=run)
def add_deploy_key(self):
def run(args, c: Session):
gitea = gitea_from_args(args, c=c)
gitea.login()
gitea.add_deploy_key(repo=args.repo_name, key=args.key_file)
self.add_deploy_key_parser = self.subparser.add_parser(
name="add_deploy_key",
description="Create repository on Gitea",
help="Add deploy key to a repository on Gitea",
)
self.__add_credentials_parser(self.add_deploy_key_parser)
self.add_deploy_key_parser.add_argument(
"repo_name",
type=str,
help="Name of the repository to which key should be added",
)
self.add_deploy_key_parser.add_argument(
"key_file", type=str, help="Path of the deploy key. Public key."
)
self.add_deploy_key_parser.set_defaults(func=run)
def dash_from_args(args, c: Session):
from .hostea import Hostea
return Hostea(
username=args.username,
email=args.email,
password=args.password,
host=args.host,
c=c,
)
class Hostea:
def __init__(self, parser, c: Session):
self.c = c
self.parser = parser
self.subparser = self.parser.add_subparsers()
self.register()
self.login()
self.support()
def __add_credentials_parser(self, parser):
group = parser.add_argument_group("credentials", "User credentials")
group.add_argument("username", type=str, help="Hostea user's username")
group.add_argument("password", type=str, help="Hostea user's password")
group.add_argument("email", type=str, help="Hostea user's email")
group.add_argument("host", type=str, help="URI at which Hostea is running")
def register(self):
def run(args, c: Session):
dash = dash_from_args(args, c=c)
dash.register(maildev_host=args.maildev_host)
self.register_parser = self.subparser.add_parser(
name="register",
description="register new user ",
help="Register new user on Hostea Dashboard",
)
self.__add_credentials_parser(self.register_parser)
self.register_parser.add_argument(
"maildev_host", type=str, help="URI at which maildev is running"
)
self.register_parser.set_defaults(func=run)
def login(self):
def run(args, c: Session):
dash = dash_from_args(args, c=c)
dash.login()
self.login_parser = self.subparser.add_parser(
name="login",
description="login",
help="Login user on Hostea Dashboard",
)
self.__add_credentials_parser(self.login_parser)
self.login_parser.set_defaults(func=run)
def support(self):
def run(args, c: Session):
from .gitea import GiteaSSO
dash = dash_from_args(args, c=c)
dash.login()
gitea = GiteaSSO(
username=dash.username,
email=dash.email,
gitea_host=args.gitea_host,
hostea_org=args.gitea_hostea_org,
support_repo=args.support_repo,
c=c,
)
dash.new_ticket(gitea.new_issues_uri)
gitea.new_issue()
self.support_parser = self.subparser.add_parser(
name="support",
description="Hostea support",
help="Support functionality on Hostea Dashboard",
)
self.__add_credentials_parser(self.support_parser)
self.support_parser.add_argument(
"gitea_host", type=str, help="URI at which Gitea is running"
)
self.support_parser.add_argument(
"gitea_hostea_org",
type=str,
help="Hostea namespace(username/org) on Gitea, where support repository is hosted",
)
self.support_parser.add_argument(
"support_repo", type=str, help="support repository name"
)
self.support_parser.set_defaults(func=run)
class Cli:
def __init__(self):
c = Session()
self.c = c
self.parser = argparse.ArgumentParser(
description="Install and Bootstrap Gitea and Hostea Dashboard"
)
self.subparser = self.parser.add_subparsers()
self.check_env()
self.gitea()
self.hostea()
def __add_credentials_parser(self, parser):
group = parser.add_argument_group("credentials", "User credentials")
group.add_argument("username", type=str, help="Gitea user's username")
group.add_argument("password", type=str, help="Gitea user's password")
group.add_argument("email", type=str, help="Gitea user's email")
def check_env(self):
def run(args, c: Session):
from .gitea import Gitea
from .hostea import Hostea
Hostea.check_online(
dashboard_host=args.hostea_host, maildev_host=args.maildev_host
)
Gitea.check_online(host=args.gitea_host)
self.check_env_parser = self.subparser.add_parser(
name="check_env",
description="Check and block until environment is ready",
help="Check and block until environment is ready",
)
self.check_env_parser.add_argument(
"gitea_host", type=str, help="URI at which Gitea is running"
)
self.check_env_parser.add_argument(
"hostea_host", type=str, help="URI at which Hostea is running"
)
self.check_env_parser.add_argument(
"maildev_host", type=str, help="URI at which maildev is running"
)
self.check_env_parser.set_defaults(func=run)
def hostea(self):
self.hostea = self.subparser.add_parser(
name="hostea",
description="Hostea Dashboard",
help="Hostea Dashboard-related functionality",
)
Hostea(parser=self.hostea, c=self.c)
def gitea(self):
self.gitea = self.subparser.add_parser(
name="gitea",
description="Gitea",
help="Gitea-related functionality",
)
Gitea(parser=self.gitea, c=self.c)
def parse(self):
return self.parser.parse_args()