Skip to content

Commit 619088d

Browse files
committed
publish: check if storage exists
1 parent a1a3c1f commit 619088d

9 files changed

Lines changed: 69 additions & 40 deletions

File tree

api/published_file_missing_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ func (s *PublishedFileMissingSuite) TestPublishedFileGoMissing(c *C) {
220220
c.Assert(resp.Code, Equals, 200, Commentf("Failed to update publish: %s", resp.Body.String()))
221221

222222
// Now check if the file is actually accessible in the published location
223-
publishedStorage := s.context.GetPublishedStorage("")
223+
publishedStorage, err := s.context.GetPublishedStorage("")
224+
c.Assert(err, IsNil)
224225
publicPath := publishedStorage.(aptly.FileSystemPublishedStorage).PublicPath()
225226

226227
// Expected file path: hrt/pool/main/h/hrt-libblobbyclient1/hrt-libblobbyclient1_20250926.152427+hrtdeb11_amd64.deb
@@ -332,7 +333,8 @@ func (s *PublishedFileMissingSuite) TestConcurrentPublishRace(c *C) {
332333
c.Assert(err, IsNil)
333334

334335
// Check published files
335-
publishedStorage := s.context.GetPublishedStorage("")
336+
publishedStorage, err := s.context.GetPublishedStorage("")
337+
c.Assert(err, IsNil)
336338
publicPath := publishedStorage.(aptly.FileSystemPublishedStorage).PublicPath()
337339

338340
missingFiles := []string{}
@@ -446,7 +448,8 @@ func (s *PublishedFileMissingSuite) TestIdenticalPackageRace(c *C) {
446448
c.Logf("[iter %d] All operations complete", iter)
447449

448450
// Check the shared pool location
449-
publishedStorage := s.context.GetPublishedStorage("")
451+
publishedStorage, err := s.context.GetPublishedStorage("")
452+
c.Assert(err, IsNil)
450453
publicPath := publishedStorage.(aptly.FileSystemPublishedStorage).PublicPath()
451454

452455
poolSubdir := string(packageName[0])
@@ -663,7 +666,8 @@ func (s *PublishedFileMissingSuite) TestConcurrentSnapshotPublishToSamePrefix(c
663666
c.Assert(bullseyePublishCode, Equals, expectedCode, Commentf("Bullseye publish/update should succeed"))
664667

665668
// Verify ALL package files exist in the published pool
666-
publishedStorage := s.context.GetPublishedStorage("")
669+
publishedStorage, err := s.context.GetPublishedStorage("")
670+
c.Assert(err, IsNil)
667671
publicPath := publishedStorage.(aptly.FileSystemPublishedStorage).PublicPath()
668672

669673
missingFiles := []string{}

api/repos.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ func reposServeInAPIMode(c *gin.Context) {
6060
storage = "filesystem:" + storage
6161
}
6262

63-
publicPath := context.GetPublishedStorage(storage).(aptly.FileSystemPublishedStorage).PublicPath()
63+
ps, err := context.GetPublishedStorage(storage)
64+
if err != nil {
65+
AbortWithJSONError(c, http.StatusNotFound, err)
66+
return
67+
}
68+
publicPath := ps.(aptly.FileSystemPublishedStorage).PublicPath()
6469
c.FileFromFS(pkgpath, http.Dir(publicPath))
6570
}
6671

aptly/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ type FileSystemPublishedStorage interface {
9595

9696
// PublishedStorageProvider is a thing that returns PublishedStorage by name
9797
type PublishedStorageProvider interface {
98-
// GetPublishedStorage returns PublishedStorage by name
99-
GetPublishedStorage(name string) PublishedStorage
98+
// GetPublishedStorage returns PublishedStorage by name, or an error if the storage is not configured
99+
GetPublishedStorage(name string) (PublishedStorage, error)
100100
}
101101

102102
// BarType used to differentiate between different progress bars

cmd/publish_snapshot.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ func aptlyPublishSnapshotOrRepo(cmd *commander.Command, args []string) error {
190190

191191
context.Progress().Printf("\n%s been successfully published.\n", message)
192192

193-
if localStorage, ok := context.GetPublishedStorage(storage).(aptly.FileSystemPublishedStorage); ok {
194-
context.Progress().Printf("Please setup your webserver to serve directory '%s' with autoindexing.\n",
195-
localStorage.PublicPath())
193+
if ps, err := context.GetPublishedStorage(storage); err == nil {
194+
if localStorage, ok := ps.(aptly.FileSystemPublishedStorage); ok {
195+
context.Progress().Printf("Please setup your webserver to serve directory '%s' with autoindexing.\n",
196+
localStorage.PublicPath())
197+
}
196198
}
197199

198200
context.Progress().Printf("Now you can add following line to apt sources:\n")

cmd/serve.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ func aptlyServe(cmd *commander.Command, args []string) error {
9797
}
9898
}
9999

100-
publicPath := context.GetPublishedStorage("").(aptly.FileSystemPublishedStorage).PublicPath()
100+
ps, err := context.GetPublishedStorage("")
101+
if err != nil {
102+
return err
103+
}
104+
publicPath := ps.(aptly.FileSystemPublishedStorage).PublicPath()
101105
ShutdownContext()
102106

103107
fmt.Printf("\nStarting web server at: %s (press Ctrl+C to quit)...\n", listen)

context/context.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ func (context *AptlyContext) config() *utils.ConfigStructure {
116116
if err != nil {
117117
fmt.Fprintf(os.Stderr, "Config file not found, creating default config at %s\n\n", homeLocation)
118118

119-
_ = utils.SaveConfigRaw(homeLocation, aptly.AptlyConf)
119+
defaultConfig := aptly.AptlyConf
120+
if len(defaultConfig) == 0 {
121+
defaultConfig = []byte("root_dir: \"\"")
122+
}
123+
124+
_ = utils.SaveConfigRaw(homeLocation, defaultConfig)
120125
err = utils.LoadConfig(homeLocation, &utils.Config)
121126
if err != nil {
122127
Fatal(fmt.Errorf("error loading config file %s: %s", homeLocation, err))
@@ -407,8 +412,8 @@ func (context *AptlyContext) PackagePool() aptly.PackagePool {
407412
return context.packagePool
408413
}
409414

410-
// GetPublishedStorage returns instance of PublishedStorage
411-
func (context *AptlyContext) GetPublishedStorage(name string) aptly.PublishedStorage {
415+
// GetPublishedStorage returns instance of PublishedStorage, or an error if the storage is not configured
416+
func (context *AptlyContext) GetPublishedStorage(name string) (aptly.PublishedStorage, error) {
412417
context.Lock()
413418
defer context.Unlock()
414419

@@ -419,14 +424,14 @@ func (context *AptlyContext) GetPublishedStorage(name string) aptly.PublishedSto
419424
} else if strings.HasPrefix(name, "filesystem:") {
420425
params, ok := context.config().FileSystemPublishRoots[name[11:]]
421426
if !ok {
422-
Fatal(fmt.Errorf("published local storage %v not configured", name[11:]))
427+
return nil, fmt.Errorf("published local storage %v not configured", name[11:])
423428
}
424429

425430
publishedStorage = files.NewPublishedStorage(params.RootDir, params.LinkMethod, params.VerifyMethod)
426431
} else if strings.HasPrefix(name, "s3:") {
427432
params, ok := context.config().S3PublishRoots[name[3:]]
428433
if !ok {
429-
Fatal(fmt.Errorf("published S3 storage %v not configured", name[3:]))
434+
return nil, fmt.Errorf("published S3 storage %v not configured", name[3:])
430435
}
431436

432437
var err error
@@ -436,39 +441,39 @@ func (context *AptlyContext) GetPublishedStorage(name string) aptly.PublishedSto
436441
params.EncryptionMethod, params.PlusWorkaround, params.DisableMultiDel,
437442
params.ForceSigV2, params.ForceVirtualHostedStyle, params.Debug)
438443
if err != nil {
439-
Fatal(err)
444+
return nil, err
440445
}
441446
} else if strings.HasPrefix(name, "swift:") {
442447
params, ok := context.config().SwiftPublishRoots[name[6:]]
443448
if !ok {
444-
Fatal(fmt.Errorf("published Swift storage %v not configured", name[6:]))
449+
return nil, fmt.Errorf("published Swift storage %v not configured", name[6:])
445450
}
446451

447452
var err error
448453
publishedStorage, err = swift.NewPublishedStorage(params.UserName, params.Password,
449454
params.AuthURL, params.Tenant, params.TenantID, params.Domain, params.DomainID, params.TenantDomain, params.TenantDomainID, params.Container, params.Prefix)
450455
if err != nil {
451-
Fatal(err)
456+
return nil, err
452457
}
453458
} else if strings.HasPrefix(name, "azure:") {
454459
params, ok := context.config().AzurePublishRoots[name[6:]]
455460
if !ok {
456-
Fatal(fmt.Errorf("published Azure storage %v not configured", name[6:]))
461+
return nil, fmt.Errorf("published Azure storage %v not configured", name[6:])
457462
}
458463

459464
var err error
460465
publishedStorage, err = azure.NewPublishedStorage(
461466
params.AccountName, params.AccountKey, params.Container, params.Prefix, params.Endpoint)
462467
if err != nil {
463-
Fatal(err)
468+
return nil, err
464469
}
465470
} else {
466-
Fatal(fmt.Errorf("unknown published storage format: %v", name))
471+
return nil, fmt.Errorf("unknown published storage format: %v", name)
467472
}
468473
context.publishedStorages[name] = publishedStorage
469474
}
470475

471-
return publishedStorage
476+
return publishedStorage, nil
472477
}
473478

474479
// UploadPath builds path to upload storage

context/context_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package context
22

33
import (
4-
"fmt"
5-
"os"
64
"reflect"
75
"testing"
86

@@ -80,10 +78,9 @@ func (s *AptlyContextSuite) SetUpTest(c *C) {
8078

8179
func (s *AptlyContextSuite) TestGetPublishedStorageBadFS(c *C) {
8280
// https://github.com/aptly-dev/aptly/issues/711
83-
// This will fail on account of us not having a config, so the
84-
// storage never exists.
85-
c.Assert(func() { s.context.GetPublishedStorage("filesystem:fuji") },
86-
FatalErrorPanicMatches,
87-
&FatalError{ReturnCode: 1, Message: fmt.Sprintf("error loading config file %s/.aptly.conf: invalid yaml (EOF) or json (EOF)",
88-
os.Getenv("HOME"))})
81+
// https://github.com/aptly-dev/aptly/issues/1477
82+
// GetPublishedStorage must return an error (not panic) when the
83+
// requested storage is not configured.
84+
_, err := s.context.GetPublishedStorage("filesystem:fuji")
85+
c.Assert(err, NotNil)
8986
}

deb/publish.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,12 @@ func (p *PublishedRepo) GetSkelFiles(skelDir string, component string) (map[stri
824824
// Publish publishes snapshot (repository) contents, links package files, generates Packages & Release files, signs them
825825
func (p *PublishedRepo) Publish(packagePool aptly.PackagePool, publishedStorageProvider aptly.PublishedStorageProvider,
826826
collectionFactory *CollectionFactory, signer pgp.Signer, progress aptly.Progress, forceOverwrite bool, skelDir string) error {
827-
publishedStorage := publishedStorageProvider.GetPublishedStorage(p.Storage)
827+
publishedStorage, err := publishedStorageProvider.GetPublishedStorage(p.Storage)
828+
if err != nil {
829+
return err
830+
}
828831

829-
err := publishedStorage.MkDir(filepath.Join(p.Prefix, "pool"))
832+
err = publishedStorage.MkDir(filepath.Join(p.Prefix, "pool"))
830833
if err != nil {
831834
return err
832835
}
@@ -1200,7 +1203,10 @@ func (p *PublishedRepo) Publish(packagePool aptly.PackagePool, publishedStorageP
12001203
// It can remove prefix fully, and part of pool (for specific component)
12011204
func (p *PublishedRepo) RemoveFiles(publishedStorageProvider aptly.PublishedStorageProvider, removePrefix bool,
12021205
removePoolComponents []string, progress aptly.Progress) error {
1203-
publishedStorage := publishedStorageProvider.GetPublishedStorage(p.Storage)
1206+
publishedStorage, err := publishedStorageProvider.GetPublishedStorage(p.Storage)
1207+
if err != nil {
1208+
return err
1209+
}
12041210

12051211
// I. Easy: remove whole prefix (meta+packages)
12061212
if removePrefix {
@@ -1213,7 +1219,7 @@ func (p *PublishedRepo) RemoveFiles(publishedStorageProvider aptly.PublishedStor
12131219
}
12141220

12151221
// II. Medium: remove metadata, it can't be shared as prefix/distribution as unique
1216-
err := publishedStorage.RemoveDirs(filepath.Join(p.Prefix, "dists", p.Distribution), progress)
1222+
err = publishedStorage.RemoveDirs(filepath.Join(p.Prefix, "dists", p.Distribution), progress)
12171223
if err != nil {
12181224
return err
12191225
}
@@ -1573,7 +1579,10 @@ func (collection *PublishedRepoCollection) CleanupAfterMultiDistToggle(published
15731579
}
15741580

15751581
// true→false: directly remove the per-distribution pool directories.
1576-
publishedStorage := publishedStorageProvider.GetPublishedStorage(published.Storage)
1582+
publishedStorage, err := publishedStorageProvider.GetPublishedStorage(published.Storage)
1583+
if err != nil {
1584+
return err
1585+
}
15771586
for _, component := range cleanComponents {
15781587
poolDir := filepath.Join(published.Prefix, "pool", published.Distribution, component)
15791588
if err := publishedStorage.RemoveDirs(poolDir, progress); err != nil {
@@ -1599,7 +1608,10 @@ func (collection *PublishedRepoCollection) CleanupPrefixComponentFiles(published
15991608
distribution := published.Distribution
16001609

16011610
rootPath := filepath.Join(prefix, "dists", distribution)
1602-
publishedStorage := publishedStorageProvider.GetPublishedStorage(published.Storage)
1611+
publishedStorage, err := publishedStorageProvider.GetPublishedStorage(published.Storage)
1612+
if err != nil {
1613+
return err
1614+
}
16031615

16041616
sort.Strings(cleanComponents)
16051617
publishedComponents := published.Components()

deb/publish_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ type FakeStorageProvider struct {
6161
storages map[string]aptly.PublishedStorage
6262
}
6363

64-
func (p *FakeStorageProvider) GetPublishedStorage(name string) aptly.PublishedStorage {
64+
func (p *FakeStorageProvider) GetPublishedStorage(name string) (aptly.PublishedStorage, error) {
6565
storage, ok := p.storages[name]
6666
if !ok {
67-
panic(fmt.Sprintf("unknown storage: %#v", name))
67+
return nil, fmt.Errorf("unknown storage: %#v", name)
6868
}
69-
return storage
69+
return storage, nil
7070
}
7171

7272
type PublishedRepoSuite struct {

0 commit comments

Comments
 (0)