Skip to content

Commit

Permalink
Use relative paths
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jhernand committed Jun 26, 2017
1 parent 9090937 commit 19696aa
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 54 deletions.
46 changes: 26 additions & 20 deletions tools/src/ovc/build/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (
//
type Image struct {
project *Project
work string
directory string
name string
tag string
dockerfile *Dockerfile
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -138,7 +144,7 @@ func (i *Image) Build() error {
"docker",
"build",
fmt.Sprintf("--tag=%s", i.Tag()),
i.work,
i.WorkingDirectory(),
)
}

Expand Down
74 changes: 40 additions & 34 deletions tools/src/ovc/build/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
//
type Project struct {
work string
directory string
root string
version string
images *ProjectImages
manifests *ProjectManifests
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
)
}

0 comments on commit 19696aa

Please sign in to comment.