cli.py 2.8 KB

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