Beispiel #1
0
def cmd_export(param):
    """
    Creates an archive of all relevant app files, incl. Kployfile and manifest directories.
    You can use the resulting archive then with `kploy init` to bootstrap you app in a different location.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE: logging.info("Exporting app based on content from %s " %(here))
    try:
        kploy, _  = util.load_yaml(filename=kployfile)
        if not param:
            param = EXPORT_ARCHIVE_FILENAME
        archive_filename, archive_file = kploycommon._export_init(here, DEPLOYMENT_DESCRIPTOR, param)
        print("Adding content of app `%s/%s` to %s" %(kploy["namespace"], kploy["name"], archive_filename))
        rc_manifests_confirmed, svc_manifests_confirmed = [], []
        services = os.path.join(here, SVC_DIR)
        rcs = os.path.join(here, RC_DIR)
        svc_list = kploycommon._visit(services, 'service', cache_remotes=True)
        rc_list = kploycommon._visit(rcs, 'RC', cache_remotes=True)
        res_list = []
        for svc in svc_list:
            svc_file_name = os.path.join(SVC_DIR, svc)
            kploycommon._export_add(archive_file, svc_file_name)
        for rc in rc_list:
            rc_file_name = os.path.join(RC_DIR, rc)
            kploycommon._export_add(archive_file, rc_file_name)
        kploycommon._export_done(archive_file)
    except (Exception) as e:
        print("Something went wrong:\n%s" %(e))
        print("Consider validating your deployment with `kploy dryrun` first!")
        sys.exit(1)
Beispiel #2
0
def cmd_export(param):
    """
    Creates an archive of all relevant app files, incl. Kployfile and manifest directories.
    You can use the resulting archive then with `kploy init` to bootstrap you app in a different location.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE:
        logging.info("Exporting app based on content from %s " % (here))
    try:
        kploy, _ = util.load_yaml(filename=kployfile)
        if not param:
            param = EXPORT_ARCHIVE_FILENAME
        archive_filename, archive_file = kploycommon._export_init(
            here, DEPLOYMENT_DESCRIPTOR, param)
        print("Adding content of app `%s/%s` to %s" %
              (kploy["namespace"], kploy["name"], archive_filename))
        rc_manifests_confirmed, svc_manifests_confirmed = [], []
        services = os.path.join(here, SVC_DIR)
        rcs = os.path.join(here, RC_DIR)
        svc_list = kploycommon._visit(services, 'service', cache_remotes=True)
        rc_list = kploycommon._visit(rcs, 'RC', cache_remotes=True)
        res_list = []
        for svc in svc_list:
            svc_file_name = os.path.join(SVC_DIR, svc)
            kploycommon._export_add(archive_file, svc_file_name)
        for rc in rc_list:
            rc_file_name = os.path.join(RC_DIR, rc)
            kploycommon._export_add(archive_file, rc_file_name)
        kploycommon._export_done(archive_file)
    except (Exception) as e:
        print("Something went wrong:\n%s" % (e))
        print("Consider validating your deployment with `kploy dryrun` first!")
        sys.exit(1)
Beispiel #3
0
def cmd_destroy(param):
    """
    Destroys the app, removing all resources.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE: logging.info("Trying to destroy app based on %s " %(kployfile))
    try:
        kploy, _  = util.load_yaml(filename=kployfile)
        logging.debug(kploy)
        pyk_client = kploycommon._connect(api_server=kploy["apiserver"], debug=DEBUG)
        # delete all services and RCs:
        rc_manifests_confirmed, svc_manifests_confirmed = [], []
        services = os.path.join(here, SVC_DIR)
        rcs = os.path.join(here, RC_DIR)
        svc_manifests_confirmed = kploycommon._visit(services, 'service', cache_remotes=True)
        rc_manifests_confirmed = kploycommon._visit(rcs, 'RC', cache_remotes=True)
        kploycommon._destroy(pyk_client, kploy["namespace"], here, SVC_DIR, svc_manifests_confirmed, 'service', VERBOSE)
        kploycommon._destroy(pyk_client, kploy["namespace"], here, RC_DIR, rc_manifests_confirmed, 'RC', VERBOSE)
        # delete secrets:
        secret_path = "".join(["/api/v1/namespaces/", kploy["namespace"], "/secrets/kploy-secrets"])
        pyk_client.delete_resource(resource_path=secret_path)
        # delete the namespace:
        ns_path = "".join(["/api/v1/namespaces/", kploy["namespace"]])
        pyk_client.delete_resource(resource_path=ns_path)
    except (Exception) as e:
        print("Something went wrong destroying your app:\n%s" %(e))
        print("Consider validating your deployment with `kploy dryrun` first!")
        sys.exit(1)
    print(80*"=")
    print("\nOK, I've destroyed `%s/%s`\n" %(kploy["namespace"], kploy["name"]))
Beispiel #4
0
def cmd_list(param):
    """
    Lists app resources and their status.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE: logging.info("Listing resource status of app based on %s " %(kployfile))
    try:
        kploy, _  = util.load_yaml(filename=kployfile)
        print("Resources of app `%s/%s`:\n" %(kploy["namespace"], kploy["name"]))
        pyk_client = kploycommon._connect(api_server=kploy["apiserver"], debug=DEBUG)
        rc_manifests_confirmed, svc_manifests_confirmed = [], []
        services = os.path.join(here, SVC_DIR)
        rcs = os.path.join(here, RC_DIR)
        svc_list = kploycommon._visit(services, 'service', cache_remotes=True)
        rc_list = kploycommon._visit(rcs, 'RC', cache_remotes=True)
        res_list = []
        # gather Services status:
        print("[Services and RCs]\n")
        for svc in svc_list:
            svc_manifest, _  = util.load_yaml(filename=os.path.join(here, SVC_DIR, svc))
            svc_name = svc_manifest["metadata"]["name"]
            svc_path = "".join(["/api/v1/namespaces/", kploy["namespace"], "/services/", svc_name])
            svc_URL = "".join([kploy["apiserver"], svc_path])
            svc_status = kploycommon._check_status(pyk_client, svc_path)
            res_list.append([svc_name, os.path.join(SVC_DIR, svc), "service", svc_status, svc_URL])
        # gather RC status:
        for rc in rc_list:
            rc_manifest, _  = util.load_yaml(filename=os.path.join(here, RC_DIR, rc))
            rc_name = rc_manifest["metadata"]["name"]
            rc_path = "".join(["/api/v1/namespaces/", kploy["namespace"], "/replicationcontrollers/", rc_name])
            rc_URL = "".join([kploy["apiserver"], rc_path])
            rc_status = kploycommon._check_status(pyk_client, rc_path)
            res_list.append([rc_name, os.path.join(RC_DIR, rc), "RC", rc_status, rc_URL])
        print(tabulate(res_list, ["NAME", "MANIFEST", "TYPE", "STATUS", "URL"], tablefmt="plain"))
        # gather Secrets status:
        print("\n" + 80*"=")
        print("[Secrets]")
        sec_list = []
        secret_path = "".join(["/api/v1/namespaces/", kploy["namespace"], "/secrets/kploy-secrets"])
        sec_URL = "".join([kploy["apiserver"], secret_path])
        secret = pyk_client.describe_resource(secret_path)
        if secret.status_code == 200:
            print("URL: %s" %(sec_URL))
            secret_data = secret.json()["data"]
            for k, v in secret_data.iteritems():
                sec_list.append([k, base64.b64decode(v)])
            print(tabulate(sec_list, ["KEY", "VALUE"], tablefmt="plain"))
        else:
            print("No env data deployed.")
        print("\n" + 80*"=")
    except (Exception) as e:
        print("Something went wrong:\n%s" %(e))
        print("Consider validating your deployment with `kploy dryrun` first!")
        sys.exit(1)
Beispiel #5
0
def cmd_run(param):
    """
    Looks for a `Kployfile` file in the current directory and tries to run it.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE: logging.info("Trying to run %s " % (kployfile))
    try:
        kploy, _ = util.load_yaml(filename=kployfile)
        logging.debug(kploy)
        pyk_client = kploycommon._connect(api_server=kploy["apiserver"],
                                          debug=DEBUG)
        # set up a Namespace for this app:
        kploycommon._create_ns(pyk_client, kploy["namespace"], VERBOSE)
        # set up a Secrets for this app:
        env = os.path.join(here, ENV_DIR)
        secrets = {}
        logging.debug("Visiting %s" % env)
        for _, _, file_names in os.walk(env):
            for afile in file_names:
                if afile.endswith(SECRETS_FILE_EXT):
                    logging.debug("Got a secret input: %s" % (afile))
                    key = os.path.splitext(afile)[0]
                    logging.debug("Secret key: %s" % (key))
                    with open(os.path.join(env, afile), "r") as sec_file:
                        raw_data = sec_file.read().strip()
                    logging.debug("Secret data: %s" % (raw_data))
                    val = base64.b64encode(raw_data)
                    logging.debug("Secret base64 encoded data: %s" % (val))
                    secrets[key] = val
        kploycommon._create_secrets(pyk_client, kploy["name"],
                                    kploy["namespace"], secrets, VERBOSE)
        # collect Services and RCs ...
        rc_manifests_confirmed, svc_manifests_confirmed = [], []
        services = os.path.join(here, SVC_DIR)
        rcs = os.path.join(here, RC_DIR)
        svc_manifests_confirmed = kploycommon._visit(
            services, 'service', cache_remotes=kploy["cache_remotes"])
        rc_manifests_confirmed = kploycommon._visit(
            rcs, 'RC', cache_remotes=kploy["cache_remotes"])
        # ... and deploy them:
        kploycommon._deploy(pyk_client, kploy["namespace"], here, SVC_DIR,
                            svc_manifests_confirmed, 'service', VERBOSE)
        kploycommon._deploy(pyk_client, kploy["namespace"], here, RC_DIR,
                            rc_manifests_confirmed, 'RC', VERBOSE)
    except (Exception) as e:
        print("Something went wrong deploying your app:\n%s" % (e))
        print("Consider validating your deployment with `kploy dryrun` first!")
        sys.exit(1)
    print(80 * "=")
    print(
        "\nOK, I've deployed `%s/%s`.\nUse `kploy list` and `kploy stats` to check how it's doing."
        % (kploy["namespace"], kploy["name"]))
Beispiel #6
0
def cmd_run(param):
    """
    Looks for a `Kployfile` file in the current directory and tries to run it.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE: logging.info("Trying to run %s " %(kployfile))
    try:
        kploy, _  = util.load_yaml(filename=kployfile)
        logging.debug(kploy)
        pyk_client = kploycommon._connect(api_server=kploy["apiserver"], debug=DEBUG)
        # set up a Namespace for this app:
        kploycommon._create_ns(pyk_client, kploy["namespace"], VERBOSE)
        # set up a Secrets for this app:
        env = os.path.join(here, ENV_DIR)
        secrets = {}
        logging.debug("Visiting %s" %env)
        for _, _, file_names in os.walk(env):
            for afile in file_names:
                if afile.endswith(SECRETS_FILE_EXT):
                    logging.debug("Got a secret input: %s" %(afile))
                    key = os.path.splitext(afile)[0]
                    logging.debug("Secret key: %s" %(key))
                    with open(os.path.join(env, afile), "r") as sec_file:
                        raw_data = sec_file.read().strip()
                    logging.debug("Secret data: %s" %(raw_data))
                    val = base64.b64encode(raw_data)
                    logging.debug("Secret base64 encoded data: %s" %(val))
                    secrets[key] = val
        kploycommon._create_secrets(pyk_client, kploy["name"], kploy["namespace"], secrets, VERBOSE)
        # collect Services and RCs ...
        rc_manifests_confirmed, svc_manifests_confirmed = [], []
        services = os.path.join(here, SVC_DIR)
        rcs = os.path.join(here, RC_DIR)
        svc_manifests_confirmed = kploycommon._visit(services, 'service', cache_remotes=kploy["cache_remotes"])
        rc_manifests_confirmed = kploycommon._visit(rcs, 'RC', cache_remotes=kploy["cache_remotes"])
        # ... and deploy them:
        kploycommon._deploy(pyk_client, kploy["namespace"], here, SVC_DIR, svc_manifests_confirmed, 'service', VERBOSE)
        kploycommon._deploy(pyk_client, kploy["namespace"], here, RC_DIR, rc_manifests_confirmed, 'RC', VERBOSE)
    except (Exception) as e:
        print("Something went wrong deploying your app:\n%s" %(e))
        print("Consider validating your deployment with `kploy dryrun` first!")
        sys.exit(1)
    print(80*"=")
    print("\nOK, I've deployed `%s/%s`.\nUse `kploy list` and `kploy stats` to check how it's doing." %(kploy["namespace"], kploy["name"]))
Beispiel #7
0
def cmd_dryrun(param):
    """
    Looks for a `Kployfile` file in the current directory and tries
    to validate its content, incl. syntax validation and mock execution.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE: logging.info("Trying to execute a dry run on %s " %(kployfile))
    try:
        kploy, _  = util.load_yaml(filename=kployfile)
        logging.debug(kploy)
        print("Validating application `%s/%s` ..." %(kploy["namespace"], kploy["name"]))

        print("\n  CHECK: Is the Kubernetes cluster up & running and accessible via `%s`?" %(kploy["apiserver"]))
        pyk_client = kploycommon._connect(api_server=kploy["apiserver"], debug=DEBUG)
        nodes = pyk_client.execute_operation(method="GET", ops_path="/api/v1/nodes")
        if VERBOSE: logging.info("Got node list %s " %(util.serialize_tojson(nodes.json())))
        print("  \o/ ... I found %d node(s) to deploy your wonderful app onto." %(len(nodes.json()["items"])))

        print("\n  CHECK: Are there RC and service manifests available around here?")
        try:
            rcs = os.path.join(here, RC_DIR)
            logging.debug("Asserting %s exists" %(os.path.dirname(rcs)))
            assert os.path.exists(rcs)
            rc_manifests_confirmed = kploycommon._visit(rcs, "RC", cache_remotes=kploy["cache_remotes"])
            print("         I found %s RC manifest(s) in %s" %(int(len(rc_manifests_confirmed)), os.path.dirname(rcs)))
            if VERBOSE: kploycommon._dump(rc_manifests_confirmed)

            services = os.path.join(here, SVC_DIR)
            logging.debug("Asserting %s exists" %(os.path.dirname(services)))
            assert os.path.exists(services)
            svc_manifests_confirmed = kploycommon._visit(services, "service", cache_remotes=kploy["cache_remotes"])
            print("         I found %s service manifest(s) in %s" %(int(len(svc_manifests_confirmed)), os.path.dirname(services)))
            if VERBOSE: kploycommon._dump(svc_manifests_confirmed)
            print("  \o/ ... I found both RC and service manifests to deploy your wonderful app!")
        except:
            print("No RC and/or service manifests found to deploy your app. You can use `kploy init` to create missing artefacts.")
            sys.exit(1)
    except (IOError, IndexError, KeyError) as e:
        print("Something went wrong:\n%s" %(e))
        sys.exit(1)
    print(80*"=")
    print("\nOK, we're looking good! You're ready to deploy your app with `kploy run` now :)\n")
Beispiel #8
0
def cmd_destroy(param):
    """
    Destroys the app, removing all resources.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE:
        logging.info("Trying to destroy app based on %s " % (kployfile))
    try:
        kploy, _ = util.load_yaml(filename=kployfile)
        logging.debug(kploy)
        pyk_client = kploycommon._connect(api_server=kploy["apiserver"],
                                          debug=DEBUG)
        # delete all services and RCs:
        rc_manifests_confirmed, svc_manifests_confirmed = [], []
        services = os.path.join(here, SVC_DIR)
        rcs = os.path.join(here, RC_DIR)
        svc_manifests_confirmed = kploycommon._visit(services,
                                                     'service',
                                                     cache_remotes=True)
        rc_manifests_confirmed = kploycommon._visit(rcs,
                                                    'RC',
                                                    cache_remotes=True)
        kploycommon._destroy(pyk_client, kploy["namespace"], here, SVC_DIR,
                             svc_manifests_confirmed, 'service', VERBOSE)
        kploycommon._destroy(pyk_client, kploy["namespace"], here, RC_DIR,
                             rc_manifests_confirmed, 'RC', VERBOSE)
        # delete secrets:
        secret_path = "".join([
            "/api/v1/namespaces/", kploy["namespace"], "/secrets/kploy-secrets"
        ])
        pyk_client.delete_resource(resource_path=secret_path)
        # delete the namespace:
        ns_path = "".join(["/api/v1/namespaces/", kploy["namespace"]])
        pyk_client.delete_resource(resource_path=ns_path)
    except (Exception) as e:
        print("Something went wrong destroying your app:\n%s" % (e))
        print("Consider validating your deployment with `kploy dryrun` first!")
        sys.exit(1)
    print(80 * "=")
    print("\nOK, I've destroyed `%s/%s`\n" %
          (kploy["namespace"], kploy["name"]))
Beispiel #9
0
def cmd_dryrun(param):
    """
    Looks for a `Kployfile` file in the current directory and tries
    to validate its content, incl. syntax validation and mock execution.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE:
        logging.info("Trying to execute a dry run on %s " % (kployfile))
    try:
        kploy, _ = util.load_yaml(filename=kployfile)
        logging.debug(kploy)
        print("Validating application `%s/%s` ..." %
              (kploy["namespace"], kploy["name"]))

        print(
            "\n  CHECK: Is the Kubernetes cluster up & running and accessible via `%s`?"
            % (kploy["apiserver"]))
        pyk_client = kploycommon._connect(api_server=kploy["apiserver"],
                                          debug=DEBUG)
        nodes = pyk_client.execute_operation(method="GET",
                                             ops_path="/api/v1/nodes")
        if VERBOSE:
            logging.info("Got node list %s " %
                         (util.serialize_tojson(nodes.json())))
        print(
            "  \o/ ... I found %d node(s) to deploy your wonderful app onto." %
            (len(nodes.json()["items"])))

        print(
            "\n  CHECK: Are there RC and service manifests available around here?"
        )
        try:
            rcs = os.path.join(here, RC_DIR)
            logging.debug("Asserting %s exists" % (os.path.dirname(rcs)))
            assert os.path.exists(rcs)
            rc_manifests_confirmed = kploycommon._visit(
                rcs, "RC", cache_remotes=kploy["cache_remotes"])
            print("         I found %s RC manifest(s) in %s" %
                  (int(len(rc_manifests_confirmed)), os.path.dirname(rcs)))
            if VERBOSE: kploycommon._dump(rc_manifests_confirmed)

            services = os.path.join(here, SVC_DIR)
            logging.debug("Asserting %s exists" % (os.path.dirname(services)))
            assert os.path.exists(services)
            svc_manifests_confirmed = kploycommon._visit(
                services, "service", cache_remotes=kploy["cache_remotes"])
            print(
                "         I found %s service manifest(s) in %s" %
                (int(len(svc_manifests_confirmed)), os.path.dirname(services)))
            if VERBOSE: kploycommon._dump(svc_manifests_confirmed)
            print(
                "  \o/ ... I found both RC and service manifests to deploy your wonderful app!"
            )
        except:
            print(
                "No RC and/or service manifests found to deploy your app. You can use `kploy init` to create missing artefacts."
            )
            sys.exit(1)
    except (IOError, IndexError, KeyError) as e:
        print("Something went wrong:\n%s" % (e))
        sys.exit(1)
    print(80 * "=")
    print(
        "\nOK, we're looking good! You're ready to deploy your app with `kploy run` now :)\n"
    )
Beispiel #10
0
def cmd_list(param):
    """
    Lists app resources and their status.
    """
    here = os.path.realpath(".")
    kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR)
    if VERBOSE:
        logging.info("Listing resource status of app based on %s " %
                     (kployfile))
    try:
        kploy, _ = util.load_yaml(filename=kployfile)
        print("Resources of app `%s/%s`:\n" %
              (kploy["namespace"], kploy["name"]))
        pyk_client = kploycommon._connect(api_server=kploy["apiserver"],
                                          debug=DEBUG)
        rc_manifests_confirmed, svc_manifests_confirmed = [], []
        services = os.path.join(here, SVC_DIR)
        rcs = os.path.join(here, RC_DIR)
        svc_list = kploycommon._visit(services, 'service', cache_remotes=True)
        rc_list = kploycommon._visit(rcs, 'RC', cache_remotes=True)
        res_list = []
        # gather Services status:
        print("[Services and RCs]\n")
        for svc in svc_list:
            svc_manifest, _ = util.load_yaml(
                filename=os.path.join(here, SVC_DIR, svc))
            svc_name = svc_manifest["metadata"]["name"]
            svc_path = "".join([
                "/api/v1/namespaces/", kploy["namespace"], "/services/",
                svc_name
            ])
            svc_URL = "".join([kploy["apiserver"], svc_path])
            svc_status = kploycommon._check_status(pyk_client, svc_path)
            res_list.append([
                svc_name,
                os.path.join(SVC_DIR, svc), "service", svc_status, svc_URL
            ])
        # gather RC status:
        for rc in rc_list:
            rc_manifest, _ = util.load_yaml(
                filename=os.path.join(here, RC_DIR, rc))
            rc_name = rc_manifest["metadata"]["name"]
            rc_path = "".join([
                "/api/v1/namespaces/", kploy["namespace"],
                "/replicationcontrollers/", rc_name
            ])
            rc_URL = "".join([kploy["apiserver"], rc_path])
            rc_status = kploycommon._check_status(pyk_client, rc_path)
            res_list.append(
                [rc_name,
                 os.path.join(RC_DIR, rc), "RC", rc_status, rc_URL])
        print(
            tabulate(res_list, ["NAME", "MANIFEST", "TYPE", "STATUS", "URL"],
                     tablefmt="plain"))
        # gather Secrets status:
        print("\n" + 80 * "=")
        print("[Secrets]")
        sec_list = []
        secret_path = "".join([
            "/api/v1/namespaces/", kploy["namespace"], "/secrets/kploy-secrets"
        ])
        sec_URL = "".join([kploy["apiserver"], secret_path])
        secret = pyk_client.describe_resource(secret_path)
        if secret.status_code == 200:
            print("URL: %s" % (sec_URL))
            secret_data = secret.json()["data"]
            for k, v in secret_data.iteritems():
                sec_list.append([k, base64.b64decode(v)])
            print(tabulate(sec_list, ["KEY", "VALUE"], tablefmt="plain"))
        else:
            print("No env data deployed.")
        print("\n" + 80 * "=")
    except (Exception) as e:
        print("Something went wrong:\n%s" % (e))
        print("Consider validating your deployment with `kploy dryrun` first!")
        sys.exit(1)