def create_pbm_session(stub): """ Creates a session with the VMware Storage Policy API Sample Usage: create_pbm_session(service_instance._stub) """ import pyVmomi import ssl # Make compatible with both Python2/3 try: from http import cookies except ImportError: import Cookie as cookies session_cookie = stub.cookie.split('"')[1] http_context = VmomiSupport.GetHttpContext() cookie = cookies.SimpleCookie() cookie["vmware_soap_session"] = session_cookie http_context["cookies"] = cookie VmomiSupport.GetRequestContext()["vcSessionCookie"] = session_cookie hostname = stub.host.split(":")[0] context = None if hasattr(ssl, "_create_unverified_context"): context = ssl._create_unverified_context() pbm_stub = pyVmomi.SoapStubAdapter(host=hostname, version="pbm.version.version1", path="/pbm/sdk", poolSize=0, sslContext=context) pbm_si = pbm.ServiceInstance("ServiceInstance", pbm_stub) return pbm_si
def connect_to_vcs(host="localhost", port=443): """ Connect to VCS - currently utilizing VSAN mgmt service on ESX (/vsan) - and return SOAP stub """ si = vmdk_ops.get_si() # pylint: disable=no-member hostSystem = pyVim.host.GetHostSystem(si) token = hostSystem.configManager.vsanSystem.FetchVsanSharedSecret() version = pyVmomi.VmomiSupport.newestVersions.Get("vim") stub = pyVmomi.SoapStubAdapter(host=host, port=port, version=version, path="/vsan", poolSize=0) vpm = vim.cluster.VsanPerformanceManager("vsan-performance-manager", stub) # Disable certificate check during SSL communication disable_certificate_check() logged_in = vpm.Login(token) if not logged_in: print("Failed to get sims stub for host %s" % host) raise OSError("Failed to login to VSAN mgmt server") return stub
def connect(host, user, password): stub = pyVmomi.SoapStubAdapter( host=host, port=443, version='vim.version.version6', path='/sdk', certKeyFile=None, certFile=None) si = vim.ServiceInstance("ServiceInstance", stub) content = si.RetrieveContent() content.sessionManager.Login(user, password, None) return si
def ConnectToVsanClusterConfigSystem(stub, context=None): # sessionCookie = stub.cookie.split('"')[1] # httpContext = pyVmomi.VmomiSupport.GetHttpContext() # cookieObj = Cookie.SimpleCookie() # cookieObj["vmware_soap_session"] = sessionCookie # httpContext["cookies"] = cookieObj hostname = stub.host.split(":")[0] vccsStub = pyVmomi.SoapStubAdapter(host=hostname, version="vim.version.version11", path="/vsanHealth", sslContext=connect) vccsStub.cookie = stub.cookie vccs = vim.cluster.VsanVcClusterHealthSystem("vsan-cluster-config-system", vccsStub) return vccs
def connect(host, user, password, verify=True): if verify: context = None else: context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) context.verify_mode = ssl.CERT_NONE stub = pyVmomi.SoapStubAdapter( host=host, port=443, version='vim.version.version6', path='/sdk', sslContext=context) si = vim.ServiceInstance("ServiceInstance", stub) content = si.RetrieveContent() content.sessionManager.Login(user, password, None) return si
def GetUserNameAndPasswordWithLocalTicket(self): soapAdapter = pyVmomi.SoapStubAdapter(self.hostname, ns=self.namespace) serviceInstance = pyVmomi.Vim.ServiceInstance("ServiceInstance", soapAdapter) try: sessionManager = serviceInstance.content.sessionManager except Exception, e: if type(e).__name__ == 'ExpatError': raise pyVmomi.Vim.Fault.HostConnectFault( msg= 'Malformed response from hostd while querying for local ticket. (%s)' % e) else: if str(e).find("503 ") != -1: # PR 482756 descr = 'Check hostd process, it may be initializing. (%s)' % e else: descr = 'Check hostd process, retrieve local ticket from session manager failed. (%s)' % e raise pyVmomi.Vim.Fault.HostConnectFault(msg=descr)
def GetPbmConnection(stub, context=None): sessionCookie = stub.cookie.split('"')[1] httpContext = VmomiSupport.GetHttpContext() cookie = Cookie.SimpleCookie() cookie["vmware_soap_session"] = sessionCookie httpContext["cookies"] = cookie VmomiSupport.GetRequestContext()["vcSessionCookie"] = sessionCookie hostname = stub.host.split(":")[0] pbmStub = pyVmomi.SoapStubAdapter( host=hostname, version="pbm.version.version1", path="/pbm/sdk", poolSize=0, sslContext=context) pbmSi = pbm.ServiceInstance("ServiceInstance", pbmStub) pbmContent = pbmSi.RetrieveContent() return (pbmSi, pbmContent)
def GetPbmConnection(vpxdStub): import Cookie import pyVmomi sessionCookie = vpxdStub.cookie.split('"')[1] httpContext = VmomiSupport.GetHttpContext() cookie = Cookie.SimpleCookie() cookie["vmware_soap_session"] = sessionCookie httpContext["cookies"] = cookie VmomiSupport.GetRequestContext()["vcSessionCookie"] = sessionCookie hostname = vpxdStub.host.split(":")[0] context = None if hasattr(ssl, "_create_unverified_context"): context = ssl._create_unverified_context() pbmStub = pyVmomi.SoapStubAdapter(host=hostname, version="pbm.version.version1", path="/pbm/sdk", poolSize=0, sslContext=context) pbmSi = pbm.ServiceInstance("ServiceInstance", pbmStub) pbmContent = pbmSi.RetrieveContent() return (pbmSi, pbmContent)
if len(sys.argv) < 4: print "Enter HostIP RootPassword CertPath KeyPath" print "Incorrect set of arguments passed." sys.exit() host_ip = sys.argv[1] root_password = sys.argv[2] cert_path = sys.argv[3] key_path = sys.argv[4] print "Apply Certificate: %s, %s, %s" % (host_ip, cert_path, key_path) print "Connecting to hostd CertificateManager" ssl._create_default_https_context = ssl._create_unverified_context stub = pyVmomi.SoapStubAdapter(host=host_ip, version="vim.version.version10", path="/sdk") si = vim.ServiceInstance("ServiceInstance", stub) content = si.RetrieveServiceContent() content.sessionManager.Login("root", root_password) cert_manager = vim.host.CertificateManager('ha-certificate-manager', stub) print "Loading certificate from file" cert = open(cert_path, 'r').read() print "Installing certificate" dName = '/CN=%s' % host_ip csr = cert_manager.GenerateCertificateSigningRequestByDn(dName) cert_manager.InstallServerCertificate(cert=cert) print "Replacing private key /etc/vmware/ssl/rui.key" shutil.copyfile(key_path, "/etc/vmware/ssl/rui.key")