예제 #1
0
func DeleteFiles(resultItems []utils.AqlSearchResultItem, flags *DeleteFlags) error {
	for _, v := range resultItems {
		fileUrl, err := utils.BuildArtifactoryUrl(flags.ArtDetails.Url, v.GetFullUrl(), make(map[string]string))
		if err != nil {
			return err
		}
		if flags.DryRun {
			log.Info("[Dry run] Deleting:", v.GetFullUrl())
			continue
		}

		log.Info("Deleting:", v.GetFullUrl())
		httpClientsDetails := utils.GetArtifactoryHttpClientDetails(flags.ArtDetails)
		resp, body, err := ioutils.SendDelete(fileUrl, nil, httpClientsDetails)
		if err != nil {
			return err
		}
		if resp.StatusCode != 204 {
			return cliutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + cliutils.IndentJson(body)))
		}

		log.Debug("Artifactory response:", resp.Status)
	}
	return nil
}
예제 #2
0
func uploadWildcard(artifacts []UploadData, flags *UploadFlags) (buildInfoArtifacts [][]utils.ArtifactBuildInfo, totalUploaded, totalFailed int, err error) {
	minChecksumDeploySize, e := getMinChecksumDeploySize()
	if e != nil {
		err = e
		return
	}

	size := len(artifacts)
	var wg sync.WaitGroup

	// Create an array of integers, to store the total file that were uploaded successfully.
	// Each array item is used by a single thread.
	uploadCount := make([]int, flags.Threads, flags.Threads)
	buildInfoArtifacts = make([][]utils.ArtifactBuildInfo, flags.Threads)
	for i := 0; i < flags.Threads; i++ {
		wg.Add(1)
		go func(threadId int) {
			logMsgPrefix := cliutils.GetLogMsgPrefix(threadId, flags.DryRun)
			for j := threadId; j < size && err == nil; j += flags.Threads {
				var e error
				var uploaded bool
				var target string
				var buildInfoArtifact utils.ArtifactBuildInfo
				target, e = utils.BuildArtifactoryUrl(flags.ArtDetails.Url, artifacts[j].Artifact.TargetPath, make(map[string]string))
				if e != nil {
					err = e
					break
				}
				buildInfoArtifact, uploaded, e = uploadFile(artifacts[j].Artifact.LocalPath, target, artifacts[j].Props, flags, minChecksumDeploySize, logMsgPrefix)
				if e != nil {
					err = e
					break
				}
				if uploaded {
					uploadCount[threadId]++
					buildInfoArtifacts[threadId] = append(buildInfoArtifacts[threadId], buildInfoArtifact)
				}
			}
			wg.Done()
		}(i)
	}
	wg.Wait()
	if err != nil {
		return
	}
	totalUploaded = 0
	for _, i := range uploadCount {
		totalUploaded += i
	}

	log.Info("Uploaded", strconv.Itoa(totalUploaded), "artifacts.")
	totalFailed = size - totalUploaded
	if totalFailed > 0 {
		log.Error("Failed uploading", strconv.Itoa(totalFailed), "artifacts.")
	}
	return
}
예제 #3
0
func BuildDistribute(buildName, buildNumber, targetRepo string, flags *BuildDistributionFlags) error {
	err := utils.PreCommandSetup(flags)
	if err != nil {
		return err
	}

	dryRun := ""
	if flags.DryRun == true {
		dryRun = "[Dry run] "
	}
	message := "Destributing build..."
	log.Info(dryRun + message)

	distributeUrl := flags.ArtDetails.Url
	restApi := path.Join("api/build/distribute/", buildName, buildNumber)
	requestFullUrl, err := utils.BuildArtifactoryUrl(distributeUrl, restApi, make(map[string]string))
	if err != nil {
		return err
	}

	data := BuildDistributionConfig{
		SourceRepos:           strings.Split(flags.SourceRepos, ","),
		TargetRepo:            targetRepo,
		Publish:               flags.Publish,
		OverrideExistingFiles: flags.OverrideExistingFiles,
		GpgPassphrase:         flags.GpgPassphrase,
		Async:                 flags.Async,
		DryRun:                flags.DryRun}
	requestContent, err := json.Marshal(data)
	if err != nil {
		return cliutils.CheckError(errors.New("Failed to execute request. " + cliutils.GetDocumentationMessage()))
	}

	httpClientsDetails := utils.GetArtifactoryHttpClientDetails(flags.ArtDetails)
	utils.SetContentType("application/json", &httpClientsDetails.Headers)

	resp, body, err := ioutils.SendPost(requestFullUrl, requestContent, httpClientsDetails)
	if err != nil {
		return err
	}
	if resp.StatusCode != 200 {
		return cliutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + cliutils.IndentJson(body)))
	}

	log.Debug("Artifactory response:", resp.Status)
	if flags.Async && !flags.DryRun {
		log.Info("Asynchronously distributed build", buildName, "#"+buildNumber, "to:", targetRepo, "repository, logs are avalable in Artifactory.")
		return nil
	}

	log.Info(dryRun+"Distributed build", buildName, "#"+buildNumber, "to:", targetRepo, "repository.")
	return nil
}
예제 #4
0
func downloadAqlResult(dependecies []DownloadData, flags *DownloadFlags) (buildDependencies [][]utils.DependenciesBuildInfo, err error) {
	size := len(dependecies)
	buildDependencies = make([][]utils.DependenciesBuildInfo, flags.Threads)
	var wg sync.WaitGroup
	for i := 0; i < flags.Threads; i++ {
		wg.Add(1)
		go func(threadId int) {
			logMsgPrefix := cliutils.GetLogMsgPrefix(threadId, flags.DryRun)
			for j := threadId; j < size && err == nil; j += flags.Threads {
				downloadPath, e := utils.BuildArtifactoryUrl(flags.ArtDetails.Url, dependecies[j].Dependency.GetFullUrl(), make(map[string]string))
				if e != nil {
					err = e
					break
				}
				log.Info(logMsgPrefix+"Downloading", dependecies[j].Dependency.GetFullUrl())
				if flags.DryRun {
					continue
				}

				regexpPattern := cliutils.PathToRegExp(dependecies[j].DownloadPath)
				placeHolderTarget, e := cliutils.ReformatRegexp(regexpPattern, dependecies[j].Dependency.GetFullUrl(), dependecies[j].Target)
				if e != nil {
					err = e
					break
				}
				localPath, localFileName := ioutils.GetLocalPathAndFile(dependecies[j].Dependency.Name, dependecies[j].Dependency.Path, placeHolderTarget, dependecies[j].Flat)
				shouldDownload, e := shouldDownloadFile(path.Join(localPath, dependecies[j].Dependency.Name), dependecies[j].Dependency.Actual_Md5, dependecies[j].Dependency.Actual_Sha1)
				if e != nil {
					err = e
					break
				}
				dependency := createBuildDependencyItem(dependecies[j].Dependency)
				if !shouldDownload {
					buildDependencies[threadId] = append(buildDependencies[threadId], dependency)
					log.Debug(logMsgPrefix, "File already exists locally.")
					continue
				}

				downloadFileDetails := createDownloadFileDetails(downloadPath, localPath, localFileName, nil, dependecies[j].Dependency.Size)
				e = downloadFile(downloadFileDetails, logMsgPrefix, flags)
				if e != nil {
					err = e
					break
				}
				buildDependencies[threadId] = append(buildDependencies[threadId], dependency)
			}
			wg.Done()
		}(i)
	}
	wg.Wait()
	logDownloadTotals(buildDependencies)
	return
}