/
internalstart.go
179 lines (149 loc) · 6.6 KB
/
internalstart.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
/*
Copyright 2017 Ontario Systems
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 cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
log "github.com/sirupsen/logrus"
"github.com/ontariosystems/iscenv/internal/app"
"github.com/ontariosystems/iscenv/internal/cmd/flags"
"github.com/ontariosystems/iscenv/internal/plugins"
"github.com/ontariosystems/iscenv/iscenv"
"github.com/ontariosystems/isclib"
"github.com/spf13/cobra"
)
var internalStartCmd = &cobra.Command{
Use: "_start",
Short: "start/manage ISC product ",
Long: "manages an ISC product instance (this command is only available within containers)",
Run: internalStart,
}
var startStatus = app.NewStartStatus()
func init() {
if err := app.EnsureWithinContainer("_start"); err != nil {
return
}
rootCmd.AddCommand(internalStartCmd)
if err := addLifecyclerFlagsIfNeeded(internalStartCmd); err != nil {
logAndExit(app.ErrorLogger(log.StandardLogger(), err), app.ErrFailedToAddPluginFlags.Error())
}
flags.AddConfigFlagP(internalStartCmd, "instance", "i", "", "The instance to manage")
flags.AddConfigFlag(internalStartCmd, "ccontrol-path", "ccontrol", "The path to the ccontrol executable in the image")
flags.AddConfigFlag(internalStartCmd, "csession-path", "csession", "The path to the csession executable in the image")
addPrimaryCommandFlags(internalStartCmd)
}
func internalStart(cmd *cobra.Command, _ []string) {
const zoneInfoPath string = "/usr/share/zoneinfo"
const localTimePath string = "/etc/localtime"
const timeZonePath string = "/etc/timezone"
go startHealthCheck()
if tz := os.Getenv("TZ"); tz != "" {
log.WithField("time_zone", tz).Debug("Using provided time zone")
if _, err := os.Stat(localTimePath); err == nil {
if err := os.Remove(localTimePath); err != nil {
logAndExit(app.ErrorLogger(log.StandardLogger().WithField("path", localTimePath), err), "Failed to remove previous time zone")
}
log.WithField("path", localTimePath).Debug("Removed previous time zone")
}
if err := os.Symlink(path.Join(zoneInfoPath, tz), localTimePath); err != nil {
logAndExit(app.ErrorLogger(log.StandardLogger().WithField("path", localTimePath), err), "Failed to set time zone")
}
log.WithField("path", localTimePath).WithField("time_zone", tz).Debug("Set time zone")
if err := ioutil.WriteFile(timeZonePath, []byte(tz+"\n"), 0644); err != nil {
logAndExit(app.ErrorLogger(log.StandardLogger().WithField("path", timeZonePath), err), "Failed to set time zone")
}
log.WithField("path", timeZonePath).WithField("time_zone", tz).Debug("Set time zone")
}
// We can't use the closing activator because we need the plugins to keep running the whole time that _start runs
pluginsToActivate := getPluginsToActivate(rootCmd)
startStatus.ActivePlugins = pluginsToActivate
startStatus.Update(app.StartPhaseInitPlugins, nil, "")
var lcs []*plugins.ActivatedLifecycler
defer getActivatedLifecyclers(pluginsToActivate, getPluginArgs(), &lcs)()
for _, lc := range lcs {
startStatus.Update(app.StartPhaseInitPlugins, nil, lc.Id)
}
primaryCommand := flags.GetString(cmd, "primary-command")
primaryCommandNS := flags.GetString(cmd, "primary-command-ns")
if _, err := os.Stat(disablePrimaryCommandFile); err == nil {
if err := os.Remove(disablePrimaryCommandFile); err != nil {
logAndExit(app.ErrorLogger(log.StandardLogger(), err), "Failed to remove disable primary command file")
}
log.WithField("command", primaryCommand).WithField("namespace", primaryCommandNS).Warn("Disabling primary command; it will be re-enabled on next restart")
primaryCommand = ""
} else if !os.IsNotExist(err) {
logAndExit(app.ErrorLogger(log.StandardLogger(), err), "Failed to check for existence of disable primary command file")
}
startStatus.Update(app.StartPhaseInitManager, nil, "")
manager, err := app.NewISCInstanceManager(
flags.GetString(cmd, "instance"),
flags.GetString(cmd, "ccontrol-path"),
flags.GetString(cmd, "csession-path"),
primaryCommand,
primaryCommandNS,
)
if err != nil {
logAndExit(app.ErrorLogger(log.StandardLogger(), err), "Failed to create instance manager")
}
startStatus.Update(app.StartPhaseEventBeforeInstance, manager.Instance, "")
for _, lc := range lcs {
plog := lifecyclerLogger(lc, "BeforeInstance")
plog.Info("Executing plugin")
startStatus.Update(app.StartPhaseEventBeforeInstance, nil, lc.Id)
if err := lc.Lifecycler.BeforeInstance(manager.Instance); err != nil {
logAndExit(app.ErrorLogger(plog, err), app.ErrFailedEventPlugin.Error())
}
}
manager.InstanceRunningHandler = func(*isclib.Instance) {
startStatus.Update(app.StartPhaseEventWithInstance, manager.Instance, "")
for _, lc := range lcs {
plog := lifecyclerLogger(lc, "WithInstance")
plog.Info("Executing plugin")
startStatus.Update(app.StartPhaseEventWithInstance, nil, lc.Id)
if err := lc.Lifecycler.WithInstance(manager.Instance); err != nil {
logAndExit(app.ErrorLogger(plog, err), app.ErrFailedEventPlugin.Error())
}
}
startStatus.Update(app.StartPhaseInstanceRunning, manager.Instance, "")
}
if err := manager.Manage(); err != nil {
logAndExit(app.ErrorLogger(log.StandardLogger(), err), "Failed to manage instance")
}
startStatus.Update(app.StartPhaseEventAfterInstance, manager.Instance, "")
for _, lc := range lcs {
plog := lifecyclerLogger(lc, "AfterInstance")
plog.Info("Executing plugin")
startStatus.Update(app.StartPhaseEventAfterInstance, nil, lc.Id)
if err := lc.Lifecycler.AfterInstance(manager.Instance); err != nil {
logAndExit(app.ErrorLogger(plog, err), app.ErrFailedEventPlugin.Error())
}
}
startStatus.Update(app.StartPhaseShutdown, nil, "")
log.Info("Completed instance management")
}
func startHealthCheck() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(startStatus); err != nil {
logAndExit(app.ErrorLogger(log.StandardLogger(), err), "Failed to encode JSON")
}
})
http.ListenAndServe(fmt.Sprintf(":%d", iscenv.PortInternalHC), nil)
}
func lifecyclerLogger(lc *plugins.ActivatedLifecycler, method string) *log.Entry {
return app.PluginLogger(lc.Id, method, lc.Path)
}