app001.go 7.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2018 The kubecfg 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.

16 17 18 19 20 21 22 23 24 25
package app

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

26
	"github.com/ghodss/yaml"
27 28 29 30 31 32 33 34 35 36 37 38
	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
	"github.com/spf13/afero"
)

const (
	// app001specJSON is the name for environment schema
	app001specJSON = "spec.json"
)

// App001 is a ksonnet 0.0.1 application.
type App001 struct {
bryanl's avatar
bryanl committed
39
	out io.Writer
40
	*baseApp
41 42 43 44 45
}

var _ App = (*App001)(nil)

// NewApp001 creates an App001 instance.
bryanl's avatar
bryanl committed
46
func NewApp001(fs afero.Fs, root string) *App001 {
47 48
	ba := newBaseApp(fs, root)

49
	return &App001{
50 51
		out:     os.Stdout,
		baseApp: ba,
bryanl's avatar
bryanl committed
52
	}
53 54 55 56
}

// AddEnvironment adds an environment spec to the app spec. If the spec already exists,
// it is overwritten.
57
func (a *App001) AddEnvironment(name, k8sSpecFlag string, spec *EnvironmentConfig, isOverride bool) error {
bryanl's avatar
bryanl committed
58 59
	// if it is an override, write the destination to override file. If not, do the normal thing.

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	envPath := filepath.Join(a.root, EnvironmentDirName, name)
	if err := a.fs.MkdirAll(envPath, DefaultFolderPermissions); err != nil {
		return err
	}

	specPath := filepath.Join(envPath, app001specJSON)

	b, err := json.Marshal(spec.Destination)
	if err != nil {
		return err
	}

	if err = afero.WriteFile(a.fs, specPath, b, DefaultFilePermissions); err != nil {
		return err
	}

bryanl's avatar
bryanl committed
76 77
	_, err = LibUpdater(a.fs, k8sSpecFlag, a.appLibPath(name), false)
	return err
78 79
}

80
func (a *App001) overrideDestintation(name string, envSpec *EnvironmentConfig) error {
bryanl's avatar
bryanl committed
81 82 83
	return nil
}

84 85
// Environment returns the spec for an environment. In 0.1.0, the file lives in
// /environments/name/spec.json.
86
func (a *App001) Environment(name string) (*EnvironmentConfig, error) {
87 88 89 90 91 92
	path := filepath.Join(a.root, EnvironmentDirName, name, app001specJSON)
	return read001EnvSpec(a.fs, name, path)
}

// Environments returns specs for all environments. In 0.1.0, the environment spec
// lives in spec.json files.
93 94
func (a *App001) Environments() (EnvironmentConfigs, error) {
	specs := EnvironmentConfigs{}
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

	root := filepath.Join(a.root, EnvironmentDirName)

	err := afero.Walk(a.fs, root, func(path string, fi os.FileInfo, err error) error {
		if fi.IsDir() {
			return nil
		}

		if fi.Name() == app001specJSON {
			dir := filepath.Dir(path)
			envName := strings.TrimPrefix(dir, root+"/")
			spec, err := read001EnvSpec(a.fs, envName, path)
			if err != nil {
				return err
			}

			specs[envName] = spec
		}

		return nil
	})
	if err != nil {
		return nil, err
	}

	return specs, nil
}

123 124 125 126 127
// Init initializes the App.
func (a *App001) Init() error {
	msg := "Your application's apiVersion is below 0.1.0. In order to use all ks features, you " +
		"can upgrade your application using `ks upgrade`."
	log.Warn(msg)
128

129 130
	return nil
}
131

132 133 134 135
// LibPath returns the lib path for an env environment.
func (a *App001) LibPath(envName string) (string, error) {
	return filepath.Join(a.envDir(envName), ".metadata"), nil
}
136

137
// Libraries returns application libraries.
138
func (a *App001) Libraries() (LibraryConfigs, error) {
bryanl's avatar
bryanl committed
139 140
	if err := a.load(); err != nil {
		return nil, errors.Wrap(err, "load configuration")
bryanl's avatar
bryanl committed
141
	}
bryanl's avatar
bryanl committed
142 143

	return a.config.Libraries, nil
144
}
145

146
// Registries returns application registries.
147
func (a *App001) Registries() (RegistryConfigs, error) {
bryanl's avatar
bryanl committed
148 149 150 151
	if err := a.load(); err != nil {
		return nil, errors.Wrap(err, "load configuration")
	}

152
	registries := RegistryConfigs{}
bryanl's avatar
bryanl committed
153 154
	for k, v := range a.config.Registries {
		registries[k] = v
bryanl's avatar
bryanl committed
155 156
	}

bryanl's avatar
bryanl committed
157 158 159 160 161
	for k, v := range a.overrides.Registries {
		registries[k] = v
	}

	return registries, nil
162 163 164
}

// RemoveEnvironment removes an environment.
bryanl's avatar
bryanl committed
165
func (a *App001) RemoveEnvironment(envName string, override bool) error {
166
	a.Fs().RemoveAll(a.envDir(envName))
167 168 169
	return nil
}

170
// RenameEnvironment renames environments.
bryanl's avatar
bryanl committed
171
func (a *App001) RenameEnvironment(from, to string, override bool) error {
172 173 174 175 176 177 178 179 180
	return moveEnvironment(a.fs, a.root, from, to)
}

// UpdateTargets returns an error since 0.0.1 based applications don't have support
// for targets.
func (a *App001) UpdateTargets(envName string, targets []string) error {
	return errors.New("ks apps with version 0.0.1 do not have support for targets")
}

181 182
// Upgrade upgrades the app to the latest apiVersion.
func (a *App001) Upgrade(dryRun bool) error {
bryanl's avatar
bryanl committed
183 184
	if err := a.load(); err != nil {
		return errors.Wrap(err, "load configuration")
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	}

	if dryRun {
		fmt.Fprintf(a.out, "\n[dry run] Upgrading application settings from version 0.0.1 to to 0.1.0.\n")
	}

	envs, err := a.Environments()
	if err != nil {
		return err
	}

	if dryRun {
		fmt.Fprintf(a.out, "[dry run] Converting 0.0.1 environments to 0.1.0a:\n")
	}
	for _, env := range envs {
		a.convertEnvironment(env.Path, dryRun)
	}

bryanl's avatar
bryanl committed
203
	a.config.APIVersion = "0.1.0"
204 205

	if dryRun {
bryanl's avatar
bryanl committed
206
		data, err := yaml.Marshal(a.config)
207 208 209 210 211 212 213 214 215
		if err != nil {
			return err
		}

		fmt.Fprintf(a.out, "\n[dry run] Upgraded app.yaml:\n%s\n", string(data))
		fmt.Fprintf(a.out, "[dry run] You can preform the migration by running `ks upgrade`.\n")
		return nil
	}

bryanl's avatar
bryanl committed
216
	return a.save()
217 218
}

219 220 221 222 223 224
type k8sSchema struct {
	Info struct {
		Version string `json:"version,omitempty"`
	} `json:"info,omitempty"`
}

225
func read001EnvSpec(fs afero.Fs, name, path string) (*EnvironmentConfig, error) {
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	b, err := afero.ReadFile(fs, path)
	if err != nil {
		return nil, err
	}

	var s EnvironmentDestinationSpec
	if err = json.Unmarshal(b, &s); err != nil {
		return nil, err
	}

	if s.Namespace == "" {
		s.Namespace = "default"
	}

	envPath := filepath.Dir(path)
	swaggerPath := filepath.Join(envPath, ".metadata", "swagger.json")

	b, err = afero.ReadFile(fs, swaggerPath)
	if err != nil {
		return nil, err
	}

	var ks k8sSchema
	if err = json.Unmarshal(b, &ks); err != nil {
		return nil, err
	}

	if ks.Info.Version == "" {
		return nil, errors.New("unable to determine environment Kubernetes version")
	}

257
	spec := EnvironmentConfig{
258 259 260 261 262 263 264 265
		Path:              name,
		Destination:       &s,
		KubernetesVersion: ks.Info.Version,
	}

	return &spec, nil
}

266
func (a *App001) convertEnvironment(envName string, dryRun bool) error {
bryanl's avatar
bryanl committed
267 268
	if err := a.load(); err != nil {
		return errors.Wrap(err, "load configuration")
269 270
	}

bryanl's avatar
bryanl committed
271 272
	path := filepath.Join(a.root, EnvironmentDirName, envName, "spec.json")
	env, err := read001EnvSpec(a.fs, envName, path)
bryanl's avatar
bryanl committed
273 274 275 276
	if err != nil {
		return err
	}

bryanl's avatar
bryanl committed
277
	a.config.Environments[envName] = env
278 279 280 281 282 283 284 285 286 287 288 289

	if dryRun {
		fmt.Fprintf(a.out, "[dry run]\t* adding the environment description in environment `%s to `app.yaml`.\n",
			envName)
		return nil
	}

	if err = a.fs.Remove(path); err != nil {
		return err
	}

	k8sSpecFlag := fmt.Sprintf("version:%s", env.KubernetesVersion)
bryanl's avatar
bryanl committed
290
	_, err = LibUpdater(a.fs, k8sSpecFlag, app010LibPath(a.root), true)
291 292 293 294
	if err != nil {
		return err
	}

bryanl's avatar
bryanl committed
295
	return a.save()
bryanl's avatar
bryanl committed
296 297 298 299
}

func (a *App001) appLibPath(envName string) string {
	return filepath.Join(a.root, EnvironmentDirName, envName, ".metadata")
300 301
}

302 303 304 305
func (a *App001) envDir(envName string) string {
	envParts := strings.Split(envName, "/")
	envRoot := filepath.Join(a.Root(), EnvironmentDirName)
	return filepath.Join(append([]string{envRoot}, envParts...)...)
306
}