NewsBlur-viq/ansible/utils/generate_inventory.py

65 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import os
import subprocess
2024-04-24 09:50:42 -04:00
import sys
import time
import digitalocean
OLD = False
# Set env var OLD=1 to use existing servers
2024-04-24 09:43:56 -04:00
if os.environ.get("OLD", False):
OLD = True
if OLD:
TOKEN_FILE = "/srv/secrets-newsblur/keys/digital_ocean.readprod.token"
else:
TOKEN_FILE = "/srv/secrets-newsblur/keys/digital_ocean.token"
try:
2024-04-24 09:43:56 -04:00
api_token = open(TOKEN_FILE, "r").read().strip()
except IOError:
print(f" ---> Missing Digital Ocean API token: {TOKEN_FILE}")
exit()
outfile = f"/srv/newsblur/ansible/inventories/digital_ocean{'.old' if OLD else ''}.ini"
2021-02-17 19:42:16 -05:00
2021-01-29 11:27:45 -05:00
# Install from https://github.com/do-community/do-ansible-inventory/releases
2024-04-24 09:43:56 -04:00
ansible_inventory_cmd = f"do-ansible-inventory -t {api_token} --out {outfile}"
2021-02-17 19:42:16 -05:00
subprocess.call(ansible_inventory_cmd, shell=True)
2024-04-24 09:43:56 -04:00
with open(outfile, "r") as original:
2021-02-17 19:42:16 -05:00
data = original.read()
2024-04-24 09:43:56 -04:00
with open(outfile, "w") as modified:
2021-02-17 19:42:16 -05:00
modified.write("127.0.0.1 ansible_connection=local\n" + data)
2024-04-24 09:43:56 -04:00
exit() # Too many requests if we run the below code
2021-02-08 19:22:51 -05:00
do = digitalocean.Manager(token=api_token)
droplets = do.get_all_droplets()
2024-04-24 09:43:56 -04:00
print("\n ---> Checking droplets: %s\n" % (" ".join([d.name for d in droplets])))
def check_droplets_created():
2021-02-08 19:22:51 -05:00
i = 0
droplets = do.get_all_droplets()
for instance in droplets:
2024-04-24 09:43:56 -04:00
if instance.status == "new":
print(".", end=" ")
sys.stdout.flush()
i += 1
time.sleep(i)
break
else:
print(" ---> All booted!")
return True
2024-04-24 09:43:56 -04:00
i = 0
while True:
if check_droplets_created():
break