def site_provision(site): """ Provision a new instance with the given parameters. :param site: A single site. :return: """ logger.debug("Site provision - {0}".format(site)) # 'db_key' needs to be added here and not in Eve so that the encryption # works properly. site["db_key"] = utilities.encrypt_string(utilities.mysql_password()) fab_task = execute(fabfile.site_provision, site=site) logger.debug(fab_task) logger.debug(fab_task.values) patch_payload = {"status": "available", "db_key": site["db_key"]} patch = utilities.patch_eve("sites", site["_id"], patch_payload) logger.debug("Site has been provisioned\n{0}".format(patch)) slack_title = "{0}/{1}".format(base_urls[environment], site["sid"]) slack_link = "{0}/{1}".format(base_urls[environment], site["sid"]) attachment_text = "{0}/sites/{1}".format(api_server, site["_id"]) if False not in fab_task.values(): slack_message = "Site provision - Success" slack_color = "good" utilities.post_to_slack( message=slack_message, title=slack_title, link=slack_link, attachment_text=attachment_text, level=slack_color, )
def site_update(site, updates, original): """ Update an instance with the given parameters. :param site: A complete site item, including new values. :param updates: A partial site item, including only changed keys. :param original: Complete original site item. :return: """ logger.debug("Site update - {0}\n{1}\n\n{2}".format(site["_id"], site, updates)) if updates.get("code"): logger.debug("Found code changes.") if "package" in updates["code"]: logger.debug("Found package changes.") execute(fabfile.site_package_update, site=site) execute(fabfile.update_database, site=site) if updates["code"].get("core") != original["code"].get("core"): logger.debug("Found core change.") execute(fabfile.site_core_update, site=site) execute(fabfile.update_database, site=site) if updates["code"].get("profile") != original["code"].get("profile"): logger.debug("Found profile change.") execute(fabfile.site_profile_update, site=site, original=original, updates=updates) execute(fabfile.update_database, site=site) if updates.get("status"): logger.debug("Found status change.") if updates["status"] in ["installing", "launching", "take_down", "restore"]: if updates["status"] == "installing": logger.debug("Status changed to installing") patch_payload = '{"status": "installed"}' elif updates["status"] == "launching": logger.debug("Status changed to launching") execute(fabfile.site_launch, site=site) patch_payload = '{"status": "launched"}' elif updates["status"] == "take_down": logger.debug("Status changed to take_down") execute(fabfile.site_take_down, site=site) patch_payload = '{"status": "down"}' elif updates["status"] == "restore": logger.debug("Status changed to restore") execute(fabfile.site_restore, site=site) execute(fabfile.update_database, site=site) patch_payload = '{"status": "installed"}' patch = utilities.patch_eve("sites", site["_id"], patch_payload) logger.debug(patch) slack_title = "{0}/{1}".format(base_urls[environment], site["sid"]) slack_link = "{0}/{1}".format(base_urls[environment], site["sid"]) attachment_text = "{0}/sites/{1}".format(api_server, site["_id"]) slack_message = "Site Update - Success" slack_color = "good" utilities.post_to_slack( message=slack_message, title=slack_title, link=slack_link, attachment_text=attachment_text, level=slack_color )
def code_remove(item): """ Remove code from the server. :param item: Item to be removed. :return: """ logger.debug("Code delete - {0}".format(item)) fab_task = execute(fabfile.code_remove, item=item) slack_title = "{0} - {1}".format(item["meta"]["name"], item["meta"]["version"]) if False not in fab_task.values(): slack_message = "Code Remove - Success" slack_color = "good" utilities.post_to_slack(message=slack_message, title=slack_title, level=slack_color)
def code_deploy(item): """ Deploy git repositories to the appropriate places. :param item: The flask request.json object. :return: """ logger.debug("Code deploy - {0}".format(item)) fab_task = execute(fabfile.code_deploy, item=item) slack_title = "{0} - {1}".format(item["meta"]["name"], item["meta"]["version"]) if False not in fab_task.values(): slack_message = "Code Deploy - Success" slack_color = "good" utilities.post_to_slack(message=slack_message, title=slack_title, level=slack_color)
def site_remove(site): """ Remove site from the server. :param site: Item to be removed. :return: """ logger.debug("Site delete\n{0}".format(site)) execute(fabfile.site_remove, site=site) delete = utilities.delete_eve("sites", site["_id"]) logger.debug(delete) slack_title = "{0}/{1}".format(base_urls[environment], site["sid"]) slack_message = "Site Remove - Success" slack_color = "good" utilities.post_to_slack(message=slack_message, title=slack_title, level=slack_color)
def code_update(updated_item, original_item): """ Update code checkout. :param updated_item: :param original_item: :return: """ logger.debug("Code update - {0}".format(updated_item)) fab_task = execute(fabfile.code_update, updated_item=updated_item, original_item=original_item) name = updated_item["meta"]["name"] if updated_item["meta"]["name"] else original_item["meta"]["name"] version = updated_item["meta"]["version"] if updated_item["meta"]["version"] else original_item["meta"]["version"] slack_title = "{0} - {1}".format(name, version) if False not in fab_task.values(): slack_message = "Code Update - Success" slack_color = "good" utilities.post_to_slack(message=slack_message, title=slack_title, level=slack_color)
def command_run(site, command, single_server): """ Run the appropriate command. :param site: A complete site item. :param command: Command to run. :param single_server: boolean Run a single server or all servers. :return: """ logger.debug("Run Command - {0} - {1}\n{2}".format(site["sid"], single_server, command)) if single_server: execute(fabfile.command_run_single, site=site, command=command) else: execute(fabfile.command_run, site=site, command=command) slack_title = "{0}/{1}".format(base_urls[environment], site["sid"]) slack_link = "{0}/{1}".format(base_urls[environment], site["sid"]) slack_message = "Command - Success" slack_color = "good" attachment_text = command utilities.post_to_slack( message=slack_message, title=slack_title, link=slack_link, attachment_text=attachment_text, level=slack_color )