forked from coreos/ignition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
243 lines (212 loc) · 6.97 KB
/
main.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2015 CoreOS, Inc.
//
// 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 main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/coreos/ignition/v2/config"
"github.com/coreos/ignition/v2/internal/apply"
"github.com/coreos/ignition/v2/internal/exec"
"github.com/coreos/ignition/v2/internal/exec/stages"
_ "github.com/coreos/ignition/v2/internal/exec/stages/disks"
_ "github.com/coreos/ignition/v2/internal/exec/stages/fetch"
_ "github.com/coreos/ignition/v2/internal/exec/stages/fetch_offline"
_ "github.com/coreos/ignition/v2/internal/exec/stages/files"
_ "github.com/coreos/ignition/v2/internal/exec/stages/kargs"
_ "github.com/coreos/ignition/v2/internal/exec/stages/mount"
_ "github.com/coreos/ignition/v2/internal/exec/stages/umount"
"github.com/coreos/ignition/v2/internal/log"
"github.com/coreos/ignition/v2/internal/platform"
"github.com/coreos/ignition/v2/internal/state"
"github.com/coreos/ignition/v2/internal/version"
"github.com/spf13/pflag"
)
func main() {
switch filepath.Base(os.Args[0]) {
case "ignition-apply":
ignitionApplyMain()
case "ignition-rmcfg":
ignitionRmCfgMain()
default:
// assume regular Ignition
ignitionMain()
}
}
func ignitionMain() {
flags := struct {
configCache string
fetchTimeout time.Duration
needNet string
platform platform.Name
root string
stage stages.Name
stateFile string
version bool
logToStdout bool
}{}
flag.StringVar(&flags.configCache, "config-cache", "/run/ignition.json", "where to cache the config")
flag.DurationVar(&flags.fetchTimeout, "fetch-timeout", exec.DefaultFetchTimeout, "initial duration for which to wait for config")
flag.StringVar(&flags.needNet, "neednet", "/run/ignition/neednet", "flag file to write from fetch-offline if networking is needed")
flag.Var(&flags.platform, "platform", fmt.Sprintf("current platform. %v", platform.Names()))
flag.StringVar(&flags.root, "root", "/", "root of the filesystem")
flag.Var(&flags.stage, "stage", fmt.Sprintf("execution stage. %v", stages.Names()))
flag.StringVar(&flags.stateFile, "state-file", "/run/ignition/state", "where to store internal state")
flag.BoolVar(&flags.version, "version", false, "print the version and exit")
flag.BoolVar(&flags.logToStdout, "log-to-stdout", false, "log to stdout instead of the system log when set")
flag.Parse()
if flags.version {
fmt.Printf("%s\n", version.String)
return
}
if flags.platform == "" {
fmt.Fprint(os.Stderr, "'--platform' must be provided\n")
os.Exit(2)
}
if flags.stage == "" {
fmt.Fprint(os.Stderr, "'--stage' must be provided\n")
os.Exit(2)
}
logger := log.New(flags.logToStdout)
defer logger.Close()
logger.Info(version.String)
logger.Info("Stage: %v", flags.stage)
platformConfig := platform.MustGet(flags.platform.String())
fetcher, err := platformConfig.NewFetcherFunc()(&logger)
if err != nil {
logger.Crit("failed to generate fetcher: %s", err)
os.Exit(3)
}
state, err := state.Load(flags.stateFile)
if err != nil {
logger.Crit("reading state: %s", err)
os.Exit(3)
}
engine := exec.Engine{
Root: flags.root,
FetchTimeout: flags.fetchTimeout,
Logger: &logger,
NeedNet: flags.needNet,
ConfigCache: flags.configCache,
PlatformConfig: platformConfig,
Fetcher: &fetcher,
State: &state,
}
err = engine.Run(flags.stage.String())
if statusErr := engine.PlatformConfig.Status(flags.stage.String(), *engine.Fetcher, err); statusErr != nil {
logger.Err("POST Status error: %v", statusErr.Error())
}
if err != nil {
logger.Crit("Ignition failed: %v", err.Error())
os.Exit(1)
}
if err := engine.State.Save(flags.stateFile); err != nil {
logger.Crit("writing state: %v", err)
os.Exit(1)
}
logger.Info("Ignition finished successfully")
}
func ignitionApplyMain() {
printVersion := false
flags := apply.Flags{}
pflag.BoolVar(&printVersion, "version", false, "print the version of ignition-apply")
pflag.StringVar(&flags.Root, "root", "/", "root of the filesystem")
pflag.BoolVar(&flags.IgnoreUnsupported, "ignore-unsupported", false, "ignore unsupported config sections")
pflag.BoolVar(&flags.Offline, "offline", false, "error out if config references remote resources")
pflag.Usage = func() {
fmt.Fprintf(pflag.CommandLine.Output(), "Usage: %s [options] config.ign\n", os.Args[0])
fmt.Fprintf(pflag.CommandLine.Output(), "Options:\n")
pflag.PrintDefaults()
}
pflag.Parse()
if printVersion {
fmt.Printf("%s\n", version.String)
return
}
if pflag.NArg() != 1 {
pflag.Usage()
os.Exit(1)
}
cfgArg := pflag.Arg(0)
logger := log.New(true)
defer logger.Close()
logger.Info(version.String)
var blob []byte
var err error
if cfgArg == "-" {
blob, err = ioutil.ReadAll(os.Stdin)
} else {
// XXX: could in the future support fetching directly from HTTP(S) + `-checksum|-insecure` ?
blob, err = ioutil.ReadFile(cfgArg)
}
if err != nil {
logger.Crit("couldn't read config: %v", err)
os.Exit(1)
}
cfg, rpt, err := config.Parse(blob)
logger.LogReport(rpt)
if rpt.IsFatal() || err != nil {
logger.Crit("couldn't parse config: %v", err)
os.Exit(1)
}
if err := apply.Run(cfg, flags, &logger); err != nil {
logger.Crit("failed to apply: %v", err)
os.Exit(1)
}
}
func ignitionRmCfgMain() {
flags := struct {
logToStdout bool
platform string
version bool
}{}
pflag.StringVar(&flags.platform, "platform", "", fmt.Sprintf("current platform. %v", platform.Names()))
pflag.BoolVar(&flags.logToStdout, "log-to-stdout", false, "log to stdout instead of the system log")
pflag.BoolVar(&flags.version, "version", false, "print the version and exit")
pflag.Usage = func() {
fmt.Fprintf(pflag.CommandLine.Output(), "Usage: %s [options]\n", os.Args[0])
fmt.Fprintf(pflag.CommandLine.Output(), "Options:\n")
pflag.PrintDefaults()
}
pflag.Parse()
if flags.version {
fmt.Printf("%s\n", version.String)
return
}
if pflag.NArg() != 0 {
pflag.Usage()
os.Exit(2)
}
if flags.platform == "" {
fmt.Fprint(os.Stderr, "'--platform' must be provided\n")
os.Exit(2)
}
logger := log.New(flags.logToStdout)
defer logger.Close()
logger.Info(version.String)
platformConfig := platform.MustGet(flags.platform)
fetcher, err := platformConfig.NewFetcherFunc()(&logger)
if err != nil {
logger.Crit("failed to generate fetcher: %s", err)
os.Exit(3)
}
if err := platformConfig.DelConfig(&fetcher); err != nil {
logger.Crit("couldn't delete config: %s", err)
os.Exit(1)
}
logger.Info("Successfully deleted config")
}