Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

yarrbox.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python3
  2. from collections import OrderedDict
  3. import sys
  4. import getpass
  5. import json
  6. # Each entry here is key => (label, hide input)
  7. PROPS = OrderedDict([
  8. ('net.vpn.host', ('VPN Provider', 'See https://github.com/sscraggles/docker-deluge-openvpn for options')),
  9. ('net.vpn.username', ('VPN Username', None)),
  10. ('net.vpn.password', ('VPN Password', None)),
  11. ('vol.media.movies', ('Movies Storage Path', None)),
  12. ('vol.media.tv', ('TV Storage Path', None)),
  13. ('vol.media.anime', ('Anime Storage Path', None)),
  14. ('vol.conf.torrent', ('Torrent Data Dir', 'Directory for torrent client(s) to store configs and state')),
  15. ('vol.conf.bootybot', ('Bootybot Config Dir', 'Config directory for Bootybot')),
  16. ('vol.conf.plex', ('Plex Data Dir', 'Directory for all of Plex\'s stuff (databases, etc.)')),
  17. ('vol.ingest.torrent', ('Torrent Ingest Dir', 'Directory to drop .torrent files to download through VPN')),
  18. ('vol.ingest.media', ('Media Ingest Dir', 'Directory to drop full media files for Bootybot to later process')),
  19. ('vol.torrent.active', ('Torrent Download Dir', 'Directory for actively downloading torrents to be stored')),
  20. ('vol.torrent.seed', ('Torrent Seeding Dir', 'Directory to move torrents after download finished for seeding')),
  21. ('vol.work.plextrans', ('Plex Transcode Dir', 'Directory for Plex to store certain transcoded files')),
  22. ('vol.work.bootybot', ('Bootybot Extract Dir', 'Directory where Bootybot temporarily stores contents of archives, etc'))
  23. ])
  24. PROMPT = '> '
  25. def ask_config_opt(config, key):
  26. label, desc = PROPS[key]
  27. nodisp = 'password' in key
  28. # Print titles
  29. title = '= ' + label + ' ='
  30. bars = '=' * len(title)
  31. print(bars + '\n' + title + '\n' + bars)
  32. if desc is not None:
  33. print(desc)
  34. # Current state stuff.
  35. if key in config:
  36. if not nodisp:
  37. print('Current: \'' + config[key] + '\'')
  38. # Actually ask for input.
  39. if not nodisp:
  40. val = input(PROMPT)
  41. if len(val) > 0:
  42. config[key] = val
  43. else:
  44. val = getpass.getpass('(hidden) ' + PROMPT)
  45. if len(val) > 0:
  46. config[key] = val
  47. def apply_template_settings(tmplt, settings):
  48. cur = tmplt
  49. for k, v in settings.items():
  50. cur = cur.replace('${' + k + '}', v)
  51. return cur
  52. def read_config():
  53. try:
  54. with open('config.json', 'r') as f:
  55. return json.loads(f.read())
  56. except e:
  57. print('error loading config, is it missing or corrupt?', e)
  58. sys.exit(1)
  59. def save_config(cfg):
  60. try:
  61. with open('config.json', 'w') as f:
  62. f.write(json.dumps(cfg, sort_keys=True, indent=' '))
  63. f.write('\n') # json.dumps doesn't put trailing newline
  64. except e:
  65. print('error writing config, do we have write perms?', e)
  66. sys.exit(1)
  67. USAGE_STR = 'usage: yarrbox.py <init|edit|generate>'
  68. def main():
  69. if len(sys.argv) != 2:
  70. print(USAGE_STR)
  71. sys.exit(1)
  72. command = sys.argv[1]
  73. if command == 'init':
  74. # Make a new config object and read all the properties.
  75. cfg = {}
  76. for k in PROPS:
  77. ask_config_opt(cfg, k)
  78. print('')
  79. # Write it to file
  80. save_config(cfg)
  81. elif command == 'edit':
  82. print('Leave options blank to leave unchanged!\n')
  83. cfg = read_config()
  84. # Go through all the properties again and reread them.
  85. for k in PROPS:
  86. ask_config_opt(cfg, k)
  87. print('')
  88. # Write it back.
  89. save_config(cfg)
  90. elif command == 'generate':
  91. # Load the config.
  92. cfg = read_config()
  93. tmplt = None
  94. try:
  95. with open('compose-template.yml', 'r') as f:
  96. tmplt = f.read()
  97. except e:
  98. print('error reading template:', e)
  99. sys.exit(1)
  100. gen = apply_template_settings(tmplt, cfg)
  101. try:
  102. with open('docker-compose.yml', 'w') as f:
  103. f.write(gen)
  104. except e:
  105. print('error writing generated file, do we have write perms?', e)
  106. sys.exit(1)
  107. else:
  108. print(USAGE_STR)
  109. if __name__ == '__main__':
  110. main()