forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configs.go
227 lines (183 loc) · 4.64 KB
/
configs.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package empire
import (
"database/sql"
"database/sql/driver"
"fmt"
"sort"
"strings"
"github.com/jinzhu/gorm"
"github.com/lib/pq/hstore"
"golang.org/x/net/context"
)
// Config represents a collection of environment variables.
type Config struct {
// A unique uuid representing this Config.
ID string
// The environment variables in this config.
Vars Vars
// The id of the app that this config relates to.
AppID string
// The app that this config relates to.
App *App
}
// newConfig initializes a new config based on the old config, with the new
// variables provided.
func newConfig(old *Config, vars Vars) *Config {
v := mergeVars(old.Vars, vars)
return &Config{
AppID: old.AppID,
Vars: v,
}
}
// Variable represents the name of an environment variable.
type Variable string
// Vars represents a variable -> value mapping.
type Vars map[Variable]*string
// Scan implements the sql.Scanner interface.
func (v *Vars) Scan(src interface{}) error {
h := hstore.Hstore{}
if err := h.Scan(src); err != nil {
return err
}
vars := make(Vars)
for k, v := range h.Map {
// Go reuses the same address space for v, so &v.String would always
// return the same address
tmp := v.String
vars[Variable(k)] = &tmp
}
*v = vars
return nil
}
// Value implements the driver.Value interface.
func (v Vars) Value() (driver.Value, error) {
m := make(map[string]sql.NullString)
for k, v := range v {
m[string(k)] = sql.NullString{
Valid: true,
String: *v,
}
}
h := hstore.Hstore{
Map: m,
}
return h.Value()
}
// ConfigsQuery is a scope implementation for common things to filter releases
// by.
type ConfigsQuery struct {
// If provided, returns finds the config with the given id.
ID *string
// If provided, filters configs for the given app.
App *App
}
// scope implements the scope interface.
func (q ConfigsQuery) scope(db *gorm.DB) *gorm.DB {
var scope composedScope
if q.ID != nil {
scope = append(scope, idEquals(*q.ID))
}
if q.App != nil {
scope = append(scope, forApp(q.App))
}
return scope.scope(db)
}
// configsFind returns the first matching config.
func configsFind(db *gorm.DB, scope scope) (*Config, error) {
var config Config
scope = composedScope{order("created_at desc"), scope}
return &config, first(db, scope, &config)
}
// ConfigsCreate inserts a Config in the database.
func configsCreate(db *gorm.DB, config *Config) (*Config, error) {
return config, db.Create(config).Error
}
type configsService struct {
*Empire
}
func (s *configsService) Set(ctx context.Context, db *gorm.DB, opts SetOpts) (*Config, error) {
app, vars := opts.App, opts.Vars
old, err := s.Config(db, app)
if err != nil {
return nil, err
}
c, err := configsCreate(db, newConfig(old, vars))
if err != nil {
return c, err
}
release, err := releasesFind(db, ReleasesQuery{App: app})
if err != nil {
if err == gorm.RecordNotFound {
err = nil
}
return c, err
}
// Create new release based on new config and old slug
_, err = s.releases.CreateAndRelease(ctx, db, &Release{
App: release.App,
Config: c,
Slug: release.Slug,
Description: configsApplyReleaseDesc(opts),
}, nil)
return c, err
}
// Returns configs for latest release or the latest configs if there are no releases.
func (s *configsService) Config(db *gorm.DB, app *App) (*Config, error) {
r, err := releasesFind(db, ReleasesQuery{App: app})
if err != nil {
if err == gorm.RecordNotFound {
// It's possible to have config without releases, this handles that.
c, err := configsFind(db, ConfigsQuery{App: app})
if err != nil {
if err == gorm.RecordNotFound {
// Return an empty config.
return &Config{
AppID: app.ID,
App: app,
Vars: make(Vars),
}, nil
}
return nil, err
}
return c, nil
}
return nil, err
}
return r.Config, nil
}
// mergeVars copies all of the vars from a, and merges b into them, returning a
// new Vars.
func mergeVars(old, new Vars) Vars {
vars := make(Vars)
for n, v := range old {
vars[n] = v
}
for n, v := range new {
if v == nil {
delete(vars, n)
} else {
vars[n] = v
}
}
return vars
}
// configsApplyReleaseDesc formats a release description based on the config variables
// being applied.
func configsApplyReleaseDesc(opts SetOpts) string {
vars := opts.Vars
verb := "Set"
plural := ""
if len(vars) > 1 {
plural = "s"
}
keys := make(sort.StringSlice, 0, len(vars))
for k, v := range vars {
keys = append(keys, string(k))
if v == nil {
verb = "Unset"
}
}
keys.Sort()
desc := fmt.Sprintf("%s %s config var%s", verb, strings.Join(keys, ", "), plural)
return appendMessageToDescription(desc, opts.User, opts.Message)
}