def ipconfig_release(res): """Release the Windows IPv4 address for all adapters. To take a snapshot of a Windows server you need to release all IPv4 ethernet connections and shutdown the server. This will allow the snapshot to not be created with any prior network connections. If this is not done, the new server provisioned based on the snapshot created. Will not have network connectivity, until you release ethernet connections/renew. :param res: windows resource """ LOG.info('Attempt SSH connection to vm: %s.', res['name']) # connect get_ssh_conn(res['public_v4'], res['win_username'], res['win_password']) LOG.info('Release all IPv4 addresses for vm: %s.', res['name']) # release ips exec_cmd_by_ssh(res['public_v4'], res['win_username'], 'ipconfig /release', password=res['win_password'], fire_forget=True) LOG.info('Successfully released IPv4 addresses for vm: %s.', res['name'])
def run(self): """Configure Windows services on supplied systems. :return: exit code :rtype: int """ self.start() for res in self.res: try: # cloud providers host = res['public_v4'] except KeyError: host = res['ip'] self.extra_vars['hosts'] = host try: self.logger.info('Attempting to establish SSH connection ' 'to %s.' % host) get_ssh_conn(host, res['win_username'], res['win_password']) self.playbook.run(self.script, self.extra_vars, results_class=self.results_class, default_callback=self.default_callback) except SSHError: self.exit_code = 1 self.logger.error('Unable to establish SSH connection to ' '%s.' % host) except (AnsibleRuntimeError, SystemExit): self.exit_code = 1 finally: self.end() self.logger.info( 'END: %s, TIME: %dh:%dm:%ds' % (self.name, self.hours, self.minutes, self.seconds)) return self.exit_code
def rearm_server(res): """Extend the Windows trial period. Windows only allows X amount of days to use their software in the trial period. You can extend this up to 3 times. Need to rearm a server if planning to take snapshot in order to extend the trial period window. - slmgr.vbs -rearm (rearm server) - slmgr.vbs /dlv (Display software licensing) - shutdown.exe /r /f /t 0 (Restart to take effect) :param res: windows resource """ LOG.info('Attempt SSH connection to vm: %s.', res['name']) # connect get_ssh_conn(res['public_v4'], res['win_username'], res['win_password']) LOG.debug("Extending the rearm count for %s" % res['name']) # rearm server exec_cmd_by_ssh(res['public_v4'], res['win_username'], 'cscript C:\\Windows\\System32\\slmgr.vbs -rearm', password=res['win_password']) LOG.info('Attempt SSH connection to vm: %s.', res['name']) # connect get_ssh_conn(res['public_v4'], res['win_username'], res['win_password']) # restart server exec_cmd_by_ssh(res['public_v4'], res['win_username'], 'shutdown.exe /r /f /t 0', password=res['win_password']) LOG.debug('Successfully extended the rearm count for %s.' % res['name'])
def provision(self): """ Provision system resource(s) in Libvirt provider""" # -- basic flow -- # assume qcow and xml files required already exist as declared in # resources.yaml file # check if files exists (windows*.qcow and windows*.xml) # check if VM name based on resources.yaml already exists # Start VM # show VM info (network, etc) self.set_libvirt_env_var() # Create empty inventory file for localhost calls inventory_init(self.inventory) # check libvirt connection - validating authentication conn = self.util.get_connection() for elem in self.resources: LOG.info('Working to provision %s VM on %s' % (elem['name'], elem['provider'])) # check for required files LOG.debug("Checking %s exist" % elem['disk_source']) if not exists(elem['disk_source']): LOG.error('File %s not found' % elem['disk_source']) LOG.warn('check PAWS documentation %s' % LIBVIRT_AUTH_HELP) raise SystemExit(1) # check for VM and delete/undefine in case already exist if self.util.vm_exist(conn, elem['name']): vm = self.util.find_vm_by_name(conn, elem['name']) if vm: self.util.stop_vm(vm) self.util.delete_vm(conn, vm, flag=None) self.util.create_vm_virtinstall(elem) # get VM vm = self.util.find_vm_by_name(conn, elem['name']) try: # get vm info vm_info = self.util.get_vm_info(conn, elem) # loop to get SSH connection with auto-retry try: get_ssh_conn(vm_info['ip'], elem['win_username'], elem['win_password']) except Exception as ex: LOG.error(ex) except Exception: LOG.debug("An error happened during provision VM %s trying \ forced teardown" % elem['name']) self.teardown() # @attention Libvirt provider doesn't need hosts inventory file but # it is required by Winsetup and Group. # preparing resource to be compatible with ansible create inventory elem['ip'] = vm_info['ip'] # append ip to resource res = {} list_resources = [elem] res['resources'] = list_resources create_inventory(self.inventory, res) self.util.generate_resources_paws(conn) return self.resources_paws
def run(self): """The main method for winsetup. This method will create a list of systems to configure based on input, verify SSH connections with remote systems and then execute PowerShell script against them. """ # pre run tasks self.pre_run() self.logger.info("START: %s", self.name) # Save start time self.start() # Get systems self.get_systems() # Run PowerShell script against supplied machines for res in self.resources: pb_vars = {} # Get resource IP try: sut_ip = res['public_v4'] except KeyError: sut_ip = res['ip'] # Get resource authentication sut_ssh_user = res['win_username'] sut_ssh_password = res['win_password'] # Initialize playbook variables pb_vars["hosts"] = sut_ip pb_vars["ps"] = self.pshell try: _psvfile = join(self.userdir, self.psv) if isfile(_psvfile): # PowerShell vars is a file pb_vars["psv"] = _psvfile pvars = "file" elif isinstance(self.psv, string_types): # PowerShell vars is a string pvars = "str" pb_vars["psv"] = self.psv else: # PowerShell is neither file or unicode pvars = None except (AttributeError, TypeError): # No PowerShell vars defined, use default pvars = self.psv # Create playbook to run PowerShell script on Windows resources self.winsetup_yaml = create_ps_exec_playbook(self.userdir, pvars) # Test if remote machine is ready for SSH connection try: self.logger.info( "Attempting to establish SSH connection to %s", sut_ip) get_ssh_conn(sut_ip, sut_ssh_user, sut_ssh_password) # Playbook call - run PowerShell script on Windows resources self.playbook.run(self.winsetup_yaml, pb_vars, results_class=GenModuleResults) except SSHError: # set exit code self.exit_code = 1 self.logger.error("Unable to establish SSH connection to %s", sut_ip) except (AnsibleRuntimeError, SystemExit): # set exit code self.exit_code = 1 finally: # save end time self.end() # clean up run time files if not self.verbose: cleanup([self.winsetup_yaml], self.userdir) self.logger.info("END: %s, TIME: %dh:%dm:%ds", self.name, self.hours, self.minutes, self.seconds) return self.exit_code