@@ -35,16 +35,18 @@ import (
35
35
)
36
36
37
37
type BuildProductOpts struct {
38
- BuildLxc bool
39
- BuildLxd bool
40
- PurgeOldImages bool
38
+ BuildLxc bool
39
+ BuildLxd bool
40
+ PurgeOldImages bool
41
+ BuildScriptHook string
41
42
}
42
43
43
44
func NewBuildProductOpts () * BuildProductOpts {
44
45
return & BuildProductOpts {
45
- BuildLxc : true ,
46
- BuildLxd : true ,
47
- PurgeOldImages : true ,
46
+ BuildLxc : true ,
47
+ BuildLxd : true ,
48
+ PurgeOldImages : true ,
49
+ BuildScriptHook : "" ,
48
50
}
49
51
}
50
52
@@ -110,39 +112,84 @@ func BuildProduct(product *config.SimpleStreamsProduct, targetDir, imageFile str
110
112
dateDir , product .Name ))
111
113
}
112
114
113
- if opts .BuildLxc {
114
- buildLxcCommand := exec .Command ("distrobuilder" ,
115
- "build-lxc" , imageFile , dateDir ,
116
- "--cache-dir" , cacheDir )
115
+ if opts .BuildScriptHook != "" {
117
116
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 )
121
123
122
- err = buildLxcCommand .Run ()
124
+ err = buildDirCommand .Run ()
123
125
if err != nil {
124
126
return err
125
127
}
126
128
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 ()
129
136
if err != nil {
130
137
return err
131
138
}
132
- }
133
139
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
+ }
138
147
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
+ }
141
155
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
+ }
145
177
}
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
+
146
193
}
147
194
148
195
if opts .PurgeOldImages {
@@ -152,6 +199,32 @@ func BuildProduct(product *config.SimpleStreamsProduct, targetDir, imageFile str
152
199
return nil
153
200
}
154
201
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
+
155
228
func purgeOldImages (productDir string , product * config.SimpleStreamsProduct ) error {
156
229
var err error
157
230
var files []os.FileInfo
0 commit comments