63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package node
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
monov1alpha1 "example.com/monok8s/pkg/apis/monok8s/v1alpha1"
|
|
"example.com/monok8s/pkg/controller/osimage"
|
|
)
|
|
|
|
func EngageControlGate(ctx context.Context, nctx *NodeContext) error {
|
|
gateFile := filepath.Join(monov1alpha1.EnvConfigDir, ".control-gate")
|
|
|
|
f, err := os.OpenFile(gateFile, os.O_CREATE|os.O_WRONLY, 0o644)
|
|
if err != nil {
|
|
return fmt.Errorf("touch %s: %w", gateFile, err)
|
|
}
|
|
defer f.Close()
|
|
|
|
return nil
|
|
}
|
|
|
|
func ReleaseControlGate(ctx context.Context, nctx *NodeContext) error {
|
|
gateFile := filepath.Join(monov1alpha1.EnvConfigDir, ".control-gate")
|
|
|
|
if err := os.Remove(gateFile); err != nil {
|
|
return fmt.Errorf("relate control gate: %w", err)
|
|
}
|
|
|
|
return WriteLastState(ctx, nctx)
|
|
}
|
|
|
|
// Required for detecting bootslot changes
|
|
func WriteLastState(ctx context.Context, nctx *NodeContext) error {
|
|
stBootPart := filepath.Join(monov1alpha1.EnvConfigDir, ".bootpart")
|
|
|
|
state, err := osimage.ReadBootState()
|
|
if err != nil {
|
|
return fmt.Errorf("read boot state: %w", err)
|
|
}
|
|
|
|
bootPart := state["BOOT_PART"]
|
|
if bootPart == "" {
|
|
return fmt.Errorf("BOOT_PART missing")
|
|
}
|
|
|
|
tmp := stBootPart + ".tmp"
|
|
if err := os.WriteFile(tmp, []byte(bootPart+"\n"), 0o644); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Rename(tmp, stBootPart); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func DiagTestDiskWrite(ctx context.Context, nctx *NodeContext) error {
|
|
return osimage.TestStreamToTarget(ctx, monov1alpha1.AltPartDeviceLink)
|
|
}
|