示例#1
0
文件: ota_jobs.go 项目: 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)
		}
	}
}
示例#2
0
文件: ota_jobs.go 项目: 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)
	}
}
示例#3
0
文件: generator.go 项目: jsli/ota
func GenerateOtaPackage(dal *models.Dal, task *models.ReleaseCreationTask, root_path string) (*models.RadioOtaRelease, error) {
	//0.parse request from json string which stored in DB.
	update_request, err := ParseRequest(task.UpdateRequest)
	if err != nil {
		return nil, err
	}
	update_request.Cps = SortCps(update_request)

	//1.create all paths
	zip_path := fmt.Sprintf("%s%s", root_path, ota_constant.ZIP_DIR_NAME)
	pathutil.MkDir(zip_path)
	template_path := ota_constant.MODEL_TO_TEMPLATE[update_request.Device.Model]
	update_pkg_path := fmt.Sprintf("%s%s", root_path, ota_constant.UPDATE_PKG_NAME)
	radio_ota_path := fmt.Sprintf("%s%s", root_path, ota_constant.RADIO_OTA_PACKAGE_NAME)
	radio_dtim_path := fmt.Sprintf("%s%s", root_path, ota_constant.RADIO_DTIM_NAME)
	radio_image_path := fmt.Sprintf("%s%s", root_path, ota_constant.RADIO_IMAGE_NAME)

	//2.copy template files
	err = file.CopyDir(template_path, zip_path)
	if err != nil {
		return nil, err
	}

	//3.create Radio.dtim, Radio.img
	err = file.WriteBytes2File(task.Data, radio_dtim_path)
	if err != nil {
		return nil, err
	}

	image_list_final := make([]string, 0, 5)
	image_list := GenerateImageList(update_request)
	for _, image_rel_path := range image_list {
		dest_path := root_path + image_rel_path
		_, err := file.CopyFile(ota_constant.CP_RELEASE_ROOT_FINAL+image_rel_path, dest_path)
		if err != nil {
			return nil, err
		}
		image_list_final = append(image_list_final, dest_path)
	}

	err = gzipCpImage(image_list_final)
	if err != nil {
		revel.INFO.Printf("zip image error : %s\n", err)
		return nil, err
	}
	for _, image_path := range image_list_final {
		file.CopyFile(image_path+".gz", image_path)
	}

	err = generateRadioImage(radio_dtim_path, radio_image_path, image_list_final)
	if err != nil {
		return nil, err
	}
	_, err = file.CopyFile(radio_image_path, fmt.Sprintf("%s%s", zip_path, ota_constant.RADIO_IMAGE_NAME))

	//	4. archive all files
	err = archive.ArchiveZipFile(zip_path, update_pkg_path)
	if err != nil {
		return nil, err
	}

	//5.generate update.zip (updatetool + update_pkg.zip)
	params := make([]string, 5)
	params[0] = ota_constant.OTA_CMD_PARAM_PLATFORM_PREFIX + update_request.Device.Platform
	params[1] = ota_constant.OTA_CMD_PARAM_PRODUCT_PREFIX + update_request.Device.Model
	params[2] = ota_constant.OTA_CMD_PARAM_OEM_PREFIX
	params[3] = ota_constant.OTA_CMD_PARAM_OUTPUT_PREFIX + radio_ota_path
	params[4] = ota_constant.OTA_CMD_PARAM_INPUT_PREFIX + update_pkg_path
	err = generateOtaPackage(params)
	if err != nil {
		return nil, err
	}

	//6.insert db
	release := &models.RadioOtaRelease{}
	release.Flag = ota_constant.FLAG_AVAILABLE
	release.FingerPrint = task.FingerPrint
	release.ReleaseNote = "empty"
	release.CreatedTs = time.Now().Unix()
	release.ModifiedTs = release.CreatedTs
	release.Model = update_request.Device.Model
	release.Platform = update_request.Device.Platform
	release.Md5, err = file.Md5SumFile(radio_ota_path)
	release.Versions = GenerateOtaPackageVersions(update_request)
	release.Images = strings.Join(image_list, " ")
	if err != nil {
		return nil, err
	}
	release.Size, err = file.GetFileSize(radio_ota_path)
	if err != nil {
		return nil, err
	}

	release.Delete(dal)
	file.DeleteDir(fmt.Sprintf("%s%s", ota_constant.RADIO_OTA_RELEASE_ROOT, release.FingerPrint))
	id, err := release.Save(dal)
	if id < 0 || err != nil {
		return nil, err
	}
	release.Id = id

	//	7.copy final file to public directory, checksum
	final_dir := fmt.Sprintf("%s%s/", ota_constant.RADIO_OTA_RELEASE_ROOT, release.FingerPrint)
	pathutil.MkDir(final_dir)
	final_path := fmt.Sprintf("%s%s", final_dir, ota_constant.RADIO_OTA_PACKAGE_NAME)
	checksum_path := fmt.Sprintf("%s%s", final_dir, ota_constant.CHECKSUM_TXT_NAME)
	_, err = file.CopyFile(radio_ota_path, final_path)
	if err != nil {
		return nil, err
	}
	RecordMd5(final_path, checksum_path)

	return release, nil
}