def scale_rc(self, manifest_filename, namespace="default", num_replicas=0): """ Changes the replicas of an RC based on a manifest. Note that it defaults to 0, meaning to effectively disable this RC. :Parameters: - `manifest_filename`: The manifest file containing the ReplicationController definition, for example: `manifest/nginx-webserver-rc.yaml` - `namespace`: In which namespace the RC is, defaulting to `default` - `num_replicas`: How many copies of the pods that match the selector are supposed to run """ rc_manifest, rc_manifest_json = util.load_yaml( filename=manifest_filename) logging.debug("%s" % (rc_manifest_json)) rc_path = "".join([ "/api/v1/namespaces/", namespace, "/replicationcontrollers/", rc_manifest["metadata"]["name"] ]) rc_manifest["spec"]["replicas"] = num_replicas res = self.execute_operation( method="PUT", ops_path=rc_path, payload=util.serialize_tojson(rc_manifest)) try: rc_url = res.json()["metadata"]["selfLink"] except KeyError: raise ResourceCRUDException("".join([ "Sorry, can not scale the RC: ", rc_manifest["metadata"]["name"] ])) logging.info("I scaled the RC %s at %s to %d replicas" % (rc_manifest["metadata"]["name"], rc_url, num_replicas)) return (res, rc_url)
def cmd_debug(pod_name): """ Enables you to debug a Pod by taking it offline through removing the `guard=pyk` label. Usage: `debug pod`, for example, `debug webserver-42abc`. """ if not pod_name: print("Sorry, I need a Pod name in order to do my work. Do a `kploy stats` first to glean the Pod name you want to debug, e.g. `webserver-42abc`.") print("With the Pod name you can then run `kploy debug webserver-42abc` to take the Pod offline and subsequently for example use `kubectl exec` to enter the Pod.") sys.exit(1) here = os.path.realpath(".") kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR) print("Trying to take Pod %s offline for debugging ..." %(pod_name)) try: kploy, _ = util.load_yaml(filename=kployfile) logging.debug(kploy) pyk_client = kploycommon._connect(api_server=kploy["apiserver"], debug=DEBUG) pod_path = "".join(["/api/v1/namespaces/", kploy["namespace"], "/pods/", pod_name]) pod = pyk_client.describe_resource(pod_path) resource = pod.json() resource["metadata"]["labels"] = {} logging.debug("Removed guard label from Pod, now labeled with: %s" %(resource["metadata"]["labels"])) pyk_client.execute_operation(method='PUT', ops_path=pod_path, payload=util.serialize_tojson(resource)) # now we just need to make sure that the newly created Pod is again owned by kploy: rc_name = pod_name[0:pod_name.rfind("-")] # NOTE: this is a hack, it assumes a certain generator pattern; need to figure a better way to find a Pod's RC logging.debug("Generating RC name from Pod: %s" %(rc_name)) rc_path = "".join(["/api/v1/namespaces/", kploy["namespace"], "/replicationcontrollers/", rc_name]) rc = pyk_client.describe_resource(rc_path) kploycommon._own_pods_of_rc(pyk_client, rc, kploy["namespace"], rc_path, VERBOSE) except (Exception) as e: print("Something went wrong when taking the Pod offline:\n%s" %(e)) sys.exit(1) print(80*"=") print("\nOK, the Pod %s is offline. Now you can, for example, use `kubectl exec` now to debug it." %(pod_name))
def cmd_scale(scale_def): """ Enables you to scale an RC up or down by setting the number of replicas. Usage: `scale rc=replica_count`, for example, `scale webserver-rc=10`. """ if not scale_def: print( "Sorry, I need a scale definition in order to do my work. Do a `kploy list` first to glean the RC name you want to scale, e.g. `webserver-rc`." ) print( "With the RC name you can then run `kploy scale webserver-rc=5` to scale the respective RC to 5 replicas." ) sys.exit(1) here = os.path.realpath(".") kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR) try: rc_name = scale_def.split("=")[0] replica_count = int(scale_def.split("=")[1]) except (Exception) as e: print("Can't parse scale definition `%s` due to: %s" % (scale_def, e)) print( "The scale definition should look as follows: `rc=replica_count`, for example, `scale webserver-rc=10`." ) sys.exit(1) print("Trying to scale RC %s to %d replicas" % (rc_name, replica_count)) try: kploy, _ = util.load_yaml(filename=kployfile) logging.debug(kploy) pyk_client = kploycommon._connect(api_server=kploy["apiserver"], debug=DEBUG) rc_path = "".join([ "/api/v1/namespaces/", kploy["namespace"], "/replicationcontrollers/", rc_name ]) rc = pyk_client.describe_resource(rc_path) resource = rc.json() old_replica_count = resource["spec"]["replicas"] if VERBOSE: logging.info("Scaling RC from %d to %d replicas" % (old_replica_count, replica_count)) logging.debug("RC about to be scaled: %s" % (resource)) resource["spec"]["replicas"] = replica_count pyk_client.execute_operation(method='PUT', ops_path=rc_path, payload=util.serialize_tojson(resource)) # and make sure that the newly created Pods are owned by kploy (on scale up) if replica_count > old_replica_count: logging.debug("Scaling up, trying to own new Pods") rc = pyk_client.describe_resource(rc_path) kploycommon._own_pods_of_rc(pyk_client, rc, kploy["namespace"], rc_path, VERBOSE) except (Exception) as e: print("Something went wrong when scaling RC:\n%s" % (e)) sys.exit(1) print(80 * "=") print( "OK, I've scaled RC %s to %d replicas. You can do a `kploy stats` now to verify it." % (rc_name, replica_count))
def cmd_debug(pod_name): """ Enables you to debug a Pod by taking it offline through removing the `guard=pyk` label. Usage: `debug pod`, for example, `debug webserver-42abc`. """ if not pod_name: print( "Sorry, I need a Pod name in order to do my work. Do a `kploy stats` first to glean the Pod name you want to debug, e.g. `webserver-42abc`." ) print( "With the Pod name you can then run `kploy debug webserver-42abc` to take the Pod offline and subsequently for example use `kubectl exec` to enter the Pod." ) sys.exit(1) here = os.path.realpath(".") kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR) print("Trying to take Pod %s offline for debugging ..." % (pod_name)) try: kploy, _ = util.load_yaml(filename=kployfile) logging.debug(kploy) pyk_client = kploycommon._connect(api_server=kploy["apiserver"], debug=DEBUG) pod_path = "".join( ["/api/v1/namespaces/", kploy["namespace"], "/pods/", pod_name]) pod = pyk_client.describe_resource(pod_path) resource = pod.json() resource["metadata"]["labels"] = {} logging.debug("Removed guard label from Pod, now labeled with: %s" % (resource["metadata"]["labels"])) pyk_client.execute_operation(method='PUT', ops_path=pod_path, payload=util.serialize_tojson(resource)) # now we just need to make sure that the newly created Pod is again owned by kploy: rc_name = pod_name[0:pod_name.rfind( "-" )] # NOTE: this is a hack, it assumes a certain generator pattern; need to figure a better way to find a Pod's RC logging.debug("Generating RC name from Pod: %s" % (rc_name)) rc_path = "".join([ "/api/v1/namespaces/", kploy["namespace"], "/replicationcontrollers/", rc_name ]) rc = pyk_client.describe_resource(rc_path) kploycommon._own_pods_of_rc(pyk_client, rc, kploy["namespace"], rc_path, VERBOSE) except (Exception) as e: print("Something went wrong when taking the Pod offline:\n%s" % (e)) sys.exit(1) print(80 * "=") print( "\nOK, the Pod %s is offline. Now you can, for example, use `kubectl exec` now to debug it." % (pod_name))
def _own_resource(pyk_client, resource_path, verbose): """ Labels a resource with `guard=pyk` so that it can be selected with `?labelSelector=guard%3Dpyk`. """ res = pyk_client.describe_resource(resource_path) resource = res.json() if "labels" in resource["metadata"]: labels = resource["metadata"]["labels"] else: labels = {} labels["guard"] = "pyk" resource["metadata"]["labels"] = labels logging.debug("Owning resource, now labeled with: %s" %(resource["metadata"]["labels"])) pyk_client.execute_operation(method='PUT', ops_path=resource_path, payload=util.serialize_tojson(resource))
def _create_ns(pyk_client, namespace, verbose): """ Creates a new namespace, unless it's `default`. """ if namespace == "default": return else: ns = {} ns["kind"] = "Namespace" ns["apiVersion"] = "v1" ns["metadata"] = {} ns["metadata"]["name"] = namespace ns["metadata"]["labels"] = {} ns["metadata"]["labels"]["guard"] = "pyk" if verbose: logging.info("Created namespace: %s" % (ns)) pyk_client.execute_operation(method='POST', ops_path="/api/v1/namespaces", payload=util.serialize_tojson(ns))
def _own_resource(pyk_client, resource_path, verbose): """ Labels a resource with `guard=pyk` so that it can be selected with `?labelSelector=guard%3Dpyk`. """ res = pyk_client.describe_resource(resource_path) resource = res.json() if "labels" in resource["metadata"]: labels = resource["metadata"]["labels"] else: labels = {} labels["guard"] = "pyk" resource["metadata"]["labels"] = labels logging.debug("Owning resource, now labeled with: %s" % (resource["metadata"]["labels"])) pyk_client.execute_operation(method='PUT', ops_path=resource_path, payload=util.serialize_tojson(resource))
def _create_secrets(pyk_client, app_name, namespace, secrets, verbose): """ Creates a Secret with a number of entries. """ secret = {} secret["kind"] = "Secret" secret["apiVersion"] = "v1" secret["metadata"] = {} secret["metadata"]["name"] = "kploy-secrets" secret["metadata"]["labels"] = {} secret["metadata"]["labels"]["guard"] = "pyk" secret["type"] = "Opaque" secret["data"] = {} for k, v in secrets.iteritems(): secret["data"][k] = v if verbose: logging.info("Created secret: %s" %(secret)) secrets_path = "".join(["/api/v1/namespaces/", namespace, "/secrets"]) pyk_client.execute_operation(method='POST', ops_path=secrets_path, payload=util.serialize_tojson(secret))
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")
def _create_secrets(pyk_client, app_name, namespace, secrets, verbose): """ Creates a Secret with a number of entries. """ secret = {} secret["kind"] = "Secret" secret["apiVersion"] = "v1" secret["metadata"] = {} secret["metadata"]["name"] = "kploy-secrets" secret["metadata"]["labels"] = {} secret["metadata"]["labels"]["guard"] = "pyk" secret["type"] = "Opaque" secret["data"] = {} for k, v in secrets.iteritems(): secret["data"][k] = v if verbose: logging.info("Created secret: %s" % (secret)) secrets_path = "".join(["/api/v1/namespaces/", namespace, "/secrets"]) pyk_client.execute_operation(method='POST', ops_path=secrets_path, payload=util.serialize_tojson(secret))
def cmd_scale(scale_def): """ Enables you to scale an RC up or down by setting the number of replicas. Usage: `scale rc=replica_count`, for example, `scale webserver-rc=10`. """ if not scale_def: print("Sorry, I need a scale definition in order to do my work. Do a `kploy list` first to glean the RC name you want to scale, e.g. `webserver-rc`.") print("With the RC name you can then run `kploy scale webserver-rc=5` to scale the respective RC to 5 replicas.") sys.exit(1) here = os.path.realpath(".") kployfile = os.path.join(here, DEPLOYMENT_DESCRIPTOR) try: rc_name = scale_def.split("=")[0] replica_count = int(scale_def.split("=")[1]) except (Exception) as e: print("Can't parse scale definition `%s` due to: %s" %(scale_def, e)) print("The scale definition should look as follows: `rc=replica_count`, for example, `scale webserver-rc=10`.") sys.exit(1) print("Trying to scale RC %s to %d replicas" %(rc_name, replica_count)) try: kploy, _ = util.load_yaml(filename=kployfile) logging.debug(kploy) pyk_client = kploycommon._connect(api_server=kploy["apiserver"], debug=DEBUG) rc_path = "".join(["/api/v1/namespaces/", kploy["namespace"], "/replicationcontrollers/", rc_name]) rc = pyk_client.describe_resource(rc_path) resource = rc.json() old_replica_count = resource["spec"]["replicas"] if VERBOSE: logging.info("Scaling RC from %d to %d replicas" %(old_replica_count, replica_count)) logging.debug("RC about to be scaled: %s" %(resource)) resource["spec"]["replicas"] = replica_count pyk_client.execute_operation(method='PUT', ops_path=rc_path, payload=util.serialize_tojson(resource)) # and make sure that the newly created Pods are owned by kploy (on scale up) if replica_count > old_replica_count: logging.debug("Scaling up, trying to own new Pods") rc = pyk_client.describe_resource(rc_path) kploycommon._own_pods_of_rc(pyk_client, rc, kploy["namespace"], rc_path, VERBOSE) except (Exception) as e: print("Something went wrong when scaling RC:\n%s" %(e)) sys.exit(1) print(80*"=") print("OK, I've scaled RC %s to %d replicas. You can do a `kploy stats` now to verify it." %(rc_name, replica_count))
def scale_rc(self, manifest_filename, namespace="default", num_replicas=0): """ Changes the replicas of an RC based on a manifest. Note that it defaults to 0, meaning to effectively disable this RC. :Parameters: - `manifest_filename`: The manifest file containing the ReplicationController definition, for example: `manifest/nginx-webserver-rc.yaml` - `namespace`: In which namespace the RC is, defaulting to `default` - `num_replicas`: How many copies of the pods that match the selector are supposed to run """ rc_manifest, rc_manifest_json = util.load_yaml(filename=manifest_filename) logging.debug("%s" %(rc_manifest_json)) rc_path = "".join(["/api/v1/namespaces/", namespace, "/replicationcontrollers/", rc_manifest["metadata"]["name"]]) rc_manifest["spec"]["replicas"] = num_replicas res = self.execute_operation(method="PUT", ops_path=rc_path, payload=util.serialize_tojson(rc_manifest)) try: rc_url = res.json()["metadata"]["selfLink"] except KeyError: raise ResourceCRUDException("".join(["Sorry, can not scale the RC: ", rc_manifest["metadata"]["name"]])) logging.info("I scaled the RC %s at %s to %d replicas" %(rc_manifest["metadata"]["name"], rc_url, num_replicas)) return (res, rc_url)
def _destroy(pyk_client, namespace, here, dir_name, alist, resource_name, verbose): """ Destroys resources based on manifest files. Currently the following resources are supported: replication controllers, services. """ for litem in alist: file_name = os.path.join(os.path.join(here, dir_name), litem) if file_name.endswith(".url"): file_name = _deref_remote(file_name) if verbose: logging.info("Trying to destroy %s %s" % (resource_name, file_name)) res_manifest, _ = util.load_yaml(filename=file_name) res_name = res_manifest["metadata"]["name"] if resource_name == "service": res_path = "".join( ["/api/v1/namespaces/", namespace, "/services/", res_name]) elif resource_name == "RC": res_path = "".join([ "/api/v1/namespaces/", namespace, "/replicationcontrollers/", res_name ]) res = pyk_client.describe_resource(res_path) if res.status_code == 404: # the replication controller is already gone break # ... don't try to scale down else: resource = res.json() resource["spec"]["replicas"] = 0 if verbose: logging.info("Scaling down RC %s to 0" % (res_path)) pyk_client.execute_operation( method='PUT', ops_path=res_path, payload=util.serialize_tojson(resource)) else: return None pyk_client.delete_resource(resource_path=res_path)
def _destroy(pyk_client, namespace, here, dir_name, alist, resource_name, verbose): """ Destroys resources based on manifest files. Currently the following resources are supported: replication controllers, services. """ for litem in alist: file_name = os.path.join(os.path.join(here, dir_name), litem) if file_name.endswith(".url"): file_name = _deref_remote(file_name) if verbose: logging.info("Trying to destroy %s %s" %(resource_name, file_name)) res_manifest, _ = util.load_yaml(filename=file_name) res_name = res_manifest["metadata"]["name"] if resource_name == "service": res_path = "".join(["/api/v1/namespaces/", namespace, "/services/", res_name]) elif resource_name == "RC": res_path = "".join(["/api/v1/namespaces/", namespace, "/replicationcontrollers/", res_name]) res = pyk_client.describe_resource(res_path) if res.status_code == 404: # the replication controller is already gone break # ... don't try to scale down else: resource = res.json() resource["spec"]["replicas"] = 0 if verbose: logging.info("Scaling down RC %s to 0" %(res_path)) pyk_client.execute_operation(method='PUT', ops_path=res_path, payload=util.serialize_tojson(resource)) else: return None pyk_client.delete_resource(resource_path=res_path)
def _create_ns(pyk_client, namespace, verbose): """ Creates a new namespace, unless it's `default`. """ if namespace == "default": return else: ns = {} ns["kind"] = "Namespace" ns["apiVersion"] = "v1" ns["metadata"] = {} ns["metadata"]["name"] = namespace ns["metadata"]["labels"] = {} ns["metadata"]["labels"]["guard"] = "pyk" if verbose: logging.info("Created namespace: %s" %(ns)) pyk_client.execute_operation(method='POST', ops_path="/api/v1/namespaces", payload=util.serialize_tojson(ns))
running_mypods = len(mypods) print "{} running".format(running_mypods) # too many? if running_mypods > REPLICAS: to_delete = running_mypods - REPLICAS print " Too many are running. Deleting {} pods:".format(to_delete) for pod in mypods[:to_delete]: print " Deleting pod {}".format(pod['metadata']['name']) kubeclient.delete_resource(pod['metadata']['selfLink']) # too few? elif REPLICAS > running_mypods: to_launch = REPLICAS - running_mypods for n in range(0, to_launch): mypod_spec, _ = util.load_yaml(filename="mypod.yaml") mypod_spec["metadata"]["name"] += "-" + shortuuid.uuid()[:4].lower( ) print "Launching pod {}".format(mypod_spec["metadata"]["name"]) response = kubeclient.execute_operation( method='POST', ops_path="/api/v1/namespaces/default/pods", payload=util.serialize_tojson(mypod_spec)) if response.status_code >= 300: print "ERROR {}: {}".format(response.status_code, response.content) time.sleep(1)
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" )
# count mypods response = kubeclient.execute_operation(method="GET", ops_path="/api/v1/namespaces/default/pods?labelSelector=name%3Dmypod") mypods = response.json()['items'] running_mypods = len(mypods) print "{} running".format(running_mypods) # too many? if running_mypods > REPLICAS: to_delete = running_mypods - REPLICAS print " Too many are running. Deleting {} pods:".format(to_delete) for pod in mypods[:to_delete]: print " Deleting pod {}".format(pod['metadata']['name']) kubeclient.delete_resource(pod['metadata']['selfLink']) # too few? elif REPLICAS > running_mypods: to_launch = REPLICAS - running_mypods for n in range(0, to_launch): mypod_spec, _ = util.load_yaml(filename="mypod.yaml") mypod_spec["metadata"]["name"] += "-" + shortuuid.uuid()[:4].lower() print "Launching pod {}".format(mypod_spec["metadata"]["name"]) response = kubeclient.execute_operation(method='POST', ops_path="/api/v1/namespaces/default/pods", payload=util.serialize_tojson(mypod_spec)) if response.status_code >= 300: print "ERROR {}: {}".format(response.status_code, response.content) time.sleep(1)