Exemple #1
0
def createServers(handle, hosts, org):
    print "Creating Kubernetes Service Profiles"
    from ucsmsdk.ucsmethodfactory import ls_instantiate_n_named_template
    from ucsmsdk.ucsbasetype import DnSet, Dn

    for i, s in enumerate(hosts):
        dn_set = DnSet()
        dn = Dn()
        sp_name = s["name"]
        dn.attr_set("value",sp_name)
        dn_set.child_add(dn)
        elem = ls_instantiate_n_named_template(cookie=handle.cookie, 
            dn=org + "/ls-Kubernetes", 
            in_error_on_existing="true", 
            in_name_set=dn_set,     
            in_target_org=org, 
            in_hierarchical="false")
        try:
            mo_list = handle.process_xml_elem(elem)
        except UcsException as err:
            if err.error_code == "105":
                print "\t" + sp_name + " already exists."
            else:
                return 1, err.error_descr
    return 0, ""
Exemple #2
0
    def create_server(handle, template, host_name, org):
        """
        Create a new service profile from a template that already exist.
        """

        err, msg = UCSServer.check_org(template, org)
        if err != 0:
            return 1, msg


        from ucsmsdk.ucsmethodfactory import ls_instantiate_n_named_template
        from ucsmsdk.ucsbasetype import DnSet, Dn
        dn_set = DnSet()
        dn = Dn()
        dn.attr_set("value", host_name)
        dn_set.child_add(dn)
        elem = ls_instantiate_n_named_template(
            cookie=handle.cookie, dn=template, in_error_on_existing="true", 
            in_name_set=dn_set, in_target_org=org, in_hierarchical="false"
        )

        try: 
            handle.process_xml_elem(elem)
        except UcsException as err:
            if err.error_code == "105":
                print "\tSP {0} already exists.".format(host_name)
            else:
                return 1, err.error_descr
        return 0, None
def sp_create_from_template(handle,
                            naming_prefix,
                            name_suffix_starting_number,
                            number_of_instance,
                            sp_template_name,
                            in_error_on_existing="true",
                            parent_dn="org-root"):
    """
    This method instantiate Service profile from a template.

    Args:
        handle (UcsHandle)
        org_name (string): Name of the organization
        naming_prefix (string): Suffix name of service profile.
        name_suffix_starting_number (string): Starting Number for Suffix
        number_of_instance (string): Total number of instances to be created.
        sp_template_name (string): SP template name.
        in_error_on_existing (string): "true" or "false"
        parent_dn (string): Parent of Org.

    Returns:
        None or List of LsServer Objects

    Raises:
        ValueError: If LsServer is not present

    Example:
        sp_create_from_template(handle, org_name="sample-org",
                     naming_prefix="sample_sp",name_suffix_starting_number="1",
                     number_of_instance="3",sp_template_name="sample_temp",
                     in_error_on_existing="true")

    """

    from ucsmsdk.ucsmethodfactory import ls_instantiate_n_named_template
    from ucsmsdk.ucsbasetype import DnSet, Dn

    sp_template_dn = parent_dn + "/ls-" + sp_template_name
    mo = handle.query_dn(sp_template_dn)
    if not mo:
        raise ValueError("SP template does not exist.")

    dn_set = DnSet()
    for num in range(
            int(name_suffix_starting_number),
            int(number_of_instance) + int(name_suffix_starting_number)):
        dn = Dn()
        sp_name = naming_prefix + str(num)
        dn.attr_set("value", sp_name)
        dn_set.child_add(dn)

    elem = ls_instantiate_n_named_template(
        cookie=handle.cookie,
        dn=sp_template_dn,
        in_error_on_existing=in_error_on_existing,
        in_name_set=dn_set,
        in_target_org=parent_dn)
    return handle.process_xml_elem(elem)
def sp_create_from_template(handle,
                            naming_prefix,
                            name_suffix_starting_number,
                            number_of_instance,
                            sp_template_name,
                            in_error_on_existing="true",
                            parent_dn="org-root"):
    """
    This method instantiate Service profile from a template.

    Args:
        handle (UcsHandle)
        org_name (string): Name of the organization
        naming_prefix (string): Suffix name of service profile.
        name_suffix_starting_number (string): Starting Number for Suffix
        number_of_instance (string): Total number of instances to be created.
        sp_template_name (string): SP template name.
        in_error_on_existing (string): "true" or "false"
        parent_dn (string): Parent of Org.

    Returns:
        None or List of LsServer Objects

    Raises:
        ValueError: If LsServer is not present

    Example:
        sp_create_from_template(handle, org_name="sample-org",
                     naming_prefix="sample_sp",name_suffix_starting_number="1",
                     number_of_instance="3",sp_template_name="sample_temp",
                     in_error_on_existing="true")

    """

    from ucsmsdk.ucsmethodfactory import ls_instantiate_n_named_template
    from ucsmsdk.ucsbasetype import DnSet, Dn

    sp_template_dn = parent_dn + "/ls-" + sp_template_name
    mo = handle.query_dn(sp_template_dn)
    if not mo:
        raise ValueError("SP template does not exist.")

    dn_set = DnSet()
    for num in range(int(name_suffix_starting_number),
                     int(number_of_instance) +
                     int(name_suffix_starting_number)):
        dn = Dn()
        sp_name = naming_prefix + str(num)
        dn.attr_set("value", sp_name)
        dn_set.child_add(dn)

    elem = ls_instantiate_n_named_template(
        cookie=handle.cookie,
        dn=sp_template_dn,
        in_error_on_existing=in_error_on_existing,
        in_name_set=dn_set,
        in_target_org=parent_dn)
    return handle.process_xml_elem(elem)
Exemple #5
0
def main():
    handle = UcsHandle("192.168.254.200","ucspe","ucspe", secure=False)
    handle.login()

    # Query Blades that are unassociated
    print("\n\n=== Query Based on Class Name with Filter equal to")
    filter = "(oper_state,'unassociated',type='eq')".format(BLADE_MODEL)
    blades = handle.query_classid("computeBlade",filter_str=filter)
    print("***Found {} Blades".format(len(blades)))

    # Error Check for available blades
    if len(blades) < NUM_SP:
        error = "There are only {} blades left to associate, and you asked for {} Servers to be generated".format(len(blades),NUM_SP)
        raise NameError(error)
        
    # Setup Variable for SP Templates to be deployed
    dn_set = DnSet()
    for i in range(1, NUM_SP+1):
        dn = Dn()
        sp_name = "SP{}".format(str(i))
        dn.attr_set("value", sp_name)
        dn_set.child_add(dn)

    # Build XML Object to submit to the API
    templates = ls_instantiate_n_named_template(
        cookie=handle.cookie,
        dn="org-root/ls-globotemplate",
        in_target_org="org-root",
        in_error_on_existing="false",
        in_name_set=dn_set
        )

    # Send XML Object to xml process handler
    sp_list = handle.process_xml_elem(templates)

    # Loop through each created sp, and associate them to blades
    i = 0
    while i < len(sp_list):
        sp = sp_list[i]
        blade = blades[i]

        # Print SP and Blade Combination
        print(sp.dn,blade.dn)

        # Get Binding Object
        mo = LsBinding(
            parent_mo_or_dn=sp.dn,
            pn_dn=blade.dn,
            restrict_migration="no"
            )
        
        # Add MO Binding Object to handler
        handle.add_mo(mo,modify_present=True)
        i=i+1

    # Bundle the SP Associates
    handle.commit()
Exemple #6
0
def main():
    handle = UcsHandle("192.168.254.200", "ucspe", "ucspe", secure=False)
    handle.login()

    # Setup Variable for SP Templates to be deployed
    dn_set = DnSet()
    for i in range(1, NUM_SP + 1):
        dn = Dn()
        sp_name = "SP{}".format(str(i))
        dn.attr_set("value", sp_name)
        dn_set.child_add(dn)

    # Build XML Object to submit to the API
    templates = ls_instantiate_n_named_template(cookie=handle.cookie,
                                                dn="org-root/ls-globotemplate",
                                                in_target_org="org-root",
                                                in_error_on_existing="false",
                                                in_name_set=dn_set)

    # Send XML Object to xml process handler
    mo_list = handle.process_xml_elem(templates)

    for mo in mo_list:
        print(mo.dn)
Exemple #7
0
def sp_create_from_template(ucs_handle,
                            naming_prefix,
                            name_suffix_starting_number,
                            number_of_instance,
                            sp_template_name,
                            in_error_on_existing="true",
                            parent_dn="org-root"):
    """
    This method instantiate Service profile from a template.

    Args:
        ucs_handle (ucs_handle)
        naming_prefix (string): Suffix name of service profile.
        name_suffix_starting_number (string): Starting Number for Suffix
        number_of_instance (string): Total number of instances to be created.
        sp_template_name (string): SP template name.
        in_error_on_existing (string): "true" or "false"
        parent_dn (string): Org dn in which service profile template resides.

    Returns:
        None or List of LsServer Objects

    Raises:
        ValueError: If LsServer is not present

    Example:
        sp_create_from_template(ucs_handle, naming_prefix="sample_sp",
                                name_suffix_starting_number="1",
                                number_of_instance="3",
                                sp_template_name="sample_temp",
                                in_error_on_existing="true",
                                parent_dn="org-root/ls-org_sample")

    """

    import os

    from ucsmsdk.ucsmethodfactory import ls_instantiate_n_named_template
    from ucsmsdk.ucsbasetype import DnSet, Dn

    mo = None
    org_dn = parent_dn
    while mo is None:
        sp_template_dn = org_dn + "/ls-" + sp_template_name
        mo = ucs_handle.query_dn(sp_template_dn)
        if mo:
            break
        elif not mo and org_dn == 'org-root':
            raise ValueError("SP template does not exist.")
        org_dn = os.path.dirname(org_dn)

    dn_set = DnSet()
    for num in range(int(name_suffix_starting_number),
                     int(number_of_instance) +
                     int(name_suffix_starting_number)):
        dn = Dn()
        sp_name = naming_prefix + str(num)
        dn.attr_set("value", sp_name)
        dn_set.child_add(dn)

    elem = ls_instantiate_n_named_template(
        cookie=ucs_handle.cookie,
        dn=sp_template_dn,
        in_error_on_existing=in_error_on_existing,
        in_name_set=dn_set,
        in_target_org=parent_dn)
    return ucs_handle.process_xml_elem(elem)
Exemple #8
0
    def instantiate_profile(self):
        self.logger(message="Instantiating " + self._CONFIG_NAME +
                    " configuration from " +
                    str(self.chassis_profile_template))

        if hasattr(self._parent, '_dn'):
            parent_mo = self._parent._dn
        else:
            self.logger(level="error",
                        message="Impossible to find the parent dn of " +
                        self._CONFIG_NAME + " : " + str(self.name))
            return False

        if not hasattr(self, 'suffix_start_number'):
            self.suffix_start_number = "1"
        if not hasattr(self, 'number_of_instances'):
            self.number_of_instances = "1"

        if self.suffix_start_number and self.number_of_instances:
            dn_set = DnSet()
            for i in range(
                    int(self.suffix_start_number),
                    int(self.number_of_instances) +
                    int(self.suffix_start_number)):
                dn = Dn()
                dn.attr_set("value", str(self.name + str(i)))
                dn_set.child_add(dn)

            elem = equipment_instantiate_n_named_template(
                cookie=self._handle.cookie,
                dn=parent_mo + "/cp-" + self.chassis_profile_template,
                in_error_on_existing="false",
                in_name_set=dn_set,
                in_target_org=parent_mo,
                in_hierarchical="false")

            for i in range(self._device.push_attempts):
                try:
                    if i:
                        self.logger(
                            level="warning",
                            message=
                            "Trying to push again the instantiated chassis profile(s) from "
                            + str(self.chassis_profile_template))
                    self._handle.process_xml_elem(elem)
                    self.logger(level='debug',
                                message=self.number_of_instances + " " +
                                self._CONFIG_NAME + " instantiated from " +
                                str(self.chassis_profile_template) +
                                " starting with " + str(self.name) +
                                self.suffix_start_number)
                    return True
                except ConnectionRefusedError:
                    self.logger(
                        level="error",
                        message=
                        "Connection refused while trying to instantiate from "
                        + str(self.chassis_profile_template))
                except UcsException as err:
                    self.logger(
                        level="error",
                        message="Error while trying to instantiate from " +
                        str(self.chassis_profile_template) + " " +
                        err.error_descr)
                except urllib.error.URLError:
                    self.logger(
                        level="error",
                        message="Timeout while trying to instantiate from " +
                        str(self.chassis_profile_template))

        else:
            elem = equipment_instantiate_template(
                cookie=self._handle.cookie,
                dn=parent_mo + "/cp-" + self.chassis_profile_template,
                in_error_on_existing="false",
                in_chassis_profile_name=self.name,
                in_target_org=parent_mo,
                in_hierarchical="false")

            for i in range(self._device.push_attempts):
                try:
                    if i:
                        self.logger(
                            level="warning",
                            message=
                            "Trying to push again the instantiated chassis profile(s) from "
                            + str(self.chassis_profile_template))
                    self._handle.process_xml_elem(elem)
                    self.logger(level='debug',
                                message=self._CONFIG_NAME + " " +
                                str(self.name) + " instantiated from " +
                                str(self.chassis_profile_template))

                    # We now need to associate the instantiated Chassis Profile to the Chassis ID if provided
                    if self.type == "instance" and self.chassis_assignment_id:
                        mo_equipment_chassis_profile = EquipmentChassisProfile(
                            parent_mo_or_dn=parent_mo, name=self.name)
                        EquipmentBinding(
                            parent_mo_or_dn=mo_equipment_chassis_profile,
                            chassis_dn="sys/chassis-" +
                            self.chassis_assignment_id,
                            restrict_migration=self.restrict_migration)

                        self._handle.add_mo(mo=mo_equipment_chassis_profile,
                                            modify_present=True)
                        if self.commit(detail=self.name) != True:
                            return False

                    return True

                except ConnectionRefusedError:
                    self.logger(
                        level="error",
                        message=
                        "Connection refused while trying to instantiate from "
                        + str(self.chassis_profile_template))
                except UcsException as err:
                    self.logger(
                        level="error",
                        message="Error while trying to instantiate from " +
                        str(self.chassis_profile_template) + " " +
                        err.error_descr)
                except urllib.error.URLError:
                    self.logger(
                        level="error",
                        message="Timeout while trying to instantiate from " +
                        str(self.chassis_profile_template))