Ejemplo n.º 1
0
    def __init__(self, options):
        super().__init__(options)

        image_options = {}
        # Adapt the node image to the OCI API
        # If the image name is just defined, else expect a dict structure
        if isinstance(options["cluster"]["node"]["image"], str):
            image_name = options["cluster"]["node"]["image"]
            image_options = dict(display_name=image_name)
            self.options["cluster"]["node"].pop("image")
        else:
            image_options = options["cluster"]["node"]["image"]

        self.options["cluster"]["node"]["image"] = image_options

        # Set clients
        self.compute_client = new_compute_client(name=options["profile"]["name"])
        self.container_engine_client = new_container_engine_client(
            name=options["profile"]["name"]
        )

        self.network_client = new_network_client(name=options["profile"]["name"])

        if (
            "kubernetes_version" not in self.options["cluster"]
            or not self.options["cluster"]["kubernetes_version"]
        ):
            self.options["cluster"]["kubernetes_version"] = get_kubernetes_version(
                self.container_engine_client
            )

        self.cluster_stack = None
        self.vcn_stack = None
        self._is_ready = False
Ejemplo n.º 2
0
def client_delete_instance(provider, provider_kwargs, instance=None):
    if "id" not in instance and "display_name" not in instance:
        return False, "Either the id or display-name of the instance must be provided"

    if not instance["id"] and not instance["display_name"]:
        return False, "Either the id or display-name of the instance must have a value"

    compute_client = new_compute_client(
        name=provider_kwargs["profile"]["name"])
    if instance["id"]:
        instance_id = instance["id"]
    else:
        instance_object = get_instance_by_name(
            compute_client,
            provider_kwargs["profile"]["compartment_id"],
            instance["display_name"],
        )
        if not instance_object:
            return (
                False,
                "Failed to find an instance with display-name: {}".format(
                    instance["display_name"]),
            )

        instance_id = instance_object.id

    deleted = delete_instance(compute_client, instance_id)
    return instance_id, deleted
Ejemplo n.º 3
0
def client_list_instances(provider,
                          provider_kwargs,
                          format_return=False,
                          **kwargs):
    client = new_compute_client(name=provider_kwargs["profile"]["name"])
    instances = list_instances(client,
                               provider_kwargs["profile"]["compartment_id"])
    if format_return:
        return to_dict(instances)
    return instances
Ejemplo n.º 4
0
    def __init__(self, options):
        super().__init__(options)
        self.compute_client = new_compute_client(
            name=options["profile"]["name"], )

        self.identity_client = new_identity_client(
            name=options["profile"]["name"], )

        self.network_client = new_network_client(
            name=options["profile"]["name"], )

        self.port = 22
        self.instance = None
        self.vcn_stack = None
Ejemplo n.º 5
0
def client_get_instance(provider,
                        provider_kwargs,
                        format_return=False,
                        instance=None,
                        details=None):
    if "id" not in instance and "display_name" not in instance:
        return (
            None,
            False,
            "Either the id or display-name of the instance must be provided",
        )

    if not instance["id"] and not instance["display_name"]:
        return (
            None,
            False,
            "Either the id or display-name of the instance must have a value",
        )

    client = new_compute_client(name=provider_kwargs["profile"]["name"])
    found_instance = None
    if instance["id"]:
        instance_id = instance["id"]
        found_instance = get_instance(
            client, provider_kwargs["profile"]["compartment_id"], instance_id)
    else:
        found_instance = get_instance_by_name(
            client,
            provider_kwargs["profile"]["compartment_id"],
            instance["display_name"],
        )

    if found_instance:
        extra_details = {}
        if details:
            if "endpoints" in details:
                network_client = new_network_client(
                    name=provider_kwargs["profile"]["name"])
                endpoints = get_instance_endpoints(
                    client,
                    network_client,
                    provider_kwargs["profile"]["compartment_id"],
                    found_instance.id,
                )
                extra_details["endpoints"] = endpoints
        if format_return:
            return found_instance.id, (to_dict(found_instance),
                                       extra_details), ""
        return found_instance.id, (found_instance, extra_details), ""
    return None, None, "Failed to find an instance"
Ejemplo n.º 6
0
    def make_resource_config(
        cls,
        provider,
        provider_profile=None,
        provider_kwargs=None,
        cpu=None,
        memory=None,
        gpus=None,
    ):
        if not provider_profile:
            provider_profile = {}

        if not provider_kwargs:
            provider_kwargs = {}

        availability_domain = ""
        if "availability_domain" in provider_kwargs:
            availability_domain = provider_kwargs["availability_domain"]
        else:
            # Try load from config
            availability_domain = load_from_env_or_config(
                {"instance": {
                    "availability_domain": {}
                }},
                prefix=gen_config_provider_prefix((provider, )),
            )

        # TODO, load OCI environment variables
        compute_client = new_compute_client(name=provider_profile["name"])

        resource_config = {}
        available_shapes = list_entities(
            compute_client,
            "list_shapes",
            provider_profile["compartment_id"],
            availability_domain=availability_domain,
        )
        # Override the name that is assigned to the instance
        if "display_name" in provider_kwargs:
            resource_config["display_name"] = provider_kwargs["display_name"]

        # Subset selection
        if cpu:
            cpu_shapes = []
            for shape in available_shapes:
                # Either dynamic or fixed ocpu count
                if (hasattr(shape, "ocpu_options") and shape.ocpu_options
                        and shape.ocpu_options.max >= cpu
                        and shape.ocpu_options.min <= cpu):
                    # Requires shape config
                    shape.ocpus = cpu
                    cpu_shapes.append(shape)
                else:
                    if shape.ocpus >= cpu:
                        cpu_shapes.append(shape)
            available_shapes = cpu_shapes

        if memory:
            memory_shapes = []
            for shape in available_shapes:
                if (hasattr(shape, "memory_options") and shape.memory_options
                        and shape.memory_options.max_in_g_bs >= memory
                        and shape.memory_options.min_in_g_bs <= memory):
                    # Dynamic memory range
                    # HACK, since you can't atm set the dynamic memory amount
                    # Ensure that we rank the flexible shapes by how
                    # much the total allocated memory is assigned to the instance
                    shape.memory_in_gbs = (
                        shape.memory_options.default_per_ocpu_in_g_bs *
                        shape.ocpus)
                    memory_shapes.append(shape)
                else:
                    if shape.memory_in_gbs >= memory:
                        memory_shapes.append(shape)
            available_shapes = memory_shapes

        if gpus:
            gpu_shapes = []
            for shape in available_shapes:
                if hasattr(shape, "gpus") and shape.gpus >= gpus:
                    gpu_shapes.append(shape)
            available_shapes = gpu_shapes

        # TODO, Minimum shape available
        if available_shapes:
            # sort on cpu and memory
            minimum_shape = sorted(available_shapes,
                                   key=lambda shape:
                                   (shape.ocpus, shape.memory_in_gbs))[0]
            # If a dynamic resource instance -> needs to be a shape_config
            if (hasattr(minimum_shape, "ocpu_options")
                    and minimum_shape.ocpu_options
                    and hasattr(minimum_shape, "memory_options")
                    and minimum_shape.memory_options):
                # pass shape values to shapeconfig
                instance_shape_details = {}
                attributes = minimum_shape.attribute_map
                for k, v in attributes.items():
                    if hasattr(InstanceShapeConfig, k):
                        instance_shape_details[k] = getattr(minimum_shape, k)
                # shape_config = InstanceShapeConfig(**instance_shape_details)
                resource_config["shape_config"] = instance_shape_details
            resource_config["shape"] = minimum_shape.shape
        return resource_config