-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
237 lines (193 loc) · 6.47 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
// rootfsbuilder - A simple tool to build Debian/Ubuntu rootfs tarballs
// Copyright (C) 2023 Hugo Melder
//
// SPDX-License-Identifier: MIT
//
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"runtime"
"strings"
)
const (
Version = "0.2.0"
ExitCodeOK = 0
ExitCodeFailure = 1
)
// Configuration Enums
const (
ConfigVersionV1 = 1
DistributionDebian = "debian"
DistributionUbuntu = "ubuntu"
TarballTypeTar = "tar"
TarballTypeTarGz = "tar.gz"
PayloadTypeTar = "tar"
PayloadTypeTarGz = "tar.gz"
VariantMinbase = "minbase"
)
type ConfigurationV1 struct {
// The distribution to use
ConfigVersion int `json:"config_version"`
Name string `json:"name"`
Distribution string `json:"distribution"`
Release string `json:"release"`
Architecture string `json:"architecture"`
Mirror string `json:"mirror"`
TarballType string `json:"tarball_type"`
// Additional options for building a squashfs
SquashFS bool `json:"squashfs,omitempty"`
SquashFSArgs []string `json:"squashfs_args,omitempty"`
// Additional options for building the rootfs
// minbase etc. (specified in debootstrap with --variant)
Variant string `json:"variant,omitempty"`
AdditionalPackages []string `json:"additional_packages,omitempty"`
ExcludedPackages []string `json:"excluded_packages,omitempty"`
// Additional components to install: e.g. "main", "universe"
Components []string `json:"components,omitempty"`
// Extracted into the root directory of the rootfs
Payload string `json:"payload,omitempty"`
PayloadType string `json:"payload_type,omitempty"`
UseHostsResolvConf bool `json:"use_hosts_resolv_conf,omitempty"`
PostInstallCommand string `json:"post_install_command,omitempty"`
// Not part of the configuration file
absoluteConfigPath string
}
func main() {
version := flag.Bool("version", false, "Print version information and exit")
flag.Usage = func() {
fmt.Printf("Usage of rootfsbuilder:\n")
fmt.Println(" --version")
fmt.Println(" Print version information and exit")
fmt.Println()
fmt.Println(" [CONFIG_FILE1, CONFIG_FILE2, ...]")
fmt.Println(" One or more configuration files to be used by the rootfsbuilder")
fmt.Println()
fmt.Println("Examples:")
fmt.Println(" rootfsbuilder --version")
fmt.Println(" rootfsbuilder config1.yaml config2.yaml")
}
flag.Parse()
nonFlagArgs := flag.Args()
if *version {
fmt.Printf("rootfsbuilder version %s\n", Version)
os.Exit(ExitCodeOK)
}
// Check Operating System
if runtime.GOOS != "linux" {
fmt.Fprintf(os.Stderr, "rootfsbuilder must be run on Linux\n")
os.Exit(ExitCodeFailure)
}
debArch, err := HostToDebArch()
if err != nil {
fmt.Fprintf(os.Stderr, "Error while determining host architecture: %s\n", err)
os.Exit(ExitCodeFailure)
}
// Check if we have root privileges
if os.Geteuid() != 0 {
fmt.Fprintf(os.Stderr, "rootfsbuilder must be run as root\n")
os.Exit(ExitCodeFailure)
}
workDir, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Error while getting working directory: %s\n", err)
os.Exit(ExitCodeFailure)
}
if len(nonFlagArgs) == 0 {
print("One or more configuration files or directories must be specified\n")
os.Exit(ExitCodeFailure)
}
configs, err := processConfiguration(nonFlagArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while processing arguments: %s\n", err)
os.Exit(ExitCodeFailure)
}
for _, config := range configs {
fmt.Printf("Processing configuration with name '%s'\n", config.Name)
builder := NewBuilder(config, debArch, workDir, os.Stdout, os.Stderr)
artefacts, err := builder.Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Error while building rootfs: %s\n", err)
os.Exit(ExitCodeFailure)
}
for i, pkg := range artefacts {
fmt.Printf("Successfully built artefact %d: %s\n", i, pkg)
}
}
}
func parseConfiguration(path string) (*ConfigurationV1, error) {
config := ConfigurationV1{}
fd, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("error while opening configuration file: %w", err)
}
defer fd.Close()
err = json.NewDecoder(fd).Decode(&config)
if err != nil {
return nil, fmt.Errorf("error while parsing configuration file '%s': %w", path, err)
}
// Lower string values were case distinction does not matter
config.Distribution = strings.ToLower(config.Distribution)
config.TarballType = strings.ToLower(config.TarballType)
config.absoluteConfigPath = path
if err = checkRequiredFields(&config); err != nil {
return nil, fmt.Errorf("error while checking required fields in configuration file '%s': %w", path, err)
}
return &config, nil
}
func processConfiguration(paths []string) ([]*ConfigurationV1, error) {
configs := make([]*ConfigurationV1, 0, len(paths))
for _, path := range paths {
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("configuration file or directory '%s' does not exist", path)
}
return nil, fmt.Errorf("error while getting path status '%s': %w", path, err)
}
// At the moment we only support files
if info.IsDir() {
return nil, fmt.Errorf("path is a directory, not a file: %s", path)
}
config, err := parseConfiguration(path)
if err != nil {
return nil, err
}
configs = append(configs, config)
}
return configs, nil
}
func checkRequiredFields(config *ConfigurationV1) error {
if config.ConfigVersion != ConfigVersionV1 {
return fmt.Errorf("unsupported configuration version in config with name '%s': %d", config.Name, config.ConfigVersion)
}
if config.Name == "" {
return fmt.Errorf("name is required")
}
if config.Distribution == "" {
return fmt.Errorf("distribution is required")
}
if config.Release == "" {
return fmt.Errorf("release is required")
}
if config.Architecture == "" {
return fmt.Errorf("architecture is required")
}
if config.Mirror == "" {
return fmt.Errorf("mirror is required")
}
if config.TarballType != "" {
if config.TarballType != TarballTypeTar && config.TarballType != TarballTypeTarGz {
return fmt.Errorf("unsupported tarball type in config with name '%s': %s", config.Name, config.TarballType)
}
if config.PayloadType != "" && config.PayloadType != PayloadTypeTar && config.PayloadType != PayloadTypeTarGz {
return fmt.Errorf("unsupported payload type in config with name '%s': %s", config.Name, config.PayloadType)
}
}
if !config.SquashFS && config.TarballType == "" {
return fmt.Errorf("squashfs is required when tarball type is not specified")
}
return nil
}