-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This transforms the Ignition binary to support the multicall convention. For maximum backwards compatibility, we default to the usual Ignition functionality without requiring an exact match. The first multicall use after `ignition` is `ignition-liveapply`, which is the main goal of this patch. This new entrypoint takes an Ignition config and applies it to the current rootfs (or one provided by `-root`). This tool could then be used as part of build processes where we want to bake an Ignition config, such as in CoreOS layering: https://github.com/coreos/enhancements/blob/main/os/coreos-layering.md This in turn should also help drain from the MCO the re-implementation of large parts of the Ignition spec. Another approach would be to build on top of `ignition -stage=files`, but in the end I think we want a cleaner dedicated UX for this workflow long-term (and in fact we should clean up the hacky places where we call Ignition like this). It shouldn't be used by end-users to live apply changes to their Ignition config. A check that we are in a container is added as a safeguard against this. By default, unsupported modifications (like partitioning) trigger errors, though one may pass `-ignore-unsupported` to override this. Also by default, remote resources are automatically fetched, though one may pass `-offline` to override this.
- Loading branch information
Showing
11 changed files
with
266 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2021 Red Hat, 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 liveapply | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/coreos/ignition/v2/config/util" | ||
"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/resource" | ||
"github.com/coreos/ignition/v2/internal/state" | ||
|
||
"github.com/coreos/ignition/v2/config/v3_4_experimental/types" | ||
) | ||
|
||
type Flags struct { | ||
Root string | ||
IgnoreUnsupported bool | ||
Offline bool | ||
} | ||
|
||
func inContainer() bool { | ||
if val, _ := os.LookupEnv("container"); val != "" { | ||
return true | ||
} | ||
|
||
paths := []string{"/run/.containerenv", "/.dockerenv"} | ||
for _, path := range paths { | ||
if _, err := os.Stat(path); err == nil { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func Run(cfg types.Config, flags Flags, logger *log.Logger) error { | ||
if !inContainer() { | ||
return errors.New("this tool is not designed to run on a host system; reprovision the machine instead") | ||
} | ||
|
||
fetcher := resource.Fetcher{ | ||
Logger: logger, | ||
Offline: flags.Offline, | ||
} | ||
|
||
state := state.State{} | ||
cfgFetcher := exec.ConfigFetcher{ | ||
Logger: logger, | ||
Fetcher: &fetcher, | ||
State: &state, | ||
} | ||
|
||
finalCfg, err := cfgFetcher.RenderConfig(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// verify upfront if we'll need networking but we're not allowed | ||
if flags.Offline { | ||
stage := stages.Get("fetch-offline").Create(logger, flags.Root, fetcher, &state) | ||
if err := stage.Run(finalCfg); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Order in which to apply live. This is overkill since effectively only | ||
// `files` supports it right now, but let's be extensible. Also ensures that | ||
// all stages are accounted for. | ||
stagesOrder := []string{"fetch-offline", "fetch", "kargs", "disks", "mount", "files", "umount"} | ||
allStages := stages.Names() | ||
if len(stagesOrder) != len(allStages) { | ||
panic(fmt.Sprintf("%v != %v", stagesOrder, allStages)) | ||
} | ||
|
||
for _, stageName := range stagesOrder { | ||
if !util.StrSliceContains(allStages, stageName) { | ||
panic(fmt.Sprintf("stage '%s' invalid", stageName)) | ||
} | ||
if strings.HasPrefix(stageName, "fetch") { | ||
continue // already fetched | ||
} | ||
stage := stages.Get(stageName).Create(logger, flags.Root, fetcher, &state) | ||
if err := stage.ApplyLive(finalCfg); err != nil { | ||
if err == stages.ErrUnsupportedLive && flags.IgnoreUnsupported { | ||
logger.Info("running stage '%s': ignoring unsupported: %v", stageName, err) | ||
continue | ||
} else { | ||
return fmt.Errorf("running stage '%s': %w", stageName, err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters