def work(self): # get socket object skt = None try: skt = self.app.queue_cmdproc.get(timeout=10) except: return is_succ = True skt, peer = skt skt = net.SafeTcp(skt) io.debug("cmdproc worker", "new client: %s" % str(peer)) try: # check auth if is_succ: is_succ, resp = self.checkAuth(skt) skt.sendPdu(resp) io.debug('TRACE', '验证通过') # dispatch if is_succ: is_succ, resp = self.dispatch(skt) skt.sendPdu(resp) except Exception, _ex: is_succ = False io.error("cmdproc worker", str(_ex))
def login(self, options): user_name, password = self._getLoginInfo(options) client = YumtoolsClient() if client.connect(options.host, options.port): io.debug("upload", "yum server %s:%d connected." % (self.cfg.server_host, self.cfg.server_port)) else: io.error("upload", "server %s:%d connect error." % (self.cfg.server_host, self.cfg.server_port)) return False, None if not client.login(user_name, password): io.error('upload', 'auth failed') return False, None else: return True, client
def login(self, options): user_name, password = self._getLoginInfo(options) client = YumtoolsClient() if client.connect(options.host, options.port): io.debug( "upload", "yum server %s:%d connected." % (self.cfg.server_host, self.cfg.server_port)) else: io.error( "upload", "server %s:%d connect error." % (self.cfg.server_host, self.cfg.server_port)) return False, None if not client.login(user_name, password): io.error('upload', 'auth failed') return False, None else: return True, client
def procRemove(self, skt, first_pdu): pdu = first_pdu # check package_name package_name = pdu.get("package_name") if not utils.isValidPackageName(package_name): return False, self._makeResponsePdu(net.PDU_ERROR_REMOVEERR, "package name is not a valid value.") # check version version = pdu.get("version") if not utils.isValidVersion(version): return False, self._makeResponsePdu(net.PDU_ERROR_REMOVEERR, "version is not a valid value.") # check release release = pdu.get("release") # check os_version os_version = pdu.get("os_version") if os_version not in self.cfg.os_version_list: return False, self._makeResponsePdu(net.PDU_ERROR_REMOVEERR, "os version not a valid value.") # check arch arch = pdu.get("arch") if arch not in self.cfg.arch_list: return False, self._makeResponsePdu(net.PDU_ERROR_REMOVEERR, "arch not a valid value.") # branch branch = pdu.get("branch") if branch not in self.cfg.branches: return False, self._makeResponsePdu(net.PDU_ERROR_REMOVEERR, "branch name not exists") # generate dist file name dest_file_name = self._makePackageFullName(branch, release, os_version, arch, package_name, version) if not os.path.exists(dest_file_name): io.debug('setbranch', '%s not exists' % dest_file_name) return False, self._makeResponsePdu(net.PDU_ERROR_REMOVEERR, "file not exists in branch %s" % branch) # move file os.unlink(dest_file_name) # put to queue self.app.queue_createrepo.put(self._makePackagePath(branch, release, os_version, arch)) text = u"删除分支\n" subject = MAIL_SUBJECT_TEMPLATE % (self.operator_name, u"remove", package_name, version, release, os_version, arch, branch, U"") sendMail(config.ServerConfig().mail_sender, config.ServerConfig().mail_receiver, subject, text) return True, self._makeResponsePdu(net.PDU_ERROR_SUCCESSED, "remove successed!")
def run(self, options, args): if not self._checkArgs(args): return error.ERROR_INVALID_PARAM_COUNT io.debug("remove", "checking arguments...") package_name = args[0] branch = args[1] if not self._checkRpmInfo(package_name): return error.ERROR_NO_SUCH_PACKAGE_NAME if not self._checkBranch(branch): return error.ERROR_INVALID_BRANCH is_login_succeed, client = self.login(options) if not is_login_succeed: return error.ERROR_AUTH_FAILED package_info = utils.getPackageInfoByName(package_name) pdu = self._getRemovePdu(package_info, branch) if not client.invokeRemote(pdu): return error.ERROR_PDU_ERROR return error.ERROR_SUCCEED
def checkAuth(self, skt): pdu = skt.recvPdu() if not isinstance(pdu, net.AuthPdu): io.error("cmdproc worker", "pdu is not a AuthPdu instance") return False, self._makeResponsePdu(net.PDU_ERROR_AUTH_FAIL, "permission denied, please try later.") username = pdu.get("username") password = pdu.get("password") self.operator_name = username io.debug("cmdproc worker", "username=%s, password=%s" % (username, password)) print self.cfg.auth_method if self.cfg.auth_method == "admin_list": if not self.cfg.admin_list.has_key(username) or self.cfg.admin_list[username] != password: return False, self._makeResponsePdu(net.PDU_ERROR_AUTH_FAIL, "permission denied, please try later.") elif self.cfg.auth_method == "ldap": try: if password == "": raise ldap.LDAPError ldap_conn = ldap.open("qunarldap.corp.qunar.com") ldap_conn.simple_bind_s("*****@*****.**" % username, password) pass except Exception, _ex: return False, self._makeResponsePdu(net.PDU_ERROR_AUTH_FAIL, "permission denied, please try later.")
def procUpload(self, skt, first_pdu): io.debug('upload', 'start upload command') pdu = first_pdu # check package_name package_name = pdu.get("package_name") if not utils.isValidPackageName(package_name): return False, self._makeResponsePdu(net.PDU_ERROR_UPLOADERR, "package name is not a valid value.") # check version version = pdu.get("version") if not utils.isValidVersion(version): return False, self._makeResponsePdu(net.PDU_ERROR_UPLOADERR, "version is not a valid value.") # check release release = pdu.get("release") # check os_version os_version = pdu.get("os_version") if os_version not in self.cfg.os_version_list: return False, self._makeResponsePdu(net.PDU_ERROR_UPLOADERR, "os version not a valid value.") # check arch arch = pdu.get("arch") if arch not in self.cfg.arch_list: return False, self._makeResponsePdu(net.PDU_ERROR_UPLOADERR, "arch not a valid value.") # check file_size file_size = pdu.get("file_size") if file_size > 1024 * self.cfg.max_file_size: return False, self._makeResponsePdu(net.PDU_ERROR_UPLOADERR, "file size is out of maximum value") # generate dist file name dest_file_name = self._makePackageFullName(self.cfg.upload_branch, release, os_version, arch, package_name, version) print dest_file_name md5_code = "" if os.path.exists(dest_file_name): return False, self._makeResponsePdu(net.PDU_ERROR_UPLOADERR, "file already exists") # send first response skt.sendPdu(self._makeResponsePdu(net.PDU_ERROR_SUCCESSED, "package meta-data check is complete.")) with tempfile.NamedTemporaryFile('wb+') as temp_file: total_binary = 0 md5_hash = md5.new() # recv blocks while True: pdu = skt.recvPdu() if pdu.commandid.name == "UPLOAD_BLOCK": binary = pdu.get('binary') total_binary += len(binary) temp_file.write( binary ) md5_hash.update( binary ) if total_binary > file_size: return False, self._makeResponsePdu(net.PDU_ERROR_UPLOADERR, "denied!") skt.sendPdu(self._makeResponsePdu(net.PDU_ERROR_SUCCESSED, "")) elif pdu.commandid.name == "UPLOAD_END": # check md5 md5_code = pdu.get("md5") break temp_file.flush() # check md5 if md5_hash.hexdigest().upper() != md5_code.upper(): io.error('upload', 'file md5 code is inequality') return False, self._makeResponsePdu(net.PDU_ERROR_UPLOADERR, "md5 check error") # cp file shutil.copy(temp_file.name, dest_file_name) os.chmod(dest_file_name, stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH | stat.S_IRGRP) #basic_info = os.popen('rpm -qip %s' % dest_file_name).readlines() #install_path = os.popen('rpm -qlp %s' % dest_file_name).readlines() text = u"RPM包信息\n" try: reload(sys) sys.setdefaultencoding('utf8') os.system(u'echo RPM包信息 > /usr/share/yumtools/rpm_basic.info') os.system(u'echo 安装信息 > /usr/share/yumtools/rpm_install.info') os.system(u'rpm -qip %s > /usr/share/yumtools/rpm_basic.info' % dest_file_name) os.system(u'rpm -qlp %s > /usr/share/yumtools/rpm_install.info' % dest_file_name) basic_info = open('/usr/share/yumtools/rpm_basic.info', 'r') install_info = open('/usr/share/yumtools/rpm_install.info', 'r') for i in basic_info: text = text + i.encode('utf8') text = text + u'--------------------------------------------------------\n' for i in install_info: text = text + i.encode('utf8') except Exception, _ex: io.error("rpm info error", str(_ex)) pass