Exemplo n.º 1
0
Arquivo: ota_jobs.go Projeto: jsli/ota
func (rrj *ReleaseRemoveJob) Run() {
	dal, err := models.NewDal()
	if err != nil {
		return
	}
	defer dal.Close()

	//0. delete all dropped records
	delete_sql := fmt.Sprintf("DELETE FROM %s where flag=%d", ota_constant.TABLE_RADIO_OTA_RELEASE, ota_constant.FLAG_DROPPED)
	models.DeleteRadioRelease(dal, delete_sql)
	delete_sql = fmt.Sprintf("DELETE FROM %s where flag=%d", ota_constant.TABLE_RELEASE_CREATION_TASK, ota_constant.FLAG_DROPPED)
	models.DeleteReleaseCreationTask(dal, delete_sql)

	//1. walk through release directory, filter un-record release
	fileInfos, err := ioutil.ReadDir(ota_constant.RADIO_OTA_RELEASE_ROOT)
	if err != nil {
		return
	}
	for _, info := range fileInfos {
		path := fmt.Sprintf("%s%s", ota_constant.RADIO_OTA_RELEASE_ROOT, info.Name())
		if info.IsDir() {
			release, err := models.FindRadioOtaReleaseByFp(dal, info.Name())
			if err != nil {
				continue
			}
			if release == nil {
				//				fmt.Println("delete dir - ", path)
				file.DeleteDir(path)
			}
		} else if info.Mode().IsRegular() {
			//			fmt.Println("delete file - ", path)
			file.DeleteFile(path)
		}
	}

	//2. traverse db, filter the records missing files
	releases, err := models.FindRadioOtaReleaseList(dal, ota_constant.FLAG_AVAILABLE)
	for _, release := range releases {
		path := fmt.Sprintf("%s%s", ota_constant.RADIO_OTA_RELEASE_ROOT, release.FingerPrint)
		exist, err := file.IsDirExist(path)
		if err != nil {
			continue
		}
		if !exist {
			//			fmt.Println("not existed : ", path)
			release.Delete(dal)
		}
	}
}
Exemplo n.º 2
0
Arquivo: ota_jobs.go Projeto: jsli/ota
func (rcj *ReleaseCreationJob) Run() {
	tag := "JOB_D: "
	revel.INFO.Println(tag, "running")
	dal, err := models.NewDal()
	if err != nil {
		revel.ERROR.Println(tag, err)
		return
	}
	defer dal.Close()

	task, err := models.PopOneCreationTask(dal)
	if err != nil {
		revel.ERROR.Println(tag, err)
		return
	}

	if task != nil {
		task.Flag = ota_constant.FLAG_CREATING
		task.ModifiedTs = time.Now().Unix()
		task.Update(dal)

		root_path := fmt.Sprintf("%s%s/", ota_constant.TMP_FILE_ROOT, policy.GenerateRandFileName())
		pathutil.MkDir(root_path)
		defer file.DeleteDir(root_path)
		revel.INFO.Println(tag, "Processing task : ", task.UpdateRequest)
		revel.INFO.Println(tag, "TEMP dir : ", root_path)

		release, err := policy.GenerateOtaPackage(dal, task, root_path)
		if err != nil || release == nil {
			revel.ERROR.Println(tag, "Failed: ", err)
			revel.INFO.Println(tag, "Failed, task id= ", task.Id, " error msg: ", err)
			if task.RetryCount >= ota_constant.RETRY_COUNT {
				task.Flag = ota_constant.FLAG_DROPPED
			} else {
				task.RetryCount = task.RetryCount + 1
				task.Flag = ota_constant.FLAG_INIT
				revel.INFO.Println(tag, "Failed, task id= ", task.Id, " retry: ", task.RetryCount)
			}
			task.ModifiedTs = time.Now().Unix()
			_, uerr := task.Update(dal)
			revel.ERROR.Println(tag, " task UPDATE Failed: ", uerr)
			return
		}

		task.Delete(dal)
	}
}
Exemplo n.º 3
0
Arquivo: radio.go Projeto: jsli/ota
func (c Radio) OtaCreate() revel.Result {
	result := models.NewResult()
	validator := &policy.RadioValidator{}
	dtim_info, err := validator.ValidateAndParseRadioDtim(c.Params)
	if err != nil {
		return c.Render400(result, err)
	}

	update_request, err := validator.ValidateUpdateRequest(c.Params)
	if err != nil {
		return c.Render400WithCode(result, ota_constant.ERROR_CODE_INVALIDATED_REQUEST,
			fmt.Sprintf(ota_constant.ERROR_MSG_NO_ILLEGAL_REQUEST, update_request))
	}

	err = validator.CompareRequestAndDtim(update_request, dtim_info)
	if err != nil {
		return c.Render400(result, err)
	}

	update_request.Cps = policy.SortCps(update_request)
	request_json_byte, err := json.Marshal(update_request)
	if err != nil {
		return c.Render400(result, err)
	}
	request_json := string(request_json_byte)

	dal, err := models.NewDal()
	if err != nil {
		return c.Render500(result, err)
	}
	defer dal.Close()

	sorted_image_list := policy.GenerateImageList(update_request)
	fp := policy.GenerateOtaPackageFingerPrint(sorted_image_list)
	fp = fmt.Sprintf("%s.%s.%s", update_request.Device.Model, update_request.Device.Platform, fp)

	radio, err := c.Provider.ProvideRadioRelease(dal, dtim_info, fp)
	if err != nil {
		return c.Render500(result, err)
	}

	if radio == nil {
		task, err := models.FindReleaseCreationTaskByFp(dal, fp)
		if err != nil {
		}
		var (
			err_code int
			err_msg  string
		)
		if task == nil {
			task := &models.ReleaseCreationTask{}
			task.Flag = ota_constant.FLAG_INIT
			task.UpdateRequest = request_json
			task.Data = dtim_info.BinaryData
			task.Model = update_request.Device.Model
			task.Platform = update_request.Device.Platform
			task.FingerPrint = fp
			task.CreatedTs = time.Now().Unix()
			task.ModifiedTs = task.CreatedTs

			id, err := task.Save(dal)
			if id < 0 || err != nil {
				err_code = ota_constant.ERROR_CODE_CREATE_REQUEST_FAILED
				err_msg = "Create creation task Failed, try later"
			} else {
				err_code = ota_constant.ERROR_CODE_NOT_CREATED
				err_msg = "Update package will be created later"
			}
		} else {
			switch task.Flag {
			case ota_constant.FLAG_DISABLE:
				fallthrough
			case ota_constant.FLAG_DROPPED:
				err_code = ota_constant.ERROR_CODE_DROPPED
				err_msg = "Bad creation task, drop it"
			case ota_constant.FLAG_AVAILABLE:
				fallthrough
			case ota_constant.FLAG_INIT:
				err_code = ota_constant.ERROR_CODE_NOT_CREATED
				err_msg = "Update package will be created later"
			case ota_constant.FLAG_CREATING:
				err_code = ota_constant.ERROR_CODE_CREATING
				err_msg = "Update package is creating"
			case ota_constant.FLAG_CREATED:
				err_code = ota_constant.ERROR_CODE_NOERR
				err_msg = ""
			case ota_constant.FLAG_CREATE_FAILED:
				err_code = ota_constant.ERROR_CODE_CREATE_REQUEST_FAILED
				err_msg = "Update package created failed"
			default:
				err_code = ota_constant.ERROR_CODE_NOERR
				err_msg = ""
			}
		}
		return c.Render404WithCode(result, err_code, err_msg)
	} else {
		data := models.ReleaseResultData{}
		data.Url = fmt.Sprintf("http://%s/static/%s/%s", c.Request.Host, radio.FingerPrint, ota_constant.RADIO_OTA_PACKAGE_NAME)
		data.Md5 = radio.Md5
		data.Size = radio.Size
		data.CreatedTime = policy.FormatTime(radio.CreatedTs)
		result.SetData(data)
	}

	return c.RenderJson(result)
}