Skip to content

Commit

Permalink
Don't restart project-clone container and persist logs.
Browse files Browse the repository at this point in the history
Signed-off-by: Angel Misevski <amisevsk@redhat.com>
  • Loading branch information
amisevsk committed Jul 13, 2021
1 parent e5b5aa9 commit 8f96431
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions project-clone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,31 @@
package main

import (
"io"
"log"
"os"
"path"

"github.com/devfile/devworkspace-operator/project-clone/internal"
"github.com/devfile/devworkspace-operator/project-clone/internal/git"
"github.com/devfile/devworkspace-operator/project-clone/internal/zip"
)

const (
logFileName = "project-clone-errors.log"
tmpLogFilePath = "/tmp/" + logFileName
)

// TODO: Handle sparse checkout
// TODO: Add support for auth
func main() {
f, err := os.Create(tmpLogFilePath)
if err != nil {
log.Printf("failed to open file %s for logging: %s", tmpLogFilePath, err)
}
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)

workspace, err := internal.ReadFlattenedDevWorkspace()
if err != nil {
log.Printf("Failed to read current DevWorkspace: %s", err)
Expand All @@ -39,11 +53,34 @@ func main() {
err = zip.SetupZipProject(project)
default:
log.Printf("Project does not specify Git or Zip source")
os.Exit(1)
copyLogFileToProjectsRoot()
os.Exit(0)
}
if err != nil {
log.Printf("Encountered error while setting up project %s: %s", project.Name, err)
os.Exit(1)
copyLogFileToProjectsRoot()
os.Exit(0)
}
}
}

// copyLogFileToProjectsRoot copies the predefined log file into a persistent directory ($PROJECTS_ROOT)
// so that issues in setting up a devfile's projects are persisted beyond workspace restarts. Note that
// not all output from the project clone container is propagated to the log file. For example, the progress
// in cloning a project using the `git` binary only appears in stdout/stderr.
func copyLogFileToProjectsRoot() {
infile, err := os.Open(tmpLogFilePath)
if err != nil {
log.Printf("Failed to open log file: %s", err)
}
defer infile.Close()
outfile, err := os.Create(path.Join(internal.ProjectsRoot, logFileName))
if err != nil {
log.Printf("Failed to create log file: %s", err)
}
defer outfile.Close()

if _, err := io.Copy(outfile, infile); err != nil {
log.Printf("Failed to copy log file to $PROJECTS_ROOT: %s", err)
}
}

0 comments on commit 8f96431

Please sign in to comment.