Esempio n. 1
0
func InsureNewVersionFiles(da *model.DesiredAgent) error {
	if FilesReady(da) {
		return nil
	}

	downloadTarballCmd := BuildCommand(da.RunUser, "wget", "-q", da.TarballUrl, "-O", path.Join(da.AgentVersionDir, da.TarballFilename))
	downloadTarballCmd.Dir = file.SelfDir()
	_, err := ExecuteCommandWithOutput(downloadTarballCmd)
	if err != nil {
		logger.Errorln("wget -q", da.TarballUrl, "-O", da.TarballFilename, "fail", err)
		return err
	}

	downloadMd5Cmd := BuildCommand(da.RunUser, "wget", "-q", da.Md5Url, "-O", path.Join(da.AgentVersionDir, da.Md5Filename))
	downloadMd5Cmd.Dir = file.SelfDir()
	_, err = ExecuteCommandWithOutput(downloadMd5Cmd)
	if err != nil {
		log.Println("wget -q", da.Md5Url, "-O", da.Md5Filename, "fail", err)
		return err
	}

	if "" != da.ConfigFileName && "" != da.ConfigRemoteUrl {
		downloadConfigCmd := BuildCommand(da.RunUser, "wget", "-q", da.ConfigRemoteUrl, "-O", path.Join(da.AgentVersionDir, da.ConfigFileName))
		downloadConfigCmd.Dir = file.SelfDir()
		_, err := ExecuteCommandWithOutput(downloadConfigCmd)
		if err != nil {
			logger.Errorln("wget -q", da.ConfigRemoteUrl, "-O", da.ConfigFileName, "fail", err)
		}
		return err
	}

	return Md5sumCheck(da.RunUser, da.AgentVersionDir, da.TarballFilename, da.Md5Filename)
}
Esempio n. 2
0
func StartDesiredAgent(da *model.DesiredAgent, last *model.DesiredAgent) error {
	if err := InsureRunUserExists(da); nil != err {
		return err
	}

	var lastRunUser, lastWorkDir string
	if nil != last {
		lastRunUser = last.RunUser
		lastWorkDir = last.WorkDir
	}

	if err := StopAgentOf(da, lastRunUser, lastWorkDir); err != nil {
		return err
	}

	if err := InsureNewVersion(da); nil != err {
		return err
	}

	if err := ControlStartIn(da.RunUser, da.AgentVersionDir); err != nil {
		logger.Errorln("ControlStartIn error", err)
		return err
	}

	if err := WriteVersion(da); nil != err {
		logger.Errorln("WriteVersion error", err)
		return err
	}
	return nil
}
Esempio n. 3
0
func QueryRelEndpointGroupList(queryDto QueryRelEndpointGroupDto, pageInfo *PageInfo) ([]RelEndpointGroup, *PageInfo) {
	var rows []RelEndpointGroup
	query := db.NewOrm().QueryTable(RelEndpointGroup{})
	if 0 < queryDto.HostGroupId {
		query = query.Filter("HostGroupId", queryDto.HostGroupId)
	}
	if "" != queryDto.RelType {
		query = query.Filter("RelType", queryDto.RelType)
	}
	if "" != queryDto.PropName {
		query = query.Filter("PropName", queryDto.PropName)
	}
	if "" != queryDto.PropValue {
		query = query.Filter("PropValue", queryDto.PropValue)
	}

	rowCount, err := query.Count()
	if nil != err {
		logger.Errorln("queryCount error", err)
		pageInfo.SetRowCount(0)
		return nil, pageInfo
	}
	pageInfo.SetRowCount(rowCount)

	_, err = query.OrderBy("RelType").Offset(pageInfo.GetStartIndex()).Limit(pageInfo.PageSize).All(&rows)
	if nil != err {
		logger.Errorln("QueryRelEndpointGroupList error", err)
	}
	return rows, pageInfo
}
Esempio n. 4
0
func QueryUserList(queryDto QueryUserDto, pageInfo *PageInfo) ([]User, *PageInfo) {
	var rows []User
	query := db.NewOrm().QueryTable(User{})
	if "" != queryDto.UserName {
		query = query.Filter("user_name__contains", queryDto.UserName)
	}
	if "" != queryDto.RealName {
		query = query.Filter("RealName", queryDto.RealName)
	}
	if "" != queryDto.RoleName {
		query = query.Filter("RoleName", queryDto.RoleName)
	}

	rowCount, err := query.Count()
	if nil != err {
		logger.Errorln("queryCount error", err)
		pageInfo.SetRowCount(0)
		return nil, pageInfo
	}
	pageInfo.SetRowCount(rowCount)

	_, err = query.OrderBy("Id").Offset(pageInfo.GetStartIndex()).Limit(pageInfo.PageSize).All(&rows,
		"UserName", "RealName", "PhoneNo", "Email", "RoleName", "AccountStatus")
	if nil != err {
		logger.Errorln("QueryUserList error", err)
	}
	return rows, pageInfo
}
Esempio n. 5
0
func ModifyRecord(domainId int, recordId, subDomain, strIp string) bool {
	data := url.Values{}

	data.Add("login_email", g.Config().LoginEmail)
	data.Add("login_password", g.Config().LoginPassword)
	data.Add("format", "json")
	data.Add("domain_id", strconv.Itoa(domainId))
	data.Add("record_id", recordId)
	data.Add("sub_domain", subDomain)
	data.Add("record_type", "A")
	data.Add("record_line", "默认")
	data.Add("value", strIp)

	strResponse, err := Post("https://dnsapi.cn/Record.Modify", data)
	if nil != err {
		logger.Errorln("RECORD_MODIFY ERROR", err)
	} else {
		logger.Infoln("RECORD_MODIFY_RESPONSE:", strResponse, err)
		if "" != strResponse && strings.Contains(strResponse, `"code":"1"`) {
			return true
		} else {
			logger.Infoln("domainId:%d,recordId:%s,sub_domain:%s\n", domainId, recordId, subDomain)
		}
	}
	return false
}
Esempio n. 6
0
func (this *HeartbeatController) Post() {
	jsonResult := make(map[string]interface{})
	jsonResult["success"] = false

	var req model.HeartbeatRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); nil != err {
		logger.Errorln("parseForm error", err)
		jsonResult["message"] = fmt.Sprintf("parseForm error: %v", err.Error())
	} else if "" == req.Hostname {
		jsonResult["message"] = "hostname is blank"
	} else {
		logger.Debugln("Heartbeat Request===>>>", req)
		strIp := this.Ctx.Request.Header.Get("X-Forwarded-For")
		if "" == strIp {
			strIp = this.Ctx.Request.Header.Get("Proxy-Client-IP")
		}
		if "" == strIp {
			strIp = this.Ctx.Request.Header.Get("WL-Proxy-Client-IP")
		}
		if "" == strIp {
			ip, _, err := net.SplitHostPort(this.Ctx.Request.RemoteAddr)
			if nil != err {
				logger.Errorln("SplitHostPort error", this.Ctx.Request.RemoteAddr)
			} else {
				strIp = ip
			}
		}

		go store.ParseHeartbeatRequest(&req, strIp)

		resp := model.HeartbeatResponse{
			ErrorMessage:  "",
			DesiredAgents: store.HostAgents.Status(req.Hostname),
		}
		logger.Debugln("Heartbeat Response<<<===", resp)

		this.Data["json"] = &resp
		jsonResult["success"] = true
	}

	if false == jsonResult["success"] {
		this.Data["json"] = &jsonResult
	}

	this.ServeJSON()
}
Esempio n. 7
0
func QueryEndpointAgentList(hostname string) []EndpointAgent {
	var rows []EndpointAgent
	_, err := db.NewOrm().QueryTable(EndpointAgent{}).Filter("Hostname", hostname).All(&rows)
	if nil != err {
		logger.Errorln("QueryEndpointAgentList error", err)
	}
	return rows
}
Esempio n. 8
0
func QueryRelAgentGroupList(agentId int64) ([]RelAgentGroupDto, error) {
	var rows []RelAgentGroupDto
	_, err := db.NewOrm().Raw("select t.id, t.gmt_create,t.gmt_modified, t.host_group_id, a.group_name from t_rel_agent_group t, t_host_group a where t.agent_id=? and t.host_group_id=a.id", agentId).QueryRows(&rows)
	if nil != err {
		logger.Errorln("QueryRelAgentGroupList error", err)
	}
	return rows, err
}
Esempio n. 9
0
func GetUserById(userId int64) *User {
	var u User
	strSql := `select id, user_name, real_name, phone_no, email, role_name, account_status from t_user where id=?`
	err := db.NewOrm().Raw(strSql, userId).QueryRow(&u)
	if nil != err {
		logger.Errorln("query error", err)
	}
	return &u
}
Esempio n. 10
0
func (this *User) Update() (int64, error) {
	strSql := `update t_user set real_name=?, phone_no=?, email=?, role_name=? where id=?`
	result, err := db.NewOrm().Raw(strSql, this.RealName, this.PhoneNo, this.Email, this.RoleName, this.Id).Exec()
	if nil != err {
		logger.Errorln("update error", err)
		return 0, err
	}
	return result.RowsAffected()
}
Esempio n. 11
0
func ReadDesiredAgents() []*model.DesiredAgent {
	var desiredAgents []*model.DesiredAgent
	strJson, err := f.ToTrimString(path.Join(f.SelfDir(), "desired_agent.json"))
	if nil != err {
		logger.Errorln("read desired agent file error", err)
		return desiredAgents
	}
	err = json.Unmarshal([]byte(strJson), &desiredAgents)
	if nil != err {
		logger.Errorln("unmarshal json error", strJson, err)
	}

	for _, da := range desiredAgents {
		actualVersion := ReadVersion(da.RunUser, path.Join(da.WorkDir, da.Name))
		if "" != actualVersion {
			da.Version = actualVersion
		}
	}
	return desiredAgents
}
Esempio n. 12
0
func QueryHostGroupList(queryDto QueryHostGroupDto, pageInfo *PageInfo) ([]HostGroup, *PageInfo) {
	var rows []HostGroup
	query := db.NewOrm().QueryTable(HostGroup{})
	if "" != queryDto.GroupName {
		query = query.Filter("GroupName__contains", queryDto.GroupName)
	}

	rowCount, err := query.Count()
	if nil != err {
		logger.Errorln("queryCount error", err)
		pageInfo.SetRowCount(0)
		return nil, pageInfo
	}
	pageInfo.SetRowCount(rowCount)

	_, err = query.OrderBy("GroupName").Offset(pageInfo.GetStartIndex()).Limit(pageInfo.PageSize).All(&rows)
	if nil != err {
		logger.Errorln("QueryHostGroupList error", err)
	}
	return rows, pageInfo
}
Esempio n. 13
0
func SelectSessionBySig(sig string) *Session {
	if sig == "" {
		return nil
	}

	obj := Session{Sig: sig}
	err := orm.NewOrm().Read(&obj, "Sig")
	if err != nil {
		if err != orm.ErrNoRows {
			logger.Errorln(err)
		}
		return nil
	}
	return &obj
}
Esempio n. 14
0
func QueryEndpointList(queryDto QueryEndpointDto, pageInfo *PageInfo) ([]Endpoint, *PageInfo) {
	var rows []Endpoint
	query := db.NewOrm().QueryTable(Endpoint{})
	if "" != queryDto.Hostname {
		query = query.Filter("hostname__icontains", queryDto.Hostname)
	}
	if "" != queryDto.Ip {
		query = query.Filter("ip__contains", queryDto.Ip)
	}

	rowCount, err := query.Count()
	if nil != err {
		logger.Errorln("queryCount error", err)
		pageInfo.SetRowCount(0)
		return nil, pageInfo
	}
	pageInfo.SetRowCount(rowCount)

	_, err = query.OrderBy("-GmtModified").Offset(pageInfo.GetStartIndex()).Limit(pageInfo.PageSize).All(&rows)
	if nil != err {
		logger.Errorln("QueryEndpointList error", err)
	}
	return rows, pageInfo
}
Esempio n. 15
0
func SelectUserById(id int64) *User {
	if id <= 0 {
		return nil
	}

	obj := User{Id: id}
	err := orm.NewOrm().Read(&obj, "Id")
	if err != nil {
		if err != orm.ErrNoRows {
			logger.Errorln(err)
		}
		return nil
	}
	return &obj
}
Esempio n. 16
0
func QueryAgentList(queryDto QueryAgentDto, pageInfo *PageInfo) ([]Agent, *PageInfo) {
	var rows []Agent
	query := db.NewOrm().QueryTable(Agent{})
	if "" != queryDto.AgentName {
		query = query.Filter("Name", queryDto.AgentName)
	}
	if "" != queryDto.RunUser {
		query = query.Filter("RunUser", queryDto.RunUser)
	}

	rowCount, err := query.Count()
	if nil != err {
		logger.Errorln("queryCount error", err)
		pageInfo.SetRowCount(0)
		return nil, pageInfo
	}
	pageInfo.SetRowCount(rowCount)

	_, err = query.OrderBy("Name").Offset(pageInfo.GetStartIndex()).Limit(pageInfo.PageSize).All(&rows)
	if nil != err {
		logger.Errorln("QueryAgentList error", err)
	}
	return rows, pageInfo
}
Esempio n. 17
0
func heartbeat() {
	hostname, err := utils.Hostname(g.Config().Hostname)
	if err != nil {
		return
	}

	desiredAgents := ReadDesiredAgents()
	heartbeatRequest := BuildHeartbeatRequest(hostname, desiredAgents)
	logger.Debugln("===>>>", heartbeatRequest)

	bs, err := json.Marshal(heartbeatRequest)
	if err != nil {
		logger.Errorln("encode heartbeat request fail", err)
		return
	}

	url := fmt.Sprintf("http://%s/api/heartbeat", g.Config().Server)
	httpRequest := httplib.Post(url).SetTimeout(time.Second*10, time.Minute)
	httpRequest.Body(bs)
	httpResponse, err := httpRequest.Bytes()
	if err != nil {
		log.Printf("curl %s fail %v", url, err)
		return
	}

	var heartbeatResponse model.HeartbeatResponse
	err = json.Unmarshal(httpResponse, &heartbeatResponse)
	if err != nil {
		logger.Errorln("decode heartbeat response fail", err)
		return
	}

	logger.Debugln("<<<<====", heartbeatResponse)

	HandleHeartbeatResponse(&heartbeatResponse, desiredAgents)
}
Esempio n. 18
0
func CheckFileOrDirExists(username, fp, fileType string) bool {
	if CurrentUser == username {
		return file.IsExist(fp)
	} else {
		cmd := BuildCommand(username, "sh", "check_file.sh", fileType, fp)
		cmd.Dir = file.SelfDir()
		strOut, err := ExecuteCommandWithOutput(cmd)
		if nil != err {
			logger.Errorln("check dir exists", strOut, err)
			return false
		}
		result, _ := strconv.ParseBool(strings.Replace(strOut, "\n", "", -1))
		return result
	}
	return false
}
Esempio n. 19
0
func BuildHeartbeatRequest(hostname string, desiredAgents []*model.DesiredAgent) model.HeartbeatRequest {
	req := model.HeartbeatRequest{Hostname: hostname}
	req.Ip = utils.GetLocalIP()
	req.UpdaterVersion = g.VERSION
	req.RunUser = CurrentUser

	realAgents := []*model.RealAgent{}
	now := time.Now().Unix()

	for _, da := range desiredAgents {
		agentDir := path.Join(da.WorkDir, da.Name)
		// 如果目录下没有.version,我们认为这根本不是一个agent
		version := ReadVersion(da.RunUser, agentDir)
		if "" == version {
			logger.Error("read %s/.version fail\n", agentDir)
			continue
		}

		controlFile := path.Join(agentDir, version, "control")
		if !CheckFileExists(da.RunUser, controlFile) {
			logger.Errorln(controlFile, "is nonexistent, user:"******"control"), "status")
		cmd.Dir = f.SelfDir()
		status, err := ExecuteCommandWithOutput(cmd)
		if err != nil {
			status = err.Error()
		}

		realAgent := &model.RealAgent{
			Name:      da.Name,
			Version:   version,
			Status:    status,
			Timestamp: now,
		}
		realAgent.RunUser = da.RunUser
		realAgent.WorkDir = da.WorkDir

		realAgents = append(realAgents, realAgent)
	}

	req.RealAgents = realAgents
	return req
}
Esempio n. 20
0
func (this *AgentController) Query() {
	jsonResult := make(map[string]interface{})

	pageInfo := models.NewPageInfo(this.Ctx.Request)

	queryDto := models.QueryAgentDto{}
	if err := this.ParseForm(&queryDto); nil != err {
		logger.Errorln("parseForm error", err)
	}
	list, pageInfo := models.QueryAgentList(queryDto, pageInfo)

	jsonResult["items"] = list
	jsonResult["total"] = pageInfo.RowCount

	this.Data["json"] = &jsonResult
	this.ServeJSON()
}
Esempio n. 21
0
func (this *Endpoint) Insert() (int64, error) {
	num, err := db.NewOrm().QueryTable(Endpoint{}).Filter("Hostname", this.Hostname).Count()
	if nil != err {
		return 0, err
	} else if 0 == num {
		this.GmtCreate = time.Now()
		this.GmtModified = time.Now()
		return db.NewOrm().Insert(this)
	} else {
		strSql := `update t_endpoint set gmt_modified=?, ip = ?, updater_version = ?, run_user = ? where hostname=?`
		result, err := db.NewOrm().Raw(strSql, utils.FormatUTCTime(time.Now()), this.Ip, this.UpdaterVersion, this.RunUser, this.Hostname).Exec()
		if nil != err {
			logger.Errorln("insert endpoint fail: ", err)
			return 0, err
		}
		return result.RowsAffected()
	}
}
Esempio n. 22
0
func (this *LoginController) Post() {
	jsonResult := make(map[string]interface{})
	jsonResult["success"] = false
	userName := this.GetString("userName")
	loginPwd := this.GetString("loginPwd")

	if "" == userName {
		jsonResult["message"] = "用户名不能为空"
	} else if "" == loginPwd {
		jsonResult["message"] = "登陆密码不能为空"
	} else {
		user, err := models.CheckLogin(userName, loginPwd)
		if nil != err {
			if "<QuerySeter> no row found" == err.Error() {
				jsonResult["message"] = "用户名或密码错误"
			} else {
				logger.Errorln("checkLogin error", userName, loginPwd, err)
				jsonResult["message"] = err.Error()
			}
		} else if nil == user {
			jsonResult["message"] = "用户名或密码错误"
		} else {
			this.SetSession(cache.SESSION_UID, user.Id)
			this.SetSession(cache.SESSION_USERNAME, user.RealName)
			this.SetSession(cache.SESSION_ROLENAME, user.RoleName)
			jsonResult["success"] = true

			originURL := this.GetString("originURL")
			if "" == originURL {
				originURL = ""
			}
			jsonResult["message"] = originURL
		}
	}

	this.Data["json"] = &jsonResult
	this.ServeJSON()
}
Esempio n. 23
0
func GetDomainList() []g.DomainResult {
	data := url.Values{}

	data.Add("login_email", g.Config().LoginEmail)
	data.Add("login_password", g.Config().LoginPassword)
	data.Add("format", "json")

	strResponse, err := Post("https://dnsapi.cn/Domain.List", data)
	if nil == err && "" != strResponse && strings.Contains(strResponse, `"code":"1"`) {
		var dlr g.DomainListResult
		err = json.Unmarshal(bytes.NewBufferString(strResponse).Bytes(), &dlr)
		if nil != err {
			logger.Errorln("decode DOMAIN_LIST response fail %v\n", err)
		} else {
			for _, domain := range dlr.Domains {
				logger.Info("domain_id:%d,name:%s,created:%v,updated:%v\n", domain.Id, domain.Name, domain.Created, domain.Updated)
			}
			return dlr.Domains
		}
	} else {
		logger.Error("GET_DOMAIN_LIST RESPONSE<<<====%s, error: %v", strResponse, err)
	}
	return nil
}
Esempio n. 24
0
func getIp() string {
	strUrl := g.Config().GetIpApi
	logger.Debug("REQUEST_URL:%s\n", strUrl)
	httpRequest := httplib.Get(strUrl).SetTimeout(3*time.Second, 10*time.Second)
	httpResponse, err := httpRequest.Bytes()
	if nil != err {
		logger.Errorln("GET_IP error", err)
		return ""
	}

	strIp := ""
	var resp g.ServletResponse
	err = json.Unmarshal(httpResponse, &resp)
	if err != nil {
		logger.Error("decode GET_IP response fail %v\n", err)
	} else if false == resp.Success {
		logger.Error("GET_IP fail %s\n", resp.Message)
	} else {
		strIp = resp.Message
	}

	logger.Infoln("RESPONSE_IP:", strIp)
	return strIp
}
Esempio n. 25
0
func (this *AgentController) Save() {
	jsonResult := g.JsonResult{}

	item := models.Agent{}
	if err := this.ParseForm(&item); nil != err {
		logger.Errorln("parseForm error", err)
		jsonResult.Message = err.Error()
	} else {
		if 0 >= item.Id {
			item.GmtCreate = time.Now()
		}
		result, err := item.Insert()
		if nil != err {
			jsonResult.Message = err.Error()
		} else if 0 < result {
			jsonResult.Success = true
		} else {
			jsonResult.Message = "保存失败,未知错误"
		}
	}

	this.Data["json"] = &jsonResult
	this.ServeJSON()
}