def __setup_heroku_server()

in mephisto/abstractions/architects/heroku_architect.py [0:0]


    def __setup_heroku_server(self) -> str:
        """
        Deploy the server using the setup server directory, return the URL
        """

        heroku_executable_path, heroku_user_identifier = self.__get_heroku_client()
        server_dir = self.__get_build_directory()

        print("Heroku: Starting server...")

        heroku_server_directory_path = os.path.join(server_dir, "router")
        sh.git(shlex.split(f"-C {heroku_server_directory_path} init"))

        heroku_app_name = self.__get_app_name()

        # Create or attach to the server
        return_dir = os.getcwd()
        os.chdir(heroku_server_directory_path)
        try:
            if self.args.architect.get("heroku_team", None) is not None:
                subprocess.check_output(
                    shlex.split(
                        "{} create {} --team {}".format(
                            heroku_executable_path,
                            heroku_app_name,
                            self.args.architect.heroku_team,
                        )
                    )
                )
            else:
                subprocess.check_output(
                    shlex.split(
                        "{} create {}".format(heroku_executable_path, heroku_app_name)
                    )
                )
                self.created = True
        except subprocess.CalledProcessError as e:  # User has too many apps?
            # TODO(#93) check response codes to determine what actually happened
            logger.exception(e, exc_info=True)
            sh.rm(shlex.split("-rf {}".format(heroku_server_directory_path)))
            raise Exception(
                "An exception has occurred when launching your heroku app. This "
                "can commonly occur when you have hit your limit on concurrent "
                "apps in heroku, especially if you are running multiple tasks "
                "at once. It also may occur if the app-name generated for your "
                "task is using illegal characters for heroku. Check the logs "
                "above for confirmation.\n"
                "If the issue is indeed the concurrent server cap, Please wait for"
                " some of your existing tasks to complete. If you have no tasks "
                "running, login to heroku and delete some of the running apps or "
                "verify your account to allow more concurrent apps"
            )

        # Enable WebSockets
        try:
            subprocess.check_output(
                shlex.split(
                    "{} features:enable http-session-affinity".format(
                        heroku_executable_path
                    )
                )
            )
        except subprocess.CalledProcessError:  # Already enabled WebSockets
            pass
        os.chdir(return_dir)

        # push config args, as desired
        if len(self.heroku_config_args) > 0:
            config_strs = [
                f"{config_key}={config_val}"
                for config_key, config_val in self.heroku_config_args.items()
            ]
            full_config_str = " ".join(config_strs)
            subprocess.check_output(
                shlex.split(
                    f"{heroku_executable_path} config:set -a {heroku_app_name} {full_config_str}"
                )
            )

        # commit and push to the heroku server
        sh.git(shlex.split(f"-C {heroku_server_directory_path} add -A"))
        sh.git(shlex.split(f'-C {heroku_server_directory_path} commit -m "app"'))
        sh.git(shlex.split(f"-C {heroku_server_directory_path} push -f heroku master"))

        os.chdir(heroku_server_directory_path)
        subprocess.check_output(
            shlex.split("{} ps:scale web=1".format(heroku_executable_path))
        )

        if self.args.architect.use_hobby is True:
            try:
                subprocess.check_output(
                    shlex.split("{} dyno:type Hobby".format(heroku_executable_path))
                )
            except subprocess.CalledProcessError:  # User doesn't have hobby access
                self.__delete_heroku_server()
                sh.rm(shlex.split("-rf {}".format(heroku_server_directory_path)))
                raise Exception(
                    "Server launched with hobby flag but account cannot create "
                    "hobby servers."
                )
        os.chdir(return_dir)

        time.sleep(HEROKU_WAIT_TIME)

        return "https://{}.herokuapp.com".format(heroku_app_name)