def _config(self,context): src_paths = self.include.split(':') router_tmp = self.dst_path( "_router.tmp") with open(router_tmp,'w') as router_fobj: for src in src_paths : self.build_rest_index(src,router_fobj); shexec.execmd(Template("sort $SRC > $DST; rm $SRC ").substitute(SRC=router_tmp,DST=self.out_idx))
def export_monitorconf(self, conf_path, files_to_check): cmd = "cd %s; %s/rigger restart -x %s" %(value_of('${PRJ_ROOT}'), rg_home() , self.__class__.__name__) prefix = os.path.dirname(conf_path) if not os.path.isdir(prefix): shexec.execmd("mkdir -p '%s'" % prefix) conf = 'FILES=\\"%s\\";' % files_to_check.replace(',', ' ') + 'CMD=\\"%s\\"' % cmd shexec.execmd('echo "%s" > "%s"' %(conf, conf_path))
def stop_service(name,pidfile,sudo=False): path=os.path.dirname(os.path.realpath(__file__)) if sudo : cmdtpl=" sudo %s/stop_proc.sh %s %s " %(path,pidfile,name) else: cmdtpl=" %s/stop_proc.sh %s %s " %(path,pidfile,name) cmd = Template(cmdtpl).substitute(NAME=name,PID_FILE=pidfile) shexec.execmd(cmd)
def _reload(self,context) : self.tpl_res._reload(context) self.link_res._reload(context) cmdtpl = "$BIN reload" cmd = Template(cmdtpl).substitute( BIN = context.nginx_def.bin ) shexec.execmd(cmd)
def _check(self,context): cmdtpl = "$VARNISHADM -T localhost:$ADMIN_PORT status "; vns_test = Template(cmdtpl).substitute( VARNISHADM = self.varnishadm, ADMIN_PORT = self.admin_port , ) cmd = " sudo rm -rf /tmp/varnish_ok ; if " + vns_test + " ; then sudo touch /tmp/varnish_ok; fi " shexec.execmd(cmd) self._check_print(os.path.exists("/tmp/varnish_ok"),"varnishd")
def _config(self,context) : self.tpl_res._config(context) self.link_res._config(context) cmdtpl = "$BIN -t" cmd = Template(cmdtpl).substitute( BIN = self.testbin ) shexec.execmd(cmd) pass
def _config(self,context): prefix_dir = value_of("${PRJ_ROOT}") + '/' + self.prefix if not os.path.isdir(prefix_dir): shexec.execmd("mkdir -p '%s'" % prefix_dir) self.export_monitorconf(prefix_dir + '/config.fpm', 'fpm.pid,fpm.sock') self.export_env2php() self.export_fpmconf() cmd = self.base_cmd + " -d config" shexec.execmd(cmd)
def _check(self,context): # pass cmdtpl = "$VARNISHADM -n $NAME status "; vns_test = Template(cmdtpl).substitute( VARNISHADM = self.varnishadm, NAME = self.name ) # cmd = " sudo rm -rf /tmp/varnish_ok ; if " + vns_test + " ; then sudo touch /tmp/varnish_ok; fi " cmd = vns_test shexec.execmd(cmd)
def _reload(self,context): confname = "vcl_%s_%d" %(time.strftime("%H_%M_%S",time.localtime()) , int(random.random() * 100 ) ) cmdtpl = """ $VARNISHADM -n $NAME vcl.load $CONFNAME $VCL ; $VARNISHADM -n $NAME vcl.use $CONFNAME """; cmd = Template(cmdtpl).substitute( VARNISHADM = self.varnishadm, VCL = self.vcl, ADMIN_PORT = self.admin_port , NAME = self.name, CONFNAME = confname ) shexec.execmd(cmd)
def export_fpmconf(self): if not self.f_conf == self.fpm_conf: fpm_conf = 'online' if self.fpm_conf == 'online' else 'dev' run_user = env_exp.value("${USER}") if env_exp.value("${USER}") in [e.pw_name for e in pwd.getpwall()] else 'nobody' cnt_min = str(int(self.fpm_cnt) - 10) cnt_max = str(int(self.fpm_cnt) + 10) conf_override = env_exp.value("\n".join(self.conf_override).replace(';' + fpm_conf, '')) tplpath = rg_home() + '/extension/res_conf/fpm_svc.conf.' + fpm_conf with open(tplpath,'r') as ftpl : tpl = ftpl.read() tpl = Template(tpl).substitute(POOL_NAME=self.prefix,USER=run_user,FPM_CNT=self.fpm_cnt,FPM_CNT_MIN=cnt_min,FPM_CNT_MAX=cnt_max,CONF_OVERRIDE=conf_override) tmp_path = value_of("${PRJ_ROOT}/run/" + self.f_conf.replace('/', '_')) with open(tmp_path, 'w') as f: f.write (tpl) shexec.execmd("mv '%s' '%s'" % (tmp_path, self.f_conf))
def _start(self,context): cmdtpl = "if ! test -s $PID ; then sudo $VARNISHD -f $VCL -s malloc,$MEM -T $ADMIN_IP:$ADMIN_PORT -a $HTTP_IP:$PORT -P$PID -n $NAME $EXTRAS ; fi" cmd = Template(cmdtpl).substitute( VARNISHD = self.varnishd, MEM = self.mem, PORT = self.port, VCL = self.vcl, ADMIN_PORT = self.admin_port, ADMIN_IP = self.admin_ip, HTTP_IP = self.http_ip, PID = self.pid, NAME = self.name, EXTRAS = self.extras ) shexec.execmd(cmd)
def _config(self,context): if not os.path.exists(self.dst) : os.makedirs(self.dst) src_paths = self.include.split(':') isfirst = True out_clspath = self.dst_path( "_autoload_clspath.idx") out_clsname = self.dst_path( "_autoload_clsname.idx") clspath_tmp = self.dst_path( "_autoload_clspath.tmp") clsname_tmp = self.dst_path( "_autoload_clsname.tmp") with open(clspath_tmp,'w') as clspath_index : with open(clsname_tmp,'w') as clsname_index : for src in src_paths : if len(src.strip()) == 0 : continue self.build_php_index(src,clspath_index,clsname_index,self.relpath); shexec.execmd(Template("sort $SRC > $DST; rm $SRC ").substitute(SRC=clspath_tmp,DST=out_clspath)) shexec.execmd(Template("sort $SRC > $DST; rm $SRC ").substitute(SRC=clsname_tmp,DST=out_clsname))
def find_class(self,src,data_file): shexec.execmd(Template('echo "" > $DST').substitute(DST=data_file)) cmdtpl = 'find $SRC/ -name "*.php" | xargs grep -H -i -E "^ *(abstract)? *class " >> $DST' for s in src : cmd = Template(cmdtpl).substitute( SRC = s ,DST=data_file) shexec.execmd(cmd,False) cmdtpl = 'find $SRC/ -name "*.php" | xargs grep -H -i -E "^ *interface " >> $DST' for s in src : cmd = Template(cmdtpl).substitute( SRC = s ,DST=data_file) shexec.execmd(cmd,False)
def _data(self,context): utls.check.must_true(hasattr(context,"mysql_def"), " !R.mysql_def no found ") sql = "DROP DATABASE IF EXISTS $DBNAME;CREATE DATABASE $DBNAME DEFAULT CHARACTER SET UTF8;" # sql +="GRANT ALL PRIVILEGES ON $DBNAME.* TO '$USER'@'$ADDR' IDENTIFIED BY '$PASSWD' ;" cmd = Template(sql).substitute(DBNAME=self.name,USER=self.user,PASSWD=self.password,ADDR=self.allow_addr) # mysql = get_env_conf().mysql mysql = context.mysql_def.bin if len(self.rootpwd) > 0 : shexec.execmd("%s -h%s -u%s -p%s -e \"%s\" " %(mysql ,self.host,self.root, self.rootpwd,cmd) ) else: print("create database , please input mysql root password: "******"%s -h%s -u%s -p -e \"%s\" " %(mysql ,self.host,self.root,cmd) ) cmdtpl = '$MYSQL -h$HOST $DBNAME -u$USER -p$PASSWD < $SQL' shexec.execmd(Template(cmdtpl).substitute(MYSQL=mysql,HOST=self.host ,DBNAME=self.name,USER=self.user,PASSWD=self.password,SQL=self.init),True)
def build_php_index(self,src_paths,dst_path,isclear = False,replace=""): # import pdb # pdb.set_trace() cls_tmp = self.dst_path( "_find_cls.tmp") out_clspath = self.dst_path( "_autoload_clspath.idx") out_clsname = self.dst_path( "_autoload_clsname.idx") self.find_class(src_paths,cls_tmp) clspath_tmp = self.dst_path( "_autoload_clspath.tmp") with open(clspath_tmp,'w') as autoload : with open(cls_tmp,'r') as find_cls : for line in find_cls.readlines(): res = self.parse_cls(line) if not res : continue file_path=res.group(1) if len(replace) > 0 : file_path= file_path.replace(replace,'') cls = res.group(3) autoload.write(Template("$CLS,$PATH\n").substitute(PATH=file_path,CLS=cls)) if isclear : shexec.execmd(Template("sort $SRC > $DST; rm $SRC ").substitute(SRC=clspath_tmp,DST=out_clspath)) else : shexec.execmd(Template("sort $SRC >> $DST; rm $SRC ").substitute(SRC=clspath_tmp,DST=out_clspath)) clsname_tmp = utls.rg_var.value_of(os.path.join(dst_path , "_autoload_clsname.tmp")) with open(clsname_tmp,'w') as autoload : with open(cls_tmp,'r') as find_cls : for line in find_cls.readlines(): res = self.parse_cls(line) if not res : continue cls = res.group(3) autoload.write(Template("cls_$LOWCLS,$CLS\n").substitute(CLS=cls,LOWCLS=cls.lower())) if isclear : shexec.execmd(Template("sort $SRC > $DST; rm $SRC ").substitute(SRC=clsname_tmp,DST=out_clsname)) else : shexec.execmd(Template("sort $SRC >> $DST; rm $SRC ").substitute(SRC=clsname_tmp,DST=out_clsname))
def clean_file(self, filename): cmdtpl = "if test -e $DST ; then rm -f $DST ; fi ; " cmd = Template(cmdtpl).substitute(DST=filename) shexec.execmd(cmd)
def _data(self,context): mysql = self.bin cmdtpl = '$MYSQL -h$HOST $DBNAME -u$USER -p$PASSWD < $SQL' shexec.execmd(Template(cmdtpl).substitute(MYSQL=mysql,HOST=self.host ,DBNAME=self.name,USER=self.user,PASSWD=self.password,SQL=self.sql),True)
def find_php(self,src,data_file): shexec.execmd(Template('echo "" > $DST').substitute(DST=data_file)) cmd = Template('find $SRC/ -name "*.php" >> $DST').substitute( SRC = src ,DST=data_file) shexec.execmd(cmd,False)
def clean_file(self,filename): cmdtpl = "if test -e $DST ; then rm -f $DST ; fi ; " cmd = Template(cmdtpl).substitute(DST=filename) shexec.execmd(cmd)
def find_php(self, src, data_file): shexec.execmd(Template('echo "" > $DST').substitute(DST=data_file)) cmd = Template('find $SRC/ -name "*.php" >> $DST').substitute( SRC=src, DST=data_file) shexec.execmd(cmd, False)
def _config(self,context): sed = """sed -r "s/.+:class\s+(\S+)\s+.+\/\/\@REST_RULE:\s+(.+)/\\2 : \\1/g" """ cmdtpl = """grep --include "*.php" -i -E "class .+ implements XService" -R $SRC | """ + sed + " > $DST " cmd = Template(cmdtpl).substitute(SRC = self.include,DST = self.out_idx) shexec.execmd(cmd,False)
def _stop(self,context): if os.path.exists(self.pid) : # if impl.rg_utls.get_key("Are you sure stop Varnishd? (y/N)", context) == "y" : cmdtpl = "cat $PID | xargs kill ; rm $PID " cmd = Template(cmdtpl).substitute( PID = self.pid) shexec.execmd(cmd)
def _stop(self,context): cmd = self.base_cmd + " -d stop" shexec.execmd(cmd)
def _reload(self,context): cmd = self.base_cmd+" -d reload" shexec.execmd(cmd)
def _restart(self,context): cmd = self.base_cmd+" -d restart" shexec.execmd(cmd)
def _clean(self,context): cmd = self.base_cmd+" -d clean" shexec.execmd(cmd)