/
PrePackager.go
340 lines (275 loc) · 7.42 KB
/
PrePackager.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package aipsetup
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/AnimusPEXUS/aipsetup/basictypes"
"github.com/AnimusPEXUS/utils/filetools"
"github.com/AnimusPEXUS/utils/logger"
)
// Some packages, like linux, and many others, have no features to precicely
// point where to install what, so, without additional file movements,
// Packager.DestDirCheckCorrectness() will return error.
// PrePackager role is to be called by builder at the final stage of package
// distribution in case if builder can't (or see it irrational to do it on
// it own) performe such movements.
type PrePackager struct {
site *BuildingSiteCtl
}
func NewPrePackager(site *BuildingSiteCtl) *PrePackager {
ret := new(PrePackager)
ret.site = site
return ret
}
func (self *PrePackager) Run(log *logger.Logger) error {
for _, i := range [](func(log *logger.Logger) error){
self.DestDirRenameEtc,
self.DestDirRenameVar,
self.DestDirMoveRootToUsr,
self.DestDirMoveUsrToPrefix,
} {
err := i(log)
if err != nil {
return err
}
}
return nil
}
func (self *PrePackager) renameRootDir(original_name string, log *logger.Logger) (bool, string, error) {
original_name = path.Base(original_name)
info, err := self.site.ReadInfo()
if err != nil {
return false, "", err
}
log.Info("checking if /" + original_name + " should be renamed")
if info.Host == info.HostArch {
log.Info(" no, it's not: this is primary installation package")
return false, original_name, nil
}
log.Info(" yes: this is secondary installation")
dst_dir := self.site.GetDIR_DESTDIR()
dst_original_dir := path.Join(dst_dir, original_name)
dst_resulting_name := fmt.Sprintf(
original_name+".%s.%s.original",
info.Host,
info.HostArch,
)
dst_resulting_dir := path.Join(dst_dir, dst_resulting_name)
if _, err := os.Stat(dst_original_dir); err != nil {
if !os.IsNotExist(err) {
return false, "", err
}
log.Info(" /" + original_name + " does not exists - exiting")
return false, original_name, nil
}
log.Info(" /" + original_name + " going to be renamed as /" + dst_resulting_name)
err = os.Rename(dst_original_dir, dst_resulting_dir)
if err != nil {
return false, "", err
}
return true, dst_resulting_name, nil
}
func (self *PrePackager) DestDirRenameEtc(log *logger.Logger) error {
renamed, resulting_name, err := self.renameRootDir("etc", log)
if err != nil {
return err
}
if renamed {
dst_dir := self.site.GetDIR_DESTDIR()
dst_etc_dir := path.Join(dst_dir, "etc")
new_etc_dir := path.Join(dst_dir, resulting_name)
etc_profile_d := path.Join(dst_etc_dir, "profile.d")
etc_profile_d_set := path.Join(etc_profile_d, "SET")
new_etc_profile_d := path.Join(new_etc_dir, "profile.d")
new_etc_profile_d_set := path.Join(new_etc_profile_d, "SET")
if _, err := os.Stat(new_etc_profile_d_set); err != nil {
if !os.IsNotExist(err) {
return err
}
log.Info(
" /" + path.Join(
resulting_name,
"profile.d",
"SET",
) +
" is not exists - exiting",
)
return nil
}
log.Info(
"/" + resulting_name + "/profile.d/SET is found and will be moved to /etc",
)
err = os.MkdirAll(etc_profile_d, 0700)
if err != nil {
return err
}
err = os.Rename(new_etc_profile_d_set, etc_profile_d_set)
if err != nil {
return err
}
}
return nil
}
func (self *PrePackager) DestDirRenameVar(log *logger.Logger) error {
_, _, err := self.renameRootDir("var", log)
if err != nil {
return err
}
return nil
}
func (self *PrePackager) DestDirMoveRootToUsr(log *logger.Logger) error {
log.Info("checking and moving *bin/lib* dirs to usr")
dst_dir := self.site.GetDIR_DESTDIR()
for _, i := range []string{
basictypes.DIRNAME_BIN,
basictypes.DIRNAME_SBIN,
basictypes.DIRNAME_LIB,
basictypes.DIRNAME_LIB64,
} {
log.Info(" searching for " + i + " directory")
i_j := path.Join(dst_dir, i)
stat, err := os.Lstat(i_j)
if err != nil {
if !os.IsNotExist(err) {
log.Error(" error lstating " + i + " directory:" + err.Error())
return err
} else {
log.Info(" " + i + " not found = ok - continuing..")
continue
}
}
log.Info(" item " + i + " is found. checking it's properties..")
if filetools.Is(stat.Mode()).Symlink() || !stat.IsDir() {
log.Error(
fmt.Sprintf(
" item has unacceptable properties: not directory == %v, symlink == %v",
!stat.IsDir(),
filetools.Is(stat.Mode()).Symlink()),
)
return errors.New("src must be directory")
}
log.Info(" properties acceptable")
d_j := path.Join(dst_dir, "usr", i)
log.Info(" starting copy process of " + i)
err = filetools.CopyTree(
i_j, d_j,
false,
false,
true,
false,
log,
false,
true,
filetools.CopyWithInfo,
)
if err != nil {
log.Error(" copy process ended with error: " + err.Error())
return err
}
log.Info(" copy succeeded")
log.Info(" removing " + i)
err = os.RemoveAll(i_j)
if err != nil {
log.Error(" error: " + err.Error())
return err
}
}
return nil
}
func (self *PrePackager) DestDirMoveUsrToPrefix(log *logger.Logger) error {
log.Info("checking and moving usr to install prefix")
usr_dir := path.Join(self.site.GetDIR_DESTDIR(), "usr")
log.Info(" checking usr dir existance")
usr_dir_stat, err := os.Stat(usr_dir)
if err != nil {
if !os.IsNotExist(err) {
log.Info(" error " + err.Error())
return err
} else {
log.Info(" usr does not exists")
return nil
}
}
log.Info(" usr found. checking it's properties")
if !usr_dir_stat.IsDir() {
err = errors.New("invalid type of /usr in DESTDIR")
log.Error(" error. " + err.Error())
return err
}
log.Info(" usr parameters looks acceptable")
log.Info("calculating new path for usr contents")
new_usr_dir, err := self.site.GetBuildingSiteValuesCalculator().CalculateDstInstallPrefix()
if err != nil {
log.Info(" error: " + err.Error())
return err
}
log.Info(" " + new_usr_dir)
log.Info("making new dir")
err = os.MkdirAll(new_usr_dir, 0700)
if err != nil {
return err
}
log.Info("listing usr")
usr_dir_lst, err := ioutil.ReadDir(usr_dir)
if err != nil {
return err
}
log.Info("copying files..")
for _, i := range usr_dir_lst {
i_joined := path.Join(usr_dir, i.Name())
new_dst_name := path.Join(new_usr_dir, i.Name())
log.Info(" from " + i_joined)
log.Info(" to " + new_dst_name)
new_dst_name_stat, err := os.Lstat(new_dst_name)
if err != nil {
if !os.IsNotExist(err) {
log.Error(" error stating " + new_dst_name)
log.Error(" " + err.Error())
return err
}
} else {
if !new_dst_name_stat.IsDir() {
log.Error("dst " + new_dst_name + " already exists and it's not a directory")
return errors.New("dst already exists and it's not a directory")
}
}
i_joined_stat, err := os.Lstat(i_joined)
if err != nil {
return err
}
if i_joined_stat.IsDir() {
err = filetools.CopyTree(
i_joined,
new_dst_name,
false,
false,
true,
false,
log,
false,
true,
filetools.CopyWithInfo,
)
if err != nil {
log.Error("copy error: " + err.Error())
return err
}
err = os.RemoveAll(i_joined)
} else {
log.Error(i_joined + " is not the directory. don't know what to do")
return errors.New(
"src is not the directory. don't know what to do",
)
}
}
log.Info("Removing usr")
err = os.RemoveAll(usr_dir)
if err != nil {
log.Error(" error: " + err.Error())
return err
}
log.Info("usr moving complete")
return nil
}