Skip to content

Commit 88faa70

Browse files
author
Daniele Rondina
committed
Add support to build product hook.
Now if an hook is defined it's used build-dir and pack-*.
1 parent 990e9fc commit 88faa70

File tree

5 files changed

+129
-30
lines changed

5 files changed

+129
-30
lines changed

cmd/root.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ Copyright (c) 2019 Mottainai
3838
3939
Mottainai - LXC/LXD Simplestreams Tree Builder`
4040

41-
SSB_ENV_PREFIX = `SSBUILDER`
42-
SSB_VERSION = `0.1.0`
41+
SSB_VERSION = `0.1.0`
4342
)
4443

4544
func initConfig(config *conf.BuilderTreeConfig) {
4645
// Set env variable
47-
config.Viper.SetEnvPrefix(SSB_ENV_PREFIX)
46+
config.Viper.SetEnvPrefix(conf.SSB_ENV_PREFIX)
4847
config.Viper.BindEnv("config")
4948
config.Viper.SetDefault("config", "")
5049

contrib/config/tree.example.yml

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ products:
3030
# this option is not needed.
3131
#prefix_path: "http://my.mottainai.org/namespace/lxd-sabayon-builder"
3232

33+
days: 1
3334
aliases:
3435
- "sabayon/builder"
3536

@@ -39,6 +40,12 @@ products:
3940
release: current
4041
os: Sabayon
4142
directory: sbi/sabayon-base
43+
# Currently distrobuilder doesn't implement a generator for inject
44+
# files from host before enter on chroot. In additional, for
45+
# oraclelinux it seems that .distrobuilder is not cleaned correctly.
46+
# If it's defined a script then instead of call build-lxc and build-lxc
47+
# I split creation in two steps: build-dir + script + pack-lxc|pack-lxd
48+
# build_script_hook: "/myscript.sh"
4249
#hidden: true
4350
# Define number of images maintains for the product. Default is 1 day/image.
4451
#days: 1

pkg/config/config.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import (
2727
v "github.com/spf13/viper"
2828
)
2929

30+
const (
31+
SSB_ENV_PREFIX = `SSBUILDER`
32+
)
33+
3034
type SimpleStreamsProduct struct {
3135
Name string `mapstructure:"name"`
3236
Architecture string `mapstructure:"arch"`
@@ -36,6 +40,7 @@ type SimpleStreamsProduct struct {
3640
Directory string `mapstructure:"directory"`
3741
Version string `mapstructure:"version"`
3842
PrefixPath string `mapstructure:"prefix_path"`
43+
BuildScriptHook string `mapstructure:"build_script_hook"`
3944
Aliases []string `mapstructure:"aliases"`
4045
Hidden bool `mapstructure:"hidden"`
4146
Days int `mapstructure:"days"`
@@ -126,11 +131,12 @@ func (p *SimpleStreamsProduct) String() string {
126131
prefix_path: %s
127132
hidden: %v
128133
days: %d
134+
build_script_hook: %s
129135
aliases: %s`,
130136
p.Name, p.Architecture, p.Release,
131137
p.ReleaseTitle, p.OperatingSystem,
132138
p.Directory, p.Version, p.PrefixPath,
133-
p.Hidden, p.Days, p.Aliases)
139+
p.Hidden, p.Days, p.BuildScriptHook, p.Aliases)
134140

135141
return ans
136142
}

pkg/images/product.go

+99-26
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ import (
3535
)
3636

3737
type BuildProductOpts struct {
38-
BuildLxc bool
39-
BuildLxd bool
40-
PurgeOldImages bool
38+
BuildLxc bool
39+
BuildLxd bool
40+
PurgeOldImages bool
41+
BuildScriptHook string
4142
}
4243

4344
func NewBuildProductOpts() *BuildProductOpts {
4445
return &BuildProductOpts{
45-
BuildLxc: true,
46-
BuildLxd: true,
47-
PurgeOldImages: true,
46+
BuildLxc: true,
47+
BuildLxd: true,
48+
PurgeOldImages: true,
49+
BuildScriptHook: "",
4850
}
4951
}
5052

@@ -110,39 +112,84 @@ func BuildProduct(product *config.SimpleStreamsProduct, targetDir, imageFile str
110112
dateDir, product.Name))
111113
}
112114

113-
if opts.BuildLxc {
114-
buildLxcCommand := exec.Command("distrobuilder",
115-
"build-lxc", imageFile, dateDir,
116-
"--cache-dir", cacheDir)
115+
if opts.BuildScriptHook != "" {
117116

118-
// https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html
119-
buildLxcCommand.Stdout = os.Stdout
120-
buildLxcCommand.Stderr = os.Stderr
117+
// Create rootfs directory
118+
rootfsDir := path.Join(dateDir, "staging")
119+
buildDirCommand := exec.Command("distrobuilder",
120+
"build-dir", imageFile, rootfsDir,
121+
"--cache-dir", cacheDir)
122+
defer tools.RemoveDirIfNotExist(rootfsDir)
121123

122-
err = buildLxcCommand.Run()
124+
err = buildDirCommand.Run()
123125
if err != nil {
124126
return err
125127
}
126128

127-
// Create cache dir cleanup by distrobuilder
128-
_, err = tools.MkdirIfNotExist(cacheDir, 0760)
129+
runHookCommand := exec.Command(opts.BuildScriptHook)
130+
// Prepare env for hook
131+
runHookCommand.Env = append(os.Environ(),
132+
fmt.Sprintf("%s_STAGING_DIR=%s", config.SSB_ENV_PREFIX, rootfsDir),
133+
fmt.Sprintf("%s_BUILD_PRODUCT=%s", config.SSB_ENV_PREFIX, product.Name))
134+
135+
err = runHookCommand.Run()
129136
if err != nil {
130137
return err
131138
}
132-
}
133139

134-
if opts.BuildLxd {
135-
buildLxdCommand := exec.Command("distrobuilder",
136-
"build-lxd", imageFile, dateDir,
137-
"--cache-dir", cacheDir)
140+
// Create LXC package
141+
if opts.BuildLxc {
142+
err = packImage(imageFile, rootfsDir, dateDir, cacheDir, "pack-lxc")
143+
if err != nil {
144+
return err
145+
}
146+
}
138147

139-
buildLxdCommand.Stdout = os.Stdout
140-
buildLxdCommand.Stderr = os.Stderr
148+
// Create LXD package
149+
if opts.BuildLxd {
150+
err = packImage(imageFile, rootfsDir, dateDir, cacheDir, "pack-lxd")
151+
if err != nil {
152+
return err
153+
}
154+
}
141155

142-
err = buildLxdCommand.Run()
143-
if err != nil {
144-
return err
156+
} else {
157+
158+
if opts.BuildLxc {
159+
buildLxcCommand := exec.Command("distrobuilder",
160+
"build-lxc", imageFile, dateDir,
161+
"--cache-dir", cacheDir)
162+
163+
// https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html
164+
buildLxcCommand.Stdout = os.Stdout
165+
buildLxcCommand.Stderr = os.Stderr
166+
167+
err = buildLxcCommand.Run()
168+
if err != nil {
169+
return err
170+
}
171+
172+
// Create cache dir cleanup by distrobuilder
173+
_, err = tools.MkdirIfNotExist(cacheDir, 0760)
174+
if err != nil {
175+
return err
176+
}
145177
}
178+
179+
if opts.BuildLxd {
180+
buildLxdCommand := exec.Command("distrobuilder",
181+
"build-lxd", imageFile, dateDir,
182+
"--cache-dir", cacheDir)
183+
184+
buildLxdCommand.Stdout = os.Stdout
185+
buildLxdCommand.Stderr = os.Stderr
186+
187+
err = buildLxdCommand.Run()
188+
if err != nil {
189+
return err
190+
}
191+
}
192+
146193
}
147194

148195
if opts.PurgeOldImages {
@@ -152,6 +199,32 @@ func BuildProduct(product *config.SimpleStreamsProduct, targetDir, imageFile str
152199
return nil
153200
}
154201

202+
func packImage(imageFile, rootfsDir, dateDir, cacheDir, subCommand string) error {
203+
var err error
204+
205+
// Create cache dir cleanup by distrobuilder
206+
_, err = tools.MkdirIfNotExist(cacheDir, 0760)
207+
if err != nil {
208+
return err
209+
}
210+
211+
fmt.Printf("Executing %s command...\n", subCommand)
212+
213+
packCommand := exec.Command("distrobuilder",
214+
subCommand, imageFile, rootfsDir, dateDir,
215+
"--cache-dir", cacheDir)
216+
217+
packCommand.Stdout = os.Stdout
218+
packCommand.Stderr = os.Stderr
219+
220+
err = packCommand.Run()
221+
if err != nil {
222+
return err
223+
}
224+
225+
return nil
226+
}
227+
155228
func purgeOldImages(productDir string, product *config.SimpleStreamsProduct) error {
156229
var err error
157230
var files []os.FileInfo

pkg/tools/utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,17 @@ func MkdirIfNotExist(dir string, mode os.FileMode) (*os.FileInfo, error) {
5454
return nil, nil
5555
}
5656
}
57+
58+
func RemoveDirIfNotExist(dir string) error {
59+
var err error
60+
61+
if dir == "" {
62+
return fmt.Errorf("Invalid directory")
63+
}
64+
65+
if _, err = os.Stat(dir); os.IsNotExist(err) {
66+
return nil
67+
}
68+
69+
return os.RemoveAll(dir)
70+
}

0 commit comments

Comments
 (0)