示例#1
0
func (a *Server) RegisterRPCFunctions(o interface{}) error {
	t := reflect.TypeOf(o)
	v := reflect.ValueOf(o)
	if v.Kind() != reflect.Ptr {
		return errors.New("Invalid object for RPC Register")
	}
	if a.Log == nil {
		a.Log, _ = toolkit.NewLog(true, false, "", "", "")
	}
	//objName := toolkit.TypeName(o)
	methodCount := t.NumMethod()
	for i := 0; i < methodCount; i++ {
		method := t.Method(i)
		mtype := method.Type
		methodName := strings.ToLower(method.Name)
		//fmt.Println("Evaluating " + toolkit.TypeName(o) + "." + methodName)

		//-- now check method signature
		if mtype.NumIn() == 2 && mtype.In(1).String() == "toolkit.M" {
			if mtype.NumOut() == 1 && mtype.Out(0).String() == "*toolkit.Result" {
				//a.Log.Info("Registering RPC Function " + objName + "." + methodName)
				a.AddFn(methodName, v.Method(i).Interface().(func(toolkit.M) *toolkit.Result), true, "session")
			}
		}
	}
	return nil
}
示例#2
0
func (d *DataGrabberController) StopTransformation(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	dataGrabber := new(colonycore.DataGrabber)
	err := r.GetPayload(dataGrabber)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	logFileName := dataGrabber.ID
	logFilePattern := ""
	logConf, err := toolkit.NewLog(false, true, dgLogPath, logFileName, logFilePattern)
	if err != nil {
		logConf.AddLog(err.Error(), "ERROR")
	}

	if _, ok := serviceHolder[dataGrabber.ID]; ok {
		serviceHolder[dataGrabber.ID] = false
		delete(serviceHolder, dataGrabber.ID)

		message := fmt.Sprintf("===> Transformation stopped! %s -> %s", dataGrabber.DataSourceOrigin, dataGrabber.DataSourceDestination)
		logConf.AddLog(message, "SUCCESS")
		fmt.Println(message)
	}

	return helper.CreateResult(true, nil, "")
}
示例#3
0
文件: log.go 项目: ariefdarmawan/labs
func main() {
	var e error
	l, e := toolkit.NewLog(true, true, "/Users/ariefdarmawan/Temp", "log-test-%s.log", "20060102")
	if e != nil {
		fmt.Println("Error: ", e.Error())
	}

	l.Info("Test Log Info")
}
示例#4
0
func (p *Page) SendFiles(EC_DATA_PATH string, serverid string) error {
	path := filepath.Join(EC_DATA_PATH, "widget", "log")
	log, _ := toolkit.NewLog(false, true, path, "sendfile-%s", "20060102-1504")

	for _, wValue := range p.Widget {
		appID := wValue.ID
		log.AddLog("Get widget with ID: "+appID, "INFO")
		widget := new(Widget)
		err := Get(widget, appID)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return err
		}

		log.AddLog("Get server with ID: "+serverid, "INFO")
		server := new(Server)
		err = Get(server, serverid)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return err
		}

		serverPathSeparator := p.GetServerPathSeparator(server)
		sourcePath := filepath.Join(EC_DATA_PATH, "widget", appID)
		destPath := strings.Join([]string{server.AppPath, "src", "widget"}, serverPathSeparator)

		if server.OS == "windows" {
			if strings.Contains(server.CmdExtract, "7z") || strings.Contains(server.CmdExtract, "zip") {
				err = p.CopyFileToServer(server, sourcePath, destPath, appID, log)
				if err != nil {
					log.AddLog(err.Error(), "ERROR")
					return err
				}
			} else {
				message := "currently only zip/7z command which is supported"
				log.AddLog(message, "ERROR")
				return err
			}
		} else {
			if strings.Contains(server.CmdExtract, "tar") || strings.Contains(server.CmdExtract, "zip") {
				err = p.CopyFileToServer(server, sourcePath, destPath, appID, log)
				if err != nil {
					log.AddLog(err.Error(), "ERROR")
					return err
				}
			} else {
				message := "currently only zip/tar command which is supported"
				log.AddLog(message, "ERROR")
				return err
			}
		}
	}
	return nil
}
示例#5
0
文件: service.go 项目: eaciit/live
/*
Generate log as condition inputed, if warning and error found will send email too
*/
func (s *Service) AddLog(logtext, logtype string) {
	if s.Log == nil {
		s.Log, _ = toolkit.NewLog(true, false, "", "", "")
	}
	s.Log.AddLog(logtext, logtype)

	subj := fmt.Sprintf("[%s] From Service %s at %s", logtype, s.Name, time.Now().Format("20060102_15:04:05"))
	msg := fmt.Sprintf("Message From Live Service : %s", logtext)

	if logtype == "ERROR" {
		s.Mail.sendEmail(subj, msg, s.EmailError)
	} else if logtype == "WARNING" {
		s.Mail.sendEmail(subj, msg, s.EmailWarning)
	}
}
示例#6
0
func (d *DataGrabberController) getLogger(dataGrabber *colonycore.DataGrabber) (*toolkit.LogEngine, error) {
	logAt := time.Now().Format("20060102-150405")
	logFileName := strings.Split(logAt, "-")[0]
	logFileNameParsed := fmt.Sprintf("%s-%s", dataGrabber.ID, logFileName)
	logFilePattern := ""

	logConf, err := toolkit.NewLog(false, true, dgLogPath, logFileNameParsed, logFilePattern)
	if err != nil {
		return nil, err
	}

	currentDataGrabber := new(colonycore.DataGrabber)
	err = colonycore.Get(currentDataGrabber, dataGrabber.ID)
	if err != nil {
		logConf.AddLog(err.Error(), "ERROR")
		return logConf, err
	}
	if currentDataGrabber.RunAt == nil {
		currentDataGrabber.RunAt = []string{}
	}
	if !toolkit.HasMember(currentDataGrabber.RunAt, logAt) {
		currentDataGrabber.RunAt = append(currentDataGrabber.RunAt, logAt)
	}

	err = colonycore.Delete(currentDataGrabber)
	if err != nil {
		logConf.AddLog(err.Error(), "ERROR")
		return logConf, err
	}

	err = colonycore.Save(currentDataGrabber)
	if err != nil {
		logConf.AddLog(err.Error(), "ERROR")
		return logConf, err
	}

	return logConf, nil
}
示例#7
0
func main() {
	var err error

	flagConfigPath := flag.String("config", "", "config file")
	flagSnapShot := flag.String("snapshot", "", "snapshot filepath")
	flagID := flag.String("id", "", "_id of the config (if array)")
	flagPID := flag.Int("pid", 0, "process id number for identify snapshot active")

	flag.Parse()
	tconfigPath := toolkit.ToString(*flagConfigPath)
	tsnapshot := toolkit.ToString(*flagSnapShot)
	_id = *flagID

	_id = strings.Replace(_id, `"`, "", -1)
	if _id == "" {
		sedotan.CheckError(errors.New("-id cannot be empty"))
	}

	configpath = strings.Replace(tconfigPath, `"`, "", -1)
	if configpath == "" {
		sedotan.CheckError(errors.New("-config cannot be empty"))
	}

	snapshot = strings.Replace(tsnapshot, `"`, "", -1)
	if snapshot == "" {
		sedotan.CheckError(errors.New("-snapshot cannot be empty"))
	}

	err = getsnapshot()
	if err != nil {
		sedotan.CheckError(errors.New(fmt.Sprintf("get snapshot error found : %v", err.Error())))
	}

	pid = *flagPID
	if pid == 0 {
		sedotan.CheckError(errors.New("-pid cannot be empty or zero value"))
	}

	err = getConfig()
	checkfatalerror(err)

	logconf, _ := toolkit.ToM(config.Get("logconf", toolkit.M{}))
	if !logconf.Has("logpath") || !logconf.Has("filename") || !logconf.Has("filepattern") {
		checkfatalerror(errors.New(fmt.Sprintf("config log is not complete")))
	}

	logpath := logconf["logpath"].(string)
	if EC_DATA_PATH != "" {
		logpath = filepath.Join(EC_DATA_PATH, "webgrabber", "log")
	}

	Log, err = toolkit.NewLog(false, true, logpath, (logconf["filename"].(string) + "-%s"), logconf["filepattern"].(string))
	checkfatalerror(err)
	Log.AddLog("Starting web grab data", "INFO")

	Log.AddLog("Start fetch the config", "INFO")
	err = fetchConfig()
	checkexiterror(err)
	Log.AddLog(fmt.Sprintf("Data grabber created : %v", toolkit.JsonString(wGrabber)), "INFO")
	Log.AddLog("Fetch the config success", "INFO")

	Log.AddLog("Get the data", "INFO")
	err = wGrabber.Grab(nil)
	if err != nil {
		checkexiterror(errors.New(fmt.Sprintf("Grab Failed : %v", err.Error())))
	}
	Log.AddLog("Get data grab success", "INFO")

	Log.AddLog("Start save data grab", "INFO")
	err = savedatagrab()
	if err != nil {
		checkexiterror(errors.New(fmt.Sprintf("Save data finish with error : %v", err.Error())))
	}
	Log.AddLog("Save data grab success", "INFO")

	err = getsnapshot()
	if err != nil {
		note := fmt.Sprintf("Unable to get last snapshot :%s", err.Error())
		Log.AddLog(note, "ERROR")
	}

	snapshotdata.Note = ""
	snapshotdata.Lastgrabstatus = "success"
	snapshotdata.Grabstatus = "done"

	if pid == snapshotdata.Pid {
		err = savesnapshot()
		if err != nil {
			checkexiterror(errors.New(fmt.Sprintf("Save snapshot error : %v", err.Error())))
		}
	}

	Log.AddLog("Finish grab data", "INFO")
}
示例#8
0
func main() {
	// runtime.GOMAXPROCS(runtime.NumCPU())
	var err error

	flagConfig := flag.String("config", "", "config file")
	flagDebugMode := flag.Bool("debug", false, "debug mode")
	flagLogPath := flag.String("logpath", "", "log path")

	flag.Parse()
	tconfig := toolkit.ToString(*flagConfig)
	tlogPath := toolkit.ToString(*flagLogPath)
	debugMode = *flagDebugMode

	configPath = strings.Replace(tconfig, `"`, "", -1)
	if tconfig == "" {
		sedotan.CheckError(errors.New("-config cannot be empty"))
	}

	logstdout := false
	logfile := true

	logPath := strings.Replace(tlogPath, `"`, "", -1)
	fmt.Println("Log Path, ", logPath)
	if logPath == "" {
		logPath, err = os.Getwd()
		if err != nil {
			logstdout = true
			logfile = false
			fmt.Println("cannot get log path")
		}
	}

	//Temporary :
	var snapshotpath string = filepath.Join(logPath, "daemonsnapshot.csv")
	// err = getDataSnapShot(snapshotpath)
	// sedotan.CheckError(err)

	Log, err = toolkit.NewLog(logstdout, logfile, logPath, "daemonlog-%s", "20060102")
	sedotan.CheckError(err)

	Log.AddLog(fmt.Sprintf("Start daemon grabbing, config path : %v", configPath), "INFO")

	for {
		err = nil

		daemoninterval := 1 * time.Second
		<-time.After(daemoninterval)
		thistime = sedotan.TimeNow()

		Log.AddLog(fmt.Sprintf("Run daemon"), "INFO")
		initiate()

		Log.AddLog(fmt.Sprintf("Fetch config grabbing started"), "INFO")
		fetchconfig()
		if configerr != nil {
			Log.AddLog(configerr.Error(), "ERROR")
			configerr = nil
		}

		Log.AddLog(fmt.Sprintf("Get data snapshot"), "INFO")
		err = getDataSnapShot(snapshotpath)
		if err != nil {
			Log.AddLog(fmt.Sprintf("Failed to start grabbing, snapshot error : %v", err.Error()), "ERROR")
			continue
		}

		if len(config) > 0 {
			Log.AddLog(fmt.Sprintf("Ready to start grabbing, found %v valid config and %v active config", validconfig, len(config)), "INFO")
		} else {
			Log.AddLog(fmt.Sprintf("Skip to start grabbing, found %v valid config and 0 active config", validconfig), "ERROR")
		}

		for _, econfig := range config {
			err = nil
			eid := econfig.Get("_id", "").(string)
			Log.AddLog(fmt.Sprintf("Check config for id : %v", eid), "INFO")
			intervalconf, _ := toolkit.ToM(econfig["intervalconf"])
			grabconf, _ := toolkit.ToM(econfig["grabconf"])

			var isonprocess bool = checkisonprocess(eid, intervalconf, grabconf)
			var isconfrun bool = econfig.Get("running", false).(bool) //check config status run/stop (isconfrun)
			var istimerun bool = checkistimerun(eid, intervalconf, grabconf)

			etype := econfig.Get("sourcetype", "").(string)
			//check grab status onprocess/done/na/error -> conf file / snapshot file ? (isonprocess)
			//check interval+time start/corn schedulling and check last running for interval(istimerun)
			// fmt.Printf("!%v && %v && %v \n", isonprocess, isconfrun, istimerun)
			if !isonprocess && isconfrun && istimerun {
				Log.AddLog(fmt.Sprintf("Start grabbing for id : %v", eid), "INFO")

				// save data snapshot using dbox save
				tsnapshot := mapsnapshot[eid]
				tsnapshot.Laststartgrab = sedotan.DateToString(thistime)
				tsnapshot.Pid = toolkit.RandInt(1000) + 1
				tsnapshot.Cgtotal = 0
				tsnapshot.Cgprocess = 0

				mapsnapshot[eid] = tsnapshot

				err = savesnapshot(eid, snapshotpath)
				if err != nil {
					Log.AddLog(fmt.Sprintf("Save snapshot id : %v, error found : %v", eid, err), "INFO")
					continue
				}
				// run grabbing
				go func(id string, etype string, thistime time.Time) {
					etype = strings.ToLower(etype)
					var cmd *exec.Cmd
					aCommand := make([]string, 0, 0)

					if runtime.GOOS == "windows" {
						aCommand = append(aCommand, "cmd")
						aCommand = append(aCommand, "/C")
					}

					apppath := ""
					// Check Type [SourceType_HttpHtml|SourceType_HttpJson|SourceType_DocExcel]
					switch {
					case strings.Contains(etype, "http"):
						if runtime.GOOS == "windows" {
							apppath = filepath.Join(EC_APP_PATH, "cli", "sedotanw.exe")
						} else {
							apppath = filepath.Join(EC_APP_PATH, "cli", "sedotanw")
						}
					case strings.Contains(etype, "doc"):
						if runtime.GOOS == "windows" {
							apppath = filepath.Join(EC_APP_PATH, "cli", "sedotans.exe")
						} else {
							apppath = filepath.Join(EC_APP_PATH, "cli", "sedotans")
						}
					}
					aCommand = append(aCommand, apppath)
					aCommand = append(aCommand, `-config="`+configPath+`"`)
					aCommand = append(aCommand, `-snapshot="`+snapshotpath+`"`)
					aCommand = append(aCommand, `-id="`+eid+`"`)
					aCommand = append(aCommand, `-pid=`+toolkit.ToString(mapsnapshot[eid].Pid))

					cmd = exec.Command(aCommand[0], aCommand[1:]...)

					Log.AddLog(fmt.Sprintf("[%v] run at %v, run : %v", eid, sedotan.DateToString(thistime), cmd.Args), "INFO")
					byteoutput, err := cmd.CombinedOutput()
					if err != nil {
						Log.AddLog(fmt.Sprintf("[%v] run at %v, found error : %v", eid, sedotan.DateToString(thistime), err.Error()), "ERROR")
					}
					Log.AddLog(fmt.Sprintf("[%v] run at %v, done with message : %v", eid, sedotan.DateToString(thistime), string(byteoutput)), "INFO")
				}(eid, etype, thistime)
			} else {
				Log.AddLog(fmt.Sprintf("Skip grabbing for id : %v", eid), "INFO")
			}
		}
	}
	// _, err := sedotan.Process(config)
	// sedotan.CheckError(err)
}
示例#9
0
func GrabHtmlConfig(data toolkit.M) (*sdt.GrabService, error) {
	var e error
	var gi, ti time.Duration
	xGrabService := sdt.NewGrabService()
	xGrabService.Name = data["nameid"].(string) //"irondcecom"
	xGrabService.Url = data["url"].(string)     //"http://www.dce.com.cn/PublicWeb/MainServlet"

	xGrabService.SourceType = sdt.SourceType_HttpHtml

	grabintervalToInt := toolkit.ToInt(data["grabinterval"], toolkit.RoundingAuto)
	timeintervalToInt := toolkit.ToInt(data["timeoutinterval"], toolkit.RoundingAuto)
	if data["intervaltype"].(string) == "seconds" {
		gi = time.Duration(grabintervalToInt) * time.Second
		ti = time.Duration(timeintervalToInt) * time.Second
	} else if data["intervaltype"].(string) == "minutes" {
		gi = time.Duration(grabintervalToInt) * time.Minute
		ti = time.Duration(timeintervalToInt) * time.Minute
	} else if data["intervaltype"].(string) == "hours" {
		gi = time.Duration(grabintervalToInt) * time.Hour
		ti = time.Duration(timeintervalToInt) * time.Hour
	}

	xGrabService.GrabInterval = gi    //* time.Minute
	xGrabService.TimeOutInterval = ti //* time.Minute //time.Hour, time.Minute, time.Second

	xGrabService.TimeOutIntervalInfo = fmt.Sprintf("%v %s", timeintervalToInt, data["intervaltype"] /*"seconds"*/)

	grabConfig := sdt.Config{}

	if data["calltype"].(string) == "POST" {
		dataurl := toolkit.M{}
		for _, grabconf := range data["grabconf"].(map[string]interface{}) {
			grabDataConf, e := toolkit.ToM(grabconf)
			if e != nil {
				return nil, e
			}
			for key, subGrabDataConf := range grabDataConf {
				if reflect.ValueOf(subGrabDataConf).Kind() == reflect.Float64 {
					i := toolkit.ToInt(subGrabDataConf, toolkit.RoundingAuto)
					toString := strconv.Itoa(i)
					dataurl[key] = toString
				} else {
					dataurl[key] = subGrabDataConf
				}
			}
		}

		grabConfig.SetFormValues(dataurl)
	}

	grabDataConf, e := toolkit.ToM(data["grabconf"])
	if e != nil {
		return nil, e
	}

	isAuthType := grabDataConf.Has("authtype")
	if isAuthType {
		grabConfig.AuthType = grabDataConf["authtype"].(string)
		grabConfig.LoginUrl = grabDataConf["loginurl"].(string)   //"http://localhost:8000/login"
		grabConfig.LogoutUrl = grabDataConf["logouturl"].(string) //"http://localhost:8000/logout"

		grabConfig.LoginValues = toolkit.M{}.
			Set("name", grabDataConf["loginvalues"].(map[string]interface{})["name"].(string)).
			Set("password", grabDataConf["loginvalues"].(map[string]interface{})["password"].(string))

	}

	xGrabService.ServGrabber = sdt.NewGrabber(xGrabService.Url, data["calltype"].(string), &grabConfig)

	logconfToMap, e := toolkit.ToM(data["logconf"])
	if e != nil {
		return nil, e
	}
	logpath := logconfToMap["logpath"].(string)           //"E:\\data\\vale\\log"
	filename := logconfToMap["filename"].(string) + "-%s" //"LOG-GRABDCETEST"
	filepattern := logconfToMap["filepattern"].(string)   //"20060102"

	logconf, e := toolkit.NewLog(false, true, logpath, filename, filepattern)
	if e != nil {
		return nil, e
	}

	xGrabService.Log = logconf

	xGrabService.ServGrabber.DataSettings = make(map[string]*sdt.DataSetting)
	xGrabService.DestDbox = make(map[string]*sdt.DestInfo)

	tempDataSetting := sdt.DataSetting{}
	tempDestInfo := sdt.DestInfo{}
	// isCondition := []interface{}{}
	tempFilterCond := toolkit.M{}
	// var condition string

	for _, dataSet := range data["datasettings"].([]interface{}) {
		dataToMap, _ := toolkit.ToM(dataSet)
		tempDataSetting.RowSelector = dataToMap["rowselector"].(string)

		for _, columnSet := range dataToMap["columnsettings"].([]interface{}) {
			columnToMap, e := toolkit.ToM(columnSet)
			if e != nil {
				return nil, e
			}
			i := toolkit.ToInt(columnToMap["index"], toolkit.RoundingAuto)
			tempDataSetting.Column(i, &sdt.GrabColumn{Alias: columnToMap["alias"].(string), Selector: columnToMap["selector"].(string)})
		}

		/*if data["calltype"].(string) == "POST" {
			// orCondition := []interface{}{}
			isRowdeletecond := dataToMap.Has("rowdeletecond")
			fmt.Println("isRowdeletecond>", isRowdeletecond)
			if hasRowdeletecond := dataToMap.Has("rowdeletecond"); hasRowdeletecond {
				for key, rowDeleteMap := range dataToMap["rowdeletecond"].(map[string]interface{}) {
					if key == "$or" || key == "$and" {
						for _, subDataRowDelete := range rowDeleteMap.([]interface{}) {
							for subIndex, getValueRowDelete := range subDataRowDelete.(map[string]interface{}) {
								orCondition = append(orCondition, map[string]interface{}{subIndex: getValueRowDelete})
							}
						}
						condition = key
					}
				}
			}
			// tempDataSetting.RowDeleteCond = toolkit.M{}.Set(condition, orCondition)
			tempFilterCond := toolkit.M{}.Set(condition, orCondition)
			tempDataSetting.SetFilterCond(tempFilterCond)
		}*/

		if hasRowdeletecond := dataToMap.Has("rowdeletecond"); hasRowdeletecond {
			rowToM, e := toolkit.ToM(dataToMap["rowdeletecond"])
			if e != nil {
				return nil, e
			}
			tempFilterCond, e = toolkit.ToM(rowToM.Get("filtercond", nil))
			tempDataSetting.SetFilterCond(tempFilterCond)
		}

		if hasRowincludecond := dataToMap.Has("rowincludecond"); hasRowincludecond {
			rowToM, e := toolkit.ToM(dataToMap["rowincludecond"])
			if e != nil {
				return nil, e
			}

			tempFilterCond, e = toolkit.ToM(rowToM.Get("filtercond", nil))
			tempDataSetting.SetFilterCond(tempFilterCond)
		}

		xGrabService.ServGrabber.DataSettings[dataToMap["name"].(string)] = &tempDataSetting //DATA01 use name in datasettings

		connToMap, e := toolkit.ToM(dataToMap["connectioninfo"])
		if e != nil {
			return nil, e
		}
		var db, usr, pwd string

		if hasDb := connToMap.Has("database"); !hasDb {
			db = ""
		} else {
			db = connToMap["database"].(string)
		}

		if hasUser := connToMap.Has("username"); !hasUser {
			usr = ""
		} else {
			usr = connToMap["username"].(string)
		}

		if hasPwd := connToMap.Has("password"); !hasPwd {
			pwd = ""
		} else {
			pwd = connToMap["username"].(string)
		}
		ci := dbox.ConnectionInfo{}
		ci.Host = connToMap["host"].(string) //"E:\\data\\vale\\Data_GrabIronTest.csv"
		ci.Database = db
		ci.UserName = usr
		ci.Password = pwd

		if hasSettings := connToMap.Has("settings"); !hasSettings {
			ci.Settings = nil
		} else {
			settingToMap, e := toolkit.ToM(connToMap["settings"])
			if e != nil {
				return nil, e
			}
			ci.Settings = settingToMap //toolkit.M{}.Set("useheader", settingToMap["useheader"].(bool)).Set("delimiter", settingToMap["delimiter"])
		}

		if hasCollection := connToMap.Has("collection"); !hasCollection {
			tempDestInfo.Collection = ""
		} else {
			tempDestInfo.Collection = connToMap["collection"].(string)
		}

		tempDestInfo.Desttype = dataToMap["desttype"].(string)

		tempDestInfo.IConnection, e = dbox.NewConnection(tempDestInfo.Desttype, &ci)
		if e != nil {
			return nil, e
		}

		xGrabService.DestDbox[dataToMap["name"].(string)] = &tempDestInfo

		//=History===========================================================
		xGrabService.HistoryPath = HistoryPath       //"E:\\data\\vale\\history\\"
		xGrabService.HistoryRecPath = HistoryRecPath //"E:\\data\\vale\\historyrec\\"
		//===================================================================
	}

	return xGrabService, nil
}
示例#10
0
func (s *ServerController) SaveServers(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	r.Request.ParseMultipartForm(32 << 20)
	r.Request.ParseForm()

	path := filepath.Join(EC_DATA_PATH, "server", "log")
	log, _ := toolkit.NewLog(false, true, path, "log-%s", "20060102-1504")

	data := new(colonycore.Server)
	if r.Request.FormValue("sshtype") == "File" {
		log.AddLog("Get forms", "INFO")
		dataRaw := map[string]interface{}{}
		err := r.GetForms(&dataRaw)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}

		log.AddLog("Serding data", "INFO")
		err = toolkit.Serde(dataRaw, &data, "json")
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}
	} else {
		log.AddLog("Get payload", "INFO")
		err := r.GetPayload(&data)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}
	}

	if data.SSHType == "File" {
		log.AddLog("Fetching public key", "INFO")
		reqFileName := "privatekey"
		file, _, err := r.Request.FormFile(reqFileName)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}

		if file != nil {
			log.AddLog("Saving public key", "INFO")
			data.SSHFile = filepath.Join(EC_DATA_PATH, "server", "privatekeys", data.ID)
			_, _, err = helper.FetchThenSaveFile(r.Request, reqFileName, data.SSHFile)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}
		}
	}
	oldData := new(colonycore.Server)

	log.AddLog(fmt.Sprintf("Find server ID: %s", data.ID), "INFO")
	cursor, err := colonycore.Find(new(colonycore.Server), dbox.Eq("_id", data.ID))
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}
	oldDataAll := []colonycore.Server{}
	err = cursor.Fetch(&oldDataAll, 0, false)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	if len(oldDataAll) > 0 {
		oldData = &oldDataAll[0]
	}

	if data.ServerType == "hdfs" {
		log.AddLog(fmt.Sprintf("SSH Connect %v", data), "INFO")
		hadeepes, err := hdfs.NewWebHdfs(hdfs.NewHdfsConfig(data.Host, data.SSHUser))
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}

		_, err = hadeepes.List("/")
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}

		hadeepes.Config.TimeOut = 5 * time.Millisecond
		hadeepes.Config.PoolSize = 100

		return helper.CreateResult(true, nil, "")
	}

	log.AddLog(fmt.Sprintf("SSH Connect %v", data), "INFO")
	sshSetting, client, err := s.SSHConnect(data)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}
	defer client.Close()

	if data.OS == "linux" {
		setEnvPath := func() error {
			cmd1 := `sed -i '/export EC_APP_PATH/d' ~/.bashrc`
			log.AddLog(cmd1, "INFO")
			sshSetting.GetOutputCommandSsh(cmd1)

			cmd2 := `sed -i '/export EC_DATA_PATH/d' ~/.bashrc`
			log.AddLog(cmd2, "INFO")
			sshSetting.GetOutputCommandSsh(cmd2)

			cmd3 := "echo 'export EC_APP_PATH=" + data.AppPath + "' >> ~/.bashrc"
			log.AddLog(cmd3, "INFO")
			sshSetting.GetOutputCommandSsh(cmd3)

			cmd4 := "echo 'export EC_DATA_PATH=" + data.DataPath + "' >> ~/.bashrc"
			log.AddLog(cmd4, "INFO")
			sshSetting.GetOutputCommandSsh(cmd4)

			return nil
		}

		if oldData.AppPath == "" || oldData.DataPath == "" {
			cmdRmAppPath := fmt.Sprintf("rm -rf %s", data.AppPath)
			log.AddLog(cmdRmAppPath, "INFO")
			sshSetting.GetOutputCommandSsh(cmdRmAppPath)

			cmdMkdirAppPath := fmt.Sprintf(`mkdir -p "%s"`, data.AppPath)
			log.AddLog(cmdMkdirAppPath, "INFO")
			_, err := sshSetting.GetOutputCommandSsh(cmdMkdirAppPath)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			cmdRmDataPath := fmt.Sprintf("rm -rf %s", data.DataPath)
			log.AddLog(cmdRmDataPath, "INFO")
			sshSetting.GetOutputCommandSsh(cmdRmDataPath)

			cmdMkdirDataPath := fmt.Sprintf(`mkdir -p "%s"`, data.DataPath)
			log.AddLog(cmdMkdirDataPath, "INFO")
			_, err = sshSetting.GetOutputCommandSsh(cmdMkdirDataPath)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			appDistSrc := filepath.Join(EC_DATA_PATH, "dist", "app-root.zip")
			err = sshSetting.SshCopyByPath(appDistSrc, data.AppPath)
			log.AddLog(fmt.Sprintf("scp from %s to %s", appDistSrc, data.AppPath), "INFO")
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			appDistSrcDest := filepath.Join(data.AppPath, "app-root.zip")
			unzipAppCmd := fmt.Sprintf("unzip %s -d %s", appDistSrcDest, data.AppPath)
			log.AddLog(unzipAppCmd, "INFO")
			_, err = sshSetting.GetOutputCommandSsh(unzipAppCmd)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			rmTempAppPath := fmt.Sprintf("rm -rf %s", appDistSrcDest)
			_, err = sshSetting.GetOutputCommandSsh(rmTempAppPath)
			log.AddLog(rmTempAppPath, "INFO")
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			dataDistSrc := filepath.Join(EC_DATA_PATH, "dist", "data-root.zip")
			err = sshSetting.SshCopyByPath(dataDistSrc, data.DataPath)
			log.AddLog(fmt.Sprintf("scp from %s to %s", dataDistSrc, data.DataPath), "INFO")
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			dataDistSrcDest := filepath.Join(data.DataPath, "data-root.zip")
			unzipDataCmd := fmt.Sprintf("unzip %s -d %s", dataDistSrcDest, data.DataPath)
			log.AddLog(unzipDataCmd, "INFO")
			_, err = sshSetting.GetOutputCommandSsh(unzipDataCmd)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			rmTempDataPath := fmt.Sprintf("rm -rf %s", dataDistSrcDest)
			_, err = sshSetting.GetOutputCommandSsh(rmTempDataPath)
			log.AddLog(rmTempDataPath, "INFO")
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			osArchCmd := "uname -m"
			log.AddLog(osArchCmd, "INFO")
			osArchRes, err := sshSetting.GetOutputCommandSsh(osArchCmd)
			osArchRes = strings.TrimSpace(osArchRes)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			for _, each := range []string{"sedotand", "sedotans", "sedotanw"} {
				src := filepath.Join(EC_APP_PATH, "cli", "dist", fmt.Sprintf("linux_%s", osArchRes), each)
				dst := filepath.Join(data.AppPath, "cli", each)

				rmSedotanCmd := fmt.Sprintf("rm -rf %s", dst)
				log.AddLog(rmSedotanCmd, "INFO")
				_, err := sshSetting.GetOutputCommandSsh(rmSedotanCmd)
				if err != nil {
					log.AddLog(err.Error(), "ERROR")
					return helper.CreateResult(false, nil, err.Error())
				}

				log.AddLog(fmt.Sprintf("scp %s to %s", src, dst), "INFO")
				err = sshSetting.SshCopyByPath(src, dst)
				if err != nil {
					log.AddLog(err.Error(), "ERROR")
					return helper.CreateResult(false, nil, err.Error())
				}

				chmodCliCmd := fmt.Sprintf("chmod 755 %s", dst)
				log.AddLog(chmodCliCmd, "INFO")
				_, err = sshSetting.GetOutputCommandSsh(chmodCliCmd)
				if err != nil {
					log.AddLog(err.Error(), "ERROR")
					return helper.CreateResult(false, nil, err.Error())
				}
			}

			checkPathCmd := fmt.Sprintf("ls %s", data.AppPath)
			isPathCreated, err := sshSetting.GetOutputCommandSsh(checkPathCmd)
			log.AddLog(checkPathCmd, "INFO")
			if err != nil || strings.TrimSpace(isPathCreated) == "" {
				errString := fmt.Sprintf("Invalid path. %s", err.Error())
				log.AddLog(errString, "ERROR")
				return helper.CreateResult(false, nil, errString)
			}

			if err := setEnvPath(); err != nil {
				return helper.CreateResult(false, nil, err.Error())
			}
		} else if oldData.AppPath != data.AppPath {
			moveDir := fmt.Sprintf(`mv %s %s`, oldData.AppPath, data.AppPath)
			log.AddLog(moveDir, "INFO")
			_, err := sshSetting.GetOutputCommandSsh(moveDir)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			if err := setEnvPath(); err != nil {
				return helper.CreateResult(false, nil, err.Error())
			}
		} else if oldData.DataPath != data.DataPath {
			moveDir := fmt.Sprintf(`mv %s %s`, oldData.DataPath, data.DataPath)
			log.AddLog(moveDir, "INFO")
			_, err := sshSetting.GetOutputCommandSsh(moveDir)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}

			if err := setEnvPath(); err != nil {
				return helper.CreateResult(false, nil, err.Error())
			}
		}
	} else {
		// windows
	}

	log.AddLog(fmt.Sprintf("Saving data ID: %s", data.ID), "INFO")
	err = colonycore.Save(data)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	log.AddLog("Restart sedotand", "INFO")
	_, err = s.ToggleSedotanService("start stop", data.ID)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
	}

	return helper.CreateResult(true, nil, "")
}
示例#11
0
文件: home.go 项目: eaciit/live
func ConfigService(data m.ServiceNew, statuslive string) string {
	var (
		err          error
		pingtype     lv.PingTypeEnum
		valtypestart lv.ValidationTypeEnum
		valtypestop  lv.ValidationTypeEnum
		// exedtype lv.CommandTypeEnum
	)
	svc := lv.NewService()

	if data.Ping.Type == "PingType_Command" {
		pingtype = lv.PingType_Command
	} else if data.Ping.Type == "PingType_Network" {
		pingtype = lv.PingType_Network
	} else if data.Ping.Type == "PingType_HttpStatus" {
		pingtype = lv.PingType_HttpStatus
	} else if data.Ping.Type == "PingType_HttpBody" {
		pingtype = lv.PingType_HttpBody
	} else {
		pingtype = lv.PingType_Custom
	}

	if data.Ping.Type == "PingType_Command" {
		var resptype lv.ResponseEnum
		if data.Ping.ResponseType == "Response_Contains" {
			resptype = lv.Response_Contains
		} else if data.Ping.ResponseType == "Response_Equals" {
			resptype = lv.Response_Equals
		} else {
			resptype = lv.Response_RegEx
		}
		svc.Ping = func() *lv.Ping {
			p := new(lv.Ping)
			p.Type = pingtype
			p.Command = data.Ping.Command
			p.CommandParms = data.Ping.CommandParm
			p.ResponseType = resptype
			p.ResponseValue = "RUNNING"
			return p
		}()
	} else if data.Ping.Type == "PingType_Network" || data.Ping.Type == "PingType_HttpStatus" {
		svc.Ping = func() *lv.Ping {
			p := new(lv.Ping)
			p.Type = pingtype
			p.Host = "http://" + data.Ping.Host + ":" + data.Ping.Port
			return p
		}()
	} else if data.Ping.Type == "PingType_HttpBody" {
		var bodytype lv.HttpBodyEnum
		if data.Ping.HttpBodyType == "HttpBody_Contains" {
			bodytype = lv.HttpBody_Contains
		} else {
			bodytype = lv.HttpBody_Equals
		}
		svc.Ping = func() *lv.Ping {
			p := new(lv.Ping)
			p.Type = pingtype
			p.Host = "http://" + data.Ping.Host + ":" + data.Ping.Port
			p.HttpBodySearch = "RUNNING"
			p.HttpBodyType = bodytype
			return p
		}()
	}

	svc.RestartAfterNCritical = data.Service.RestartAfterNCritical
	svc.Interval = time.Duration(data.Service.Interval) * time.Second

	if data.ExedCommandStart.ValidationType == "ValidationType_Contain" {
		valtypestart = lv.ValidationType_Contain
	} else if data.ExedCommandStart.ValidationType == "ValidationType_Equal" {
		valtypestart = lv.ValidationType_Equal
	} else {
		valtypestart = lv.ValidationType_Regex
	}
	if data.ExedCommandStop.ValidationType == "ValidationType_Contain" {
		valtypestop = lv.ValidationType_Contain
	} else if data.ExedCommandStop.ValidationType == "ValidationType_Equal" {
		valtypestop = lv.ValidationType_Equal
	} else {
		valtypestop = lv.ValidationType_Regex
	}

	// Exec Start
	if data.ExedCommandStart.Type == "CommandType_Local" {
		svc.CommandStart = &lv.Command{
			Type:            lv.CommandType_Local,
			CommandText:     data.ExedCommandStart.CommandText,
			CommandParms:    data.ExedCommandStart.CommandParm,
			ValidationType:  valtypestart,
			ValidationValue: "RUNNING",
		}
	} else if data.ExedCommandStart.Type == "CommandType_SSH" {
		if data.ExedCommandStart.SshAuthType == "SSHAuthType_Password" {
			svc.CommandStart = &lv.Command{
				Type: lv.CommandType_SSH,
				SshClient: &lv.SshSetting{
					SSHHost:     data.ExedCommandStart.SshHost + ":" + data.ExedCommandStart.SshPort,
					SSHUser:     data.ExedCommandStart.SshUser,
					SSHPassword: data.ExedCommandStart.SshPassword,
					SSHAuthType: lv.SSHAuthType_Password,
				},
				CommandText:     data.ExedCommandStart.CommandText,
				ValidationType:  valtypestart,
				ValidationValue: "running",
			}
		} else {
			svc.CommandStart = &lv.Command{
				Type: lv.CommandType_SSH,
				SshClient: &lv.SshSetting{
					SSHHost:        data.ExedCommandStart.SshHost + ":" + data.ExedCommandStart.SshPort,
					SSHUser:        data.ExedCommandStart.SshUser,
					SSHKeyLocation: data.ExedCommandStart.SshKeyLocation,
					SSHAuthType:    lv.SSHAuthType_Certificate,
				},
				CommandText:     data.ExedCommandStart.CommandText,
				ValidationType:  valtypestart,
				ValidationValue: "running",
			}
		}
	} else {
		var valrestaunth lv.RESTAuthTypeEnum
		if data.ExedCommandStart.RestAuthType == "RESTAuthType_None" {
			valrestaunth = lv.RESTAuthType_None
		} else {
			valrestaunth = lv.RESTAuthType_Basic
		}
		svc.CommandStart = &lv.Command{
			Type:            lv.CommandType_REST,
			RESTUrl:         data.ExedCommandStart.RestUrl,
			RESTMethod:      data.ExedCommandStart.RestMenthod, //POST,GET
			RESTUser:        data.ExedCommandStart.RestUser,
			RESTPassword:    data.ExedCommandStart.RestPassword,
			RESTAuthType:    valrestaunth,
			ValidationType:  valtypestart,
			ValidationValue: "SUCCESS",
		}
	}

	// Exec Stop
	if data.ExedCommandStop.Type == "CommandType_Local" {
		svc.CommandStop = &lv.Command{
			Type:            lv.CommandType_Local,
			CommandText:     data.ExedCommandStop.CommandText,
			CommandParms:    data.ExedCommandStop.CommandParm,
			ValidationType:  valtypestop,
			ValidationValue: "STOP_PENDING",
		}
	} else if data.ExedCommandStop.Type == "CommandType_SSH" {
		if data.ExedCommandStop.SshAuthType == "SSHAuthType_Password" {
			svc.CommandStop = &lv.Command{
				Type: lv.CommandType_SSH,
				SshClient: &lv.SshSetting{
					SSHHost:     data.ExedCommandStop.SshHost + ":" + data.ExedCommandStop.SshPort,
					SSHUser:     data.ExedCommandStop.SshUser,
					SSHPassword: data.ExedCommandStop.SshPassword,
					SSHAuthType: lv.SSHAuthType_Password,
				},
				CommandText:     data.ExedCommandStop.CommandText,
				ValidationType:  valtypestop,
				ValidationValue: "running",
			}
		} else {
			svc.CommandStop = &lv.Command{
				Type: lv.CommandType_SSH,
				SshClient: &lv.SshSetting{
					SSHHost:        data.ExedCommandStop.SshHost + ":" + data.ExedCommandStop.SshPort,
					SSHUser:        data.ExedCommandStop.SshUser,
					SSHKeyLocation: data.ExedCommandStop.SshKeyLocation,
					SSHAuthType:    lv.SSHAuthType_Certificate,
				},
				CommandText:     data.ExedCommandStop.CommandText,
				ValidationType:  valtypestop,
				ValidationValue: "running",
			}
		}
	} else {
		var valrestaunth lv.RESTAuthTypeEnum
		if data.ExedCommandStop.RestAuthType == "RESTAuthType_None" {
			valrestaunth = lv.RESTAuthType_None
		} else {
			valrestaunth = lv.RESTAuthType_Basic
		}
		svc.CommandStop = &lv.Command{
			Type:            lv.CommandType_REST,
			RESTUrl:         data.ExedCommandStop.RestUrl,
			RESTMethod:      data.ExedCommandStop.RestMenthod, //POST,GET
			RESTUser:        data.ExedCommandStop.RestUser,
			RESTPassword:    data.ExedCommandStop.RestPassword,
			RESTAuthType:    valrestaunth,
			ValidationType:  valtypestop,
			ValidationValue: "SUCCESS",
		}
	}

	svc.EmailError = data.Service.EmailWarning
	svc.EmailWarning = data.Service.EmailError

	svc.Mail = &lv.EmailSetting{
		SenderEmail:   "*****@*****.**",
		HostEmail:     "smtp.office365.com",
		PortEmail:     587,
		UserEmail:     "*****@*****.**",
		PasswordEmail: "B920Support",
	}

	svc.Log, err = toolkit.NewLog(false, true, logservicepath+"/", "LogService"+strconv.Itoa(data.Service.ID), "20060102")
	if err != nil {
		fmt.Println("Error Start Log : %s", err.Error())
	}
	datasvc := modelsvc{}
	datasvc.ID = data.Service.ID
	datasvc.svc = svc

	if statuslive == "Start" && data.Service.StatusService == "Start" {
		if len(arrsvc) == 0 && data.Service.StatusService == "Start" {
			svc.KeepAlive()
			arrsvc = append(arrsvc, datasvc)
			return "Preparing"
		}
		for j := 0; j < len(arrsvc); j++ {
			if arrsvc[j].ID != data.Service.ID && data.Service.StatusService == "Start" {
				if statuslive == "Start" {
					svc.KeepAlive()
					arrsvc = append(arrsvc, datasvc)
					return "Preparing"
				}
			} else if data.Service.StatusService == "Start" {
				if statuslive == "Start" {
					svc.KeepAlive()
					arrsvc[j] = datasvc
					return "Preparing"
				}
			}
		}
	} else if statuslive == "Live" && data.Service.StatusService == "Start" {
		for j := 0; j < len(arrsvc); j++ {
			if arrsvc[j].ID == data.Service.ID {
				if arrsvc[j].svc.MonitorStatus == "Running" {
					if arrsvc[j].svc.PingStatus == "OK" {
						return arrsvc[j].svc.PingStatus
					} else if arrsvc[j].svc.PingStatus == "Fail" {
						return arrsvc[j].svc.PingStatus
					} else {
						return arrsvc[j].svc.PingStatus
					}
				}
			}
		}
	}
	return "Fail"
}
示例#12
0
func GrabDocConfig(data toolkit.M) (*sdt.GrabService, error) {
	var e error
	var gi, ti time.Duration

	GrabService := sdt.NewGrabService()
	GrabService.Name = data["nameid"].(string) //"iopriceindices"
	GrabService.SourceType = sdt.SourceType_DocExcel

	grabintervalToInt := toolkit.ToInt(data["grabinterval"], toolkit.RoundingAuto)
	timeintervalToInt := toolkit.ToInt(data["timeoutinterval"], toolkit.RoundingAuto)
	if data["intervaltype"].(string) == "seconds" {
		gi = time.Duration(grabintervalToInt) * time.Second
		ti = time.Duration(timeintervalToInt) * time.Second
	} else if data["intervaltype"].(string) == "minutes" {
		gi = time.Duration(grabintervalToInt) * time.Minute
		ti = time.Duration(timeintervalToInt) * time.Minute
	} else if data["intervaltype"].(string) == "hours" {
		gi = time.Duration(grabintervalToInt) * time.Hour
		ti = time.Duration(timeintervalToInt) * time.Hour
	}
	GrabService.GrabInterval = gi
	GrabService.TimeOutInterval = ti //time.Hour, time.Minute, time.Second
	GrabService.TimeOutIntervalInfo = fmt.Sprintf("%v %s", timeintervalToInt, data["intervaltype"])

	ci := dbox.ConnectionInfo{}

	grabDataConf, e := toolkit.ToM(data["grabconf"])
	if e != nil {
		return nil, e
	}
	isDoctype := grabDataConf.Has("doctype")
	if isDoctype {
		connToMap, e := toolkit.ToM(grabDataConf["connectioninfo"])
		if e != nil {
			return nil, e
		}
		ci.Host = connToMap["host"].(string) //"E:\\data\\sample\\IO Price Indices.xlsm"
		if hasSettings := connToMap.Has("settings"); !hasSettings {
			ci.Settings = nil
		} else {
			settingToMap, e := toolkit.ToM(connToMap["settings"])
			if e != nil {
				return nil, e
			}
			ci.Settings = settingToMap //toolkit.M{}.Set("useheader", settingToMap["useheader"].(bool)).Set("delimiter", settingToMap["delimiter"])
		}
		GrabService.ServGetData, e = sdt.NewGetDatabase(ci.Host, grabDataConf["doctype"].(string), &ci)
	}

	logconfToMap, e := toolkit.ToM(data["logconf"])
	if e != nil {
		return nil, e
	}
	logpath := logconfToMap["logpath"].(string)           //"E:\\data\\vale\\log"
	filename := logconfToMap["filename"].(string) + "-%s" //"LOG-LOCALXLSX-%s"
	filepattern := logconfToMap["filepattern"].(string)   //"20060102"

	logconf, e := toolkit.NewLog(false, true, logpath, filename, filepattern)
	if e != nil {
		return nil, e
	}

	GrabService.Log = logconf

	GrabService.ServGetData.CollectionSettings = make(map[string]*sdt.CollectionSetting)
	GrabService.DestDbox = make(map[string]*sdt.DestInfo)

	tempDataSetting := sdt.CollectionSetting{}
	tempDestInfo := sdt.DestInfo{}

	for _, dataSet := range data["datasettings"].([]interface{}) {
		dataToMap, e := toolkit.ToM(dataSet)
		if e != nil {
			return nil, e
		}
		tempDataSetting.Collection = dataToMap["rowselector"].(string) //"HIST"
		for _, columnSet := range dataToMap["columnsettings"].([]interface{}) {
			columnToMap, e := toolkit.ToM(columnSet)
			if e != nil {
				return nil, e
			}
			tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &sdt.GrabColumn{Alias: columnToMap["alias"].(string), Selector: columnToMap["selector"].(string)})
		}
		GrabService.ServGetData.CollectionSettings[dataToMap["name"].(string)] = &tempDataSetting //DATA01 use name in datasettings

		// fmt.Println("doctype>", grabDataConf["doctype"])
		connToMap, e := toolkit.ToM(dataToMap["connectioninfo"])
		if e != nil {
			return nil, e
		}
		var db, usr, pwd string

		if hasDb := connToMap.Has("database"); !hasDb {
			db = ""
		} else {
			db = connToMap["database"].(string)
		}
		if hasUser := connToMap.Has("username"); !hasUser {
			usr = ""
		} else {
			usr = connToMap["username"].(string)
		}

		if hasPwd := connToMap.Has("password"); !hasPwd {
			pwd = ""
		} else {
			pwd = connToMap["username"].(string)
		}
		ci = dbox.ConnectionInfo{}
		ci.Host = connToMap["host"].(string) //"localhost:27017"
		ci.Database = db                     //"valegrab"
		ci.UserName = usr                    //""
		ci.Password = pwd                    //""

		//tempDestInfo.Collection = "iopriceindices"
		if hasCollection := connToMap.Has("collection"); !hasCollection {
			tempDestInfo.Collection = ""
		} else {
			tempDestInfo.Collection = connToMap["collection"].(string)
		}
		tempDestInfo.Desttype = dataToMap["desttype"].(string) //"mongo"

		tempDestInfo.IConnection, e = dbox.NewConnection(tempDestInfo.Desttype, &ci)
		if e != nil {
			return nil, e
		}

		GrabService.DestDbox[dataToMap["name"].(string)] = &tempDestInfo
		//=History===========================================================
		GrabService.HistoryPath = HistoryPath       //"E:\\data\\vale\\history\\"
		GrabService.HistoryRecPath = HistoryRecPath //"E:\\data\\vale\\historyrec\\"
		//===================================================================
	}

	return GrabService, e
}
示例#13
0
func (s *ServerController) SaveServers(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	r.Request.ParseMultipartForm(32 << 20)
	r.Request.ParseForm()

	path := filepath.Join(EC_DATA_PATH, "server", "log")
	log, _ := toolkit.NewLog(false, true, path, "log-%s", "20060102-1504")

	data := new(colonycore.Server)
	if r.Request.FormValue("serviceSSH[type]") == "File" {
		log.AddLog("Get forms", "INFO")
		dataRaw := map[string]interface{}{}
		err := r.GetForms(&dataRaw)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}

		log.AddLog("Serding data", "INFO")
		err = toolkit.Serde(dataRaw, &data, "json")
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}
	} else {
		log.AddLog("Get payload", "INFO")
		err := r.GetPayload(&data)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}
	}

	if data.ServiceSSH.Type == "File" {
		log.AddLog("Fetching public key", "INFO")
		reqFileName := "privatekey"
		file, _, err := r.Request.FormFile(reqFileName)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}

		if file != nil {
			log.AddLog("Saving public key", "INFO")
			data.ServiceSSH.File = filepath.Join(EC_DATA_PATH, "server", "privatekeys", data.ID)
			_, _, err = helper.FetchThenSaveFile(r.Request, reqFileName, data.ServiceSSH.File)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				return helper.CreateResult(false, nil, err.Error())
			}
		}
	}

	if data.IsAccessValid("node") && data.IsColonyServer {
		if data.OS == "linux" {
			if err := data.InstallColonyOnLinux(log); err != nil {
				return helper.CreateResult(false, nil, err.Error())
			}
		} else {
			if err := data.InstallColonyOnWindows(log); err != nil {
				return helper.CreateResult(false, nil, err.Error())
			}
		}

		data.DetectInstalledLang()
		data.DetectService()
		data.UpdateInternalAppDeploymentStatus("add")

		log.AddLog("Restart sedotand", "INFO")
		if _, err := data.ToggleSedotanService("start stop", data.ID); err != nil {
			log.AddLog(err.Error(), "ERROR")
		}
	}

	if data.IsAccessValid("hdfs") {
		log.AddLog(fmt.Sprintf("SSH Connect %v", data), "INFO")
		hdfsConfig := hdfs.NewHdfsConfig(data.ServiceHDFS.Host, data.ServiceHDFS.User)
		hdfsConfig.Password = data.ServiceHDFS.Pass

		hadeepes, err := hdfs.NewWebHdfs(hdfsConfig)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			return helper.CreateResult(false, nil, err.Error())
		}

		_, err = hadeepes.List("/")
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}

		hadeepes.Config.TimeOut = 5 * time.Millisecond
		hadeepes.Config.PoolSize = 100
	}

	runsrv, e := new(colonycore.ServerService).GetAll()
	if e != nil {
		return helper.CreateResult(false, nil, e.Error())
	}

	svr, _, a := data.Connect()
	if a != nil {
		return helper.CreateResult(false, nil, a.Error())
	}
	for _, sr := range runsrv {
		b, e := svr.RunCommandSshAsMap(fmt.Sprintf("which %s", sr.ID))
		if e != nil {

			return helper.CreateResult(false, nil, e.Error())
		}

		res := strings.TrimSpace(b[0].Output)
		i := strings.Index(res, "/")
		if i > -1 {
			sr.IsInstalled = true

		}
		hh := colonycore.ServerService{ID: sr.ID, Name: sr.Name, IsInstalled: sr.IsInstalled}
		data.OtherServices = append(data.OtherServices, &hh)
	}

	log.AddLog(fmt.Sprintf("Saving data ID: %s", data.ID), "INFO")
	err := colonycore.Save(data)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, nil, "")
}
示例#14
0
文件: server.go 项目: eaciit/knot
func (s *Server) Log() *toolkit.LogEngine {
	if s.log == nil {
		s.log, _ = toolkit.NewLog(true, false, "", "", "")
	}
	return s.log
}
示例#15
0
文件: z2_test.go 项目: arfian/sedotan
func TestServiceGrabDocument(t *testing.T) {
	var e error

	xGrabService := NewGrabService()
	xGrabService.Name = "iopriceindices"

	xGrabService.SourceType = SourceType_DocExcel

	xGrabService.GrabInterval = 5 * time.Minute
	xGrabService.TimeOutInterval = 10 * time.Second //time.Hour, time.Minute, time.Second

	xGrabService.TimeOutIntervalInfo = fmt.Sprintf("%v %s", 1, "seconds")

	//==must have grabconf and Connection info inside grabconf         ===========================================
	// mapValConfig, e := toolkit.ToM(mapVal.Get("grabconf", nil).(map[string]interface{}))
	// mapConnVal, e := toolkit.ToM(mapValConfig.Get("connectioninfo", nil).(map[string]interface{}))

	ci := dbox.ConnectionInfo{}

	ci.Host = "E:\\data\\sample\\IO Price Indices.xlsm"
	// ci.Database = mapConnVal.Get("database", "").(string)
	// ci.UserName = mapConnVal.Get("userName", "").(string)
	// ci.Password = mapConnVal.Get("password", "").(string)

	// if have setting inside of connection info
	// ci.Settings, e = toolkit.ToM(tempSetting.(map[string]interface{}))
	ci.Settings = nil

	xGrabService.ServGetData, e = NewGetDatabase(ci.Host, "xlsx", &ci)

	//===================================================================

	//==For Data Log          ===========================================

	// 	logpath = tempLogConf.Get("logpath", "").(string)
	// 	filename = tempLogConf.Get("filename", "").(string)
	// 	filepattern = tempLogConf.Get("filepattern", "").(string)

	logpath := "E:\\data\\vale\\log"
	filename := "LOG-LOCALXLSX-%s"
	filepattern := "20060102"

	logconf, e := toolkit.NewLog(false, true, logpath, filename, filepattern)
	if e != nil {
		t.Errorf("Error Found : ", e)
	}

	xGrabService.Log = logconf
	//===================================================================

	//===================================================================
	//==Data Setting and Destination Save =====================

	xGrabService.ServGetData.CollectionSettings = make(map[string]*CollectionSetting)
	xGrabService.DestDbox = make(map[string]*DestInfo)

	// ==For Every Data Setting ===============================
	tempDataSetting := CollectionSetting{}
	tempDestInfo := DestInfo{}

	// .Collection = mapxVal.Get("rowselector", "").(string)
	tempDataSetting.Collection = "HIST"
	tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &GrabColumn{Alias: "Date", Selector: "1"})
	tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &GrabColumn{Alias: "Platts 62% Fe IODEX", Selector: "2"})
	tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &GrabColumn{Alias: "Platts 65% Fe", Selector: "4"})
	tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &GrabColumn{Alias: "TSI 62% Fe", Selector: "15"})
	tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &GrabColumn{Alias: "TSI 65% Fe", Selector: "16"})
	tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &GrabColumn{Alias: "TSI 62% Fe LOW ALUMINA", Selector: "17"})
	tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &GrabColumn{Alias: "MB 62% Fe", Selector: "26"})
	tempDataSetting.SelectColumn = append(tempDataSetting.SelectColumn, &GrabColumn{Alias: "MB 65% Fe", Selector: "29"})

	// tempDataSetting.SetFilterCond(tempFilterCond)
	// -Check "filtercond" in config-
	// tempFilterCond, e = toolkit.ToM(mapxVal.Get("filtercond", nil).(map[string]interface{}))
	// tempDataSetting.SetFilterCond(tempFilterCond)

	xGrabService.ServGetData.CollectionSettings["DATA01"] = &tempDataSetting //DATA01 use name in datasettings

	ci = dbox.ConnectionInfo{}
	ci.Host = "localhost:27017"
	ci.Database = "valegrab"
	ci.UserName = ""
	ci.Password = ""
	// ci.Settings = toolkit.M{}.Set("useheader", true).Set("delimiter", ",")
	// setting will be depend on config file

	tempDestInfo.Collection = "iopriceindices"
	tempDestInfo.Desttype = "mongo"

	tempDestInfo.IConnection, e = dbox.NewConnection(tempDestInfo.Desttype, &ci)
	if e != nil {
		t.Errorf("Error Found : ", e)
	}

	xGrabService.DestDbox["DATA01"] = &tempDestInfo
	//=History===========================================================
	xGrabService.HistoryPath = "E:\\data\\vale\\history\\"
	xGrabService.HistoryRecPath = "E:\\data\\vale\\historyrec\\"
	//===================================================================

	e = xGrabService.StartService()
	if e != nil {
		t.Errorf("Error Found : ", e)
	} else {

		for i := 0; i < 100; i++ {
			fmt.Printf(".")
			time.Sleep(1000 * time.Millisecond)
		}

		e = xGrabService.StopService()
		if e != nil {
			t.Errorf("Error Found : ", e)
		}
	}
}
示例#16
0
文件: service.go 项目: arfian/live
func (s *Service) AddLog(logtype, logtext string) {
	if s.logEngine == nil {
		s.logEngine = toolkit.NewLog(true, false, "", "", false)
	}
	s.logEngine.AddLog(logtext, logtype)
}
示例#17
0
文件: live_test.go 项目: arfian/live
func TestSshExecPingHttpBody(t *testing.T) {

	fmt.Println("[START](Ping HttpBody|Exec Ssh)")

	//	fmt.Println("Test Check for MySql Service Local With Execution Ssh And Ping HttpBody[REST]")
	var (
		err error
		i   int = 30
	)

	svc := NewService()
	svc.Ping = func() *Ping {
		p := new(Ping)
		p.Type = PingType_HttpBody
		p.Host = "http://192.168.56.101:8080/status"
		p.HttpBodySearch = "RUNNING"
		p.HttpBodyType = HttpBody_Contains
		return p
	}()

	svc.RestartAfterNCritical = 3
	svc.Interval = 1 * time.Second
	svc.CommandStart = &Command{
		Type: CommandType_SSH,
		SshClient: &SshParm{
			SSHHost:     "192.168.56.101:22",
			SSHUser:     "******",
			SSHPassword: "******",
			SSHAuthType: SSHAuthType_Password,
		},
		CommandText:     "sudo service mysql start",
		ValidationType:  ValidationType_Contain,
		ValidationValue: "running",
	}

	svc.CommandStop = &Command{
		Type: CommandType_SSH,
		SshClient: &SshParm{
			SSHHost:     "192.168.56.101:22",
			SSHUser:     "******",
			SSHPassword: "******",
			SSHAuthType: SSHAuthType_Password,
		},
		CommandText:     "sudo service mysql stop",
		ValidationType:  ValidationType_Contain,
		ValidationValue: "running",
	}

	svc.Log, err = toolkit.NewLog(false, true, "E:\\goproject\\LOG", "TestSshExecPingHttpBody", "20060102")

	if err != nil {
		t.Errorf("Error Start Log : %s", err.Error())
	}

	svc.KeepAlive()

	if svc.MonitorStatus != "Running" {
		t.Errorf("Error, service status monitor check is %s \n", svc.MonitorStatus)
	}

	for svc.MonitorStatus == "Running" {
		time.Sleep(2000 * time.Millisecond)
		i = i - 1
		if i < 0 {
			svc.StopMonitor()
		}

		if i == 10 {
			svc.CommandStop.Exec()
		}

		if svc.criticalFound == 0 {
			fmt.Print(".")
		} else {
			fmt.Print("S")
		}

	}
	fmt.Println("[END] \n\n")
}
示例#18
0
func (a *ApplicationController) Deploy(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	path := filepath.Join(EC_DATA_PATH, "application", "log")
	log, _ := toolkit.NewLog(false, true, path, "log-%s", "20060102-1504")

	log.AddLog("Get payload", "INFO")
	payload := struct {
		ID     string `json:"_id",bson:"_id"`
		Server string
	}{}
	err := r.GetPayload(&payload)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	log.AddLog("Get application with ID: "+payload.ID, "INFO")
	app := new(colonycore.Application)
	err = colonycore.Get(app, payload.ID)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	log.AddLog("Get server with ID: "+payload.Server, "INFO")
	server := new(colonycore.Server)
	err = colonycore.Get(server, payload.Server)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	serverPathSeparator := a.GetServerPathSeparator(server)

	changeDeploymentStatus := func(status bool) {
		deployedTo := []string{}
		for _, each := range app.DeployedTo {
			if each != server.ID {
				deployedTo = append(deployedTo, server.ID)
			}
		}

		if status {
			app.DeployedTo = append(deployedTo, server.ID)
		}
		colonycore.Save(app)
	}

	log.AddLog(fmt.Sprintf("Connect to server %v", server), "INFO")
	sshSetting, sshClient, err := new(ServerController).SSHConnect(server)
	defer sshClient.Close()

	if server.OS == "windows" {
		sourcePath := filepath.Join(EC_APP_PATH, "src", app.ID)
		destinationPath := strings.Join([]string{server.AppPath, "src"}, serverPathSeparator)
		destinationZipPathOutput := strings.Join([]string{destinationPath, app.ID}, serverPathSeparator)
		var sourceZipPath string
		var unzipCmd string
		var destinationZipPath string

		if strings.Contains(server.CmdExtract, "7z") || strings.Contains(server.CmdExtract, "zip") {
			sourceZipPath = filepath.Join(EC_APP_PATH, "src", fmt.Sprintf("%s.zip", app.ID))
			destinationZipPath = fmt.Sprintf("%s.zip", destinationZipPathOutput)
			deszip := fmt.Sprintf("%s%s%s", destinationPath, serverPathSeparator, app.ID)

			// cmd /C 7z e -o %s -y %s
			unzipCmd = fmt.Sprintf("cmd /C %s", server.CmdExtract)
			unzipCmd = strings.Replace(unzipCmd, `%1`, deszip, -1)
			unzipCmd = strings.Replace(unzipCmd, `%2`, destinationZipPath, -1)

			log.AddLog(unzipCmd, "INFO")
			err = toolkit.ZipCompress(sourcePath, sourceZipPath)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				changeDeploymentStatus(false)
				return helper.CreateResult(false, nil, err.Error())
			}
		} else {
			message := "currently only zip/7z command which is supported"
			log.AddLog(message, "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, message)
		}

		rmCmdZip := fmt.Sprintf("rm -rf %s", destinationZipPath)
		log.AddLog(rmCmdZip, "INFO")
		_, err = sshSetting.GetOutputCommandSsh(rmCmdZip)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}

		err = sshSetting.SshCopyByPath(sourceZipPath, destinationPath)
		log.AddLog(fmt.Sprintf("scp from %s to %s", sourceZipPath, destinationPath), "INFO")

		rmCmdZipOutput := fmt.Sprintf("rm -rf %s", destinationZipPathOutput)
		log.AddLog(rmCmdZipOutput, "INFO")
		_, err = sshSetting.GetOutputCommandSsh(rmCmdZipOutput)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}

		mkdirDestCmd := fmt.Sprintf("%s %s%s%s", server.CmdMkDir, destinationPath, serverPathSeparator, app.ID)
		log.AddLog(mkdirDestCmd, "INFO")
		_, err = sshSetting.GetOutputCommandSsh(mkdirDestCmd)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}

		chmodDestCmd := fmt.Sprintf("chmod -R 755 %s%s%s", destinationPath, serverPathSeparator, app.ID)
		log.AddLog(chmodDestCmd, "INFO")
		_, err = sshSetting.GetOutputCommandSsh(chmodDestCmd)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}

		log.AddLog(unzipCmd, "INFO")
		_, err = sshSetting.GetOutputCommandSsh(unzipCmd)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}

		err = os.Remove(sourceZipPath)
		log.AddLog(fmt.Sprintf("remove %s", sourceZipPath), "INFO")
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}

		findCommand := fmt.Sprintf(`find %s -name "*install.bat"`, destinationZipPathOutput)
		log.AddLog(findCommand, "INFO")
		installerPath, err := sshSetting.GetOutputCommandSsh(findCommand)
		installerPath = strings.TrimSpace(installerPath)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}

		if installerPath == "" {
			errString := "installer not found"
			log.AddLog(errString, "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, errString)
		}

		chmodCommand := fmt.Sprintf("chmod 755 %s%sinstall.bat", destinationZipPathOutput, serverPathSeparator)
		log.AddLog(chmodCommand, "INFO")
		_, err = sshSetting.GetOutputCommandSsh(chmodCommand)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}

		installerBasePath, _ := func(path string) (string, string) {
			comps := strings.Split(path, serverPathSeparator)
			ibp := strings.Join(comps[:len(comps)-1], serverPathSeparator)
			ilf := comps[len(comps)-1]

			return ibp, ilf
		}(installerPath)
		fmt.Println(installerBasePath)
		cRunCommand := make(chan string, 1)
		go func() {
			runCommand := fmt.Sprintf("cmd /C %s%sinstall.bat", destinationZipPathOutput, serverPathSeparator)
			log.AddLog(runCommand, "INFO")
			res, err := sshSetting.RunCommandSsh(runCommand)
			fmt.Println(res)
			if err != nil {
				log.AddLog(err.Error(), "ERROR")
				cRunCommand <- err.Error()
			} else {
				cRunCommand <- ""
			}
		}()

		errorMessage := ""
		select {
		case receiveRunCommandOutput := <-cRunCommand:
			errorMessage = receiveRunCommandOutput
		case <-time.After(time.Second * 3):
			errorMessage = ""
		}

		if errorMessage != "" {
			log.AddLog(errorMessage, "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, errorMessage)
		}

		if app.DeployedTo == nil {
			app.DeployedTo = []string{}
		}

		changeDeploymentStatus(true)
		return helper.CreateResult(true, nil, "")
	}

	sourcePath := filepath.Join(EC_APP_PATH, "src", app.ID)
	destinationPath := strings.Join([]string{server.AppPath, "src"}, serverPathSeparator)
	destinationZipPathOutput := strings.Join([]string{destinationPath, app.ID}, serverPathSeparator)
	var sourceZipPath string
	var unzipCmd string
	var destinationZipPath string

	if strings.Contains(server.CmdExtract, "tar") {
		sourceZipPath = filepath.Join(EC_APP_PATH, "src", fmt.Sprintf("%s.tar", app.ID))
		destinationZipPath = fmt.Sprintf("%s.tar", destinationZipPathOutput)

		// tar -xvf %s -C %s
		unzipCmd = strings.Replace(server.CmdExtract, `%1`, destinationZipPath, -1)
		unzipCmd = strings.Replace(unzipCmd, `%2`, destinationZipPathOutput, -1)

		log.AddLog(unzipCmd, "INFO")
		err = toolkit.TarCompress(sourcePath, sourceZipPath)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}
	} else if strings.Contains(server.CmdExtract, "zip") {
		sourceZipPath = filepath.Join(EC_APP_PATH, "src", fmt.Sprintf("%s.zip", app.ID))
		destinationZipPath = fmt.Sprintf("%s.zip", destinationZipPathOutput)

		// unzip %s -d %s
		unzipCmd = strings.Replace(server.CmdExtract, `%1`, destinationZipPath, -1)
		unzipCmd = strings.Replace(unzipCmd, `%2`, destinationZipPathOutput, -1)

		log.AddLog(unzipCmd, "INFO")
		err = toolkit.ZipCompress(sourcePath, sourceZipPath)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			changeDeploymentStatus(false)
			return helper.CreateResult(false, nil, err.Error())
		}
	} else {
		message := "currently only zip/tar command which is supported"
		log.AddLog(message, "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, message)
	}

	rmCmdZip := fmt.Sprintf("rm -rf %s", destinationZipPath)
	log.AddLog(rmCmdZip, "INFO")
	_, err = sshSetting.GetOutputCommandSsh(rmCmdZip)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, err.Error())
	}

	err = sshSetting.SshCopyByPath(sourceZipPath, destinationPath)
	log.AddLog(fmt.Sprintf("scp from %s to %s", sourceZipPath, destinationPath), "INFO")

	rmCmdZipOutput := fmt.Sprintf("rm -rf %s", destinationZipPathOutput)
	log.AddLog(rmCmdZipOutput, "INFO")
	_, err = sshSetting.GetOutputCommandSsh(rmCmdZipOutput)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, err.Error())
	}

	mkdirDestCmd := fmt.Sprintf("%s %s%s%s", server.CmdMkDir, destinationPath, serverPathSeparator, app.ID)
	log.AddLog(mkdirDestCmd, "INFO")
	_, err = sshSetting.GetOutputCommandSsh(mkdirDestCmd)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, err.Error())
	}

	chmodDestCmd := fmt.Sprintf("chmod -R 777 %s%s%s", destinationPath, serverPathSeparator, app.ID)
	log.AddLog(chmodDestCmd, "INFO")
	_, err = sshSetting.GetOutputCommandSsh(chmodDestCmd)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, err.Error())
	}

	log.AddLog(unzipCmd, "INFO")
	_, err = sshSetting.GetOutputCommandSsh(unzipCmd)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, err.Error())
	}

	err = os.Remove(sourceZipPath)
	log.AddLog(fmt.Sprintf("remove %s", sourceZipPath), "INFO")
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, err.Error())
	}

	findCommand := fmt.Sprintf(`find %s -name "*install.sh"`, destinationZipPathOutput)
	log.AddLog(findCommand, "INFO")
	installerPath, err := sshSetting.GetOutputCommandSsh(findCommand)
	installerPath = strings.TrimSpace(installerPath)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, err.Error())
	}

	if installerPath == "" {
		errString := "installer not found"
		log.AddLog(errString, "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, errString)
	}

	chmodCommand := fmt.Sprintf("chmod 755 %s", installerPath)
	log.AddLog(chmodCommand, "INFO")
	_, err = sshSetting.GetOutputCommandSsh(chmodCommand)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, err.Error())
	}

	installerBasePath, installerFile := func(path string) (string, string) {
		comps := strings.Split(path, serverPathSeparator)
		ibp := strings.Join(comps[:len(comps)-1], serverPathSeparator)
		ilf := comps[len(comps)-1]

		return ibp, ilf
	}(installerPath)

	cRunCommand := make(chan string, 1)
	go func() {
		runCommand := fmt.Sprintf("cd %s && ./%s &", installerBasePath, installerFile)
		log.AddLog(runCommand, "INFO")
		_, err = sshSetting.RunCommandSsh(runCommand)
		if err != nil {
			log.AddLog(err.Error(), "ERROR")
			cRunCommand <- err.Error()
		} else {
			cRunCommand <- ""
		}
	}()

	errorMessage := ""
	select {
	case receiveRunCommandOutput := <-cRunCommand:
		errorMessage = receiveRunCommandOutput
	case <-time.After(time.Second * 3):
		errorMessage = ""
	}

	if errorMessage != "" {
		log.AddLog(errorMessage, "ERROR")
		changeDeploymentStatus(false)
		return helper.CreateResult(false, nil, errorMessage)
	}

	if app.DeployedTo == nil {
		app.DeployedTo = []string{}
	}

	changeDeploymentStatus(true)
	return helper.CreateResult(true, nil, "")
}
示例#19
0
func (a *ApplicationController) Undeploy(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	path := filepath.Join(EC_DATA_PATH, "application", "log")
	log, _ := toolkit.NewLog(false, true, path, "log-%s", "20060102-1504")

	payload := struct {
		ID     string `json:"_id",bson:"_id"`
		Server string
	}{}
	err := r.GetPayload(&payload)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	log.AddLog("Get application with ID: "+payload.ID, "INFO")
	app := new(colonycore.Application)
	err = colonycore.Get(app, payload.ID)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	log.AddLog("Get server with ID: "+payload.Server, "INFO")
	server := new(colonycore.Server)
	err = colonycore.Get(server, payload.Server)
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	sshSetting, _, err := server.Connect()
	if err != nil {
		log.AddLog(err.Error(), "ERROR")
		return helper.CreateResult(false, nil, err.Error())
	}

	if server.OS == "windows" {
		cmds := []string{
			fmt.Sprintf("rmdir /s /q $EC_APP_PATH/src/%s", app.ID),
			fmt.Sprintf("rmdir /s /q $EC_APP_PATH/src/%s.*", app.ID),
			fmt.Sprintf("rmdir /s /q $EC_APP_PATH/web/%s", app.ID),
		}

		for _, each := range cmds {
			result, err := sshSetting.RunCommandSshAsMap(each)
			if err != nil {
				log.AddLog(err.Error()+result[0].Output, "ERROR")
			}
		}
	} else {
		cmds := []string{
			fmt.Sprintf("rm -rf $EC_APP_PATH/src/%s", app.ID),
			fmt.Sprintf("rm -rf $EC_APP_PATH/src/%s.*", app.ID),
			fmt.Sprintf("rm -rf $EC_APP_PATH/web/%s", app.ID),
		}

		for _, each := range cmds {
			result, err := sshSetting.RunCommandSshAsMap(each)
			if err != nil {
				log.AddLog(err.Error()+result[0].Output, "ERROR")
			}
		}
	}

	return helper.CreateResult(true, nil, "")
}
示例#20
0
文件: live_test.go 项目: arfian/live
func TestLocalCommandPingHttpStatus(t *testing.T) {

	fmt.Println("[START](Ping HttpStatus|Exec LocalCommand)")

	//	fmt.Println("Test Check for MongoDb Service Local With Execution Command And Ping HttpStatus")
	var (
		err error
		i   int = 30
	)

	svc := NewService()
	svc.Ping = func() *Ping {
		p := new(Ping)
		p.Type = PingType_HttpStatus
		p.Host = "http://localhost:27017"
		return p
	}()

	svc.RestartAfterNCritical = 3
	svc.Interval = 1 * time.Second
	svc.CommandStart = &Command{
		Type:            CommandType_Local,
		CommandText:     "cmd",
		CommandParms:    []string{"/C", "sc", "start", "mongodb"},
		ValidationType:  ValidationType_Contain,
		ValidationValue: "RUNNING",
	}

	svc.CommandStop = &Command{
		Type:            CommandType_Local,
		CommandText:     "cmd",
		CommandParms:    []string{"/C", "sc", "stop", "mongodb"},
		ValidationType:  ValidationType_Contain,
		ValidationValue: "STOP_PENDING",
	}

	svc.Log, err = toolkit.NewLog(false, true, "E:\\goproject\\LOG", "TestLocalCommandPingHttpStatus", "20060102")

	if err != nil {
		t.Errorf("Error Start Log : %s", err.Error())
	}

	svc.KeepAlive()

	if svc.MonitorStatus != "Running" {
		t.Errorf("Error, service status monitor check is %s \n", svc.MonitorStatus)
	}

	for svc.MonitorStatus == "Running" {
		time.Sleep(2000 * time.Millisecond)
		i = i - 1
		if i < 0 {
			svc.StopMonitor()
		}

		if i == 10 {

			svc.CommandStop.Exec()
		}

		if svc.criticalFound == 0 {
			fmt.Print(".")
		} else {
			fmt.Print("S")
		}

	}
	fmt.Println("[END] \n\n")
}
示例#21
0
文件: live_test.go 项目: arfian/live
func TestRestExecPingHttpBody(t *testing.T) {

	fmt.Println("[START](Ping HttpBody|Exec REST)")

	//	fmt.Println("Test Check for MySql Service With Execution Rest And Ping HttpBody[REST]")
	var (
		err error
		i   int = 30
	)

	svc := NewService()
	svc.Ping = func() *Ping {
		p := new(Ping)
		p.Type = PingType_HttpBody
		p.Host = "http://192.168.56.101:8080/status"
		p.HttpBodySearch = "RUNNING"
		p.HttpBodyType = HttpBody_Contains
		return p
	}()

	svc.Name = "MongoDb 3.0 WT Port 27123"
	svc.RestartAfterNCritical = 3
	svc.Interval = 1 * time.Second
	svc.CommandStart = &Command{
		Type:            CommandType_REST,
		RESTUrl:         "http://192.168.56.101:8080/start",
		RESTMethod:      "GET", //POST,GET
		RESTUser:        "******",
		RESTPassword:    "******",
		RESTAuthType:    RESTAuthType_None,
		ValidationType:  ValidationType_Contain,
		ValidationValue: "SUCCESS",
	}

	svc.CommandStop = &Command{
		Type:            CommandType_REST,
		RESTUrl:         "http://192.168.56.101:8080/stop",
		RESTMethod:      "GET", //POST,GET
		RESTUser:        "******",
		RESTPassword:    "******",
		RESTAuthType:    RESTAuthType_None,
		ValidationType:  ValidationType_Contain,
		ValidationValue: "SUCCESS",
	}

	svc.Log, err = toolkit.NewLog(false, true, "E:\\goproject\\LOG", "TestRestExecPingHttpBody", "20060102")

	if err != nil {
		t.Errorf("Error Start Log : %s", err.Error())
	}

	svc.KeepAlive()

	if svc.MonitorStatus != "Running" {
		t.Errorf("Error, service status monitor check is %s \n", svc.MonitorStatus)
	}

	for svc.MonitorStatus == "Running" {
		time.Sleep(2000 * time.Millisecond)
		i = i - 1
		if i < 0 {
			svc.StopMonitor()
		}

		if i == 10 {
			svc.CommandStop.Exec()
		}

		if svc.criticalFound == 0 {
			fmt.Print(".")
		} else {
			fmt.Print("S")
		}
	}
	fmt.Println("[END] \n\n")
}
示例#22
0
func (a *Server) Start(address string) error {
	if a.Log == nil {
		le, e := toolkit.NewLog(true, false, "", "", "")
		if e == nil {
			a.Log = le
		} else {
			return errors.New("Unable to setup log")
		}
	}

	if a.rpcObject == nil {
		//return errorlib.Error(packageName, objServer, "Start", "RPC Object is not yet properly initialized")
		a.rpcObject = new(Rpc)
	}
	/*
		if reloadConfig {
			a.ReadConfig()
		}
	*/

	if a.Address == "" {
		if address != "" {
			a.Address = address
		}
		/*else {
			a.Address = fmt.Sprintf("%s:%d", a.ServerName, a.Port)
		}
		*/
		if a.Address == "" {
			return errors.New("RPC Server address is empty")
		}
	}

	//init a ping method. Ping method will return "EACIIT RPC Application Server"
	a.AddFn("ping", func(in toolkit.M) *toolkit.Result {
		result := toolkit.NewResult()
		result.Data = "EACIIT RPC Application Server"
		return result
	}, false, "")

	a.AddFn("addsession", func(in toolkit.M) *toolkit.Result {
		referenceID := in.GetString("auth_referenceid")
		result := toolkit.NewResult()
		if referenceID == "" {
			return result.SetErrorTxt("Empty user provided")
		}

		//session, exist := a.sessions[referenceID]
		var session *Session

		for _, session = range a.sessions {
			if session.ReferenceID == referenceID && session.IsValid() && !a.AllowMultiLogin {
				return result.SetErrorTxt(referenceID + " already has active session on other connection")
			} else if session.ReferenceID == referenceID && !session.IsValid() && !a.AllowMultiLogin {
				delete(a.sessions, session.SessionID)
			}
		}
		session = a.RegisterSession(referenceID)
		//a.sessions[session.SessionID] = session
		//result.SetBytes(session, MarshallingMethod())
		result.Data = toolkit.M{}.Set("referenceid", session.SessionID).Set("secret", session.Secret).ToBytes("gob")
		a.Log.Info(a.Address + " has new session " + referenceID + " : " + session.SessionID)
		return result
	}, true, "")

	a.AddFn("removesession", func(in toolkit.M) *toolkit.Result {
		result := toolkit.NewResult()
		referenceID := in.GetString("auth_referenceid")
		delete(a.sessions, referenceID)
		return result
	}, true, "session")

	a.Log.Info("Starting server " + a.Address + ". Registered functions are: " + strings.Join(func() []string {
		ret := []string{}
		for k, _ := range a.rpcObject.Fns {
			ret = append(ret, k)
		}
		return ret
	}(), ", "))

	a.rpcServer = rpc.NewServer()
	a.rpcServer.Register(a.rpcObject)
	l, e := net.Listen("tcp", fmt.Sprintf("%s", a.Address))
	if e != nil {
		return e
	}

	a.sessions = map[string]*Session{}
	a.listener = l
	go func() {
		a.rpcServer.Accept(l)
		//rpc.Accept(l)
		/*
			listenerConnection, err := l.Accept()
			if err != nil {
				a.Log.Error("Unable to setup RPC Listener Connection " + err.Error())
				return
			}
			go a.rpcServer.ServeConn(listenerConnection)
		*/
	}()
	return nil
}
示例#23
0
var sourceFile, outPath string
var fieldImports = make(map[string]string)

func init() {
	fieldImports["time"] = "time"
	fieldImports["bson"] = "gopkg.in/mgo.v2/bson"
}

var (
	wd = func() string {
		d, _ := os.Getwd()
		return d
	}()

	isLinux    = runtime.GOOS != "windows"
	log, _     = toolkit.NewLog(true, false, "", "", "")
	flagSource = flag.String("source", "./", "name or pattern of source file(s). If empty it will be default.orm")
	flagOut    = flag.String("out", wd, "Output path. If empty it will be current working directory")
)

func check(e error, fatal bool, pre string) {
	if e == nil {
		return
	}

	if pre == "" {
		log.Error(pre + " " + e.Error())
	} else {
		log.Error(e.Error())
	}