def install_kubernetes_dependencies(capture_output): parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(os.path.join(parent_dir, 'aimmo_runner')) os.chdir(ROOT_DIR_LOCATION) run_command(['pip', 'install', '-r', os.path.join(ROOT_DIR_LOCATION, 'minikube_requirements.txt')], capture_output=capture_output) os.environ['AIMMO_MODE'] = 'minikube'
def start_cluster(minikube): """ Starts the cluster unless it has been already started by the user. :param minikube: Executable minikube installed beforehand. """ try: run_command([minikube, 'status'], True) print('Cluster already running') except CalledProcessError: run_command([minikube, 'start', '--memory=2048', '--cpus=2'])
def restart_ingress_addon(minikube): """ Ingress needs to be restarted for old paths to be removed at startup. :param minikube: Executable minikube installed beforehand. """ try: run_command([minikube, 'addons', 'disable', 'ingress']) except: pass run_command([minikube, 'addons', 'enable', 'ingress'])
def start_cluster(self): """ Starts the cluster unless it has been already started by the user. :param minikube: Executable minikube installed beforehand. """ status = run_command([MINIKUBE_EXECUTABLE, 'status'], True) if 'minikube: Running' in status: print('Cluster already running') else: run_command([MINIKUBE_EXECUTABLE, 'start', '--memory=2048', '--cpus=2'])
def run(use_minikube, server_wait=True, capture_output=False): logging.basicConfig() sys.path.append(os.path.join(ROOT_DIR_LOCATION, 'example_project')) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings") run_command(['pip', 'install', '-e', ROOT_DIR_LOCATION], capture_output=capture_output) run_command(['python', _MANAGE_PY, 'migrate', '--noinput'], capture_output=capture_output) run_command(['python', _MANAGE_PY, 'collectstatic', '--noinput'], capture_output=capture_output) create_superuser_if_missing(username='******', password='******') server_args = [] if use_minikube: # Import minikube here, so we can install the deps first parent_dir = os.path.dirname(os.path.dirname( os.path.abspath(__file__))) sys.path.append(os.path.join(parent_dir, 'aimmo_runner')) os.chdir(ROOT_DIR_LOCATION) run_command([ 'pip', 'install', '-r', os.path.join(ROOT_DIR_LOCATION, 'minikube_requirements.txt') ], capture_output=capture_output) from aimmo_runner import minikube minikube.start() server_args.append('0.0.0.0:8000') os.environ['AIMMO_MODE'] = 'minikube' else: time.sleep(2) game = run_command_async(['python', _SERVICE_PY, '127.0.0.1', '5000'], capture_output=capture_output) PROCESSES.append(game) os.environ['AIMMO_MODE'] = 'threads' os.environ['NODE_ENV'] = 'development' if settings.DEBUG else 'production' server = run_command_async(['python', _MANAGE_PY, 'runserver'] + server_args, capture_output=capture_output) frontend_bundler = run_command_async(['node', _FRONTEND_BUNDLER_JS], capture_output=capture_output) PROCESSES.append(server) PROCESSES.append(frontend_bundler) if server_wait is True: try: game.wait() except NameError: pass server.wait() return PROCESSES
def build_docker_images(minikube): print('Building docker images') raw_env_settings = run_command([minikube, 'docker-env', '--shell="bash"'], True) matches = re.finditer(r'^export (.+)="(.+)"$', raw_env_settings, re.MULTILINE) env = dict([(m.group(1), m.group(2)) for m in matches]) client = docker.from_env( environment=env, version='auto', ) dirs = ('aimmo-game', 'aimmo-game-creator', 'aimmo-game-worker') for dir in dirs: path = os.path.join(BASE_DIR, dir) tag = 'ocadotechnology/%s:test' % dir print("Building %s..." % tag) client.images.build(path=path, tag=tag, encoding='gzip')
def run(use_minikube, use_vagrant=False, server_wait=True, capture_output=False): logging.basicConfig() sys.path.append(os.path.join(ROOT_DIR_LOCATION, 'example_project')) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings") run_command(['pip', 'install', '-e', ROOT_DIR_LOCATION], capture_output=capture_output) run_command(['python', _MANAGE_PY, 'migrate', '--noinput'], capture_output=capture_output) run_command(['python', _MANAGE_PY, 'collectstatic', '--noinput'], capture_output=capture_output) create_superuser_if_missing(username='******', password='******') if use_minikube: setup_minikube(capture_output) elif use_vagrant: setup_vagrant(capture_output) else: time.sleep(2) game = run_command_async(['python', _SERVICE_PY, '127.0.0.1', '5000'], capture_output=capture_output) PROCESSES.append(game) os.environ['AIMMO_MODE'] = 'threads' os.environ['NODE_ENV'] = 'development' if settings.DEBUG else 'production' if use_vagrant: server = run_command_async( ['python', _MANAGE_PY, 'runserver', '0.0.0.0:8000'], capture_output=capture_output) else: server = run_command_async(['python', _MANAGE_PY, 'runserver'], capture_output=capture_output) frontend_bundler = run_command_async(['node', _FRONTEND_BUNDLER_JS], capture_output=capture_output) PROCESSES.append(server) PROCESSES.append(frontend_bundler) if server_wait is True: try: game.wait() except NameError: pass server.wait() return PROCESSES
def create_docker_client(self): """ Creates a docker client using the python SDK. :param raw_env_settings: String that is returned by the 'minikube docker-env' command. :return: """ raw_env_settings = run_command( [MINIKUBE_EXECUTABLE, 'docker-env', '--shell="bash"'], True) if self.vm_none_enabled(raw_env_settings): matches = re.finditer(r'^export (.+)="(.+)"$', raw_env_settings, re.MULTILINE) env_variables = dict([(m.group(1), m.group(2)) for m in matches]) return docker.from_env( environment=env_variables, version='auto', ) else: # VM driver is set return docker.from_env(version='auto')
def create_docker_client(use_raw_env=False, minikube=None): """ Create a docker client using the python SDK. :param raw_env_settings: String that is returned by the 'minikube docker-env' command. :return: """ if use_raw_env: raw_env_settings = run_command( [minikube, 'docker-env', '--shell="bash"'], True) matches = re.finditer(r'^export (.+)="(.+)"$', raw_env_settings, re.MULTILINE) env_variables = dict([(m.group(1), m.group(2)) for m in matches]) else: # VM driver is set env_variables = os.environ env_variables['DOCKER_BUILDKIT'] = "1" return docker.from_env( environment=env_variables, version='auto', )
def start_cluster(minikube): status = run_command([minikube, 'status'], True) if 'minikube: Running' in status: print('Cluster already running') else: run_command([minikube, 'start', '--memory=2048', '--cpus=2'])
def restart_ingress_addon(minikube): try: run_command([minikube, 'addons', 'disable', 'ingress']) except: pass run_command([minikube, 'addons', 'enable', 'ingress'])
def run(use_minikube, server_wait=True, capture_output=False, test_env=False, build_target=None): logging.basicConfig() if test_env: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_settings") else: sys.path.append(os.path.join(ROOT_DIR_LOCATION, 'example_project')) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings") django.setup() run_command(['pip', 'install', '-e', ROOT_DIR_LOCATION], capture_output=capture_output) if not test_env: run_command(['python', _MANAGE_PY, 'migrate', '--noinput'], capture_output=capture_output) run_command(['python', _MANAGE_PY, 'collectstatic', '--noinput'], capture_output=capture_output) create_superuser_if_missing(username='******', password='******') server_args = [] if use_minikube: parent_dir = os.path.dirname(os.path.dirname( os.path.abspath(__file__))) sys.path.append(os.path.join(parent_dir, 'aimmo_runner')) os.chdir(ROOT_DIR_LOCATION) run_command([ 'pip', 'install', '-r', os.path.join(ROOT_DIR_LOCATION, 'minikube_requirements.txt') ], capture_output=capture_output) # Import minikube here, so we can install the dependencies first from aimmo_runner import minikube minikube.start(build_target=build_target) server_args.append('0.0.0.0:8000') os.environ['AIMMO_MODE'] = 'minikube' else: time.sleep(2) os.environ['AIMMO_MODE'] = 'threads' docker_scripts.delete_containers() if build_target == 'tester': run_command(['python', 'all_tests.py']) else: docker_scripts.build_docker_images(build_target=build_target) docker_scripts.start_game_creator() os.environ['NODE_ENV'] = 'development' if settings.DEBUG else 'production' server = run_command_async(['python', _MANAGE_PY, 'runserver'] + server_args, capture_output=capture_output) frontend_bundler = run_command_async(['node', _FRONTEND_BUNDLER_JS], capture_output=capture_output) PROCESSES.append(server) PROCESSES.append(frontend_bundler) if server_wait is True: try: game.wait() except NameError: pass server.wait() return PROCESSES
def create_roles(): """ Applies the service accounts, roles, and bindings for restricting the rights of certain pods and their processses. """ run_command(['kubectl', 'apply', '-Rf', 'rbac'])