-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.go
167 lines (143 loc) · 3.96 KB
/
app.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
// Copyright © 2015 Victor Antonovich <[email protected]>
//
// 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.
//go:generate go run cmd/genclient/main.go
//go:generate go run cmd/gendeps/main.go
//go:generate go run cmd/gentemplate/main.go
package main
import (
"fmt"
"os"
"time"
"github.com/golang/glog"
)
type App struct {
// Stop channel
stopCh chan struct{}
// Done channel
doneCh chan struct{}
// Do not write template output flag
dryRun bool
// Template output update period
updatePeriod time.Duration
// Dependency manager
dm *DependencyManager
// Templates to process
templates []*Template
}
func newApp(cfg *Config) (*App, error) {
// Create Kubernetes client
stopCh := make(chan struct{})
client, err := newClientForConfig(cfg, stopCh)
if err != nil {
close(stopCh)
return nil, err
}
// Create dependency manager
dm := newDependencyManager(client)
// Add all configured templates
templates, err := newTemplatesFromConfig(cfg, dm)
if err != nil {
return nil, err
}
doneCh := make(chan struct{})
return &App{
stopCh: stopCh,
doneCh: doneCh,
dm: dm,
templates: templates,
dryRun: cfg.DryRun,
updatePeriod: cfg.PollPeriod,
}, nil
}
func (app *App) Start() {
glog.V(1).Infoln("starting templates processing...")
defer close(app.doneCh)
defer glog.V(1).Infoln("templates processing stopped")
// Initial templates processing run
app.Run()
if app.updatePeriod.Nanoseconds() <= 0 {
<-app.stopCh
return
}
for {
select {
case <-app.stopCh:
return
case <-time.After(app.updatePeriod):
app.Run()
}
}
}
func (app *App) RunOnce() {
glog.V(1).Infoln("run once templates processing...")
app.Run()
glog.V(1).Infoln("templates processed")
}
func (app *App) Run() {
// Commands to execute are stored in list instead of map to ensure correct execution order
var commands []string
commandTimeouts := make(map[string]time.Duration)
// Flush cached dependencies
app.dm.flushCachedDependencies()
// Process templates
for _, t := range app.templates {
glog.V(2).Infof("processing template: %s", t.name)
if updated, err := t.Process(app.dryRun); err == nil {
if updated {
if !app.dryRun {
glog.V(2).Infof("template output updated: %s", t.name)
} else {
fmt.Printf("(dry-run) %s:\n%s", t.name, t.lastOutput)
}
if cmd := t.desc.Command; len(cmd) > 0 {
// Normalize command path, if applicable
if _, err := os.Stat(cmd); err == nil {
if c, err := NormPath(cmd); err == nil {
cmd = c
}
}
// Check template command is already in list of commands to execute
if !IsPresent(commands, cmd) {
glog.V(4).Infof("template %s: scheduled command: %q", t.name, cmd)
commands = append(commands, cmd)
commandTimeouts[cmd] = t.desc.CommandTimeout
} else {
glog.V(4).Infof("template %s: command already scheduled: %q", t.name, cmd)
}
}
} else {
glog.V(2).Infof("template output not changed: %s", t.name)
}
} else {
glog.Errorf("can't render %v", err)
}
}
// Execute commands for templates
for _, cmd := range commands {
if !app.dryRun {
glog.V(4).Infof("executing: %q", cmd)
if err := Execute(cmd, commandTimeouts[cmd]); err == nil {
glog.V(4).Infof("executed: %q", cmd)
} else {
glog.Errorf("command %q: %v", cmd, err)
}
} else {
fmt.Printf("(dry-run) executing: %q\n", cmd)
}
}
}
func (app *App) Stop() {
glog.V(1).Infoln("stopping templates processing...")
close(app.stopCh)
}