Unverified Commit c26d985d authored by bryanl's avatar bryanl
Browse files

Add env update command



Env update updates the libs for an environment.

Fixes #454
Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent a77a1a24
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package actions
import (
"fmt"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/pkg/lib"
)
// RunEnvUpdate runs `env update`.
func RunEnvUpdate(m map[string]interface{}) error {
a, err := newEnvUpdate(m)
if err != nil {
return err
}
return a.run()
}
// EnvUpdate updates ksonnet lib for an environment.
type EnvUpdate struct {
app app.App
envName string
genLibFn func(app.App, string, string) error
}
// RunEnvUpdate runs `env update`
func newEnvUpdate(m map[string]interface{}) (*EnvUpdate, error) {
ol := newOptionLoader(m)
eu := &EnvUpdate{
app: ol.LoadApp(),
envName: ol.LoadString(OptionEnvName),
genLibFn: genLib,
}
if ol.err != nil {
return nil, ol.err
}
return eu, nil
}
func (eu *EnvUpdate) run() error {
envSpec, err := eu.app.Environment(eu.envName)
if err != nil {
return err
}
k8sSpecFlag := fmt.Sprintf("version:%s", envSpec.KubernetesVersion)
libPath, err := eu.app.LibPath(eu.envName)
if err != nil {
return err
}
return eu.genLibFn(eu.app, k8sSpecFlag, libPath)
}
func genLib(a app.App, k8sSpecFlag, libPath string) error {
libManager, err := lib.NewManager(k8sSpecFlag, a.Fs(), libPath)
if err != nil {
return err
}
if err = a.Fs().RemoveAll(libPath); err != nil {
return err
}
return libManager.GenerateLibData(false)
}
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package actions
import (
"testing"
"github.com/ksonnet/ksonnet/metadata/app"
amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEnvUpdate(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
envSpec := &app.EnvironmentSpec{
KubernetesVersion: "v1.8.9",
}
appMock.On("Environment", "envName").Return(envSpec, nil)
appMock.On("LibPath", "envName").Return("/app/lib/v1.8.9", nil)
in := map[string]interface{}{
OptionApp: appMock,
OptionEnvName: "envName",
}
a, err := newEnvUpdate(in)
require.NoError(t, err)
a.genLibFn = func(_ app.App, k8sSpecFlag, libPath string) error {
assert.Equal(t, "version:v1.8.9", k8sSpecFlag)
assert.Equal(t, "/app/lib/v1.8.9", libPath)
return nil
}
err = a.run()
require.NoError(t, err)
})
}
...@@ -35,6 +35,7 @@ const ( ...@@ -35,6 +35,7 @@ const (
actionEnvRm actionEnvRm
actionEnvSet actionEnvSet
actionEnvTargets actionEnvTargets
actionEnvUpdate
actionImport actionImport
actionInit actionInit
actionModuleCreate actionModuleCreate
...@@ -76,6 +77,7 @@ var ( ...@@ -76,6 +77,7 @@ var (
actionEnvRm: actions.RunEnvRm, actionEnvRm: actions.RunEnvRm,
actionEnvSet: actions.RunEnvSet, actionEnvSet: actions.RunEnvSet,
actionEnvTargets: actions.RunEnvTargets, actionEnvTargets: actions.RunEnvTargets,
actionEnvUpdate: actions.RunEnvUpdate,
actionImport: actions.RunImport, actionImport: actions.RunImport,
actionInit: actions.RunInit, actionInit: actions.RunInit,
actionModuleCreate: actions.RunModuleCreate, actionModuleCreate: actions.RunModuleCreate,
......
...@@ -41,6 +41,7 @@ var ( ...@@ -41,6 +41,7 @@ var (
"list": "List all environments in a ksonnet application", "list": "List all environments in a ksonnet application",
"rm": "Delete an environment from a ksonnet application", "rm": "Delete an environment from a ksonnet application",
"set": "Set environment-specific fields (name, namespace, server)", "set": "Set environment-specific fields (name, namespace, server)",
"update": "Updates the libs for an environment",
} }
) )
......
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"github.com/ksonnet/ksonnet/actions"
"github.com/spf13/cobra"
)
var envUpdateCmd = &cobra.Command{
Use: "update <env-name>",
Short: envShortDesc["update"],
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("'env update' takes a single argument, that is the name of the environment")
}
m := map[string]interface{}{
actions.OptionApp: ka,
actions.OptionEnvName: args[0],
}
return runAction(actionEnvUpdate, m)
},
Long: `
The ` + "`update`" + ` command updates libraries for an environment.
### Related Commands
* ` + "`ks env list` " + `— ` + protoShortDesc["list"] + `
* ` + "`ks env add` " + `— ` + protoShortDesc["add"] + `
* ` + "`ks env set` " + `— ` + protoShortDesc["set"] + `
* ` + "`ks delete` " + `— ` + `Delete all the app components running in an environment (cluster)` + `
### Syntax
`,
Example: `
# Update the environment 'us-west/staging' libs.
ks env update us-west/staging`,
}
func init() {
envCmd.AddCommand(envUpdateCmd)
}
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"testing"
"github.com/ksonnet/ksonnet/actions"
)
func Test_envUpdateCmd(t *testing.T) {
cases := []cmdTestCase{
{
name: "in general",
args: []string{"env", "update", "prod"},
action: actionEnvUpdate,
expected: map[string]interface{}{
actions.OptionApp: ka,
actions.OptionEnvName: "prod",
},
},
}
runTestCmd(t, cases)
}
...@@ -83,4 +83,5 @@ ks env [flags] ...@@ -83,4 +83,5 @@ ks env [flags]
* [ks env rm](ks_env_rm.md) - Delete an environment from a ksonnet application * [ks env rm](ks_env_rm.md) - Delete an environment from a ksonnet application
* [ks env set](ks_env_set.md) - Set environment-specific fields (name, namespace, server) * [ks env set](ks_env_set.md) - Set environment-specific fields (name, namespace, server)
* [ks env targets](ks_env_targets.md) - targets * [ks env targets](ks_env_targets.md) - targets
* [ks env update](ks_env_update.md) - Updates the libs for an environment
## ks env update
Updates the libs for an environment
### Synopsis
The `update` command updates libraries for an environment.
### Related Commands
* `ks env list` — List all locally available ksonnet prototypes
* `ks env add`
* `ks env set`
* `ks delete` — Delete all the app components running in an environment (cluster)
### Syntax
```
ks env update <env-name> [flags]
```
### Examples
```
# Update the environment 'us-west/staging' libs.
ks env update us-west/staging
```
### Options
```
-h, --help help for update
```
### Options inherited from parent commands
```
--as string Username to impersonate for the operation
--as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
--certificate-authority string Path to a cert file for the certificate authority
--client-certificate string Path to a client certificate file for TLS
--client-key string Path to a client key file for TLS
--cluster string The name of the kubeconfig cluster to use
--context string The name of the kubeconfig context to use
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
--kubeconfig string Path to a kubeconfig file. Alternative to env var $KUBECONFIG.
-n, --namespace string If present, the namespace scope for this CLI request
--password string Password for basic authentication to the API server
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
--server string The address and port of the Kubernetes API server
--token string Bearer token for authentication to the API server
--user string The name of the kubeconfig user to use
--username string Username for basic authentication to the API server
-v, --verbose count[=-1] Increase verbosity. May be given multiple times.
```
### SEE ALSO
* [ks env](ks_env.md) - Manage ksonnet environments
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"path/filepath" "path/filepath"
"sort" "sort"
"github.com/ksonnet/ksonnet/metadata/lib" "github.com/ksonnet/ksonnet/pkg/lib"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/ksonnet/ksonnet/metadata/lib" "github.com/ksonnet/ksonnet/pkg/lib"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/afero" "github.com/spf13/afero"
......
Markdown is supported
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