def try_ssh(virtual_machine: VirtualMachine, retries: int = 10): """ Try to connect to a virtual machine using ssh :param virtual_machine: the virtual machine :param retries: the maximum of retries """ retry = 0 connected = False ssh = SSHClient() ssh.set_missing_host_key_policy(AutoAddPolicy()) ssh.load_system_host_keys() while retry < retries and not connected: try: logger.debug('Trying ssh connection to %s %s out of %s retries', virtual_machine.hostname, retry, retries) ssh.connect(virtual_machine.hostname, port=virtual_machine.ssh_port, username=virtual_machine.ssh_username, password=virtual_machine.ssh_password) connected = True except (BadHostKeyException, AuthenticationException, SSHException, socket.error): time.sleep(10) retry += 1 if not connected: raise Exception( '[{}] Unable to connect to the machine after {} tries'.format( virtual_machine.hostname, retry)) logger.info('Connection established to %s after %d out of %d retries', virtual_machine.name, retry, retries)
def invoke(self, ctx: click.Context): type_name: str = ctx.params.get('project_type') project_type = config.get('project_type').get(type_name) logger.debug("Project Type virtual machine is %s", project_type.virtual_machine) if project_type.virtual_machine: self.start_virtual_machine(project_type.virtual_machine)
def _copy_directory(self, target_folder: str): """ Copy the template folder into the target folder :param target_folder: the target folder :return: """ template_directory = self.template_path() logger.debug("[Template.py] Copy files from %s to %s", template_directory, target_folder) copy_tree(template_directory, target_folder)
def refresh_mutagen_entries(self): """ Refresh current mutagen data :return: """ if not self.is_mutagened(): self.mutagen_entries = [] return logger.debug(self.name + " : refresh mutagen") mutagen_helper = Manager() self.mutagen_entries = mutagen_helper.list(path=self.get_folder())
def invoke(self, ctx: click.Context): name: str = ctx.params.get('project') virtual_machine: bool = ctx.params.get('vm') if not self.exists(name): logger.error('Unable to find %s path', name) sys.exit(1) project_type = self.detect_project_type() if project_type is None: logger.error('Unable to determine %s project type', name) sys.exit(1) logger.info('Project type detected for %s is %s', name, project_type.name) if project_type.is_mutagened(): logger.info("Mutagen configuration detected") mutagen_helper = Manager() mutagen_helper.down(path=project_type.get_folder(), project=name) logger.debug("Project virtual machine is %s", project_type.virtual_machine) if project_type.virtual_machine and virtual_machine: self.stop_virtual_machine(project_type.virtual_machine)
def invoke(self, ctx: click.Context): name: str = ctx.params.get('project') noexec: bool = ctx.params.get('noexec') if not StartCommand.exists(name): logger.error("Unable to find %s path", name) sys.exit(1) project_type = self.detect_project_type() if project_type is None: logger.error("Unable to determine %s project type", name) sys.exit(1) logger.debug("Project virtual machine is %s", project_type.virtual_machine) if project_type.virtual_machine: self.start_virtual_machine(project_type.virtual_machine) logger.info("Project type detected for %s is %s", name, project_type.name) if project_type.is_mutagened(): logger.info("Mutagen configuration detected") mutagen_helper = Manager() mutagen_helper.up(path=project_type.get_folder(), project=name) if not noexec: project_type.exec_commands(self.path(name))
def _copy_git(self, target_folder): """ Copy the git template into the target folder :param target_folder: the target folder :return: """ template_directory = self.template_path() if os.path.isdir(template_directory): logger.debug("[Template.py] Updating template from git") Git(template_directory).pull() else: logger.debug("[Template.py] Cloning template from git") Repo.clone_from(url=self.path, to_path=template_directory) self._copy_directory(target_folder) logger.debug("[Template.py] Removal of .git folder coming from template") shutil.rmtree(os.path.join(target_folder, '.git'), onerror=lambda func, path, exc_info: (os.chmod(path, stat.S_IWRITE), os.unlink(path)))