Example #1
0
File: handler.go Project: utako/atc
func FetchTemplateData(jobDB JobDB, jobName string) (TemplateData, error) {
	config, _, err := jobDB.GetConfig()
	if err != nil {
		return TemplateData{}, err
	}

	job, found := config.Jobs.Lookup(jobName)
	if !found {
		return TemplateData{}, ErrJobConfigNotFound
	}

	bs, err := jobDB.GetAllJobBuilds(job.Name)
	if err != nil {
		return TemplateData{}, err
	}

	var bsr []BuildWithInputsOutputs

	for _, build := range bs {
		inputs, outputs, err := jobDB.GetBuildResources(build.ID)
		if err != nil {
			return TemplateData{}, err
		}

		bsr = append(bsr, BuildWithInputsOutputs{
			Build:   build,
			Inputs:  inputs,
			Outputs: outputs,
		})
	}

	currentBuild, err := jobDB.GetCurrentBuild(job.Name)
	if err != nil {
		currentBuild.Status = db.StatusPending
	}

	dbJob, err := jobDB.GetJob(job.Name)
	if err != nil {
		return TemplateData{}, err
	}

	return TemplateData{
		Job:    job,
		DBJob:  dbJob,
		Builds: bsr,

		GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
			for _, groupJob := range g.Jobs {
				if groupJob == job.Name {
					return true
				}
			}

			return false
		}),

		CurrentBuild: currentBuild,
		PipelineName: jobDB.GetPipelineName(),
	}, nil
}
Example #2
0
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
	client := handler.clientFactory.Build(r)

	pipelineName := r.FormValue(":pipeline")

	pipeline, found, err := client.Pipeline(pipelineName)
	if err != nil {
		handler.logger.Error("failed-to-load-config", err)
		return err
	}

	if !found {
		w.WriteHeader(http.StatusNotFound)
		return nil
	}

	groups := map[string]bool{}
	for _, group := range pipeline.Groups {
		groups[group.Name] = false
	}

	enabledGroups, found := r.URL.Query()["groups"]
	if !found && len(pipeline.Groups) > 0 {
		enabledGroups = []string{pipeline.Groups[0].Name}
	}

	for _, name := range enabledGroups {
		groups[name] = true
	}

	data := TemplateData{
		Groups: groups,
		GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
			return groups[g.Name]
		}),
		PipelineName: pipelineName,
	}

	log := handler.logger.Session("index")

	err = handler.template.Execute(w, data)
	if err != nil {
		log.Fatal("failed-to-build-template", err, lager.Data{
			"template-data": data,
		})
		return err
	}

	return nil
}
Example #3
0
func FetchTemplateData(jobDB JobDB, jobName string) (TemplateData, error) {
	config, _, err := jobDB.GetConfig()
	if err != nil {
		return TemplateData{}, err
	}

	job, found := config.Jobs.Lookup(jobName)
	if !found {
		return TemplateData{}, ErrJobConfigNotFound
	}

	bs, err := jobDB.GetAllJobBuilds(job.Name)
	if err != nil {
		return TemplateData{}, err
	}

	currentBuild, err := jobDB.GetCurrentBuild(job.Name)
	if err != nil {
		currentBuild.Status = db.StatusPending
	}

	dbJob, err := jobDB.GetJob(job.Name)
	if err != nil {
		return TemplateData{}, err
	}

	return TemplateData{
		Job:    job,
		DBJob:  dbJob,
		Builds: bs,

		GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
			for _, groupJob := range g.Jobs {
				if groupJob == job.Name {
					return true
				}
			}

			return false
		}),

		CurrentBuild: currentBuild,
		PipelineName: jobDB.GetPipelineName(),
	}, nil
}
Example #4
0
File: handler.go Project: utako/atc
func (server *server) GetPipeline(pipelineDB db.PipelineDB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		config, _, err := pipelineDB.GetConfig()
		if err != nil {
			server.logger.Error("failed-to-load-config", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		groups := map[string]bool{}
		for _, group := range config.Groups {
			groups[group.Name] = false
		}

		enabledGroups, found := r.URL.Query()["groups"]
		if !found && len(config.Groups) > 0 {
			enabledGroups = []string{config.Groups[0].Name}
		}

		for _, name := range enabledGroups {
			groups[name] = true
		}

		data := TemplateData{
			Groups: groups,
			GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
				return groups[g.Name]
			}),
			PipelineName: pipelineDB.GetPipelineName(),
		}

		log := server.logger.Session("index")

		err = server.template.Execute(w, data)
		if err != nil {
			log.Fatal("failed-to-task-template", err, lager.Data{
				"template-data": data,
			})
		}
	})
}
Example #5
0
func FetchTemplateData(resourceDB ResourcesDB, resourceName string) (TemplateData, error) {
	config, _, err := resourceDB.GetConfig()
	if err != nil {
		return TemplateData{}, err
	}

	configResource, found := config.Resources.Lookup(resourceName)
	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	history, err := resourceDB.GetResourceHistory(configResource.Name)
	if err != nil {
		return TemplateData{}, err
	}

	resource, err := resourceDB.GetResource(configResource.Name)
	if err != nil {
		return TemplateData{}, err
	}

	templateData := TemplateData{
		Resource:     configResource,
		DBResource:   resource,
		History:      history,
		PipelineName: resourceDB.GetPipelineName(),
		GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
			for _, groupResource := range g.Resources {
				if groupResource == configResource.Name {
					return true
				}
			}

			return false
		}),
	}

	return templateData, nil
}
Example #6
0
File: handler.go Project: ACPK/atc
func FetchTemplateData(
	pipelineName string,
	client concourse.Client,
	jobName string,
	page concourse.Page,
) (TemplateData, error) {
	pipeline, pipelineFound, err := client.Pipeline(pipelineName)
	if err != nil {
		return TemplateData{}, err
	}

	if !pipelineFound {
		return TemplateData{}, ErrConfigNotFound
	}

	job, jobFound, err := client.Job(pipelineName, jobName)
	if err != nil {
		return TemplateData{}, err
	}

	if !jobFound {
		return TemplateData{}, ErrJobConfigNotFound
	}

	bs, pagination, _, err := client.JobBuilds(pipelineName, jobName, page)
	if err != nil {
		return TemplateData{}, err
	}

	var bsr []BuildWithInputsOutputs

	for _, build := range bs {
		buildInputsOutputs, _, err := client.BuildResources(build.ID)
		if err != nil {
			return TemplateData{}, err
		}

		bsr = append(bsr, BuildWithInputsOutputs{
			Build:     build,
			Resources: buildInputsOutputs,
		})
	}

	return TemplateData{
		PipelineName: pipelineName,
		Job:          job,

		Builds:     bsr,
		Pagination: pagination,

		GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
			for _, groupJob := range g.Jobs {
				if groupJob == job.Name {
					return true
				}
			}

			return false
		}),

		CurrentBuild: job.FinishedBuild,
	}, nil
}
Example #7
0
File: handler.go Project: utako/atc
func FetchTemplateData(resourceDB ResourcesDB, authenticated bool, resourceName string, id int, newerResourceVersions bool) (TemplateData, error) {
	config, _, err := resourceDB.GetConfig()
	if err != nil {
		return TemplateData{}, err
	}

	configResource, found := config.Resources.Lookup(resourceName)
	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	dbResource, err := resourceDB.GetResource(configResource.Name)
	if err != nil {
		return TemplateData{}, err
	}

	maxID, err := resourceDB.GetResourceHistoryMaxID(dbResource.ID)
	if err != nil {
		return TemplateData{}, err
	}

	startingID := maxID

	if id < maxID && id != 0 {
		startingID = id
	}

	history, hasNext, err := resourceDB.GetResourceHistoryCursor(configResource.Name, startingID, newerResourceVersions, 100)
	if err != nil {
		return TemplateData{}, err
	}

	resource := present.Resource(configResource, config.Groups, dbResource, authenticated)

	maxIDFromResults := maxID
	var olderStartID int
	var newerStartID int

	if len(history) > 0 {
		maxIDFromResults = history[0].VersionedResource.ID
		minIDFromResults := history[len(history)-1].VersionedResource.ID
		olderStartID = minIDFromResults - 1
		newerStartID = maxIDFromResults + 1
	}

	hasNewer := maxID > maxIDFromResults
	hasOlder := newerResourceVersions || hasNext
	hasPagination := hasOlder || hasNewer

	templateData := TemplateData{
		Resource: resource,
		History:  history,
		PaginationData: PaginationData{
			HasPagination: hasPagination,
			HasOlder:      hasOlder,
			HasNewer:      hasNewer,
			OlderStartID:  olderStartID,
			NewerStartID:  newerStartID,
		},
		PipelineName: resourceDB.GetPipelineName(),
		GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
			for _, groupResource := range g.Resources {
				if groupResource == configResource.Name {
					return true
				}
			}

			return false
		}),
	}

	return templateData, nil
}
Example #8
0
File: handler.go Project: ACPK/atc
func NewHandler(logger lager.Logger, clientFactory web.ClientFactory, template *template.Template) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		client := clientFactory.Build(r)

		pipelineName, jobName, buildName, err := getNames(r)
		if err != nil {
			logger.Error("failed-to-get-names", err)
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		job, found, err := client.Job(pipelineName, jobName)
		if err != nil {
			logger.Error("failed-to-load-job", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		log := logger.Session("get-build", lager.Data{
			"job":   job.Name,
			"build": buildName,
		})

		requestedBuild, found, err := client.JobBuild(pipelineName, jobName, buildName)
		if err != nil {
			log.Error("get-build-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		buildInputsOutputs, _, err := client.BuildResources(requestedBuild.ID)
		if err != nil {
			log.Error("failed-to-get-build-resources", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		builds, err := getAllJobBuilds(client, pipelineName, jobName)
		if err != nil {
			log.Error("get-all-builds-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		pipeline, _, err := client.Pipeline(pipelineName)
		if err != nil {
			log.Error("get-pipeline-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		templateData := TemplateData{
			GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
				for _, groupJob := range g.Jobs {
					if groupJob == job.Name {
						return true
					}
				}

				return false
			}),

			Job:    job,
			Builds: builds,

			Build:        requestedBuild,
			Inputs:       buildInputsOutputs.Inputs,
			PipelineName: pipelineName,
		}

		err = template.Execute(w, templateData)
		if err != nil {
			log.Fatal("failed-to-build-template", err, lager.Data{
				"template-data": templateData,
			})
		}
	})
}
Example #9
0
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
	logger := handler.logger.Session("handler")

	client := handler.clientFactory.Build(r)

	pipelineName, jobName, buildName, err := getNames(r)
	if err != nil {
		logger.Error("failed-to-get-names", err)
		w.WriteHeader(http.StatusBadRequest)
		return nil
	}

	job, found, err := client.Job(pipelineName, jobName)
	if err != nil {
		logger.Error("failed-to-load-job", err)
		return err
	}

	if !found {
		w.WriteHeader(http.StatusNotFound)
		return nil
	}

	log := logger.Session("get-build", lager.Data{
		"job":   job.Name,
		"build": buildName,
	})

	requestedBuild, found, err := client.JobBuild(pipelineName, jobName, buildName)
	if err != nil {
		log.Error("failed-to-get-build", err)
		return err
	}

	if !found {
		w.WriteHeader(http.StatusNotFound)
		return nil
	}

	pipeline, _, err := client.Pipeline(pipelineName)
	if err != nil {
		log.Error("failed-to-get-pipeline", err)
		return err
	}

	templateData := TemplateData{
		GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
			for _, groupJob := range g.Jobs {
				if groupJob == job.Name {
					return true
				}
			}

			return false
		}),

		Job: job,

		Build:        requestedBuild,
		PipelineName: pipelineName,
	}

	buildPlan, found, err := client.BuildPlan(requestedBuild.ID)
	if err != nil {
		log.Error("failed-to-get-build-plan", err)
		return err
	}

	if buildPlan.Schema == "exec.v2" || !found {
		// either it's definitely a new build, or it hasn't started yet (and thus
		// must be new), so render with the new UI
		err = handler.template.Execute(w, templateData)
		if err != nil {
			log.Fatal("failed-to-build-template", err, lager.Data{
				"template-data": templateData,
			})

			return err
		}
	} else {
		buildInputsOutputs, _, err := client.BuildResources(requestedBuild.ID)
		if err != nil {
			log.Error("failed-to-get-build-resources", err)
			return err
		}

		builds, err := getAllJobBuilds(client, pipelineName, jobName)
		if err != nil {
			log.Error("get-all-builds-failed", err)
			return err
		}

		oldBuildTemplateData := OldBuildTemplateData{
			TemplateData: templateData,
			Builds:       builds,
			Inputs:       buildInputsOutputs.Inputs,
		}

		err = handler.oldBuildTemplate.Execute(w, oldBuildTemplateData)
		if err != nil {
			log.Fatal("failed-to-build-template", err, lager.Data{
				"template-data": oldBuildTemplateData,
			})
			return err
		}
	}

	return nil
}
Example #10
0
func FetchTemplateData(pipelineName string, resourceName string, client concourse.Client, page concourse.Page) (TemplateData, error) {
	pipeline, pipelineFound, err := client.Pipeline(pipelineName)
	if err != nil {
		return TemplateData{}, err
	}

	if !pipelineFound {
		return TemplateData{}, ErrConfigNotFound
	}

	resource, resourceFound, err := client.Resource(pipelineName, resourceName)
	if err != nil {
		return TemplateData{}, err
	}

	if !resourceFound {
		return TemplateData{}, ErrResourceNotFound
	}

	versionedResources, pagination, resourceVersionsFound, err := client.ResourceVersions(pipelineName, resourceName, page)
	if err != nil {
		return TemplateData{}, err
	}

	if !resourceVersionsFound {
		return TemplateData{}, ErrResourceNotFound
	}

	versions := []VersionedResourceWithInputsAndOutputs{}
	for _, versionedResource := range versionedResources {
		inputs, _, err := client.BuildsWithVersionAsInput(pipelineName, resourceName, versionedResource.ID)
		if err != nil {
			return TemplateData{}, err
		}

		outputs, _, err := client.BuildsWithVersionAsOutput(pipelineName, resourceName, versionedResource.ID)
		if err != nil {
			return TemplateData{}, err
		}

		inputsTo := map[string][]atc.Build{}
		outputsOf := map[string][]atc.Build{}

		for _, input := range inputs {
			if _, ok := inputsTo[input.JobName]; !ok {
				inputsTo[input.JobName] = []atc.Build{}
			}

			inputsTo[input.JobName] = append(inputsTo[input.JobName], input)
		}

		for _, output := range outputs {
			if _, ok := outputsOf[output.JobName]; !ok {
				outputsOf[output.JobName] = []atc.Build{}
			}

			outputsOf[output.JobName] = append(outputsOf[output.JobName], output)
		}

		versions = append(versions, VersionedResourceWithInputsAndOutputs{
			VersionedResource: versionedResource,
			InputsTo:          inputsTo,
			OutputsOf:         outputsOf,
		})
	}

	return TemplateData{
		Resource:     resource,
		Versions:     versions,
		PipelineName: pipelineName,
		Pagination:   pagination,
		GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
			for _, groupResource := range g.Resources {
				if groupResource == resource.Name {
					return true
				}
			}

			return false
		}),
	}, nil
}
Example #11
0
File: handler.go Project: utako/atc
func (server *server) GetBuild(pipelineDB db.PipelineDB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		jobName := r.FormValue(":job")
		if len(jobName) == 0 {
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		buildName := r.FormValue(":build")
		if len(buildName) == 0 {
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		config, _, err := pipelineDB.GetConfig()
		if err != nil {
			server.logger.Error("failed-to-load-config", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		job, found := config.Jobs.Lookup(jobName)
		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		log := server.logger.Session("get-build", lager.Data{
			"job":   job.Name,
			"build": buildName,
		})

		build, err := pipelineDB.GetJobBuild(jobName, buildName)
		if err != nil {
			log.Error("get-build-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		inputs, outputs, err := pipelineDB.GetBuildResources(build.ID)
		if err != nil {
			log.Error("failed-to-get-build-resources", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		bs, err := pipelineDB.GetAllJobBuilds(jobName)
		if err != nil {
			log.Error("get-all-builds-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		templateData := TemplateData{
			GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
				for _, groupJob := range g.Jobs {
					if groupJob == job.Name {
						return true
					}
				}

				return false
			}),

			Job:    job,
			Builds: bs,

			Build:        build,
			Inputs:       inputs,
			Outputs:      outputs,
			PipelineName: pipelineDB.GetPipelineName(),
		}

		err = server.template.Execute(w, templateData)
		if err != nil {
			log.Fatal("failed-to-task-template", err, lager.Data{
				"template-data": templateData,
			})
		}
	})
}
Example #12
0
File: handler.go Project: ACPK/atc
func FetchTemplateData(resourceDB ResourcesDB, authenticated bool, resourceName string, startingID int, resultsGreaterThanStartingID bool) (TemplateData, error) {
	config, _, found, err := resourceDB.GetConfig()
	if err != nil {
		return TemplateData{}, err
	}

	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	configResource, found := config.Resources.Lookup(resourceName)
	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	dbResource, err := resourceDB.GetResource(configResource.Name)
	if err != nil {
		return TemplateData{}, err
	}

	maxID, err := resourceDB.GetResourceHistoryMaxID(dbResource.ID)
	if err != nil {
		return TemplateData{}, err
	}

	if startingID == 0 && !resultsGreaterThanStartingID {
		startingID = maxID
	}

	history, moreResultsInGivenDirection, err := resourceDB.GetResourceHistoryCursor(configResource.Name, startingID, resultsGreaterThanStartingID, 100)
	if err != nil {
		return TemplateData{}, err
	}

	var paginationData pagination.PaginationData

	if len(history) > 0 {
		paginationData = pagination.NewPaginationData(
			resultsGreaterThanStartingID,
			moreResultsInGivenDirection,
			maxID,
			history[0].VersionedResource.ID,
			history[len(history)-1].VersionedResource.ID,
		)
	} else {
		paginationData = pagination.PaginationData{}
	}

	resource := present.Resource(configResource, config.Groups, dbResource, authenticated)

	templateData := TemplateData{
		Resource:       resource,
		History:        history,
		PaginationData: paginationData,
		PipelineName:   resourceDB.GetPipelineName(),
		GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
			for _, groupResource := range g.Resources {
				if groupResource == configResource.Name {
					return true
				}
			}

			return false
		}),
	}

	return templateData, nil
}