base_app.go 6.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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 app

import (
	"path/filepath"
bryanl's avatar
bryanl committed
20
	"sync"
21

bryanl's avatar
bryanl committed
22 23
	"github.com/ghodss/yaml"
	"github.com/pkg/errors"
24
	log "github.com/sirupsen/logrus"
25 26 27 28 29 30
	"github.com/spf13/afero"
)

type baseApp struct {
	root string
	fs   afero.Fs
bryanl's avatar
bryanl committed
31 32 33 34 35

	config    *Spec
	overrides *Override

	mu sync.Mutex
36 37 38 39
}

func newBaseApp(fs afero.Fs, root string) *baseApp {
	return &baseApp{
bryanl's avatar
bryanl committed
40 41 42 43
		fs:     fs,
		root:   root,
		config: &Spec{},
		overrides: &Override{
44
			Environments: EnvironmentConfigs{},
45
			Registries:   RegistryConfigs{},
bryanl's avatar
bryanl committed
46
		},
47 48 49
	}
}

bryanl's avatar
bryanl committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
func (ba *baseApp) CurrentEnvironment() string {
	currentPath := filepath.Join(ba.root, currentEnvName)
	data, err := afero.ReadFile(ba.fs, currentPath)
	if err != nil {
		return ""
	}

	return string(data)
}

func (ba *baseApp) SetCurrentEnvironment(name string) error {
	currentPath := filepath.Join(ba.root, currentEnvName)
	return afero.WriteFile(ba.fs, currentPath, []byte(name), DefaultFilePermissions)
}

bryanl's avatar
bryanl committed
65 66 67 68 69 70
func (ba *baseApp) configPath() string {
	return filepath.Join(ba.root, "app.yaml")
}

func (ba *baseApp) overridePath() string {
	return filepath.Join(ba.root, "app.override.yaml")
71 72
}

bryanl's avatar
bryanl committed
73
func (ba *baseApp) save() error {
74 75 76
	log := log.WithField("action", "baseApp.save")
	log.Debugf("called [%p]", ba)

bryanl's avatar
bryanl committed
77 78 79
	ba.mu.Lock()
	defer ba.mu.Unlock()

80 81 82 83 84 85 86 87
	if ba.config == nil {
		return errors.Errorf("cannot save nil app configuration")
	}

	// Signal we have converted to new app version
	ba.config.APIVersion = DefaultAPIVersion
	log.Debugf("saving app version %v", ba.config.APIVersion)

bryanl's avatar
bryanl committed
88
	configData, err := yaml.Marshal(ba.config)
89
	if err != nil {
bryanl's avatar
bryanl committed
90 91 92 93 94
		return errors.Wrap(err, "convert application configuration to YAML")
	}

	if err = afero.WriteFile(ba.fs, ba.configPath(), configData, DefaultFilePermissions); err != nil {
		return errors.Wrapf(err, "write %s", ba.configPath())
95 96
	}

97
	if err = removeOverride(ba.fs, ba.root); err != nil {
bryanl's avatar
bryanl committed
98 99 100
		return errors.Wrap(err, "clean overrides")
	}

101 102
	if ba.overrides.IsDefined() {
		return SaveOverride(defaultYAMLEncoder, ba.fs, ba.root, ba.overrides)
bryanl's avatar
bryanl committed
103 104 105 106 107 108
	}

	return nil
}

func (ba *baseApp) load() error {
109 110 111
	log := log.WithField("action", "baseApp.load")
	log.Debugf("called [%p]", ba)

bryanl's avatar
bryanl committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125
	ba.mu.Lock()
	defer ba.mu.Unlock()

	configData, err := afero.ReadFile(ba.fs, ba.configPath())
	if err != nil {
		return errors.Wrapf(err, "read %s", ba.configPath())
	}

	var config Spec
	if err = yaml.Unmarshal(configData, &config); err != nil {
		return errors.Wrapf(err, "unmarshal application YAML config")
	}

	exists, err := afero.Exists(ba.fs, ba.overridePath())
126 127 128 129
	if err != nil {
		return err
	}

bryanl's avatar
bryanl committed
130
	if len(config.Environments) == 0 {
131
		config.Environments = EnvironmentConfigs{}
bryanl's avatar
bryanl committed
132 133 134
	}

	if len(config.Registries) == 0 {
135
		config.Registries = RegistryConfigs{}
bryanl's avatar
bryanl committed
136 137 138
	}

	override := Override{
139
		Environments: EnvironmentConfigs{},
140
		Registries:   RegistryConfigs{},
bryanl's avatar
bryanl committed
141 142 143 144 145 146 147 148 149 150
	}
	if exists {
		overrideData, err := afero.ReadFile(ba.fs, ba.overridePath())
		if err != nil {
			return errors.Wrapf(err, "read %s", ba.overridePath())
		}
		if err = yaml.Unmarshal(overrideData, &override); err != nil {
			return errors.Wrapf(err, "unmarshal override YAML config")
		}

151 152
		if err = override.Validate(); err != nil {
			return errors.Wrap(err, "validating override")
bryanl's avatar
bryanl committed
153 154 155
		}

		if len(override.Environments) == 0 {
156
			override.Environments = EnvironmentConfigs{}
bryanl's avatar
bryanl committed
157 158 159
		}

		if len(override.Registries) == 0 {
160
			override.Registries = RegistryConfigs{}
bryanl's avatar
bryanl committed
161 162
		}

163 164 165 166 167 168 169 170
		for k := range override.Registries {
			override.Registries[k].isOverride = true
		}

		for k := range override.Environments {
			override.Environments[k].isOverride = true
		}

bryanl's avatar
bryanl committed
171 172 173 174 175 176 177 178
	}

	ba.overrides = &override
	ba.config = &config

	return ba.config.validate()
}

179
func (ba *baseApp) AddRegistry(newReg *RegistryConfig, isOverride bool) error {
bryanl's avatar
bryanl committed
180 181 182 183
	if err := ba.load(); err != nil {
		return errors.Wrap(err, "load configuration")
	}

184 185 186 187
	if newReg.Name == "" {
		return ErrRegistryNameInvalid
	}

bryanl's avatar
bryanl committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201
	if isOverride {
		_, exists := ba.overrides.Registries[newReg.Name]
		if exists {
			return ErrRegistryExists
		}

		newReg.isOverride = true

		ba.overrides.Registries[newReg.Name] = newReg
		return ba.save()
	}

	_, exists := ba.config.Registries[newReg.Name]
	if exists {
202 203 204
		return ErrRegistryExists
	}

bryanl's avatar
bryanl committed
205 206
	newReg.isOverride = false
	ba.config.Registries[newReg.Name] = newReg
207

bryanl's avatar
bryanl committed
208
	return ba.save()
209 210
}

211
func (ba *baseApp) UpdateLib(name string, libSpec *LibraryConfig) error {
bryanl's avatar
bryanl committed
212 213
	if err := ba.load(); err != nil {
		return errors.Wrap(err, "load configuration")
214 215
	}

bryanl's avatar
bryanl committed
216 217
	ba.config.Libraries[name] = libSpec
	return ba.save()
218 219
}

220
// UpdateRegistry updates a registry spec and persists in app[.override].yaml
221
func (ba *baseApp) UpdateRegistry(spec *RegistryConfig) error {
222 223 224 225 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
	if err := ba.load(); err != nil {
		return errors.Wrap(err, "load configuration")
	}

	if spec.Name == "" {
		return ErrRegistryNameInvalid
	}

	// Figure out where the registry is defined (app or override)
	var ok, okOverride bool
	if ba.config != nil {
		_, ok = ba.config.Registries[spec.Name]
	}
	if ba.overrides != nil {
		_, okOverride = ba.overrides.Registries[spec.Name]
	}

	if !ok && !okOverride {
		return errors.Errorf("registry not found: %v", spec.Name)
	}

	if ok && okOverride {
		return errors.Errorf("registry %v found in both app.yaml and app.override.yaml", spec.Name)
	}

	if ok {
		ba.config.Registries[spec.Name] = spec
	} else {
		ba.overrides.Registries[spec.Name] = spec
	}

	return ba.save()
}

256 257 258 259 260 261 262 263 264
func (ba *baseApp) Fs() afero.Fs {
	return ba.fs
}

func (ba *baseApp) Root() string {
	return ba.root
}

func (ba *baseApp) EnvironmentParams(envName string) (string, error) {
bryanl's avatar
bryanl committed
265 266 267
	if envName == "" {
		return "", errors.New("environment name is blank")
	}
268 269 270
	envParamsPath := filepath.Join(ba.Root(), EnvironmentDirName, envName, "params.libsonnet")
	b, err := afero.ReadFile(ba.Fs(), envParamsPath)
	if err != nil {
bryanl's avatar
bryanl committed
271
		return "", errors.Wrapf(err, "unable to read params for environment %s", envName)
272 273 274 275
	}

	return string(b), nil
}
bryanl's avatar
bryanl committed
276 277 278 279

func (ba *baseApp) VendorPath() string {
	return filepath.Join(ba.Root(), "vendor")
}