Skip to content

Commit 271fa8c

Browse files
author
atighineanu
committed
added cleanup option for maintupd
1 parent 70e111c commit 271fa8c

File tree

1 file changed

+62
-20
lines changed

1 file changed

+62
-20
lines changed

cmd/updates.go

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ to quickly create a Cobra application.`,
5050
justSearch bool
5151
thisMU string
5252
cleanup bool
53+
autoApprove bool
5354
)
5455

5556
func init() {
@@ -58,6 +59,7 @@ func init() {
5859
RootCmd.PersistentFlags().BoolVarP(&justSearch, "search", "s", false, "flag that would trigger only looking for updates on OBS")
5960
RootCmd.PersistentFlags().StringVarP(&thisMU, "maintupdate", "m", "", "flag that consumes the name of an MU, like 'SUSE:Maintenance:Incident:ReleaseRequest'")
6061
RootCmd.PersistentFlags().BoolVarP(&cleanup, "cleanup", "k", false, "flag that triggers cleaning up the storage (from old MU channels)")
62+
RootCmd.PersistentFlags().BoolVar(&autoApprove, "auto-approve", false, "triggers automatic deleting of repositories")
6163
}
6264

6365
func muFindAndSync() {
@@ -74,7 +76,7 @@ func muFindAndSync() {
7476
if err != nil {
7577
log.Fatalf("There is an error: %v", err)
7678
}
77-
err = RemoveOldChannels(config, updateList)
79+
err = RemoveOldChannels(config, updateList, autoApprove)
7880
if err != nil {
7981
log.Fatalf("There is an error: %v", err)
8082
}
@@ -261,45 +263,85 @@ func GetUpdatesAndChannels(usr, passwd string, justsearch bool) (updlist []Updat
261263
return updlist, err
262264
}
263265

264-
func RemoveOldChannels(config Config, updates []Updates) (err error) {
266+
func RemoveOldChannels(config Config, updates []Updates, deleteNow bool) (err error) {
265267
mappedUpdates := MakeAMap(updates)
268+
fmt.Printf("Mapped Updates: %s\n", mappedUpdates)
266269
switch config.Storage.Type {
267270
case "file":
268271
var muChannelList []string
269-
err = filepath.Walk(filepath.Join(config.Storage.Path, "ibs/SUSE:/Maintenance:/"), func(path string, info os.FileInfo, err error) error {
270-
if info.IsDir() {
271-
muChannelList = append(muChannelList, path)
272-
}
273-
return nil
274-
})
275-
if err != nil {
276-
return
272+
if _, err := os.Stat(filepath.Join(config.Storage.Path, "ibs/SUSE:/Maintenance:/")); os.IsNotExist(err) {
273+
log.Printf("This path: \"%s\" does not exist! Check your mirror...\n")
274+
} else {
275+
err = filepath.Walk(filepath.Join(config.Storage.Path, "ibs/SUSE:/Maintenance:/"), func(path string, info os.FileInfo, err error) error {
276+
if info.Name() == "repodata" {
277+
muChannelList = append(muChannelList, path)
278+
}
279+
if info.IsDir() {
280+
if files, err := ioutil.ReadDir(path); len(files) == 0 {
281+
if err != nil {
282+
log.Fatal(err)
283+
}
284+
log.Printf("Removing unused empty folders: %s\n", path)
285+
os.RemoveAll(filepath.Join(config.Storage.Path, "ibs/SUSE:/Maintenance:/", path))
286+
}
287+
}
288+
return nil
289+
})
277290
}
278-
//templ := regexp.MustCompile(`/\d{5,6}/`)
291+
if err != nil {
292+
return
293+
}
294+
//List of repositories that will be deleted: reposToDel
295+
var reposToDel map[string]bool
296+
fmt.Printf("MuchannelList: %v\n", muChannelList)
279297
for _, elem := range muChannelList {
280-
if regexp.MustCompile(`/\d{5,6}/`).FindString(elem) != "" {
281-
_, exists := mappedUpdates[strings.Replace(regexp.MustCompile(`/\d{5,6}/`).FindString(elem), "/", "", 10)]
282-
if !exists {
283-
log.Printf("removing: %s...\n", elem)
284-
err = os.RemoveAll(elem)
285-
if err != nil {
286-
return
298+
if regexp.MustCompile(`/\d{5,6}/`).FindString(elem) != "" {
299+
_, exists := mappedUpdates[strings.Replace(regexp.MustCompile(`/\d{5,6}/`).FindString(elem), "/", "", 10)]
300+
if !exists {
301+
if deleteNow {
302+
log.Printf("removing: %s...\n", elem)
303+
err = os.RemoveAll(elem)
304+
if err != nil {
305+
return
306+
}
307+
} else {
308+
reposToDel[elem] = true
309+
}
287310
}
288311
}
289-
}
290-
}
312+
}
313+
log.Println("Mirror will have following changes:")
314+
if reposToDel == nil {
315+
log.Println("...there is nothing to delete!")
316+
}
317+
for index, _ := range reposToDel {
318+
fmt.Printf("- %s", index)
319+
}
320+
case "s3":
291321
}
292322
return
293323
}
294324

295325
func MakeAMap(updates []Updates) (updatesMap map[string]bool){
296326
updatesMap = make(map[string]bool)
297327
for _, elem := range updates {
328+
if elem.IncidentNumber == "" {
329+
elem.IncidentNumber = fmt.Sprintf("%s", elem.Repositories[0])
330+
}
298331
updatesMap[elem.IncidentNumber] = true
299332
}
300333
return
301334
}
302335

336+
func CheckIFMapHasKey(updatesMap map[string]bool, prefix, elem string) (yes bool) {
337+
for key, _ := range updatesMap {
338+
if strings.Contains(key, strings.Replace(elem, prefix, "", 1)) {
339+
yes = true
340+
}
341+
}
342+
return
343+
}
344+
303345
type Updates struct {
304346
IncidentNumber string
305347
ReleaseRequest string

0 commit comments

Comments
 (0)