Skip to content
Snippets Groups Projects
Commit 5e63dcba authored by Jessica Yuen's avatar Jessica Yuen
Browse files

Logging for env subcommands

This commit will add logging to all env subcommands 'list', 'set',
'add', and 'rm'. It adds both Info level and Debug level logging.

Fixes #137
parent b081f0bc
No related branches found
No related tags found
No related merge requests found
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"regexp" "regexp"
"strings" "strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero" "github.com/spf13/afero"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/ksonnet" "github.com/ksonnet/ksonnet-lib/ksonnet-gen/ksonnet"
...@@ -53,15 +54,21 @@ type EnvironmentSpec struct { ...@@ -53,15 +54,21 @@ type EnvironmentSpec struct {
func (m *manager) CreateEnvironment(name, uri string, spec ClusterSpec) error { func (m *manager) CreateEnvironment(name, uri string, spec ClusterSpec) error {
extensionsLibData, k8sLibData, specData, err := m.generateKsonnetLibData(spec) extensionsLibData, k8sLibData, specData, err := m.generateKsonnetLibData(spec)
if err != nil { if err != nil {
log.Debugf("Failed to write '%s'", specFilename)
return err return err
} }
return m.createEnvironment(name, uri, extensionsLibData, k8sLibData, specData) err = m.createEnvironment(name, uri, extensionsLibData, k8sLibData, specData)
if err == nil {
log.Infof("Environment '%s' pointing to cluster at URI '%s' successfully created", name, uri)
}
return err
} }
func (m *manager) createEnvironment(name, uri string, extensionsLibData, k8sLibData, specData []byte) error { func (m *manager) createEnvironment(name, uri string, extensionsLibData, k8sLibData, specData []byte) error {
exists, err := m.environmentExists(name) exists, err := m.environmentExists(name)
if err != nil { if err != nil {
log.Debug("Failed to check whether environment exists")
return err return err
} }
if exists { if exists {
...@@ -73,28 +80,38 @@ func (m *manager) createEnvironment(name, uri string, extensionsLibData, k8sLibD ...@@ -73,28 +80,38 @@ func (m *manager) createEnvironment(name, uri string, extensionsLibData, k8sLibD
return fmt.Errorf("Environment '%s' is not valid; must not contain punctuation or trailing slashes", name) return fmt.Errorf("Environment '%s' is not valid; must not contain punctuation or trailing slashes", name)
} }
log.Infof("Creating environment '%s' with uri '%s'", name, uri)
envPath := appendToAbsPath(m.environmentsPath, name) envPath := appendToAbsPath(m.environmentsPath, name)
err = m.appFS.MkdirAll(string(envPath), defaultPermissions) err = m.appFS.MkdirAll(string(envPath), defaultPermissions)
if err != nil { if err != nil {
return err return err
} }
log.Infof("Generating environment metadata at path '%s'", envPath)
// Generate the schema file. // Generate the schema file.
log.Debugf("Generating '%s', length: %d", schemaFilename, len(specData))
schemaPath := appendToAbsPath(envPath, schemaFilename) schemaPath := appendToAbsPath(envPath, schemaFilename)
err = afero.WriteFile(m.appFS, string(schemaPath), specData, defaultPermissions) err = afero.WriteFile(m.appFS, string(schemaPath), specData, defaultPermissions)
if err != nil { if err != nil {
log.Debugf("Failed to write '%s'", schemaFilename)
return err return err
} }
log.Debugf("Generating '%s', length: %d", k8sLibFilename, len(k8sLibData))
k8sLibPath := appendToAbsPath(envPath, k8sLibFilename) k8sLibPath := appendToAbsPath(envPath, k8sLibFilename)
err = afero.WriteFile(m.appFS, string(k8sLibPath), k8sLibData, defaultPermissions) err = afero.WriteFile(m.appFS, string(k8sLibPath), k8sLibData, defaultPermissions)
if err != nil { if err != nil {
log.Debugf("Failed to write '%s'", k8sLibFilename)
return err return err
} }
log.Debugf("Generating '%s', length: %d", extensionsLibFilename, len(extensionsLibData))
extensionsLibPath := appendToAbsPath(envPath, extensionsLibFilename) extensionsLibPath := appendToAbsPath(envPath, extensionsLibFilename)
err = afero.WriteFile(m.appFS, string(extensionsLibPath), extensionsLibData, defaultPermissions) err = afero.WriteFile(m.appFS, string(extensionsLibPath), extensionsLibData, defaultPermissions)
if err != nil { if err != nil {
log.Debugf("Failed to write '%s'", extensionsLibFilename)
return err return err
} }
...@@ -104,6 +121,7 @@ func (m *manager) createEnvironment(name, uri string, extensionsLibData, k8sLibD ...@@ -104,6 +121,7 @@ func (m *manager) createEnvironment(name, uri string, extensionsLibData, k8sLibD
return err return err
} }
log.Debugf("Generating '%s', length: %d", specFilename, len(envSpecData))
envSpecPath := appendToAbsPath(envPath, specFilename) envSpecPath := appendToAbsPath(envPath, specFilename)
return afero.WriteFile(m.appFS, string(envSpecPath), envSpecData, defaultPermissions) return afero.WriteFile(m.appFS, string(envSpecPath), envSpecData, defaultPermissions)
} }
...@@ -114,19 +132,24 @@ func (m *manager) DeleteEnvironment(name string) error { ...@@ -114,19 +132,24 @@ func (m *manager) DeleteEnvironment(name string) error {
// Check whether this environment exists // Check whether this environment exists
envExists, err := m.environmentExists(name) envExists, err := m.environmentExists(name)
if err != nil { if err != nil {
log.Debug("Failed to check whether environment exists")
return err return err
} }
if !envExists { if !envExists {
return fmt.Errorf("Environment '%s' does not exist", name) return fmt.Errorf("Environment '%s' does not exist", name)
} }
log.Infof("Deleting environment '%s' at path '%s'", name, envPath)
// Remove the directory and all files within the environment path. // Remove the directory and all files within the environment path.
err = m.appFS.RemoveAll(envPath) err = m.appFS.RemoveAll(envPath)
if err != nil { if err != nil {
log.Debugf("Failed to remove environment directory at path '%s'", envPath)
return err return err
} }
// Need to ensure empty parent directories are also removed. // Need to ensure empty parent directories are also removed.
log.Debug("Removing empty parent directories, if any")
parentDir := name parentDir := name
for parentDir != "." { for parentDir != "." {
parentDir = filepath.Dir(parentDir) parentDir = filepath.Dir(parentDir)
...@@ -134,9 +157,11 @@ func (m *manager) DeleteEnvironment(name string) error { ...@@ -134,9 +157,11 @@ func (m *manager) DeleteEnvironment(name string) error {
isEmpty, err := afero.IsEmpty(m.appFS, parentPath) isEmpty, err := afero.IsEmpty(m.appFS, parentPath)
if err != nil { if err != nil {
log.Debugf("Failed to check whether parent directory at path '%s' is empty", parentPath)
return err return err
} }
if isEmpty { if isEmpty {
log.Debugf("Failed to remove parent directory at path '%s'", parentPath)
err := m.appFS.RemoveAll(parentPath) err := m.appFS.RemoveAll(parentPath)
if err != nil { if err != nil {
return err return err
...@@ -144,15 +169,18 @@ func (m *manager) DeleteEnvironment(name string) error { ...@@ -144,15 +169,18 @@ func (m *manager) DeleteEnvironment(name string) error {
} }
} }
log.Infof("Successfully removed environment '%s'", name)
return nil return nil
} }
func (m *manager) GetEnvironments() ([]Environment, error) { func (m *manager) GetEnvironments() ([]Environment, error) {
envs := []Environment{} envs := []Environment{}
log.Info("Retrieving all environments")
err := afero.Walk(m.appFS, string(m.environmentsPath), func(path string, f os.FileInfo, err error) error { err := afero.Walk(m.appFS, string(m.environmentsPath), func(path string, f os.FileInfo, err error) error {
isDir, err := afero.IsDir(m.appFS, path) isDir, err := afero.IsDir(m.appFS, path)
if err != nil { if err != nil {
log.Debugf("Failed to check whether the path at '%s' is a directory", path)
return err return err
} }
...@@ -161,20 +189,24 @@ func (m *manager) GetEnvironments() ([]Environment, error) { ...@@ -161,20 +189,24 @@ func (m *manager) GetEnvironments() ([]Environment, error) {
specPath := filepath.Join(path, specFilename) specPath := filepath.Join(path, specFilename)
specFileExists, err := afero.Exists(m.appFS, specPath) specFileExists, err := afero.Exists(m.appFS, specPath)
if err != nil { if err != nil {
log.Debugf("Failed to check whether spec file at '$s' exists", specPath)
return err return err
} }
if specFileExists { if specFileExists {
envName := filepath.Clean(strings.TrimPrefix(path, string(m.environmentsPath)+"/")) envName := filepath.Clean(strings.TrimPrefix(path, string(m.environmentsPath)+"/"))
specFile, err := afero.ReadFile(m.appFS, specPath) specFile, err := afero.ReadFile(m.appFS, specPath)
if err != nil { if err != nil {
log.Debugf("Failed to read spec file at path '%s'", specPath)
return err return err
} }
var envSpec EnvironmentSpec var envSpec EnvironmentSpec
err = json.Unmarshal(specFile, &envSpec) err = json.Unmarshal(specFile, &envSpec)
if err != nil { if err != nil {
log.Debugf("Failed to convert the spec file at path '%s' to JSON", specPath)
return err return err
} }
log.Debugf("Found environment '%s', with uri '%s", envName, envSpec.URI)
envs = append(envs, Environment{Name: envName, Path: path, URI: envSpec.URI}) envs = append(envs, Environment{Name: envName, Path: path, URI: envSpec.URI})
} }
} }
...@@ -193,16 +225,13 @@ func (m *manager) SetEnvironment(name string, desired Environment) error { ...@@ -193,16 +225,13 @@ func (m *manager) SetEnvironment(name string, desired Environment) error {
// Check whether this environment exists // Check whether this environment exists
envExists, err := m.environmentExists(name) envExists, err := m.environmentExists(name)
if err != nil { if err != nil {
log.Debugf("Failed to check whether '%s' exists", name)
return err return err
} }
if !envExists { if !envExists {
return fmt.Errorf("Environment '%s' does not exist", name) return fmt.Errorf("Environment '%s' does not exist", name)
} }
if name == desired.Name {
return nil
}
// ensure new environment name does not contain punctuation // ensure new environment name does not contain punctuation
if !isValidName(desired.Name) { if !isValidName(desired.Name) {
return fmt.Errorf("Environment '%s' is not valid; must not contain punctuation or trailing slashes", desired.Name) return fmt.Errorf("Environment '%s' is not valid; must not contain punctuation or trailing slashes", desired.Name)
...@@ -210,10 +239,13 @@ func (m *manager) SetEnvironment(name string, desired Environment) error { ...@@ -210,10 +239,13 @@ func (m *manager) SetEnvironment(name string, desired Environment) error {
// If the name has changed, the directory location needs to be moved to // If the name has changed, the directory location needs to be moved to
// reflect the change. // reflect the change.
if len(desired.Name) != 0 { if name != desired.Name && len(desired.Name) != 0 {
log.Infof("Setting environment name from '%s' to '%s'", name, desired.Name)
// Ensure not overwriting another environment // Ensure not overwriting another environment
desiredExists, err := m.environmentExists(desired.Name) desiredExists, err := m.environmentExists(desired.Name)
if err != nil { if err != nil {
log.Debugf("Failed to check whether environment '%s' already exists", desired.Name)
return err return err
} }
if desiredExists { if desiredExists {
...@@ -223,8 +255,10 @@ func (m *manager) SetEnvironment(name string, desired Environment) error { ...@@ -223,8 +255,10 @@ func (m *manager) SetEnvironment(name string, desired Environment) error {
// Move the directory // Move the directory
pathOld := string(appendToAbsPath(m.environmentsPath, name)) pathOld := string(appendToAbsPath(m.environmentsPath, name))
pathNew := string(appendToAbsPath(m.environmentsPath, desired.Name)) pathNew := string(appendToAbsPath(m.environmentsPath, desired.Name))
log.Debugf("Moving directory at path '%s' to '%s'", pathOld, pathNew)
err = m.appFS.Rename(pathOld, pathNew) err = m.appFS.Rename(pathOld, pathNew)
if err != nil { if err != nil {
log.Debugf("Failed to move path '%s' to '%s", pathOld, pathNew)
return err return err
} }
...@@ -233,16 +267,25 @@ func (m *manager) SetEnvironment(name string, desired Environment) error { ...@@ -233,16 +267,25 @@ func (m *manager) SetEnvironment(name string, desired Environment) error {
// Update fields in spec.json // Update fields in spec.json
if len(desired.URI) != 0 { if len(desired.URI) != 0 {
log.Infof("Setting environment URI to '%s'", desired.URI)
newSpec, err := generateSpecData(desired.URI) newSpec, err := generateSpecData(desired.URI)
if err != nil { if err != nil {
log.Debugf("Failed to generate %s with URI '%s'", specFilename, desired.URI)
return err return err
} }
envPath := appendToAbsPath(m.environmentsPath, name) envPath := appendToAbsPath(m.environmentsPath, name)
specPath := appendToAbsPath(envPath, specFilename) specPath := appendToAbsPath(envPath, specFilename)
return afero.WriteFile(m.appFS, string(specPath), newSpec, defaultPermissions)
err = afero.WriteFile(m.appFS, string(specPath), newSpec, defaultPermissions)
if err != nil {
log.Debugf("Failed to write %s at path '%s'", specFilename, specPath)
return err
}
} }
log.Infof("Successfully updated environment '%s'", name)
return nil return nil
} }
......
...@@ -21,6 +21,8 @@ import ( ...@@ -21,6 +21,8 @@ import (
"sort" "sort"
"strings" "strings"
log "github.com/sirupsen/logrus"
"github.com/ksonnet/kubecfg/metadata" "github.com/ksonnet/kubecfg/metadata"
) )
...@@ -37,6 +39,7 @@ func NewEnvAddCmd(name, uri, specFlag string, rootPath metadata.AbsPath) (*EnvAd ...@@ -37,6 +39,7 @@ func NewEnvAddCmd(name, uri, specFlag string, rootPath metadata.AbsPath) (*EnvAd
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Debugf("Generating ksonnetLib data with spec: %s", specFlag)
return &EnvAddCmd{name: name, uri: uri, spec: spec, rootPath: rootPath}, nil return &EnvAddCmd{name: name, uri: uri, spec: spec, rootPath: rootPath}, nil
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment