NewsBlur/utils/hostname_ssh.py

34 lines
1,010 B
Python
Raw Normal View History

2021-11-14 18:43:10 -05:00
#!/usr/bin/env python3
import sys
import os
import subprocess
import json
2015-09-21 16:13:09 -07:00
import digitalocean
2024-04-24 09:43:56 -04:00
2021-11-14 18:43:10 -05:00
# from django.conf import settings
2024-04-24 09:43:56 -04:00
sys.path.append("/srv/newsblur")
2024-04-24 09:43:56 -04:00
os.environ["DJANGO_SETTINGS_MODULE"] = "newsblur_web.settings"
2024-04-24 09:43:56 -04:00
if __name__ == "__main__":
# Check and clean second argument (ex: sshdo task 2)
second_arg = sys.argv[2] if len(sys.argv) > 2 else "1"
2021-11-14 18:43:10 -05:00
droplet_index = int(second_arg) if str(second_arg).isnumeric() else 1
droplet_name = sys.argv[1]
# Use correct Digital Ocean team based on "old"
2024-04-24 09:43:56 -04:00
commands = ["ansible-inventory", "--list"]
2021-11-14 18:43:10 -05:00
env = None
if second_arg == "old":
2021-11-14 18:43:10 -05:00
env = dict(os.environ, ANSIBLE_CONFIG="ansible.old.cfg")
hosts = subprocess.check_output(commands, env=env)
if not hosts:
print(" ***> Could not load ansible-inventory!")
2021-11-14 18:43:10 -05:00
hosts = json.loads(hosts)
2024-04-24 09:43:56 -04:00
for host, ip_host in hosts["_meta"]["hostvars"].items():
2021-11-14 18:43:10 -05:00
if host.startswith(droplet_name):
2024-04-24 09:43:56 -04:00
print(ip_host["ansible_host"])
2021-11-14 18:43:10 -05:00
break