Best Python code snippet using autotest_python
install_server.py
Source:install_server.py
...34 if self.xmlrpc_url:35 self.server = xmlrpclib.Server(self.xmlrpc_url)36 self.token = self.server.login(self.user, self.password)37 self.num_attempts = int(kwargs.get('num_attempts', 2))38 def get_system_handle(self, host):39 """40 Get a system handle, needed to perform operations on the given host41 :param host: Host name42 :return: Tuple (system, system_handle)43 """44 try:45 system = self.server.find_system({"name": host.hostname})[0]46 except IndexError, detail:47 # TODO: Method to register this system as brand new48 logging.error("Error finding %s: %s", host.hostname, detail)49 raise ValueError("No system %s registered on install server" %50 host.hostname)51 system_handle = self.server.get_system_handle(system, self.token)52 return (system, system_handle)53 def _set_host_profile(self, host, profile=''):54 system, system_handle = self.get_system_handle(host)55 system_info = self.server.get_system(system)56 # If no fallback profile is enabled, we don't want to mess57 # with the currently profile set for that machine.58 if profile:59 self.server.modify_system(system_handle, 'profile', profile,60 self.token)61 self.server.save_system(system_handle, self.token)62 # Enable netboot for that machine (next time it'll reboot and be63 # reinstalled)64 self.server.modify_system(system_handle, 'netboot_enabled', 'True',65 self.token)66 self.server.save_system(system_handle, self.token)67 try:68 # Cobbler only generates the DHCP configuration for netboot enabled69 # machines, so we need to synchronize the dhcpd file after changing70 # the value above71 self.server.sync_dhcp(self.token)72 except xmlrpclib.Fault, err:73 # older Cobbler will not recognize the above command74 if "unknown remote method" not in err.faultString:75 logging.error("DHCP sync failed, error code: %s, error string: %s",76 err.faultCode, err.faultString)77 def _disable_host_installation(self, host):78 system, system_handle = self.get_system_handle(host)79 system_info = self.server.get_system(system)80 # Disable netboot for that machine (principle of least surprise on81 # failure)82 self.server.modify_system(system_handle, 'netboot_enabled', 'False',83 self.token)84 self.server.save_system(system_handle, self.token)85 try:86 # Cobbler only generates the DHCP configuration for netboot enabled87 # machines, so we need to synchronize the dhcpd file after changing88 # the value above89 self.server.sync_dhcp(self.token)90 except xmlrpclib.Fault, err:91 # older Cobbler will not recognize the above command92 if "unknown remote method" not in err.faultString:93 logging.error("DHCP sync failed, error code: %s, error string: %s",94 err.faultCode, err.faultString)95 def install_host(self, host, profile='', timeout=None, num_attempts=2):96 """97 Install a host object with profile name defined by distro.98 :param host: Autotest host object.99 :param profile: String with cobbler profile name.100 :param timeout: Amount of time to wait for the install.101 :param num_attempts: Maximum number of install attempts.102 """103 if not self.xmlrpc_url:104 return105 installations_attempted = 1106 step_time = 60107 if timeout is None:108 # 1 hour of timeout by default109 timeout = 3600110 system, system_handle = self.get_system_handle(host)111 if not profile:112 profile = self.server.get_system(system).get('profile')113 if not profile:114 e_msg = 'Unable to determine profile for host %s' % host.hostname115 raise error.HostInstallProfileError(e_msg)116 host.record("START", None, "install", host.hostname)117 host.record("GOOD", None, "install.start", host.hostname)118 logging.info("Installing machine %s with profile %s (timeout %s s)",119 host.hostname, profile, timeout)120 install_start = time.time()121 time_elapsed = 0122 install_successful = False123 while ((not install_successful) and124 (installations_attempted <= self.num_attempts) and125 (time_elapsed < timeout)):126 self._set_host_profile(host, profile)127 self.server.power_system(system_handle,128 'reboot', self.token)129 installations_attempted += 1130 while time_elapsed < timeout:131 time.sleep(step_time)132 # Cobbler signals that installation if finished by running133 # a %post script that unsets netboot_enabled. So, if it's134 # still set, installation has not finished. Loop and sleep.135 if not self.server.get_system(system).get('netboot_enabled'):136 logging.debug('Cobbler got signaled that host %s '137 'installation is finished',138 host.hostname)139 break140 # Check if the installed profile matches what we asked for141 installed_profile = self.server.get_system(system).get('profile')142 install_successful = (installed_profile == profile)143 if install_successful:144 logging.debug('Host %s installation successful', host.hostname)145 break146 else:147 logging.info('Host %s installation resulted in different '148 'profile', host.hostname)149 time_elapsed = time.time() - install_start150 if not install_successful:151 e_msg = 'Host %s install timed out' % host.hostname152 host.record("END FAIL", None, "install", e_msg)153 self._disable_host_installation(host)154 raise error.HostInstallTimeoutError(e_msg)155 remove_hosts_file()156 host.wait_for_restart()157 host.record("END GOOD", None, "install", host.hostname)158 time_elapsed = time.time() - install_start159 logging.info("Machine %s installed successfully after %d s (%d min)",160 host.hostname, time_elapsed, time_elapsed / 60)161 def power_host(self, host, state='reboot'):162 """163 Power on/off/reboot a host through cobbler.164 :param host: Autotest host object.165 :param state: Allowed states - one of 'on', 'off', 'reboot'.166 """167 if self.xmlrpc_url:168 system_handle = self.get_system_handle(host)[1]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!