Skip to content

Commit 1581b54

Browse files
author
atighineanu
committed
added cleanup option for storage + scc
1 parent 271fa8c commit 1581b54

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

cmd/sync.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package cmd
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"log"
67
"net/url"
78
"os"
89
"path/filepath"
910
"strings"
1011

12+
"github.com/manifoldco/promptui"
1113
"github.com/spf13/cobra"
1214
"github.com/uyuni-project/minima/get"
1315
yaml "gopkg.in/yaml.v2"
@@ -166,7 +168,9 @@ func syncersFromConfig(configString string) (result []*get.Syncer, err error) {
166168
}
167169
result = append(result, get.NewSyncer(*repoURL, archs, storage))
168170
}
169-
171+
if cleanup {
172+
RemoveOldChannelsFromFileStorage(config)
173+
}
170174
return
171175
}
172176

@@ -176,3 +180,66 @@ func init() {
176180
RootCmd.PersistentFlags().StringVarP(&archs, "arch", "a", "", "flag that specifies covered archs in the given repo")
177181
RootCmd.PersistentFlags().BoolVarP(&syncLegacyPackages, "legacypackages", "l", false, "flag that triggers mirroring of i586 pkgs in x86_64 repos")
178182
}
183+
184+
func RemoveOldChannelsFromFileStorage (config Config) (err error) {
185+
// DO CLEANUP - TO BE IMPLEMENTED
186+
log.Println("searching for outdated repositories...")
187+
//fmt.Printf("List of Repos from config: %s ---> %s\n", config.SCC.RepoNames, config.HTTP)
188+
mappedRepos := make(map[string]bool)
189+
var urlink *url.URL
190+
for _, elem := range config.HTTP {
191+
urlink, err = url.Parse(elem.URL)
192+
if err != nil {
193+
panic(err)
194+
}
195+
mappedRepos[filepath.Join(config.Storage.Path, urlink.Path)] = true
196+
}
197+
//fmt.Printf("MAPPED REPOS: %v\n", mappedRepos)
198+
path := config.Storage.Path
199+
muChannelList := make(map[string]bool)
200+
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
201+
if info.IsDir() {
202+
if info.Name() == "repodata" {
203+
muChannelList[strings.Replace(path, "/repodata", "", 1)] = true
204+
return nil
205+
}
206+
if files, err := ioutil.ReadDir(path); len(files) == 0 && path != config.Storage.Path {
207+
if err != nil {
208+
log.Fatal(err)
209+
}
210+
log.Printf("Removing unused empty folders: %s\n", path)
211+
os.RemoveAll(path)
212+
}
213+
}
214+
return nil
215+
})
216+
//fmt.Printf("CHANNEL LIST: %v\n", muChannelList)
217+
for ind, _ := range muChannelList {
218+
if mappedRepos[ind] {
219+
log.Printf("Repo %s is registered...\n", ind)
220+
} else {
221+
log.Printf("Repo %s is not registered in the yaml file...", ind)
222+
if autoApprove {
223+
log.Printf("Removing repo %s ...\n", ind)
224+
os.RemoveAll(ind)
225+
} else {
226+
prompt := promptui.Select{
227+
Label: fmt.Sprintf("Delete repo: %s ??? [Yes/No]", ind),
228+
Items: []string{"Yes", "No"},
229+
}
230+
_, result, err := prompt.Run()
231+
if err != nil {
232+
log.Fatalf("Prompt failed %v\n", err)
233+
}
234+
if result == "Yes" {
235+
log.Printf("Removing repo: %s ...\n", ind)
236+
os.RemoveAll(ind)
237+
} else {
238+
log.Printf("Keeping repo: %s ...\n", ind)
239+
}
240+
}
241+
}
242+
}
243+
log.Println("...done!")
244+
return
245+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1212
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7 // indirect
1313
github.com/jtolds/gls v4.2.1+incompatible // indirect
14+
github.com/manifoldco/promptui v0.8.0
1415
github.com/smartystreets/assertions v0.0.0-20170818220048-9c0ea8acbc1d // indirect
1516
github.com/smartystreets/goconvey v0.0.0-20170825221426-e5b2b7c91115 // indirect
1617
github.com/smartystreets/gunit v0.0.0-20170705041418-d235ada78c07 // indirect

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ github.com/aws/aws-sdk-go v0.0.0-20170914235556-ad2c5a3ad388 h1:Aa5a+YIfo9fdEe2f
4747
github.com/aws/aws-sdk-go v0.0.0-20170914235556-ad2c5a3ad388/go.mod h1:ZRmQr0FajVIyZ4ZzBYKG5P3ZqPz9IHG41ZoMu1ADI3k=
4848
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
4949
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
50+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
5051
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
5152
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
5253
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
@@ -155,12 +156,22 @@ github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfE
155156
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
156157
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
157158
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
159+
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
160+
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
158161
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
159162
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
160163
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
161164
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
162165
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
163166
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
167+
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
168+
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
169+
github.com/manifoldco/promptui v0.8.0 h1:R95mMF+McvXZQ7j1g8ucVZE1gLP3Sv6j9vlF9kyRqQo=
170+
github.com/manifoldco/promptui v0.8.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
171+
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
172+
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
173+
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
174+
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
164175
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
165176
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
166177
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@@ -300,6 +311,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
300311
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
301312
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
302313
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
314+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
303315
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
304316
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
305317
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 commit comments

Comments
 (0)