cli.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import argparse
  2. from argparse import Namespace
  3. import sys
  4. from pathlib import Path
  5. from pc_backup.container import BorgmaticContainer, Configuration
  6. from pc_backup.keepass import KeePass
  7. from pc_backup.secret import Secret
  8. class CliArguments:
  9. @staticmethod
  10. def read_command(parser):
  11. match parser.parse_known_args():
  12. case (Namespace(command=command) as args, extra_args) if (
  13. command == CommandExec
  14. ):
  15. args.extra = extra_args
  16. return command(args)
  17. case (Namespace(command=_) as args, []):
  18. return args.command(args)
  19. case (Namespace(command=_), extra_args):
  20. return parser.error("You passed extra arguments")
  21. case _:
  22. return parser.error("You should call at least one of the commands")
  23. @staticmethod
  24. def new() -> argparse.ArgumentParser:
  25. parser = argparse.ArgumentParser(prog=sys.argv[0])
  26. subparsers = parser.add_subparsers()
  27. for sub in [
  28. CommandStart,
  29. CommandRm,
  30. CommandExec,
  31. CommandBash,
  32. CommandCreateRepo,
  33. CommandExportKey,
  34. CommandCreateSecrets,
  35. ]:
  36. p = subparsers.add_parser(sub.command, help=sub.help)
  37. sub.init_subparser(p)
  38. p.set_defaults(command=sub)
  39. return parser
  40. class Command:
  41. def __init__(self, namespace) -> None:
  42. for k, v in vars(namespace).items():
  43. if k != "type_":
  44. setattr(self, k, v)
  45. @classmethod
  46. def init_subparser(cls, p): ...
  47. class CommandStart(Command):
  48. command = "start"
  49. help = "start container"
  50. def run(
  51. self,
  52. *,
  53. container: BorgmaticContainer,
  54. config: Configuration,
  55. **kwargs,
  56. ):
  57. container.run(config)
  58. class CommandRm(Command):
  59. command = "rm"
  60. help = "remove container"
  61. def run(self, *, container: BorgmaticContainer, **kwargs):
  62. container.rm()
  63. class CommandExec(Command):
  64. command = "exec"
  65. help = "run command in container"
  66. env_vars = ["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
  67. def run(self, *, container: BorgmaticContainer, **kwargs):
  68. container.exec(self.extra, self.env_vars)
  69. class CommandBash(Command):
  70. command = "bash"
  71. help = "run shell in container"
  72. def run(self, *, container: BorgmaticContainer, **kwargs):
  73. container.exec(["bash"])
  74. class CommandCreateRepo(Command):
  75. command = "create_repo"
  76. help = "create repository"
  77. env_vars = ["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
  78. def run(self, *, container: BorgmaticContainer, **kwargs):
  79. container.exec(
  80. ["borgmatic", "repo-create", "--encryption", "repokey"], self.env_vars
  81. )
  82. class CommandExportKey(Command):
  83. command = "export_key"
  84. help = "export the repository key"
  85. env_vars = ["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
  86. def run(self, *, container: BorgmaticContainer, **kwargs):
  87. container.exec(["borgmatic", "export", "key"], self.env_vars)
  88. class CommandCreateSecrets(Command):
  89. command = "create_secrets"
  90. help = "create podman secrets"
  91. def run(self, *, secret_sources: list[Secret], **kwargs):
  92. keepass = KeePass.new(self.keepass_path)
  93. for s in secret_sources:
  94. s.create(keepass)
  95. @classmethod
  96. def init_subparser(cls, p):
  97. p.add_argument("keepass_path", type=Path, help="Path to the keepass")