From 19696aa8ba5b8ca54ac37e478eefd3f5b16c1357 Mon Sep 17 00:00:00 2001 From: Juan Hernandez Date: Mon, 26 Jun 2017 17:05:44 +0200 Subject: [PATCH] Use relative paths Currently the objects that contain the information about the project have the 'directory' and 'work' field, containing the absolute paths of the source and work directories for those objects. Using absolute directories complicates moving the project to a different directory, which is needed by the next patch adding support for embedding the project configuration inside the binary. In order to address that this patch replaces 'directory' with 'root' (absolute, only for the project) and 'path' (relative, for everything else). The 'work' attribute is removed, and instead work directories are calculted as needed. Change-Id: I9005bec9791125348b0f03f7f8a0c3edeea2953c Signed-off-by: Juan Hernandez --- tools/src/ovc/build/images.go | 46 ++++++++++++--------- tools/src/ovc/build/project.go | 74 ++++++++++++++++++---------------- 2 files changed, 66 insertions(+), 54 deletions(-) diff --git a/tools/src/ovc/build/images.go b/tools/src/ovc/build/images.go index 3b21c0c..fb55086 100644 --- a/tools/src/ovc/build/images.go +++ b/tools/src/ovc/build/images.go @@ -31,8 +31,6 @@ import ( // type Image struct { project *Project - work string - directory string name string tag string dockerfile *Dockerfile @@ -53,6 +51,12 @@ func NewImage(project *Project, name string) *Image { // the Dockerfile, if it exists. // func (i *Image) Load() error { + // If the tag already has a value then the image has been loaded + // before, so we don't need to do anything: + if i.tag != "" { + return nil + } + // Calculate the tag: i.tag = fmt.Sprintf( "%s/%s:%s", @@ -69,21 +73,17 @@ func (i *Image) Load() error { ) } - // Calculate the directory: - i.directory = filepath.Join(i.project.Images().Directory(), i.name) - - // Process the templates, if needed: - if i.work == "" { - i.work = filepath.Join(i.project.Images().WorkingDirectory(), i.name) - err := ProcessTemplates(i.project, i.directory, i.work) - if err != nil { - return err - } + // Process the templates: + source := i.Directory() + work := i.WorkingDirectory() + err := ProcessTemplates(i.project, source, work) + if err != nil { + return err } - // Check if there is a Dockerfile in the directory, and if it - // does then load it: - dockerfilePath := filepath.Join(i.work, "Dockerfile") + // Check if there is a Dockerfile in the working directory, and + // if it does then load it: + dockerfilePath := filepath.Join(work, "Dockerfile") if _, err := os.Stat(dockerfilePath); err == nil { i.dockerfile = NewDockerfile() i.dockerfile.Load(dockerfilePath) @@ -92,12 +92,18 @@ func (i *Image) Load() error { return nil } - -// Directory returns the path of the directory that contains the source -// files of the image. +// Directory returns the absolute path of the directory that contains the +// source files of the image. // func (i *Image) Directory() string { - return i.directory + return filepath.Join(i.project.Images().Directory(), i.name) +} + +// WorkingDirectory returns the absolute path of the work directory for the +// image. +// +func (i *Image) WorkingDirectory() string { + return filepath.Join(i.project.Images().WorkingDirectory(), i.name) } // Name returns the name of the image. @@ -138,7 +144,7 @@ func (i *Image) Build() error { "docker", "build", fmt.Sprintf("--tag=%s", i.Tag()), - i.work, + i.WorkingDirectory(), ) } diff --git a/tools/src/ovc/build/project.go b/tools/src/ovc/build/project.go index 27f1f3a..be473c9 100644 --- a/tools/src/ovc/build/project.go +++ b/tools/src/ovc/build/project.go @@ -34,7 +34,7 @@ import ( // type Project struct { work string - directory string + root string version string images *ProjectImages manifests *ProjectManifests @@ -44,32 +44,34 @@ type Project struct { // of the project. // type ProjectImages struct { - work string - directory string - prefix string - registry string - list []*Image - index map[string]*Image + project *Project + path string + prefix string + registry string + list []*Image + index map[string]*Image } // ProjectManifests contains the information about the manifests that // are part of the project. // type ProjectManifests struct { - work string - directory string + project *Project + path string } -// WorkingDirectory returns the working directory of the project. +// WorkingDirectory returns the absolute path of the working directory +// of the project. // func (p *Project) WorkingDirectory() string { return p.work } -// Directory returns the root directory of the project. +// Directory returns the the absolute path of the root directory of the +// project. // func (p *Project) Directory() string { - return p.directory + return p.root } // Version returns the version of the project. @@ -92,18 +94,18 @@ func (p *Project) Manifests() *ProjectManifests { return p.manifests } -// WorkingDirectory returns the working directory for the images of the -// project. +// WorkingDirectory returns the absolute path of the working directory for the +// images of the project. // func (pi *ProjectImages) WorkingDirectory() string { - return pi.work + return filepath.Join(pi.project.work, pi.path) } -// Directory returns the path of the directory containing the source +// Directory returns the absolute path of the directory containing the source // files of the image specifications. // func (pi *ProjectImages) Directory() string { - return pi.directory + return filepath.Join(pi.project.root, pi.path) } // Prefix returns the prefix that should be used to tag the images of @@ -134,18 +136,18 @@ func (pi *ProjectImages) Index() map[string]*Image { return pi.index } -// WorkingDirectory returns the working directory for the OpenShift -// manifests of the project. +// WorkingDirectory returns the absolute path of the working directory for the +// OpenShift manifests of the project. // func (pm *ProjectManifests) WorkingDirectory() string { - return pm.work + return filepath.Join(pm.project.work, pm.path) } -// Directory returns the path of the directory containing the source -// files of the OpenShift manifests. +// Directory returns the absolute path of the directory containing the +// source files of the OpenShift manifests. // func (pm *ProjectManifests) Directory() string { - return pm.directory + return filepath.Join(pm.project.root, pm.path) } // Close releases all the resources used by the project, including the @@ -187,6 +189,10 @@ func LoadProject(path string) (project *Project, err error) { // Create an initially empty project: project = new(Project) + // Calculate the absolute path of the project: + root, _ := filepath.Abs(filepath.Dir(path)) + project.root = root + // Create a temporary directory that will be used to store the // results of generating files from templates, and maybe other // temporary files. @@ -260,13 +266,11 @@ func loadImages(file *ini.File, project *Project) error { // Load basic attributes: images := new(ProjectImages) project.images = images - images.directory = section.Key("directory").MustString("") + images.project = project + images.path = section.Key("directory").MustString("") images.prefix = section.Key("prefix").MustString("") images.registry = section.Key("registry").MustString("") - // Prepare the work directory: - images.work = filepath.Join(project.work, filepath.Base(images.directory)) - // The source files of images may be templates, and those // templates may refer to some properties of other images. In // particular the Dockerfile of one image may refer to its @@ -278,7 +282,7 @@ func loadImages(file *ini.File, project *Project) error { // of the images, to have at least the name. After that we can // load the image details, which will process the templates. images.list = []*Image{} - paths, err := filepath.Glob(filepath.Join(images.directory, "*")) + paths, err := filepath.Glob(filepath.Join(project.root, images.path, "*")) if err != nil { return err } @@ -340,7 +344,7 @@ func loadImages(file *ini.File, project *Project) error { func loadImage(image *Image) error { // Check if there is a Dockerfile in the directory, and if it // does then load it: - path := filepath.Join(image.directory, "Dockerfile") + path := filepath.Join(image.Directory(), "Dockerfile") if _, err := os.Stat(path); err == nil { image.dockerfile = NewDockerfile() image.dockerfile.Load(path) @@ -391,11 +395,13 @@ func loadManifests(file *ini.File, project *Project) error { // Create the manifests object, and get the directory: manifests := new(ProjectManifests) project.manifests = manifests - manifests.directory = section.Key("directory").MustString("") - - // Prepare the work directory: - manifests.work = filepath.Join(project.work, filepath.Base(manifests.directory)) + manifests.project = project + manifests.path = section.Key("directory").MustString("") // Process the templates: - return ProcessTemplates(project, manifests.directory, manifests.work) + return ProcessTemplates( + project, + manifests.Directory(), + manifests.WorkingDirectory(), + ) }