def test_nebula_connection_with_host_uri(self):
        nebula_user = os.getenv("NEBULA_TEST_USERNAME", "nebula")
        nebula_password = os.getenv("NEBULA_TEST_PASSWORD", "nebula")
        nebula_host_uri = os.getenv("NEBULA_HOST_URI", "http://127.0.0.1:80")
        connection = Nebula(username=nebula_user, password=nebula_password, host_uri=nebula_host_uri)

        # check that the host param is set correctly
        self.assertEqual(connection.host, nebula_host_uri)

        # check that connects to manager successfully
        reply = connection.check_api()
        self.assertEqual(reply["status_code"], 200)
        self.assertEqual(reply["reply"]["api_available"], True)
    def __init__(self, host: Optional[str] = "127.0.0.1", username: Optional[str] = None,
                 password: Optional[str] = None, token: Optional[str] = None, port: int = 80, protocol: str = "http"):
        """Init the nebula api connection object

            Arguments:
                host -- the hostname of the nebula host, defaults to 127.0.0.1
                username -- the username to connect to nebula with, defaults to None
                password -- the password to connect to nebula with, defaults to None
                token -- the token to connect to nebula with, defaults to None
                port --the port of the nebula host, defaults to 80
                protocol -- the protocol of the nebula host, "http" or "https", default to "http"
        """
        self.nebula_connection = Nebula(host=host, username=username, password=password, token=token, port=port,
                                        protocol=protocol)
 def __init__(self, ):
     try:
         home = expanduser("~")
         auth_file = open(home + "/.nebula.json", "r")
         auth_json = json.load(auth_file)
         self.connection = Nebula(username=auth_json["username"],
                                  password=base64.b64decode(
                                      auth_json["password"]),
                                  host=auth_json["host"],
                                  port=auth_json["port"],
                                  protocol=auth_json["protocol"])
     except:
         click.echo(
             click.style(
                 "error reading ~/nebula.json auth file, try logging in first",
                 fg="red"))
         exit(2)
Example #4
0
def check_required_connections(
        registry_host, registry_auth_user, registry_auth_password,
        nebula_manager_auth_user, nebula_manager_auth_password,
        nebula_manager_host, nebula_manager_port, nebula_manager_protocol,
        nebula_manager_uri, nebula_manager_request_timeout,
        nebula_manager_auth_token):
    while True:
        time.sleep(10)
        try:
            # login to the docker registry - if no registry login details are configured will just print a message stating
            # that
            docker_socket.registry_login(registry_host=registry_host,
                                         registry_user=registry_auth_user,
                                         registry_pass=registry_auth_password)

            # login to the nebula manager
            nebula_connection = Nebula(
                username=nebula_manager_auth_user,
                password=nebula_manager_auth_password,
                host=nebula_manager_host,
                port=nebula_manager_port,
                protocol=nebula_manager_protocol,
                host_uri=nebula_manager_uri,
                request_timeout=nebula_manager_request_timeout,
                token=nebula_manager_auth_token)

            # make sure the nebula manager connects properly

            print("checking nebula manager connection")
            api_check = nebula_connection.check_api()
            if api_check["status_code"] == 200 and api_check["reply"][
                    "api_available"] is True:
                print("nebula manager connection ok")
                # case when nebula manager and docker registry both are up and connected
                return nebula_connection
            else:
                print(
                    "nebula manager initial connection check failure, retrying in some time!"
                )
        except Exception as e:
            print(e, file=sys.stderr)
            print(
                "Error confirming connection to nebula manager or docker registry - please check connection & authentication params and "
                "that the manager and docker-registry is online")
def nebula_connection():
    nebula_user = os.getenv("NEBULA_TEST_USERNAME", "nebula")
    nebula_password = os.getenv("NEBULA_TEST_PASSWORD", "nebula")
    nebula_token = os.getenv("NEBULA_TEST_TOKEN", None)
    nebula_hostname = os.getenv("NEBULA_TEST_HOST", "127.0.0.1")
    nebula_port = int(os.getenv("NEBULA_TEST_PORT", "80"))
    nebula_protocol = os.getenv("NEBULA_TEST_PROTOCOL", "http")
    nebula_request_timeout = int(os.getenv("NEBULA_TEST_REQUEST_TIMEOUT", "60"))
    connection = Nebula(username=nebula_user, password=nebula_password, host=nebula_hostname, port=nebula_port,
                        protocol=nebula_protocol, request_timeout=nebula_request_timeout, token=nebula_token)
    return connection
class NebulaCall:

    # the init step reads from the authfile, not keeping connection open as it's a CLI
    def __init__(self, ):
        try:
            home = expanduser("~")
            auth_file = open(home + "/.nebula.json", "r")
            auth_json = json.load(auth_file)
            self.connection = Nebula(username=auth_json["username"],
                                     password=base64.b64decode(
                                         auth_json["password"]),
                                     host=auth_json["host"],
                                     port=auth_json["port"],
                                     protocol=auth_json["protocol"])
        except:
            click.echo(
                click.style(
                    "error reading ~/nebula.json auth file, try logging in first",
                    fg="red"))
            exit(2)

    def create_app(self, app, config):
        reply = self.connection.create_app(app, config)
        if reply.status_code == 202:
            click.echo(click.style("creating nebula app: " + app, fg="green"))
        elif reply.status_code == 400:
            click.echo(
                click.style("error creating " + app +
                            ", missing\incorrect parameters",
                            fg="red"))
        elif reply.status_code == 403:
            click.echo(
                click.style("error creating " + app + ", app already exist",
                            fg="red"))
        else:
            click.echo(
                click.style(
                    "error creating " + app +
                    ", are you logged in? did you sent the right params & app name?",
                    fg="red"))

    def delete_app(self, app):
        reply = self.connection.delete_app(app)
        if reply.status_code == 202:
            click.echo(click.style("deleting nebula app: " + app,
                                   fg="magenta"))
        elif reply.status_code == 403:
            click.echo(
                click.style("error deleting " + app + ", app doesn't exist",
                            fg="red"))
        else:
            click.echo(
                click.style(
                    "error deleting " + app +
                    ", are you logged in? did you sent the right app name?",
                    fg="red"))

    def list_apps(self):
        reply = self.connection.list_apps()
        if reply.status_code == 200:
            reply_json = reply.json()
            if len(reply_json["apps"]) == 0:
                click.echo("no apps in nebula cluster")
            else:
                click.echo("nebula cluster apps:")
                for app in reply_json["apps"]:
                    click.echo(app)
        else:
            click.echo(
                click.style(
                    "error retuning list of nebula apps, are you logged in?",
                    fg="red"))

    def check_api(self):
        reply = self.connection.check_api()
        if reply.status_code == 200:
            reply_json = reply.json()
            if reply_json == {'api_available': 'True'}:
                click.echo("nebula responding as expected")
            else:
                click.echo(
                    click.style(
                        "nebula api not responding, are you logged in?",
                        fg="red"))
        else:
            click.echo(
                click.style("nebula api not responding, are you logged in?",
                            fg="red"))

    def list_app_info(self, app):
        reply = self.connection.list_app_info(app)
        reply_json = reply.json()
        if reply.status_code == 200:
            for key, value in reply_json.items():
                click.echo(str(key) + ": " + json.dumps(value))
        else:
            click.echo(
                click.style(
                    "error listing " + app +
                    " info, are you logged in? did you sent the right app name?",
                    fg="red"))

    def stop_app(self, app):
        reply = self.connection.stop_app(app)
        if reply.status_code == 202:
            click.echo("stopping nebula app: " + app)
        else:
            click.echo(
                click.style(
                    "error stopping " + app +
                    ", are you logged in? did you sent the right app name?",
                    fg="red"))

    def start_app(self, app):
        reply = self.connection.start_app(app)
        if reply.status_code == 202:
            click.echo("starting nebula app: " + app)
        else:
            click.echo(
                click.style(
                    "error starting " + app +
                    ", are you logged in? did you sent the right app name?",
                    fg="red"))

    def restart_app(self, app):
        reply = self.connection.restart_app(app)
        if reply.status_code == 202:
            click.echo(
                click.style("restarting nebula app: " + app, fg="yellow"))
        else:
            click.echo(
                click.style(
                    "error restarting " + app +
                    ", are you logged in? did you sent the right app name?",
                    fg="red"))

    def update_app(self, app, config):
        reply = self.connection.update_app(app, config)
        if reply.status_code == 202:
            click.echo("updating nebula app: " + app)
        elif reply.status_code == 400:
            click.echo(
                click.style("error updating " + app +
                            ", missing\incorrect parameters",
                            fg="red"))
        else:
            click.echo(
                click.style(
                    "error updating " + app +
                    ", are you logged in? did you sent the right params & app name?",
                    fg="red"))

    def roll_app(self, app):
        reply = self.connection.roll_app(app)
        if reply.status_code == 202:
            click.echo(click.style("rolling nebula app: " + app, fg="yellow"))
        else:
            click.echo(
                click.style(
                    "error rolling " + app +
                    ", are you logged in? did you sent the right app name?",
                    fg="red"))
Example #7
0
        total_memory_size_in_mb = get_total_memory_size_in_mb()

        # work against docker socket
        docker_socket = DockerFunctions()

        # ensure default "nebula" named network exists
        docker_socket.create_docker_network("nebula", "bridge")

        # login to the docker registry - if no registry login details are configured will just print a message stating
        # that
        docker_socket.registry_login(registry_host=registry_host, registry_user=registry_auth_user,
                                     registry_pass=registry_auth_password)

        # login to the nebula manager
        nebula_connection = Nebula(username=nebula_manager_auth_user, password=nebula_manager_auth_password,
                                   host=nebula_manager_host, port=nebula_manager_port, protocol=nebula_manager_protocol,
                                   host_uri=nebula_manager_uri, request_timeout=nebula_manager_request_timeout,
                                   token=nebula_manager_auth_token)

        # make sure the nebula manager connects properly
        try:
            print("checking nebula manager connection")
            api_check = nebula_connection.check_api()
            if api_check["status_code"] == 200 and api_check["reply"]["api_available"] is True:
                print("nebula manager connection ok")
            else:
                print("nebula manager initial connection check failure, dropping container")
                os._exit(2)
        except Exception as e:
            print(e, file=sys.stderr)
            print("error confirming connection to nebula manager - please check connection & authentication params and "
                  "that the manager is online")
class NebulaDeploy:

    def __init__(self, host: Optional[str] = "127.0.0.1", username: Optional[str] = None,
                 password: Optional[str] = None, token: Optional[str] = None, port: int = 80, protocol: str = "http"):
        """Init the nebula api connection object

            Arguments:
                host -- the hostname of the nebula host, defaults to 127.0.0.1
                username -- the username to connect to nebula with, defaults to None
                password -- the password to connect to nebula with, defaults to None
                token -- the token to connect to nebula with, defaults to None
                port --the port of the nebula host, defaults to 80
                protocol -- the protocol of the nebula host, "http" or "https", default to "http"
        """
        self.nebula_connection = Nebula(host=host, username=username, password=password, token=token, port=port,
                                        protocol=protocol)

    def check_nebula_app_exists(self, job_name: str) -> bool:
        """Checks if a given nebula app exists or not & raise an error if it can't tell

            Arguments:
                job_name -- a string to apply the templating to without a prefixed slash (/)
            Returns:
                True if the app exists, False if it's not
        """
        response = self.nebula_connection.list_app_info(job_name)
        if response["status_code"] == 200:
            return True
        elif response["status_code"] == 403:
            return False
        else:
            print(response)
            print("failed checking nebula app status")
            raise Exception

    def create_nebula_app(self, job_json: dict) -> dict:
        """creates a nebula app & raise an error if it can't

            Arguments:
                job_json -- a dict of the app JSON description (string because it's taken directly from a file)
            Returns:
                response_json -- the response JSON returned from nebula
        """
        response = self.nebula_connection.create_app(job_json["app_name"], job_json)
        if response["status_code"] == 200:
            return response
        else:
            print(response)
            print("failed creating nebula app")
            raise Exception

    def update_nebula_app(self, job_json: dict) -> dict:
        """Updates a nebula app & raise an error if it can't

            Arguments:
                job_json -- a dict of the app JSON description (string because it's taken directly from a file)
            Returns:
                response_json -- the response JSON returned from nebula
        """
        response = self.nebula_connection.update_app(job_json["app_name"], job_json, force_all=True)
        if response["status_code"] == 202:
            return response
        else:
            print(response)
            print("failed updating nebula app")
            raise Exception

    def create_or_update_nebula_app(self, job_json: dict) -> dict:
        """Creates a nebula app if it does not exist or updates a nebula app if it does exist

            Arguments:
                job_json -- a dict of the app JSON description
            Returns:
                response_json -- the response JSON returned from nebula
        """
        job_exists = self.check_nebula_app_exists(job_json["app_name"])

        if job_exists is False:
            response_json = self.create_nebula_app(job_json)
        elif job_exists is True:
            response_json = self.update_nebula_app(job_json)

        return response_json

    def check_nebula_cron_job_exists(self, job_name: str) -> bool:
        """Checks if a given nebula cron_job exists or not & raise an error if it can't tell

            Arguments:
                job_name -- a string to apply the templating to without a prefixed slash (/)
            Returns:
                True if the job exists, False if it's not
        """
        response = self.nebula_connection.list_cron_job_info(job_name)
        if response["status_code"] == 200:
            return True
        elif response["status_code"] == 403:
            return False
        else:
            print(response)
            print("failed checking nebula cron_job status")
            raise Exception

    def create_nebula_cron_job(self, job_json: dict) -> dict:
        """creates a nebula cron_job & raise an error if it can't

            Arguments:
                job_json -- a dict of the cron_job JSON description (string because it's taken directly from a file)
            Returns:
                response_json -- the response JSON returned from nebula
        """
        response = self.nebula_connection.create_cron_job(job_json["cron_job_name"], job_json)
        if response["status_code"] == 200:
            return response
        else:
            print(response)
            print("failed creating nebula cron_job")
            raise Exception

    def update_nebula_cron_job(self, job_json: dict) -> dict:
        """Updates a nebula cron_job & raise an error if it can't

            Arguments:
                job_json -- a dict of the cron_job JSON description (string because it's taken directly from a file)
            Returns:
                response_json -- the response JSON returned from nebula
        """
        response = self.nebula_connection.update_cron_job(job_json["cron_job_name"], job_json, force_all=True)
        if response["status_code"] == 202:
            return response
        else:
            print(response)
            print("failed updating nebula cron_job")
            raise Exception

    def create_or_update_nebula_cron_job(self, job_json: dict) -> dict:
        """Creates a nebula app if it does not exist or updates a nebula cron_job if it does exist

            Arguments:
                job_json -- a dict of the cron_job JSON description
            Returns:
                response_json -- the response JSON returned from nebula
        """
        job_exists = self.check_nebula_cron_job_exists(job_json["cron_job_name"])

        if job_exists is False:
            response_json = self.create_nebula_cron_job(job_json)
        elif job_exists is True:
            response_json = self.update_nebula_cron_job(job_json)

        return response_json