Esempio n. 1
0
def send(hostname=None,
         port=22,
         username='******',
         password='******',
         config_dict=None,
         local_file=None,
         remote_file=None):
    """
    上传文件到服务器
    """
    if hostname is None:
        hostname = config_dict['hostname']
        port = config_dict['port']
        username = config_dict['username']
        password = config_dict['password']

    try:
        svn_logger.debug("connect to %s:%s " % (hostname, port))
        t = paramiko.Transport((hostname, port))
        t.connect(username=username, password=password)

        sftp = paramiko.SFTPClient.from_transport(t)

        sftp.put(local_file, remote_file)
        svn_logger.debug("send file from  local(%s) to remote(%s) " %
                         (local_file, remote_file))
        t.close()
    except Exception, e:
        svn_logger.error("connect to %s:%s failed " % (hostname, port))
        import traceback
        traceback.print_exc()
        try:
            t.close()
        except:
            pass
Esempio n. 2
0
    def write_authfile(self,authfile):
        svn_logger.info("write svnauth into authfile file(%s)" % authfile)
        try:
            authfh = open(authfile,"w")
               #print group and user list
            authfh.write("[groups]\n")
            for group_name in sorted(self.group_dict.keys()):
                group = self.group_dict[group_name]
                userlist = group.get_idlist_as_string()
                authfh.write("%-20s = %s\n" % (group_name,userlist) )
            authfh.write("\n")
            authfh.write ("##########group privilege start #############\n")
            for group_name in sorted(self.group_dict.keys()):
                group = self.group_dict[group_name]
                for dir,mode in group.privilege.items():
                    authfh.write("[%s]\n" % dir )
                    authfh.write("@%s = %s\n" %(group_name,mode))
                authfh.write("\n")

            authfh.write ("##########group privilege  end #############\n")
            authfh.write("\n")
            authfh.write ("##########user privilege  start #############\n")
            for id_name,id in self.id_dict.items():
                #authfh.write ("##########%s #############\n" % id_name)
                for dir,mode in id.privilege.items():
                    authfh.write("[%s]\n" % dir)
                    authfh.write("%s = %s\n" %(id_name,mode))
                if id.privilege.keys():
                    authfh.write("\n")
            authfh.write ("##########user privilege  end #############\n")
        except IOError:
            svn_logger.error("can not open (%s)" % authfile)
        finally:
            authfh.close()
Esempio n. 3
0
def get(hostname=None,port=22,username='******',password='******',config_dict=None,remote_file=None,local_file=None):
    """
    从服务器上下载文件
    支持参数,支持字典
    """
    if hostname is None:
        hostname = config_dict['hostname']
        port     = config_dict['port']
        username = config_dict['username']
        password = config_dict['password']

    try:
        svn_logger.debug("connect to %s:%s " % (hostname,port))
        t = paramiko.Transport((hostname, port))
        t.connect(username=username, password=password)

        sftp =paramiko.SFTPClient.from_transport(t)

        sftp.get(remote_file,local_file)
        svn_logger.debug("get file from  remote(%s) to local (%s) " % (remote_file,local_file))
        t.close();
    except Exception, e:
        svn_logger.error("connect to %s:%s failed " % (hostname,port))
        import traceback
        traceback.print_exc()
        try:
            t.close()
        except:
            pass
Esempio n. 4
0
 def write_priv_to_file(self, filename):
     svn_logger.debug("write(%s) to file(%s)" % (self.name, filename))
     try:
         fh = open(filename, "a")
         for dir, mode in self.privilege.items():
             fh.write("[%s]" % dir)
             fh.write("%s = %s " % (self.name, mode))
     except IOError:
         svn_logger.error("can not open (%s) !" % filename)
     finally:
         fh.close()
Esempio n. 5
0
 def write_priv_to_file(self, filename):
     svn_logger.debug("write(%s) to file(%s)" % (self.name, filename))
     try:
         fh = open(filename, "a")
         for dir, mode in self.privilege.items():
             fh.write("[%s]" % dir)
             fh.write("%s = %s " % (self.name, mode))
     except IOError:
         svn_logger.error("can not open (%s) !" % filename)
     finally:
         fh.close()
Esempio n. 6
0
 def write_passwdfile(self,passwdfile):
     svn_logger.info("write svnauth into passwd file(%s)" % passwdfile)
     try:
         passfh = open(passwdfile,'w')
         passfh.write("[users]\n")
         for id_name in sorted(self.id_dict.keys()):
             id = self.id_dict[id_name]
             passfh.write("%s = %s\n" % (id.name, id.passwd))
         passfh.close()
     except IOError:
         svn_logger.error("can not open (%s)" % passwdfile)
     finally:
         passfh.close()
Esempio n. 7
0
    def read_passwdfile(self,passwdfile):
        svn_logger.info("read passwd from file(%s)" % passwdfile)
        try:
            passfh = open(passwdfile,"r")
            re1 = r"^(\w+)\s*=\s*(\w+)"
            pattern = re.compile(re1)

            for line in passfh:
                match = pattern.match(line)
                if match:
                    name   = match.group(1)
                    passwd = match.group(2)
                    self.id_add(name,passwd)
                else:
                    svn_logger.debug("line(%s) did not match id=passwd  ;ignore this line" %  line )
        except IOError:
            svn_logger.error("The file(%s) do not exist" % passwdfile)
            exit()
        finally:
            passfh.close()
Esempio n. 8
0
def read_config_to_dict(ini='/etc/svn.ini'):
    """
    从ini文件里读取svn_server字段
    :param ini:
    :return:svn_sftp_dict
    """
    cf = ConfigParser.ConfigParser()
    svn_sftp_dict = {}
    field = "svn_server"
    local_field = "local"
    try:
        svn_logger.debug("read svn ini file(%s) " % ini)
        cf.read(ini)
        svn_sftp_dict['hostname'] = cf.get(field, 'hostname')
        svn_sftp_dict['port'] = cf.getint(field, 'port')
        svn_sftp_dict['username'] = cf.get(field, 'username')
        svn_sftp_dict['password'] = cf.get(field, 'password')
        svn_sftp_dict['passwdfile'] = cf.get(field, 'passwdfile')
        svn_sftp_dict['authfile'] = cf.get(field, 'authfile')
        svn_sftp_dict['local_passwdfile'] = cf.get(local_field, 'passwdfile')
        svn_sftp_dict['local_authfile'] = cf.get(local_field, 'authfile')
    except IOError as e:
        svn_logger.error("open svn ini file (%s) failed" % ini)
    return svn_sftp_dict
Esempio n. 9
0
def read_config_to_dict(ini='/etc/svn.ini'):
    """
    从ini文件里读取svn_server字段
    :param ini:
    :return:svn_sftp_dict
    """
    cf = ConfigParser.ConfigParser()
    svn_sftp_dict={}
    field="svn_server"
    local_field="local"
    try:
        svn_logger.debug("read svn ini file(%s) " % ini )
        cf.read(ini)
        svn_sftp_dict['hostname'] = cf.get(field,'hostname')
        svn_sftp_dict['port'] = cf.getint(field,'port')
        svn_sftp_dict['username'] = cf.get(field,'username')
        svn_sftp_dict['password'] = cf.get(field,'password')
        svn_sftp_dict['passwdfile'] = cf.get(field,'passwdfile')
        svn_sftp_dict['authfile'] = cf.get(field,'authfile')
        svn_sftp_dict['local_passwdfile'] = cf.get(local_field,'passwdfile')
        svn_sftp_dict['local_authfile'] = cf.get(local_field,'authfile')
    except IOError as e:
        svn_logger.error("open svn ini file (%s) failed" % ini)
    return svn_sftp_dict
Esempio n. 10
0
    def read_authfile(self,authfile):
        svn_logger.info("read line from file(%s)" % authfile)
        try:
            authfh = open(authfile,'r')#,encoding='utf-8')

            group_flag_re=r"\[groups\]"
            pattern_group_flag=re.compile(group_flag_re)
            #组名 和成员列表
            group_userlist_re=r"^(\w+)\s*=\s*(.*)$"
            pattern_userlist=re.compile(group_userlist_re)
            #权限路径
            path_re = r"^\[(\w+:)?(/\S*)\]"
            pattern_path=re.compile(path_re)
            #成员名称和权限
            user_priv_re=r"^(\w+)\s*=\s*(\w+)"
            pattern_user_priv=re.compile(user_priv_re)
            #组名称和权限
            group_priv_re=r"^@(\w+)\s*=\s*(\w+)"
            pattern_group_priv=re.compile(group_priv_re)

            tmp_status = 0
            tmp_dir    = ''
            # 状态0  为初始状态
            # 状态1   遇到[group]标志之后
            # 状态2   遇到[/dir] 目录之后 ,并记录目录名称
            for line in authfh:
                svn_logger.debug("read line from file(%s)" % line)
                if tmp_status == 0:
                    match = pattern_group_flag.match(line)
                    if match:
                        tmp_status = 1
                        svn_logger.debug("match group flag")
                elif tmp_status == 1:
                    match = pattern_userlist.match(line)
                    if match:
                        #匹配到
                        svn_logger.debug("match userlist in group")

                        group_name = match.group(1)
                        self.group_create(group_name)
                        userlist_string = match.group(2)
                        userlist_string = userlist_string.replace(' ','')
                        userlist = userlist_string.split(',')
                        svn_logger.debug("group(%s) will add userlist(%s)" % (group_name,userlist))
                        for id_name in userlist:
                            if id_name == "":
                                continue
                            self.group_add_id(group_name,id_name)

                    elif pattern_path.match(line):
                        #匹配到目录

                        match = pattern_path.match(line)
                        if  match.group(1):
                            tmp_dir =  match.group(1) + match.group(2)
                        else:
                            tmp_dir = match.group(2)
                        svn_logger.debug("match diretory(%s)" % tmp_dir)
                        tmp_status = 2
                    else:
                        pass
                elif tmp_status == 2:
                    if pattern_group_priv.match(line):
                        svn_logger.debug("match group priv")
                        match = pattern_group_priv.match(line)
                        group_name = match.group(1)
                        mode = match.group(2)
                        self.group_add_priv(group_name,tmp_dir,mode)
                    elif pattern_user_priv.match(line):
                        svn_logger.debug("match user priv")
                        match = pattern_user_priv.match(line)
                        name = match.group(1)
                        mode = match.group(2)
                       # print("(%s) add (%s)" % (name,tmp_dir))
                        self.id_add_priv(name,tmp_dir,mode)
                       # print("(%s) add (%s)" % (name,tmp_dir))
                    elif pattern_path.match(line):
                        #匹配到目录
                        match = pattern_path.match(line)
                        if  match.group(1):
                            tmp_dir =  match.group(1) + match.group(2)
                        else:
                            tmp_dir = match.group(2)
                        svn_logger.debug("match diretory(%s)" % tmp_dir)
                    else:
                        pass
        except IOError:
            svn_logger.error("The file(%s) do not exist" % authfile)
            exit()

        authfh.close()
        return