import os import logging import subprocess # import stat from jinja2 import Environment, FileSystemLoader import concurrent.futures from configs.wowza_license_manager import wowza_license, wowza_manager_creds from configs.wowza_app_config import applications def setup_environment(): """Call the functions to write the license and manager credentials""" wowza_license() wowza_manager_creds() # Load the Jinja2 environment env = Environment(loader=FileSystemLoader("templates/")) # Set the current environment for the application settings current_environment = os.getenv("Environment", "dev") # Check if the current environment is 'qa' or 'prod' if current_environment in ["dev", "qa", "prod"]: """The 'applications' dictionary contains the settings like 'allow_origin' and 'cupertinoEncryptionBaseURL' for each application in the 'qa' and 'prod' environments in wowza_app_config.""" current_applications = applications[current_environment] else: logging.error( f"Invalid environment: {current_environment}. Please set " f"environment to either 'dev, 'qa' or 'prod'." ) current_applications = {} return env, current_environment, current_applications def create_directories(application): os.makedirs( f"/usr/local/WowzaStreamingEngine/applications/{application}", exist_ok=True, mode=0o644, ) os.makedirs( f"/usr/local/WowzaStreamingEngine/conf/{application}", exist_ok=True, mode=0o644, ) def render_application_template(env, current_environment, current_applications): # Define default values for cupertinoEncryptionBaseURL default_cupertino_encryption_base_urls = { "dev": "https://dev-aws-wowza.dev.theorchard.io", "qa": "https://qa-aws-wowza.qaorch.com", "prod": "https://prod-aws-wowza.theorchard.com", } # Define default value for cupertinoEncryptionSharedSecret for all applications cupertino_encryption_shared_secret = os.getenv( "CUPERTINO_ENCRYPTION_SHARED_SECRET", "dummy" ) # Now 'current_applications' contains the applications for the # current environment for application, application_details in current_applications.items(): create_directories(application) template = env.get_template("Application.xml.j2") # Get cupertinoEncryptionBaseURL from application_details if present, otherwise use default cupertino_encryption_base_url = application_details.get( "cupertinoEncryptionBaseURL", default_cupertino_encryption_base_urls[current_environment], ) rendered_template = template.render( name=application, app_name=application_details["app_name"], allow_origin=application_details["allow_origin"], cupertino_encryption_shared_secret=cupertino_encryption_shared_secret, cupertino_encryption_base_url=cupertino_encryption_base_url, ) with open( f"/usr/local/WowzaStreamingEngine/conf/{application}/Application.xml", "w", ) as file: file.write(rendered_template) def render_mediacache_template(env): # Render MediaCache.xml.j2 template template = env.get_template("MediaCache.xml.j2") rendered_template = template.render( aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"), aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), ) with open(f"/usr/local/WowzaStreamingEngine/conf/MediaCache.xml", "w") as file: file.write(rendered_template) def start_services(): # Start the Wowza Streaming Engine and Wowza Streaming Engine Manager commands = [ ["/usr/local/WowzaStreamingEngine/bin/startup.sh"], ["/usr/local/WowzaStreamingEngine/manager/bin/startmgr.sh"], ] # Use a ThreadPoolExecutor to run the commands in parallel with concurrent.futures.ThreadPoolExecutor() as executor: executor.map(subprocess.call, commands) def main(): env, current_environment, current_applications = setup_environment() render_application_template(env, current_environment, current_applications) render_mediacache_template(env) start_services() if __name__ == "__main__": main()