def _build_image_on_openstack(self, user_info, node_instance): self._thread_local.driver = self._get_driver(user_info) listener = self._get_listener() if not user_info.get_private_key() and self.tempPrivateKey: user_info.set_private_key(self.tempPrivateKey) machine_name = node_instance.get_name() vm = self._get_vm(machine_name) util.printAndFlush("\n node_instance: %s \n" % str(node_instance)) util.printAndFlush("\n VM: %s \n" % str(vm)) ip_address = self._vm_get_ip(vm) vm_id = self._vm_get_id(vm) instance = vm['instance'] self._wait_instance_in_running_state(vm_id) self._build_image_increment(user_info, node_instance, ip_address) util.printStep('Creation of the new Image.') listener.write_for(machine_name, 'Saving the image') newImg = self._thread_local.driver.create_image(instance, node_instance.get_image_short_name(), metadata=None) self._wait_image_creation_completed(newImg.id) listener.write_for(machine_name, 'Image saved !') return newImg.id
def _stop_vms_by_ids(self, ids): util.printAndFlush("Stop ids: %s" % ids) for _, node in self.get_vms().items(): util.printAndFlush(" Node: %s" % node['host']) if node['host'] in ids: self.__stop_image(node['username'], node['private_key'], node['password'], node['host'])
def _remote_run_build_target(self, node_instance, host, target, username, password, private_key_file): for subtarget in target: target_name = subtarget.get('name') full_target_name = '%s.%s' % (subtarget.get('module'), target_name) if not MachineExecutor.need_to_execute_build_step( node_instance, target, subtarget): message = 'Component already built. Nothing to do on target "%s" for node "%s"\n' \ % (full_target_name, node_instance.get_name()) util.printAndFlush(message) continue script = subtarget.get('script') if script: message = 'Executing target "%s" on remote host "%s"' % ( full_target_name, node_instance.get_name()) self._print_step(message) remoteRunScript(username, host, script, sshKey=private_key_file, password=password) else: message = 'Nothing to do for target "%s" on node "%s""\n' % ( full_target_name, node_instance.get_name()) util.printAndFlush(message)
def _start_stop_instances_by_ids(self): vm_ids = [] try: vm_ids = self._start_instances() finally: util.printAndFlush('Stopping VMs by ids\n') self.client.stop_vms_by_ids(vm_ids)
def _launchWindowsBootstrapScript(self, image_info, nodename, ip): username, password = self._getSshUsernamePassword(image_info, nodename) script = self._getBootstrapScript(nodename, username=username) winrm = self._getWinrm(ip, username, password) self._waitCanConnectWithWinrmOrAbort(winrm) self._printDetail("Launching bootstrap script on %s:\n%s\n" % (ip, script)) util.printAndFlush(script) winrm.timeout = winrm.set_timeout(600) output = self._runScriptWithWinrm(winrm, script) self._printDetail("Launched bootstrap script on %s:\n%s\n" % (ip, output))
def test_1_startStopImages(self): self.client.startNodesAndClients(self.user_info, [self.node_info]) util.printAndFlush('Instances started') vms = self.client.getVms() assert len(vms) == self.multiplicity self.client.stopDeployment()
def _run_target_script(self, target_script, exports=None, ignore_abort=False, name=None): '''Return exit code of the user script and the last line of stderr Output of the script goes to stdout/err and will end up in the node executor's log file. ''' _name = self._get_script_name(name) if not target_script: util.printAndFlush('Script "%s" is empty\n' % (_name,)) return self.SCRIPT_EXIT_SUCCESS if not isinstance(target_script, basestring): raise ExecutionException('Not a string buffer provided as target for script "%s". Type is: %s' % (_name, type(target_script))) process = self._launch_process(target_script, exports, name) result = Queue() t = Thread(target=self.print_and_keep_last_stderr, args=(process.stderr, result)) t.daemon = True # thread dies with the program t.start() try: # The process is still working on the background. while process.poll() is None: # Ask server whether the abort flag is set. If so, kill the # process and exit. Otherwise, sleep for some time. if not ignore_abort and self.wrapper.isAbort(): try: util.printDetail('Abort flag detected. ' 'Terminating execution of script "%s"...' % (_name,)) process.terminate() util.sleep(5) if process.poll() is None: util.printDetail('Termination is taking too long. ' 'Killing the script "%s"...' % (_name,)) process.kill() except OSError: pass break util.sleep(self.TARGET_POLL_INTERVAL) except IOError as e: if e.errno != errno.EINTR: raise else: util.printDetail('Signal EINTR detected. Ignoring it.') return 0 util.printDetail("End of the script '%s'" % (_name,)) stderr_last_line = '' try: stderr_last_line = result.get(timeout=60) except Empty: pass return process.returncode, stderr_last_line
def xtest_1_startStopImages(self): self.client.run_category = RUN_CATEGORY_DEPLOYMENT self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started') vms = self.client.get_vms() assert len(vms) == self.multiplicity self.client.stop_deployment()
def xtest_1_startStopImages(self): self.client.start_nodes_and_clients(self.user_info, self.nodes_instances) util.printAndFlush('Instances started') vms = self.client.get_vms() assert len(vms) == 3 # You need to put a breakpoint on the next line and manually check /tmp/slipstream* on the two nodes self.client._stop_deployment()
def _start_images(self): for node_instance in self.node_instances: self.log('Starting %s' % node_instance) self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started\n') vms = self.client.get_vms() for vm_name in vms: vm = vms[vm_name] self.log('Started %s: %s' % (vm_name, vm)) instanceId = self.client._vm_get_id(vm) self.node_instances[vm_name].set_parameter(NodeDecorator.INSTANCEID_KEY, instanceId) assert len(vms) == self.multiplicity
def __launch_windows_bootstrap_script(self, node_instance, ip): username, password = self.__get_vm_username_password(node_instance, 'administrator') script = self._get_bootstrap_script(node_instance, username=username) winrm = self._getWinrm(ip, username, password) self._waitCanConnectWithWinrmOrAbort(winrm) self._print_detail("Launching bootstrap script on %s:\n%s\n" % (ip, script)) util.printAndFlush(script) winrm.timeout = winrm.set_timeout(600) output = self._runScriptWithWinrm(winrm, script) self._print_detail("Launched bootstrap script on %s:\n%s\n" % (ip, output))
def onInitializing(self): util.printAction('Initializing') self._addSshPubkeyIfNeeded() util.printStep('Getting deployment targets') self.targets = self.wrapper.getTargets() util.printDetail('Deployment targets:') for target, script in self.targets.items(): util.printAndFlush('-' * 25) util.printDetail('Target: %s' % target) util.printDetail('Script:\n%s\n' % script[0])
def _start_instances(self): self.client._get_max_workers = Mock(return_value=self.max_iaas_workers) util.printAndFlush('Starting instances\n') self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started\n') vms = self.client.get_vms() assert len(vms) == int(self.multiplicity) vm_ids = [] runners = vms.values() util.printAndFlush('Waiting VMs to go into Running state\n') for runner in runners: vm_id = runner.vmIds[0] state = self.client._wait_vm_in_state([ 'Running', ], runner, vm_id, counts=50, sleep=6, throw=True) assert 'Running' == state util.printAndFlush('VM %s is Running\n' % vm_id) vm_ids.append(vm_id) return vm_ids
def __launch_windows_bootstrap_script(self, node_instance, ip): username, password = self.__get_vm_username_password( node_instance, 'administrator') script = self._get_bootstrap_script(node_instance, username=username) winrm = self._getWinrm(ip, username, password) self._waitCanConnectWithWinrmOrAbort(winrm) self._print_detail("Launching bootstrap script on %s:\n%s\n" % (ip, script)) util.printAndFlush(script) winrm.timeout = winrm.set_timeout(600) output = self._runScriptWithWinrm(winrm, script) self._print_detail("Launched bootstrap script on %s:\n%s\n" % (ip, output))
def xtest_1_startStopImages(self): self.client._get_max_workers = Mock(return_value=self.max_iaas_workers) self.client.run_category = RUN_CATEGORY_DEPLOYMENT try: self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started') vms = self.client.get_vms() assert len(vms) == self.multiplicity finally: self.client.stop_deployment()
def _start_wait_running_stop_images(self): try: self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started\n') vms = self.client.get_vms() assert len(vms) == self.multiplicity for vm in vms.values(): self.client._wait_vm_in_state_running_or_timeout(vm['id']) time.sleep(2) finally: self.client.stop_deployment()
def _remote_run_build_target(self, node_instance, host, target, username, password, private_key_file): for subtarget in target: target_name = subtarget.get('name') full_target_name = '%s.%s' % (subtarget.get('module'), target_name) if not MachineExecutor.need_to_execute_build_step(node_instance, target, subtarget): message = 'Component already built. Nothing to do on target "%s" for node "%s"\n' \ % (full_target_name, node_instance.get_name()) util.printAndFlush(message) continue script = subtarget.get('script') if script: message = 'Executing target "%s" on remote host "%s"' % (full_target_name, node_instance.get_name()) self._print_step(message) remoteRunScript(username, host, script, sshKey=private_key_file, password=password) else: message = 'Nothing to do for target "%s" on node "%s""\n' % (full_target_name, node_instance.get_name()) util.printAndFlush(message)
def _runCommandWithWinrm(self, winrm, command, shellId=None, runAndContinue=False): if shellId: _shellId = shellId else: _shellId = winrm.open_shell() util.printAndFlush('\nwinrm.run_command\n') commandId = winrm.run_command(_shellId, command, []) stdout, stderr, returnCode = ('N/A', 'N/A', 0) if not runAndContinue: util.printAndFlush('\nwinrm.get_command_output\n') try: stdout, stderr, returnCode = winrm.get_command_output( _shellId, commandId) except Exception as e: # pylint: disable=broad-except print 'WINRM Exception: %s' % str(e) util.printAndFlush('\nwinrm.cleanup_command\n') winrm.cleanup_command(_shellId, commandId) if not shellId: winrm.close_shell(_shellId) return stdout, stderr, returnCode
def _execute_target(self, target_name, exports=None, abort_on_err=False, ssdisplay=True, ignore_abort=False): target = self.node_instance.get_image_target(target_name) display_target_name = { 'prerecipe': 'Pre-install', 'recipe': 'Post-install', 'execute': 'Deployment', 'report': 'Reporting', 'onvmadd': 'On VM Add', 'onvmremove': 'On VM Remove' }.get(target_name, target_name) if target is None: util.printAndFlush('Nothing to do for script: %s' % display_target_name) return for subtarget in target: full_target_name = '%s:%s' % (subtarget.get('module_uri'), display_target_name) if target_name in [NodeDecorator.NODE_PRERECIPE, NodeDecorator.NODE_RECIPE] \ and not self._need_to_execute_build_step(target, subtarget): util.printAndFlush('Component already built. Nothing to do on target: %s' % full_target_name) continue script = subtarget.get('script') if script: message = "Executing script '%s'" % full_target_name util.printStep(message) if ssdisplay: self.wrapper.set_statecustom(message) fail_msg = "Failed running '%s' script on '%s'" % (full_target_name, self._get_node_instance_name()) self._launch_script(script, exports, abort_on_err, ignore_abort, fail_msg, full_target_name) else: util.printAndFlush('Nothing to do for script: %s' % full_target_name)
def _getBootstrapScript(self, nodename, preExport=None, preBootstrap=None, postBootstrap=None, username=None): script = '' addEnvironmentVariableCommand = '' if self.isWindows(): addEnvironmentVariableCommand = 'set' else: addEnvironmentVariableCommand = 'export' script += '#!/bin/sh -ex\n' if preExport: script += '%s\n' % preExport util.printAndFlush(str(os.environ)) for var, val in os.environ.items(): if var.startswith('SLIPSTREAM_') and var != 'SLIPSTREAM_NODENAME': if var == 'SLIPSTREAM_REPORT_DIR' and self.isWindows(): val = Client.WINDOWS_REPORTSDIR if re.search(' ', val): val = '"%s"' % val script += '%s %s=%s\n' % (addEnvironmentVariableCommand, var, val) script += '%s SLIPSTREAM_NODENAME=%s\n' % (addEnvironmentVariableCommand, nodename) script += '%s %s=%s\n' % (addEnvironmentVariableCommand, util.ENV_NEED_TO_ADD_SSHPUBKEY, self.hasCapability(self.ADD_SSHPUBKEY_ON_NODE)) if preBootstrap: script += '%s\n' % preBootstrap script += '%s\n' % self._buildSlipStreamBootstrapCommand(nodename, username) if postBootstrap: script += '%s\n' % postBootstrap return script
def _buildImageOnOpenStack(self, userInfo, imageInfo): self._thread_local.driver = self._getDriver(userInfo) vm = self.getVm(NodeDecorator.MACHINE_NAME) util.printAndFlush("\n imageInfo: %s \n" % str(imageInfo)) util.printAndFlush("\n VM: %s \n" % str(vm)) ipAddress = self.vmGetIp(vm) self._creatorVmId = self.vmGetId(vm) instance = vm['instance'] self._waitInstanceInRunningState(self._creatorVmId) self._buildImageIncrement(userInfo, imageInfo, ipAddress) attributes = self.getAttributes(imageInfo) util.printStep('Creation of the new Image.') newImg = self._thread_local.driver.ex_save_image(instance, attributes['shortName'], metadata=None) self._waitImageCreationCompleted(newImg.id) self._newImageId = newImg.id
def _runCommandWithWinrm(self, winrm, command, shellId=None, runAndContinue=False): if shellId: _shellId = shellId else: _shellId = winrm.open_shell() util.printAndFlush('\nwinrm.run_command\n') commandId = winrm.run_command(_shellId, command, []) stdout, stderr, returnCode = ('N/A', 'N/A', 0) util.printAndFlush('\nwinrm.get_command_output\n') if not runAndContinue: try: stdout, stderr, returnCode = winrm.get_command_output(_shellId, commandId) except Exception as e: print 'WINRM Exception: %s' % str(e) util.printAndFlush('\nwinrm.cleanup_command\n') if not runAndContinue: winrm.cleanup_command(_shellId, commandId) if not shellId: winrm.close_shell(_shellId) return stdout, stderr, returnCode
def _install_user_packages(self): util.printAndFlush('Installing packages') if self.is_image_built(): util.printAndFlush( 'Component already built. No packages to install') return packages = self.node_instance.get_packages() if packages: message = 'Installing packages: %s' % ', '.join(packages) fail_msg = "Failed installing packages on '%s'" % self._get_node_instance_name( ) util.printStep(message) self.wrapper.set_statecustom(message) cmd = util.get_packages_install_command( self.node_instance.get_platform(), packages) self._launch_script('#!/bin/sh -xe\n%s' % cmd, fail_msg=fail_msg, name='Install packages') else: util.printAndFlush('No packages to install')
def _start_stop_instances(self): try: self._start_instances() finally: util.printAndFlush('Stopping deployment\n') self.client.stop_deployment()
def stopVmsByIds(self, ids): util.printAndFlush("Stop ids: %s" % ids) for _, node in self.getVms().items(): util.printAndFlush(" Node: %s" % node['host']) if node['host'] in ids: self._stopImages(node['username'], node['privateKey'], node['password'], node['host'])