-
Notifications
You must be signed in to change notification settings - Fork 4
/
scope.go
106 lines (85 loc) · 2.92 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Ultimate Provisioner: UP cmd
// Copyright (c) 2019 Stephen Cheng and contributors
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package impl
import (
"github.com/imdario/mergo"
ms "github.com/mitchellh/mapstructure"
"github.com/mohae/deepcopy"
"github.com/spf13/viper"
"github.com/upcmd/up/model/core"
u "github.com/upcmd/up/utils"
"io/ioutil"
)
type Scope struct {
Name string
Ref string
RefDir string
Members []string
Vars core.Cache
Dvars Dvars
}
type Scopes []Scope
func DecryptAndRegister(securetag *u.SecureSetting, dvar *Dvar, contextVars *core.Cache, mergeTarget *core.Cache) {
s := securetag
if s == nil {
u.InvalidAndExit("check secure setting", "secure setting has to be explicit in dvar secure node, or as a default setting in upconfig.yml")
}
var encryptionkey string
if s.KeyRef != "" {
data, err := ioutil.ReadFile(s.KeyRef)
u.LogErrorAndExit("load secure key from ref file", err, "please fix file loading problem")
encryptionkey = string(data)
}
if s.Key != "" {
encryptionkey = (*contextVars).Get(s.Key).(string)
}
encrypted := dvar.Rendered
if encrypted != "" && encryptionkey != "" {
data := map[string]string{"enc_key": encryptionkey, "encrypted": encrypted}
decrypted := Render("{{ decryptAES .enc_key .encrypted}}", data)
secureName := u.Spf("%s_%s", "secure", dvar.Name)
(*mergeTarget).Put(secureName, decrypted)
} else {
u.InvalidAndExit("dvar decrypt", u.Spf("please double check secure settings for [%s]", dvar.Name))
}
}
func GlobalVarsMergedWithDvars(scope *Scope) (vars *core.Cache) {
return VarsMergedWithDvars(scope.Name, &scope.Vars, &scope.Dvars, &(scope.Vars))
}
func ScopeVarsMergedWithDvars(scope *Scope, contextMergedVars *core.Cache) *core.Cache {
return VarsMergedWithDvars(scope.Name, &scope.Vars, &scope.Dvars, contextMergedVars)
}
/*
given vars as base vars space to expand from, expand dvars against contextVars
*/
func VarsMergedWithDvars(mark string, baseVars *core.Cache, dvars *Dvars, contextMergedVars *core.Cache) *core.Cache {
var mergedVars core.Cache
mergedVars = deepcopy.Copy(*baseVars).(core.Cache)
if dvars != nil {
expandedVars := dvars.Expand(mark, contextMergedVars)
mergo.Merge(&mergedVars, expandedVars, mergo.WithOverride)
}
u.Pfvvvvv("scope[%s] merged: %s", mark, u.Sppmsg(mergedVars))
//procDvarsNew(dvars, &mergedVars)
return &mergedVars
}
func loadRefVars(yamlroot *viper.Viper) *core.Cache {
scopesData := yamlroot.Get("vars")
vars := core.Cache{}
err := ms.Decode(scopesData, &vars)
u.Dvvvvv(vars)
u.LogError("load ref vars", err)
return &vars
}
//get instance vars from scope definition, eg dev
func (ss *Scopes) GetInstanceVars(instanceName string) *core.Cache {
for _, s := range *ss {
if s.Name == instanceName {
return &s.Vars
}
}
return nil
}