-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathenv.go
More file actions
233 lines (182 loc) · 5.12 KB
/
Copy pathenv.go
File metadata and controls
233 lines (182 loc) · 5.12 KB
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
// um env is a feature for ultramarine bootc specifically that lets you build local derivations "transactionally"
//
// it works similar to what you would do with `cargo add` or `pnpm add` would do, as in it:
//
// - adds the desired system package into a manifest file (`environment.toml`)
// - rebuilds the system derivation with the changes using Podman
// - optionally, attempt to apply the changes to the running system (unstable, may not work depending on the system's state)
// - use `bootc switch` to apply the update transactionally, marking the new local build as the next default
//
// Requires Podman to be already installed on the base image, or installed temporarily on the ephemeral environment (via `bootc usr-overlay`)
package main
// import (
// "fmt"
// "github.com/charmbracelet/huh"
// "github.com/urfave/cli/v2"
// "github.com/BurntSushi/toml"
// )
//
import (
"fmt"
"os"
"os/exec"
"github.com/Ultramarine-Linux/um/env"
"github.com/Ultramarine-Linux/um/pkg/util"
"github.com/charmbracelet/huh"
"github.com/urfave/cli/v2"
)
// todo: don't just print image name lol
func envStatus(c *cli.Context) error {
image, err := env.GetBootcImage()
if err != nil {
return err
}
fmt.Println("Bootc image:", image)
return nil
}
func envApplyChanges(c *cli.Context) error {
fmt.Println("Applying changes...")
manifest, err := env.LoadEnvManifest()
if err != nil {
return err
}
err = manifest.ApplyChanges()
if err != nil {
return err
}
fmt.Println("Changes applied successfully.")
return nil
}
// initializes a new environment by creating an environment.toml and a template Containerfile
func envInit(c *cli.Context) error {
var confirmed bool
baseImage, err := env.GetBootcImage()
if err != nil {
return err
}
if c.Bool("yes") {
confirmed = true
} else {
if err := huh.NewConfirm().
Title("Create a local bootc derivation?").
Description(fmt.Sprintf("This will create environment.toml and a template Containerfile based on `%s` at `%s`."+
"\n\n"+
"The system bootc image will be switched to `%s` and updates must now be managed via `um env update` rather than from the app store.", baseImage, env.UmEnvContext, env.UmEnvManagedImage)).
Affirmative("Initialize").
Negative("Cancel").
Value(&confirmed).
Run(); err != nil {
return err
}
}
if !confirmed {
fmt.Println("Aborting...")
return nil
}
if err := env.InitEnvironment(baseImage); err != nil {
return err
}
fmt.Println("Initialized environment in", env.UmEnvContext)
fmt.Println("Building initial derivation...")
if err := envBuild(c); err != nil {
return err
}
fmt.Println("Initial derivation built successfully, switching to umcli managed image...")
if err := env.EnvBootcSwitch(); err != nil {
return err
}
return nil
}
func envBuild(c *cli.Context) error {
util.SudoIfNeeded([]string{})
manifest, err := env.LoadEnvManifest()
if err != nil {
return err
}
if err := manifest.BuildContainerfile(); err != nil {
return err
}
fmt.Println("Containerfile built successfully.")
return nil
}
func handleApplyLive(c *cli.Context) error {
var applyLive bool
if c.Bool("apply-live") {
applyLive = true
}
if applyLive {
fmt.Println("Applying changes live...")
// enable bootc usr-overlay
if err := exec.Command("bootc", "usr-overlay").Run(); err != nil {
return err
}
if err := envApplyChanges(c); err != nil {
return err
}
}
return nil
}
// add a package to the environment
func envAddPackage(c *cli.Context) error {
util.SudoIfNeeded([]string{})
manifest, err := env.LoadEnvManifest()
if err != nil {
return err
}
for _, pkg := range c.Args().Slice() {
if manifest.AddPackage(pkg) {
fmt.Println("Adding package:", pkg)
} else {
fmt.Println("Package already exists:", pkg)
}
}
if err := manifest.Save(); err != nil {
return err
}
if err := handleApplyLive(c); err != nil {
return err
}
fmt.Println("Packages added successfully.")
fmt.Println("Added packages successfully. Commit pending changes with `um env update`")
return nil
}
func envRemovePackage(c *cli.Context) error {
util.SudoIfNeeded([]string{})
manifest, err := env.LoadEnvManifest()
if err != nil {
return err
}
for _, pkg := range c.Args().Slice() {
if manifest.RemovePackage(pkg) {
fmt.Println("Removed package from install list:", pkg)
} else {
fmt.Println("Adding package to removal list:", pkg)
}
}
if err := manifest.Save(); err != nil {
return err
}
if err := handleApplyLive(c); err != nil {
return err
}
fmt.Println("Packages removed successfully.")
fmt.Println("Commit pending changes with `um env update`")
return nil
}
func envUpdate(c *cli.Context) error {
util.SudoIfNeeded([]string{})
if err := envBuild(c); err != nil {
return err
}
fmt.Println("Rebuilt environment image, applying changes with bootc")
// bootc update actually pulls straight from containers-storage,
// so we can simply just do this instead of bootc switch again, assuming
// um env is already initialized
bootcCmd := exec.Command("bootc", "update")
bootcCmd.Stdout = os.Stdout
bootcCmd.Stderr = os.Stderr
if err := bootcCmd.Run(); err != nil {
return err
}
return nil
}