2022-10-28 10:19:44 +00:00
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
import shutil
|
2022-11-01 03:52:51 +00:00
|
|
|
|
2022-11-01 09:04:44 +00:00
|
|
|
import yaml
|
2022-11-01 03:52:51 +00:00
|
|
|
from django.utils import timezone
|
2022-10-28 10:19:44 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2022-11-01 03:52:51 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from common.db.utils import safe_db_connection
|
2022-10-28 10:19:44 +00:00
|
|
|
from ops.ansible import PlaybookRunner, JMSInventory
|
|
|
|
|
2022-11-01 03:52:51 +00:00
|
|
|
logger = get_logger(__name__)
|
2022-10-28 10:19:44 +00:00
|
|
|
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
class DeployAppletHostManager:
|
2022-11-01 03:52:51 +00:00
|
|
|
def __init__(self, deployment):
|
|
|
|
self.deployment = deployment
|
2022-10-28 10:19:44 +00:00
|
|
|
self.run_dir = self.get_run_dir()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_run_dir():
|
|
|
|
base = os.path.join(settings.ANSIBLE_DIR, 'applet_host_deploy')
|
|
|
|
now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
|
|
|
|
return os.path.join(base, now)
|
|
|
|
|
|
|
|
def generate_playbook(self):
|
|
|
|
playbook_src = os.path.join(CURRENT_DIR, 'playbook.yml')
|
2022-11-01 09:04:44 +00:00
|
|
|
with open(playbook_src) as f:
|
|
|
|
plays = yaml.safe_load(f)
|
|
|
|
for play in plays:
|
|
|
|
play['vars'].update(self.deployment.host.deploy_options)
|
|
|
|
play['vars']['DownloadHost'] = settings.BASE_URL + '/download/'
|
|
|
|
play['vars']['CORE_HOST'] = settings.BASE_URL
|
|
|
|
play['vars']['BOOTSTRAP_TOKEN'] = settings.BOOSTRAP_TOKEN
|
|
|
|
play['vars']['HOST_NAME'] = self.deployment.host.name
|
|
|
|
|
2022-10-28 10:19:44 +00:00
|
|
|
playbook_dir = os.path.join(self.run_dir, 'playbook')
|
|
|
|
playbook_dst = os.path.join(playbook_dir, 'main.yml')
|
|
|
|
os.makedirs(playbook_dir, exist_ok=True)
|
2022-11-01 09:04:44 +00:00
|
|
|
with open(playbook_dst, 'w') as f:
|
|
|
|
yaml.safe_dump(plays, f)
|
2022-10-28 10:19:44 +00:00
|
|
|
return playbook_dst
|
|
|
|
|
|
|
|
def generate_inventory(self):
|
2022-11-01 03:52:51 +00:00
|
|
|
inventory = JMSInventory([self.deployment.host], account_policy='privileged_only')
|
2022-10-28 10:19:44 +00:00
|
|
|
inventory_dir = os.path.join(self.run_dir, 'inventory')
|
|
|
|
inventory_path = os.path.join(inventory_dir, 'hosts.yml')
|
|
|
|
inventory.write_to_file(inventory_path)
|
|
|
|
return inventory_path
|
|
|
|
|
2022-11-01 03:52:51 +00:00
|
|
|
def _run(self, **kwargs):
|
2022-10-28 10:19:44 +00:00
|
|
|
inventory = self.generate_inventory()
|
|
|
|
playbook = self.generate_playbook()
|
|
|
|
runner = PlaybookRunner(
|
|
|
|
inventory=inventory, playbook=playbook, project_dir=self.run_dir
|
|
|
|
)
|
|
|
|
return runner.run(**kwargs)
|
2022-11-01 03:52:51 +00:00
|
|
|
|
|
|
|
def run(self, **kwargs):
|
|
|
|
try:
|
|
|
|
self.deployment.date_start = timezone.now()
|
|
|
|
cb = self._run(**kwargs)
|
|
|
|
self.deployment.status = cb.status
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Error: {}".format(e))
|
|
|
|
self.deployment.status = 'error'
|
|
|
|
finally:
|
|
|
|
self.deployment.date_finished = timezone.now()
|
|
|
|
with safe_db_connection():
|
|
|
|
self.deployment.save()
|
|
|
|
|
|
|
|
|
|
|
|
|