def create_virtual_media(handle, org, kubam_ip, opersys): from ucsmsdk.mometa.cimcvmedia.CimcvmediaMountConfigPolicy import CimcvmediaMountConfigPolicy from ucsmsdk.mometa.cimcvmedia.CimcvmediaConfigMountEntry import CimcvmediaConfigMountEntry print "Adding Virtual Media Policy" from urlparse import urlparse import os.path url = "http://" + kubam_ip + "/kubam/" + opersys + "-boot.iso" if opersys.startswith("win"): url = "http://" + kubam_ip + "/kubam/" + "KUBAM_WinPE.iso" o = urlparse(url) paths = os.path.split(o.path) scheme = o.scheme # HTTP, HTTPS if scheme == "": scheme = "http" filename = paths[-1] address = o.hostname path = "/".join(paths[:-1]) name = ".".join(paths[-1].split(".")[:-1]) mo = CimcvmediaMountConfigPolicy( name="KUBAM_" + opersys , retry_on_mount_fail="yes", parent_mo_or_dn=org, policy_owner="local", descr="KUBAM vmedia policy for " + opersys ) CimcvmediaConfigMountEntry( parent_mo_or_dn=mo, mapping_name=name, device_type="cdd", mount_protocol=scheme, remote_ip_address=address, image_name_variable="none", image_file_name=filename, image_path=path ) CimcvmediaConfigMountEntry( parent_mo_or_dn=mo, mapping_name="ServerImage", device_type="hdd", mount_protocol=scheme, remote_ip_address=address, image_name_variable="service-profile-name", image_path=path ) handle.add_mo(mo, modify_present=True) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\talready exists" else: return 1, err.error_descr return 0, None
def setup_vmedia_policy(server, module): from ucsmsdk.mometa.cimcvmedia.CimcvmediaMountConfigPolicy import CimcvmediaMountConfigPolicy from ucsmsdk.mometa.cimcvmedia.CimcvmediaConfigMountEntry import CimcvmediaConfigMountEntry print module ansible = module.params args_mo = _get_mo_params(ansible) changed = False policy = args_mo['name'] mo = server.query_dn(args_mo['org_dn'] + "/mnt-cfg-policy-" + policy) exists = False if mo: exists = True if ansible['state'] == 'absent': if exists: changed = True if not module.check_mode: server.remove_mo(mo) server.commit() else: if not exists: changed = True if not module.check_mode: mo = CimcvmediaMountConfigPolicy( name=args_mo['name'], retry_on_mount_fail=args_mo['retry'], parent_mo_or_dn=args_mo['org_dn'], descr=args_mo['descr']) for mnt in args_mo['mounts']: if mnt['file'] == "variable": mo_x = CimcvmediaConfigMountEntry( parent_mo_or_dn=mo, mapping_name=mnt['name'], device_type=mnt['device'], mount_protocol=mnt['protocol'], remote_ip_address=mnt['remote_ip'], image_name_variable="service-profile-name", image_path=mnt['path']) else: mo_x = CimcvmediaConfigMountEntry( parent_mo_or_dn=mo, mapping_name=mnt['name'], device_type=mnt['device'], mount_protocol=mnt['protocol'], remote_ip_address=mnt['remote_ip'], image_name_variable="none", image_file_name=mnt['file'], image_path=mnt['path']) server.add_mo(mo, True) server.commit() return changed
def createKubeVirtualMedia(handle, org): print "Adding Virtual Media Policy" from urlparse import urlparse import os.path yn = False url = "" while yn == False: print "What is the URL for the Boot ISO image?" url = raw_input( "(E.g.: http://192.168.2.2/kubam/centos7.2-boot.iso) : ") print "You entered: " + url yn = raw_input("Is this correct? [y/N]: ") if yn != "y": yn = False o = urlparse(url) paths = os.path.split(o.path) scheme = o.scheme # http, https if scheme == "": scheme = "http" filename = paths[-1] address = o.hostname path = "/".join(paths[:-1]) name = ".".join(paths[-1].split(".")[:-1]) from ucsmsdk.mometa.cimcvmedia.CimcvmediaMountConfigPolicy import CimcvmediaMountConfigPolicy from ucsmsdk.mometa.cimcvmedia.CimcvmediaConfigMountEntry import CimcvmediaConfigMountEntry mo = CimcvmediaMountConfigPolicy(name="kube", retry_on_mount_fail="yes", parent_mo_or_dn=org, policy_owner="local", descr="Kubernetes Boot Media") mo_1 = CimcvmediaConfigMountEntry(parent_mo_or_dn=mo, mapping_name=name, device_type="cdd", mount_protocol=scheme, remote_ip_address=address, image_name_variable="none", image_file_name=filename, image_path=path) mo_2 = CimcvmediaConfigMountEntry( parent_mo_or_dn=mo, mapping_name="kickstartImage", device_type="hdd", mount_protocol=scheme, remote_ip_address=address, image_name_variable="service-profile-name", image_path=path) handle.add_mo(mo, modify_present=True) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\talready exists"
def createKubeVirtualMedia(handle, org, kubam_ip, hosts): print "Adding Virtual Media Policy" from urlparse import urlparse import os.path yn = False url = "http://" + kubam_ip + "/kubam/" + hosts[0]["os"] + "-boot.iso" o = urlparse(url) paths = os.path.split(o.path) scheme = o.scheme # http, https if scheme == "": scheme = "http" filename = paths[-1] address = o.hostname path = "/".join(paths[:-1]) name = ".".join(paths[-1].split(".")[:-1]) from ucsmsdk.mometa.cimcvmedia.CimcvmediaMountConfigPolicy import CimcvmediaMountConfigPolicy from ucsmsdk.mometa.cimcvmedia.CimcvmediaConfigMountEntry import CimcvmediaConfigMountEntry mo = CimcvmediaMountConfigPolicy(name="kube", retry_on_mount_fail="yes", parent_mo_or_dn=org, policy_owner="local", descr="Kubernetes Boot Media") mo_1 = CimcvmediaConfigMountEntry(parent_mo_or_dn=mo, mapping_name=name, device_type="cdd", mount_protocol=scheme, remote_ip_address=address, image_name_variable="none", image_file_name=filename, image_path=path) mo_2 = CimcvmediaConfigMountEntry(parent_mo_or_dn=mo, mapping_name="kickstartImage", device_type="hdd", mount_protocol=scheme, remote_ip_address=address, image_name_variable="service-profile-name", image_path=path) handle.add_mo(mo, modify_present=True) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\talready exists" else: return 1, err.error_descr return 0, ""
def vmedia_mount_add(handle, vmedia_policy_dn, mapping_name, device_type, mount_protocol, remote_ip_address, image_name_variable, image_file_name, image_path, remote_port="0", user_id="", password="", description="", auth_option="default"): """ Creates a new vMedia mount entry Args: handle (UcsHandle) vmedia_policy_dn (string): the dn of the vmedia policy mapping_name (string) : name of the map. This is just a string label. device_type (string): "cdd", "hdd" mount_protocol (string): "cifs", "http", "https", "nfs" remote_ip_address (string): IP address of the remote server image_name_variable (string): image_file_name (string): filename of the image to be downloaded image_path (string): path of the image on the remote server remote_port (string): port number to connect to. user_id (string): username password (string): password description (string): description for the mount entry auth_option (string): "default", "none", "ntlm", "ntlmi", "ntlmssp", "ntlmsspi", "ntlmv2", "ntlmv2i" Returns: CimcvmediaConfigMountEntry: Managed Object Example: mo = vmedia_mount_add(handle, vmedia_policy_dn, mapping_name="ubuntu_1404", device_type="cdd", mount_protocol="nfs", remote_ip_address="10.105.219.128", image_name_variable="none", image_file_name="ubuntu-14.04.3-desktop-amd64.iso", image_path="/isoimages") """ from ucsmsdk.mometa.cimcvmedia.CimcvmediaConfigMountEntry import \ CimcvmediaConfigMountEntry mo = CimcvmediaConfigMountEntry(parent_mo_or_dn=vmedia_policy_dn, mapping_name=mapping_name, device_type=device_type, mount_protocol=mount_protocol, remote_ip_address=remote_ip_address, image_name_variable=image_name_variable, image_file_name=image_file_name, image_path=image_path, remote_port=remote_port, user_id=user_id, password=password, description=description, auth_option=auth_option) handle.add_mo(mo, modify_present=True) handle.commit() return mo
def manage_vmedia(inputs): """ Manage UCS Vmedia Policy Mounts""" from ucsmsdk.mometa.cimcvmedia.CimcvmediaConfigMountEntry import CimcvmediaConfigMountEntry handle = ucs_login() object_op = inputs[0] org_parent = inputs[1] vmedia_name = inputs[2] vmedia_mount_name = inputs[3] parent_org_dn = 'org-' + org_parent.replace('/', '/org-') vmedia_dn = parent_org_dn + "/mnt-cfg-policy-" + vmedia_name vmedia_mount_dn = parent_org_dn + "/mnt-cfg-policy-" + vmedia_name + "/cfg-mnt-entry-" + vmedia_mount_name print(vmedia_dn) print(vmedia_mount_dn) if object_op == "add" or object_op == "update": vmedia_mount_image_path = inputs[4] vmedia_mount_image_file_name = inputs[5] vmedia_mount_device_type = inputs[6] vmedia_mount_protocol = inputs[7] vmedia_mount_remote_ip_address = inputs[8] ucs_mo = handle.query_dn(vmedia_dn) action = "" if ucs_mo: vmedia_mount_mo = handle.query_dn(vmedia_mount_dn) if vmedia_mount_mo == None: vmedia_mount_mo = CimcvmediaConfigMountEntry( parent_mo_or_dn=parent_org_dn + "/mnt-cfg-policy-" + vmedia_name, mapping_name=vmedia_mount_name, device_type=vmedia_mount_device_type, mount_protocol=vmedia_mount_protocol, remote_ip_address=vmedia_mount_remote_ip_address, image_path=vmedia_mount_image_path, image_file_name=vmedia_mount_image_file_name) action = "Added" else: print(vmedia_mount_device_type) vmedia_mount_mo.device_type = vmedia_mount_device_type vmedia_mount_mo.mount_protocol = vmedia_mount_protocol vmedia_mount_mo.remote_ip_address = vmedia_mount_remote_ip_address vmedia_mount_mo.image_path = vmedia_mount_image_path vmedia_mount_mo.image_file_name = vmedia_mount_image_file_name action = "Updated" handle.add_mo(vmedia_mount_mo, modify_present=True) handle.commit() response = "Vmedia Mount: " + org_parent + '/' + vmedia_name + '/' + vmedia_mount_name + " - " + action + "." else: response = "Vmedia Policy: " + org_parent + '/' + vmedia_name + " - Not Found." elif object_op == "remove": vmedia_mount_mo = handle.query_dn(vmedia_mount_dn) if vmedia_mount_mo: handle.remove_mo(vmedia_mount_mo) handle.commit() response = "Vmedia Mount: " + org_parent + '/' + vmedia_name + '/' + vmedia_mount_name + " - Removed." else: response = "Vmedia Mount: " + org_parent + '/' + vmedia_name + '/' + vmedia_mount_name + " - Not Found." ucs_logout(handle) print(response) return response
def main(): logging.basicConfig(level=logging.INFO) argument_spec = ucs_argument_spec argument_spec.update( name=dict(type='str', required=True), description=dict(type='str', default=''), state=dict(type='str', default='present', choices=['present', 'absent']), retry=dict(type='str', default='yes', choices=['yes', 'no']), mounts=dict(type='list'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, ) ucs = UCSModule(module) err = False # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below. from ucsmsdk.mometa.cimcvmedia.CimcvmediaMountConfigPolicy import CimcvmediaMountConfigPolicy from ucsmsdk.mometa.cimcvmedia.CimcvmediaConfigMountEntry import CimcvmediaConfigMountEntry changed = False try: mo_exists = False parent_props_match = False child_props_match = False # dn is org-root/mnt-cfg-policy-<name> dn_base = 'org-root' dn = dn_base + '/mnt-cfg-policy-' + module.params['name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = True if module.params['state'] == 'absent': # mo must exist but all properties do not have to match if mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() changed = True else: mount_entry_matches = dict() mount_entry_dict = dict() if mo_exists: # check top-level mo props kwargs = dict(name=module.params['name']) kwargs['descr'] = module.params['description'] kwargs['retry_on_mount_fail'] = module.params['retry'] if (mo.check_prop_match(**kwargs)): parent_props_match = True logging.info("Parent Props Match") # check mount setting mount_entries = module.params.get(VMEDIA_MOUNT) if mount_entries: child_props_match = verify_mount_entries( mo, dn, mount_entries, mount_entry_dict, mount_entry_matches, ucs) logging.info("Child Props Match: %s", child_props_match) logging.debug(mount_entry_matches) else: child_props_match = True if not (parent_props_match and child_props_match): if not module.check_mode: # remove if some entries are not required if mo_exists: check_and_remove_mount_entries(mo, mount_entry_matches, ucs) # create if mo does not already exist if not parent_props_match: mo = CimcvmediaMountConfigPolicy( parent_mo_or_dn=dn_base, name=module.params['name'], descr=module.params['description'], retry_on_mount_fail=module.params['retry']) ucs.login_handle.add_mo(mo, True) for mount_entry_match in mount_entry_matches.keys(): if not mount_entry_matches[mount_entry_match]: logging.info("Adding child dn: %s", mount_entry_match) mount_entry = mount_entry_dict[mount_entry_match] mo_child = CimcvmediaConfigMountEntry( parent_mo_or_dn=mo, mapping_name=mount_entry.get('name'), description=mount_entry.get('description'), device_type=mount_entry.get('device'), mount_protocol=mount_entry.get('protocol'), remote_ip_address=mount_entry.get('remote_ip'), remote_host=mount_entry.get('remote_host'), image_name_variable=mount_entry.get( 'image_name_variable'), image_file_name=mount_entry.get('file'), image_path=mount_entry.get('path'), user_id=mount_entry.get('username'), password=mount_entry.get('password'), remap_on_eject=mount_entry.get( 'remap_on_eject'), auth_option=mount_entry.get('auth_option')) ucs.login_handle.add_mo(mo_child, True) ucs.login_handle.commit() changed = True except Exception as e: err = True ucs.result['msg'] = "setup error: %s " % str(e) traceback.print_exc() ucs.result['changed'] = changed if err: module.fail_json(**ucs.result) module.exit_json(**ucs.result)