Add logs in directories for each app

This commit is contained in:
Radu Matei 2018-04-12 16:33:56 +03:00
Родитель 63124095da
Коммит f0ca0b3ac3
3 изменённых файлов: 47 добавлений и 25 удалений

Просмотреть файл

@ -2,7 +2,8 @@ package main
import (
"fmt"
"os"
"github.com/Azure/draft/pkg/osutil"
pluginbase "k8s.io/helm/pkg/plugin"
@ -21,13 +22,9 @@ func (i *initCmd) ensureDirectories() error {
i.home.Logs(),
}
for _, p := range configDirectories {
if fi, err := os.Stat(p); err != nil {
fmt.Fprintf(i.out, "Creating %s \n", p)
if err := os.MkdirAll(p, 0755); err != nil {
return fmt.Errorf("Could not create %s: %s", p, err)
}
} else if !fi.IsDir() {
return fmt.Errorf("%s must be a directory", p)
err := osutil.EnsureDirectory(p)
if err != nil {
return err
}
}
@ -38,19 +35,7 @@ func (i *initCmd) ensureDirectories() error {
//
// If it does not exist, this function will create it.
func (i *initCmd) ensureConfig() error {
fi, err := os.Stat(i.home.Config())
if err != nil {
fmt.Fprintf(i.out, "Creating %s \n", i.home.Config())
f, err := os.Create(i.home.Config())
if err != nil {
return fmt.Errorf("Could not create %s: %s", i.home.Config(), err)
}
defer f.Close()
} else if fi.IsDir() {
return fmt.Errorf("%s must not be a directory", i.home.Config())
}
return nil
return osutil.EnsureFile(i.home.Config())
}
// ensurePacks checks to see if the default packs exist.

Просмотреть файл

@ -17,6 +17,7 @@ import (
"github.com/Azure/draft/pkg/draft/local"
"github.com/Azure/draft/pkg/draft/manifest"
"github.com/Azure/draft/pkg/draft/pack"
"github.com/Azure/draft/pkg/osutil"
"github.com/Azure/draft/pkg/storage"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/image/build"
@ -60,8 +61,8 @@ type Builder struct {
// Logs returns the path to the build logs.
//
// Set after Up is called (otherwise "").
func (b *Builder) Logs() string {
return filepath.Join(b.LogsDir, b.id)
func (b *Builder) Logs(appName string) string {
return filepath.Join(b.LogsDir, appName, b.id)
}
// ID returns the build id.
@ -125,14 +126,20 @@ func newAppContext(b *Builder, buildCtx *Context) (*AppContext, error) {
if err := strvals.ParseInto(inject, vals); err != nil {
return nil, err
}
logf, err := os.OpenFile(b.Logs(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
err = osutil.EnsureDirectory(filepath.Dir(b.Logs(buildCtx.Env.Name)))
if err != nil {
return nil, err
}
logf, err := os.OpenFile(b.Logs(buildCtx.Env.Name), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
state := &storage.Object{
BuildID: b.ID(),
ContextID: ctxtID,
LogsFileRef: b.Logs(),
LogsFileRef: b.Logs(buildCtx.Env.Name),
}
return &AppContext{
obj: state,

Просмотреть файл

@ -1,6 +1,7 @@
package osutil
import (
"fmt"
"os"
"runtime"
"syscall"
@ -35,3 +36,32 @@ func SymlinkWithFallback(oldname, newname string) (err error) {
}
return
}
// EnsureDirectory checks if a directory exists and creates it if it doesn't
func EnsureDirectory(dir string) error {
if fi, err := os.Stat(dir); err != nil {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("Could not create %s: %s", dir, err)
}
} else if !fi.IsDir() {
return fmt.Errorf("%s must be a directory", dir)
}
return nil
}
// EnsureFile checks if a file exists and creates it if it doesn't
func EnsureFile(file string) error {
fi, err := os.Stat(file)
if err != nil {
f, err := os.Create(file)
if err != nil {
return fmt.Errorf("Could not create %s: %s", file, err)
}
defer f.Close()
} else if fi.IsDir() {
return fmt.Errorf("%s must not be a directory", file)
}
return nil
}