Skip to content

[WIP] Terraform state package #34331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models/packages/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func getPackageDescriptor(ctx context.Context, pv *PackageVersion, c *cache.Ephe
metadata = &rubygems.Metadata{}
case TypeSwift:
metadata = &swift.Metadata{}
case TypeTerraform:
// terraform packages have no metadata
case TypeVagrant:
metadata = &vagrant.Metadata{}
default:
Expand Down
6 changes: 6 additions & 0 deletions models/packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
TypeRpm Type = "rpm"
TypeRubyGems Type = "rubygems"
TypeSwift Type = "swift"
TypeTerraform Type = "terraform"
TypeVagrant Type = "vagrant"
)

Expand All @@ -76,6 +77,7 @@ var TypeList = []Type{
TypeRpm,
TypeRubyGems,
TypeSwift,
TypeTerraform,
TypeVagrant,
}

Expand Down Expand Up @@ -124,6 +126,8 @@ func (pt Type) Name() string {
return "RubyGems"
case TypeSwift:
return "Swift"
case TypeTerraform:
return "Terraform"
case TypeVagrant:
return "Vagrant"
}
Expand Down Expand Up @@ -175,6 +179,8 @@ func (pt Type) SVGName() string {
return "gitea-rubygems"
case TypeSwift:
return "gitea-swift"
case TypeTerraform:
return "gitea-terraform"
case TypeVagrant:
return "gitea-vagrant"
}
Expand Down
4 changes: 4 additions & 0 deletions modules/globallock/globallock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func TryLock(ctx context.Context, key string) (bool, ReleaseFunc, error) {
return DefaultLocker().TryLock(ctx, key)
}

func Unlock(ctx context.Context, key string) error {
return DefaultLocker().Unlock(ctx, key)
}

// LockAndDo tries to acquire a lock for the given key and then calls the given function.
// It uses the default locker, and it will return an error if failed to acquire the lock.
func LockAndDo(ctx context.Context, key string, f func(context.Context) error) error {
Expand Down
2 changes: 2 additions & 0 deletions modules/globallock/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Locker interface {
// And if it fails to acquire the lock because it's already locked, not other reasons like redis is down,
// it will return false without any error.
TryLock(ctx context.Context, key string) (bool, ReleaseFunc, error)

Unlock(ctx context.Context, key string) error
}

// ReleaseFunc is a function that releases a lock.
Expand Down
5 changes: 5 additions & 0 deletions modules/globallock/memory_locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (l *memoryLocker) TryLock(_ context.Context, key string) (bool, ReleaseFunc
return false, func() {}, nil
}

func (l *memoryLocker) Unlock(_ context.Context, key string) error {
l.locks.Delete(key)
return nil
}

func (l *memoryLocker) tryLock(key string) bool {
_, loaded := l.locks.LoadOrStore(key, struct{}{})
return !loaded
Expand Down
13 changes: 13 additions & 0 deletions modules/globallock/redis_locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ func (l *redisLocker) TryLock(ctx context.Context, key string) (bool, ReleaseFun
return err == nil, f, err
}

func (l *redisLocker) Unlock(ctx context.Context, key string) error {
mutex, ok := l.mutexM.Load(key)
if ok {
l.mutexM.Delete(key)

// It's safe to ignore the error here,
// if it failed to unlock, it will be released automatically after the lock expires.
// Do not call mutex.UnlockContext(ctx) here, or it will fail to release when ctx has timed out.
_, _ = mutex.(*redsync.Mutex).Unlock()
}
return nil
}

// Close closes the locker.
// It will stop extending the locks and refuse to acquire new locks.
// In actual use, it is not necessary to call this function.
Expand Down
2 changes: 2 additions & 0 deletions modules/setting/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
LimitSizeRpm int64
LimitSizeRubyGems int64
LimitSizeSwift int64
LimitSizeTerraform int64
LimitSizeVagrant int64

DefaultRPMSignEnabled bool
Expand Down Expand Up @@ -86,6 +87,7 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) {
Packages.LimitSizeRpm = mustBytes(sec, "LIMIT_SIZE_RPM")
Packages.LimitSizeRubyGems = mustBytes(sec, "LIMIT_SIZE_RUBYGEMS")
Packages.LimitSizeSwift = mustBytes(sec, "LIMIT_SIZE_SWIFT")
Packages.LimitSizeTerraform = mustBytes(sec, "LIMIT_SIZE_TERRAFORM")
Packages.LimitSizeVagrant = mustBytes(sec, "LIMIT_SIZE_VAGRANT")
Packages.DefaultRPMSignEnabled = sec.Key("DEFAULT_RPM_SIGN_ENABLED").MustBool(false)
return nil
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3673,6 +3673,8 @@ rubygems.required.rubygems = Requires RubyGem version
swift.registry = Setup this registry from the command line:
swift.install = Add the package in your <code>Package.swift</code> file:
swift.install2 = and run the following command:
terraform.install = Setup this registry to your backend config
terraform.install2 = and run the following command:
vagrant.install = To add a Vagrant box, run the following command:
settings.link = Link this package to a repository
settings.link.description = If you link a package with a repository, the package is listed in the repository's package list.
Expand Down
1 change: 1 addition & 0 deletions public/assets/img/svg/gitea-terraform.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions routers/api/packages/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"code.gitea.io/gitea/routers/api/packages/rpm"
"code.gitea.io/gitea/routers/api/packages/rubygems"
"code.gitea.io/gitea/routers/api/packages/swift"
"code.gitea.io/gitea/routers/api/packages/terraform"
"code.gitea.io/gitea/routers/api/packages/vagrant"
"code.gitea.io/gitea/services/auth"
"code.gitea.io/gitea/services/context"
Expand Down Expand Up @@ -662,6 +663,22 @@ func CommonRoutes() *web.Router {
r.Get("/identifiers", swift.CheckAcceptMediaType(swift.AcceptJSON), swift.LookupPackageIdentifiers)
}, reqPackageAccess(perm.AccessModeRead))
})
r.Group("/terraform", func() {
r.Group("/{packagename}", func() {
r.Delete("", reqPackageAccess(perm.AccessModeWrite), terraform.DeletePackage)
r.Group("/state/{filename}", func() {
r.Get("", terraform.DownloadPackageFile)
r.Group("", func() {
r.Put("", terraform.UploadPackage)
r.Delete("", terraform.DeletePackageFile)
}, reqPackageAccess(perm.AccessModeWrite))
r.Group("/lock", func() {
r.Post("", terraform.LockPackage)
r.Delete("", terraform.UnlockPackage)
}, reqPackageAccess(perm.AccessModeWrite))
})
})
}, reqPackageAccess(perm.AccessModeRead))
r.Group("/vagrant", func() {
r.Group("/authenticate", func() {
r.Get("", vagrant.CheckAuthenticate)
Expand Down
Loading
Loading