Example #1
0
    def server_repository_url(self):
        server_id = self.install_job.server_id
        server = self.db_session.query(Server).filter(Server.id == server_id).first()

        if server is not None:
            server_type = server.server_type

            if server_type == ServerType.TFTP_SERVER:
                url = server.server_url
                server_sub_directory = self.install_job.server_directory
                
                if server_sub_directory is not None and len(server_sub_directory) > 0:
                    url += '/' + server_sub_directory                       
                return url
            
            elif server_type == ServerType.FTP_SERVER or server_type == ServerType.SFTP_SERVER:                              
                protocol = 'ftp' if server_type == ServerType.FTP_SERVER else 'sftp'
                url = protocol + "://{}:{}@{}".format(server.username, server.password, server.server_url) 
                
                remote_directory = concatenate_dirs(server.server_directory, self.install_job.server_directory)              
                if len(remote_directory) > 0:
                    url = url + "/{}".format(remote_directory)
                return url
            elif server_type == ServerType.LOCAL_SERVER:
                return server.server_url
        
        return None
Example #2
0
    def get_file_and_directory_dict(self, sub_directory=None):
        result_list = []
        is_reachable = False
  
        try:
            ftp = ftplib.FTP(self.server.server_url, user=self.server.username, passwd=self.server.password)
                   
            remote_directory = concatenate_dirs(self.server.server_directory, sub_directory)
            if len(remote_directory) > 0:
                ftp.cwd(remote_directory)
            
            dirs, nondirs = self.listdir(ftp)
    
            for file_tuple in nondirs:
                result_list.append({'filename': file_tuple[0], 'is_directory': False})
                
            for file_tuple in dirs:
                if sub_directory is None or len(sub_directory) == 0:
                    result_list.append({'filename': file_tuple[0], 'is_directory': True})
                else:
                    result_list.append({'filename': os.path.join(sub_directory, file_tuple[0]), 'is_directory': True})

            is_reachable = True
        except Exception as e:
            logger.exception('FTPServer hit exception - ' + e.message)
 
        return result_list, is_reachable
Example #3
0
    def server_repository_url(self):
        server_id = self.install_job.server_id
        server = self.db_session.query(Server).filter(Server.id == server_id).first()

        if server is not None:
            server_type = server.server_type

            if server_type == ServerType.TFTP_SERVER:
                url = server.server_url
                server_sub_directory = self.install_job.server_directory
                
                if server_sub_directory is not None and len(server_sub_directory) > 0:
                    url += '/' + server_sub_directory                       
                return url
            
            elif server_type == ServerType.FTP_SERVER or server_type == ServerType.SFTP_SERVER:                              
                protocol = 'ftp' if server_type == ServerType.FTP_SERVER else 'sftp'
                url = protocol + "://{}:{}@{}".format(server.username, server.password, server.server_url) 
                
                remote_directory = concatenate_dirs(server.server_directory, self.install_job.server_directory)              
                if len(remote_directory) > 0:
                    url = url + "/{}".format(remote_directory)
                return url
        
        return None
Example #4
0
    def get_file_and_directory_dict(self, sub_directory=None):
        result_list = []
        is_reachable = False

        if not SFTP_SUPPORTED:
            return result_list, is_reachable
        
        try:
            with pysftp.Connection(self.server.server_url, username=self.server.username, password=self.server.password) as sftp:
                remote_directory = concatenate_dirs(self.server.server_directory, sub_directory)
                if len(remote_directory) > 0:
                    sftp.chdir(remote_directory)

                file_info_list = sftp.listdir()
                for file_info in file_info_list:
                    file = {}
                    lstatout = str(sftp.lstat(file_info)).split()[0]

                    if 'd' in lstatout:
                        if sub_directory is None or len(sub_directory) == 0:
                            file['filename'] = file_info
                        else:
                            file['filename'] = sub_directory + '/' + file_info
                        file['is_directory'] = True
                    else:
                        file['filename'] = file_info
                        file['is_directory'] = False

                    result_list.append(file)

            is_reachable = True
        except Exception as e:
            logger.exception('SFTPServer hit exception - ' + e.message)
            
        return result_list, is_reachable
Example #5
0
    def upload_file(self,
                    source_file_path,
                    dest_filename,
                    sub_directory=None,
                    callback=None):
        try:
            file = open(source_file_path, 'rb')

            ftp = ftplib.FTP(self.server.server_url,
                             user=self.server.username,
                             passwd=self.server.password)

            remote_directory = concatenate_dirs(self.server.server_directory,
                                                sub_directory)
            if len(remote_directory) > 0:
                ftp.cwd(remote_directory)

            # default block size is 8912
            if callback:
                ftp.storbinary('STOR ' + dest_filename,
                               file,
                               callback=callback)
            else:
                ftp.storbinary('STOR ' + dest_filename, file)
            ftp.quit()
            file.close()

        finally:
            if file is not None:
                file.close()
Example #6
0
    def get_file_and_directory_dict(self, sub_directory=None):
        result_list = []
        is_reachable = True
        
        try:
            sftp_module = import_module('pysftp')
            if sftp_module is not None:
                with sftp_module.Connection(self.server.server_url, username=self.server.username, password=self.server.password) as sftp:
                    remote_directory = concatenate_dirs(self.server.server_directory, sub_directory)
                    if len(remote_directory) > 0:
                        sftp.chdir(remote_directory)
    
                    file_info_list = sftp.listdir()
                    for file_info in file_info_list:
                        file = {}
                        lstatout = str(sftp.lstat(file_info)).split()[0]
                        if 'd' in lstatout:
                            if sub_directory is None or len(sub_directory) == 0:
                                file['filename'] = file_info
                            else:
                                file['filename'] = sub_directory + '/' + file_info
                            file['is_directory'] = True 
                        else:
                            file['filename'] = file_info
                            file['is_directory'] = False

                        result_list.append(file)
        except:
            logger.exception('SFTPServer hit exception')
            is_reachable = False
            
        return result_list, is_reachable
Example #7
0
    def delete_file(self, filename, sub_directory=None, callback=None):
        ftp = ftplib.FTP(self.server.server_url, user=self.server.username, passwd=self.server.password)

        remote_directory = concatenate_dirs(self.server.server_directory, sub_directory)
        if len(remote_directory) > 0:
            ftp.cwd(remote_directory)

        ftp.delete(filename)
Example #8
0
    def upload_file(self, source_file_path, dest_filename, sub_directory=None):
        sftp_module = import_module('pysftp')

        with sftp_module.Connection(self.server.server_url, username=self.server.username, password=self.server.password) as sftp:
            remote_directory = concatenate_dirs(self.server.server_directory, sub_directory)
            if len(remote_directory) > 0:
                sftp.chdir(remote_directory)
            sftp.put(source_file_path) 
Example #9
0
    def server_repository_url(self):
        """
        Return the server repository URL (TFTP/FTP) where the packages can be found.
        tftp://223.255.254.254;VRF/auto/tftp-gud/sit
        ftp://username:[email protected];VRF/remote/directory
        """
        server_id = self.install_job.server_id
        server = self.db_session.query(Server).filter(
            Server.id == server_id).first()

        if server is not None:
            server_type = server.server_type

            if server_type == ServerType.TFTP_SERVER:
                tftp_string = 'tftp://'
                url = '{}{}'.format(tftp_string,
                                    server.server_url.replace(tftp_string, ''))

                if not is_empty(server.vrf):
                    try:
                        pos = url.index('/', len(tftp_string))
                    except ValueError:
                        pos = len(url)
                    url = url[:pos] + ';' + server.vrf + url[pos:]

                server_sub_directory = self.install_job.server_directory

                if not is_empty(server_sub_directory):
                    url += '/' + server_sub_directory

                return url

            elif server_type == ServerType.FTP_SERVER or server_type == ServerType.SFTP_SERVER:
                protocol = 'ftp' if server_type == ServerType.FTP_SERVER else 'sftp'
                url = protocol + "://{}:{}@{}".format(
                    server.username, server.password, server.server_url)

                if server_type == ServerType.FTP_SERVER and not is_empty(
                        server.vrf):
                    url = url + ";{}".format(server.vrf)

                remote_directory = concatenate_dirs(
                    server.server_directory, self.install_job.server_directory)
                if not is_empty(remote_directory):
                    url = url + "/{}".format(remote_directory)

                return url
            elif server_type == ServerType.SCP_SERVER:
                # scp root:[email protected]:/home_directory destination_on_host
                return "scp {}:{}@{}:{} {}".format(server.username,
                                                   server.password,
                                                   server.server_url,
                                                   server.server_directory,
                                                   server.destination_on_host)
            elif server_type == ServerType.LOCAL_SERVER:
                return server.server_url

        return None
Example #10
0
    def upload_file(self, source_file_path, dest_filename, sub_directory=None):
        sftp_module = import_module('pysftp')

        with sftp_module.Connection(self.server.server_url,
                                    username=self.server.username,
                                    password=self.server.password) as sftp:
            remote_directory = concatenate_dirs(self.server.server_directory,
                                                sub_directory)
            if len(remote_directory) > 0:
                sftp.chdir(remote_directory)
            sftp.put(source_file_path)
Example #11
0
    def delete_file(self, filename, sub_directory=None, callback=None):
        ftp = ftplib.FTP(self.server.server_url,
                         user=self.server.username,
                         passwd=self.server.password)

        remote_directory = concatenate_dirs(self.server.server_directory,
                                            sub_directory)
        if len(remote_directory) > 0:
            ftp.cwd(remote_directory)

        ftp.delete(filename)
Example #12
0
    def upload_file(self, source_file_path, dest_filename, sub_directory=None, callback=None):
        if SFTP_SUPPORTED:
            with pysftp.Connection(self.server.server_url, username=self.server.username, password=self.server.password) as sftp:
                remote_directory = concatenate_dirs(self.server.server_directory, sub_directory)
                if len(remote_directory) > 0:
                    sftp.chdir(remote_directory)

                if callback:
                    sftp.put(source_file_path, remotepath=dest_filename, callback=callback)
                else:
                    sftp.put(source_file_path, remotepath=dest_filename)
Example #13
0
    def server_repository_url(self):
        """
        Return the server repository URL (TFTP/FTP) where the packages can be found.
        tftp://223.255.254.254;VRF/auto/tftp-gud/sit
        ftp://username:[email protected];VRF/remote/directory
        """
        server_id = self.install_job.server_id
        server = self.db_session.query(Server).filter(Server.id == server_id).first()

        if server is not None:
            server_type = server.server_type

            if server_type == ServerType.TFTP_SERVER:
                tftp_string = 'tftp://'
                url = '{}{}'.format(tftp_string, server.server_url.replace(tftp_string, ''))

                if not is_empty(server.vrf):
                    try:
                        pos = url.index('/', len(tftp_string))
                    except ValueError:
                        pos = len(url)
                    url = url[:pos] + ';' + server.vrf + url[pos:]

                server_sub_directory = self.install_job.server_directory
                
                if not is_empty(server_sub_directory):
                    url += '/' + server_sub_directory  

                return url
            
            elif server_type == ServerType.FTP_SERVER or server_type == ServerType.SFTP_SERVER:                              
                protocol = 'ftp' if server_type == ServerType.FTP_SERVER else 'sftp'
                url = protocol + "://{}:{}@{}".format(server.username, server.password, server.server_url) 
                
                if server_type == ServerType.FTP_SERVER and not is_empty(server.vrf):
                    url = url + ";{}".format(server.vrf)

                remote_directory = concatenate_dirs(server.server_directory, self.install_job.server_directory)              
                if not is_empty(remote_directory):
                    url = url + "/{}".format(remote_directory)

                return url
            elif server_type == ServerType.SCP_SERVER:
                # scp root:[email protected]:/home_directory destination_on_host
                return "scp {}:{}@{}:{} {}".format(server.username, server.password, server.server_url,
                                                   server.server_directory, server.destination_on_host)
            elif server_type == ServerType.LOCAL_SERVER:
                return server.server_url
        
        return None
Example #14
0
 def upload_file(self, source_file_path, dest_filename, sub_directory=None, callback=None):
     with open(source_file_path, 'rb') as file:
         
         ftp = ftplib.FTP(self.server.server_url, user=self.server.username, passwd=self.server.password)
                
         remote_directory = concatenate_dirs(self.server.server_directory, sub_directory)
         if len(remote_directory) > 0:
             ftp.cwd(remote_directory)
             
         # default block size is 8912
         if callback:
             ftp.storbinary('STOR ' + dest_filename, file, callback=callback)
         else:
             ftp.storbinary('STOR ' + dest_filename, file)
         ftp.quit()
Example #15
0
    def upload_file(self, source_file_path, dest_filename, sub_directory=None):
        try:
            file = open(source_file_path, 'rb')
            
            ftp = ftplib.FTP(self.server.server_url, user=self.server.username, passwd=self.server.password)
                   
            remote_directory = concatenate_dirs(self.server.server_directory, sub_directory)
            if len(remote_directory) > 0:
                ftp.cwd(remote_directory)
                
            # default block size is 8912
            ftp.storbinary('STOR ' + dest_filename, file, callback=self.handler)
            ftp.quit()    
            file.close()

        finally:
            if file is not None:
                file.close()
Example #16
0
    def server_repository_url(self):
        """
        Return the server repository URL (TFTP/FTP) where the packages can be found.
        tftp://223.255.254.254/auto/tftp-gud/sit;VRF
        ftp://username:[email protected];VRF/remote/directory
        """
        server_id = self.install_job.server_id
        server = self.db_session.query(Server).filter(Server.id == server_id).first()

        if server is not None:
            server_type = server.server_type

            if server_type == ServerType.TFTP_SERVER:
                url = 'tftp://{}'.format(server.server_url.replace('tftp://',''))

                if not is_empty(server.vrf):
                    url = url + ";{}".format(server.vrf)

                server_sub_directory = self.install_job.server_directory
                
                if not is_empty(server_sub_directory):
                    url += '/' + server_sub_directory  
                
                return url
            
            elif server_type == ServerType.FTP_SERVER or server_type == ServerType.SFTP_SERVER:                              
                protocol = 'ftp' if server_type == ServerType.FTP_SERVER else 'sftp'
                url = protocol + "://{}:{}@{}".format(server.username, server.password, server.server_url) 
                
                if not is_empty(server.vrf):
                    url = url + ";{}".format(server.vrf)

                remote_directory = concatenate_dirs(server.server_directory, self.install_job.server_directory)              
                if not is_empty(remote_directory):
                    url = url + "/{}".format(remote_directory)

                return url
            elif server_type == ServerType.LOCAL_SERVER:
                return server.server_url
        
        return None
Example #17
0
    def get_file_and_directory_dict(self, sub_directory=None):
        result_list = []
        is_reachable = True

        try:
            sftp_module = import_module('pysftp')
            if sftp_module is not None:
                with sftp_module.Connection(
                        self.server.server_url,
                        username=self.server.username,
                        password=self.server.password) as sftp:
                    remote_directory = concatenate_dirs(
                        self.server.server_directory, sub_directory)
                    if len(remote_directory) > 0:
                        sftp.chdir(remote_directory)

                    file_info_list = sftp.listdir()
                    for file_info in file_info_list:
                        file = {}
                        lstatout = str(sftp.lstat(file_info)).split()[0]
                        if 'd' in lstatout:
                            if sub_directory is None or len(
                                    sub_directory) == 0:
                                file['filename'] = file_info
                            else:
                                file[
                                    'filename'] = sub_directory + '/' + file_info
                            file['is_directory'] = True
                        else:
                            file['filename'] = file_info
                            file['is_directory'] = False

                        result_list.append(file)
        except:
            logger.exception('SFTPServer hit exception')
            is_reachable = False

        return result_list, is_reachable
Example #18
0
    def _copy_files_from_sftp_to_device(self, server, source_filenames, dest_files, timeout=600):
        """
        Copy files from their locations in the user selected server directory in the SFTP server repository
        to locations on device.

        Arguments:
        :param server: the sftp server object
        :param source_filenames: a list of string filenames in the designated directory in the server repository.
        :param dest_files: a list of string file paths that each points to a file to be created on device.
                    i.e., ["harddiskb:/asr9k-mini-x64.tar"]
        :param timeout: the timeout for the sftp copy operation on device. The default is 10 minutes.
        :return: None if no error occurred.
        """
        source_path = server.server_url

        remote_directory = concatenate_dirs(server.server_directory, self.ctx._csm.install_job.server_directory)
        if not is_empty(remote_directory):
            source_path = source_path + ":{}".format(remote_directory)

        def send_password(ctx):
            ctx.ctrl.sendline(server.password)
            if ctx.ctrl._session.logfile_read:
                ctx.ctrl._session.logfile_read = None
            return True

        def send_yes(ctx):
            ctx.ctrl.sendline("yes")
            if ctx.ctrl._session.logfile_read:
                ctx.ctrl._session.logfile_read = None
            return True

        def reinstall_logfile(ctx):
            if self.ctx._connection._session_fd and (not ctx.ctrl._session.logfile_read):
                ctx.ctrl._session.logfile_read = self.ctx._connection._session_fd
            else:
                ctx.message = "Error reinstalling session.log."
                return False
            return True

        def error(ctx):
            if self.ctx._connection._session_fd and (not ctx.ctrl._session.logfile_read):
                ctx.ctrl._session.logfile_read = self.ctx._connection._session_fd
            ctx.message = "Error copying file."
            return False

        for x in range(0, len(source_filenames)):
            if is_empty(server.vrf):
                command = "sftp {}@{}/{} {}".format(server.username, source_path, source_filenames[x], dest_files[x])
            else:
                command = "sftp {}@{}/{} {} vrf {}".format(server.username, source_path, source_filenames[x],
                                                           dest_files[x], server.vrf)

            PASSWORD = re.compile("Password:"******"Overwrite.*continue\? \[yes/no\]:")
            COPIED = re.compile("bytes copied in", re.MULTILINE)
            NO_SUCH_FILE = re.compile("src.*does not exist")
            DOWNLOAD_ABORTED = re.compile("Download aborted.")

            PROMPT = self.ctx.prompt
            TIMEOUT = self.ctx.TIMEOUT

            events = [PROMPT, PASSWORD, CONFIRM_OVERWRITE, COPIED, TIMEOUT, NO_SUCH_FILE, DOWNLOAD_ABORTED]
            transitions = [
                (PASSWORD, [0], 1, send_password, timeout),
                (CONFIRM_OVERWRITE, [1], 2, send_yes, timeout),
                (COPIED, [1, 2], -1, reinstall_logfile, 0),
                (PROMPT, [1, 2], -1, reinstall_logfile, 0),
                (TIMEOUT, [0, 1, 2], -1, error, 0),
                (NO_SUCH_FILE, [0, 1, 2], -1, error, 0),
                (DOWNLOAD_ABORTED, [0, 1, 2], -1, error, 0),
            ]

            self.ctx.info("Copying {}/{} to {} on device".format(source_path,
                                                                 source_filenames[x],
                                                                 dest_files[x]))

            if not self.ctx.run_fsm("Copy file from sftp to device", command, events, transitions, timeout=20):
                self.ctx.error("Error copying {}/{} to {} on device".format(source_path,
                                                                            source_filenames[x],
                                                                            dest_files[x]))

            if self.ctx._connection._session_fd and (not self.ctx._connection._driver.ctrl._session.logfile_read):
                self.ctx._connection._driver.ctrl._session.logfile_read = self.ctx._connection._session_fd
            output = self.ctx.send("dir {}".format(dest_files[x]))
            if "No such file" in output:
                self.ctx.error("Failed to copy {}/{} to {} on device".format(source_path,
                                                                             source_filenames[x],
                                                                             dest_files[x]))