mirror of https://github.com/harness/drone.git
feat: [CDE-127]: read multi-table and multi query data in a single SQL txn (#2193)
* feat: [CDE-127]: starting and stopping states. * feat: [CDE-127]: read multi-table and multi query data in a single SQL txnunified-ui
parent
d577102d5e
commit
6e525fca4a
|
@ -87,45 +87,28 @@ func (c *Controller) startGitspace(ctx context.Context, config *types.GitspaceCo
|
|||
savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID, config.SpaceID)
|
||||
const resourceNotFoundErr = "Failed to find gitspace: resource not found"
|
||||
if err != nil && err.Error() != resourceNotFoundErr { // TODO fix this
|
||||
return nil, fmt.Errorf("failed to find gitspace with config ID : %s %w", config.Identifier, err)
|
||||
return nil, fmt.Errorf("failed to find gitspace instance for config ID : %s %w", config.Identifier, err)
|
||||
}
|
||||
config.GitspaceInstance = savedGitspaceInstance
|
||||
_, err = c.gitspaceBusyOperation(ctx, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if savedGitspaceInstance == nil || savedGitspaceInstance.State.IsFinalStatus() {
|
||||
codeServerPassword := defaultAccessKey
|
||||
gitspaceMachineUser := defaultMachineUser
|
||||
now := time.Now().UnixMilli()
|
||||
suffixUID, err := gonanoid.Generate(allowedUIDAlphabet, 6)
|
||||
gitspaceInstance, err := c.createGitspaceInstance(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not generate UID for gitspace config : %q %w", config.Identifier, err)
|
||||
}
|
||||
identifier := strings.ToLower(config.Identifier + "-" + suffixUID)
|
||||
var gitspaceInstance = &types.GitspaceInstance{
|
||||
GitSpaceConfigID: config.ID,
|
||||
Identifier: identifier,
|
||||
State: enum.GitspaceInstanceStateUninitialized,
|
||||
UserID: config.UserID,
|
||||
SpaceID: config.SpaceID,
|
||||
SpacePath: config.SpacePath,
|
||||
Created: now,
|
||||
Updated: now,
|
||||
TotalTimeUsed: 0,
|
||||
}
|
||||
if config.IDE == enum.IDETypeVSCodeWeb || config.IDE == enum.IDETypeVSCode {
|
||||
gitspaceInstance.AccessKey = &codeServerPassword
|
||||
gitspaceInstance.AccessType = enum.GitspaceAccessTypeUserCredentials
|
||||
gitspaceInstance.MachineUser = &gitspaceMachineUser
|
||||
return nil, err
|
||||
}
|
||||
if err = c.gitspaceInstanceStore.Create(ctx, gitspaceInstance); err != nil {
|
||||
return nil, fmt.Errorf("failed to create gitspace instance for %s %w", config.Identifier, err)
|
||||
}
|
||||
newGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID, config.SpaceID)
|
||||
newGitspaceInstance.SpacePath = config.SpacePath
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find gitspace with config ID : %s %w", config.Identifier, err)
|
||||
}
|
||||
config.GitspaceInstance = newGitspaceInstance
|
||||
}
|
||||
|
||||
newGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID, config.SpaceID)
|
||||
newGitspaceInstance.SpacePath = config.SpacePath
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find gitspace with config ID : %s %w", config.Identifier, err)
|
||||
}
|
||||
config.GitspaceInstance = newGitspaceInstance
|
||||
updatedGitspace, err := c.orchestrator.StartGitspace(ctx, config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find start gitspace : %s %w", config.Identifier, err)
|
||||
|
@ -138,6 +121,53 @@ func (c *Controller) startGitspace(ctx context.Context, config *types.GitspaceCo
|
|||
return config, nil
|
||||
}
|
||||
|
||||
func (c *Controller) createGitspaceInstance(config *types.GitspaceConfig) (*types.GitspaceInstance, error) {
|
||||
codeServerPassword := defaultAccessKey
|
||||
gitspaceMachineUser := defaultMachineUser
|
||||
now := time.Now().UnixMilli()
|
||||
suffixUID, err := gonanoid.Generate(allowedUIDAlphabet, 6)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not generate UID for gitspace config : %q %w", config.Identifier, err)
|
||||
}
|
||||
identifier := strings.ToLower(config.Identifier + "-" + suffixUID)
|
||||
var gitspaceInstance = &types.GitspaceInstance{
|
||||
GitSpaceConfigID: config.ID,
|
||||
Identifier: identifier,
|
||||
State: enum.GitspaceInstanceStateStarting,
|
||||
UserID: config.UserID,
|
||||
SpaceID: config.SpaceID,
|
||||
SpacePath: config.SpacePath,
|
||||
Created: now,
|
||||
Updated: now,
|
||||
TotalTimeUsed: 0,
|
||||
}
|
||||
if config.IDE == enum.IDETypeVSCodeWeb || config.IDE == enum.IDETypeVSCode {
|
||||
gitspaceInstance.AccessKey = &codeServerPassword
|
||||
gitspaceInstance.AccessType = enum.GitspaceAccessTypeUserCredentials
|
||||
gitspaceInstance.MachineUser = &gitspaceMachineUser
|
||||
}
|
||||
return gitspaceInstance, nil
|
||||
}
|
||||
|
||||
func (c *Controller) gitspaceBusyOperation(
|
||||
ctx context.Context,
|
||||
config *types.GitspaceConfig,
|
||||
) (*types.GitspaceConfig, error) {
|
||||
if config.GitspaceInstance == nil {
|
||||
return config, nil
|
||||
}
|
||||
if config.GitspaceInstance.State.IsBusyStatus() &&
|
||||
time.Since(time.UnixMilli(config.Updated)) <= (10*60*1000) {
|
||||
return nil, fmt.Errorf("gitspace start/stop is already pending for : %q", config.Identifier)
|
||||
} else if config.GitspaceInstance.State.IsBusyStatus() {
|
||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateError
|
||||
if err := c.gitspaceInstanceStore.Update(ctx, config.GitspaceInstance); err != nil {
|
||||
return nil, fmt.Errorf("failed to update gitspace config for %s %w", config.Identifier, err)
|
||||
}
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func (c *Controller) stopGitspace(ctx context.Context, config *types.GitspaceConfig) (*types.GitspaceConfig, error) {
|
||||
savedGitspace, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID, config.SpaceID)
|
||||
if err != nil {
|
||||
|
@ -145,9 +175,17 @@ func (c *Controller) stopGitspace(ctx context.Context, config *types.GitspaceCon
|
|||
}
|
||||
if savedGitspace.State.IsFinalStatus() {
|
||||
return nil, fmt.Errorf(
|
||||
"gitspace Instance cannot be stopped with ID %s %w", savedGitspace.Identifier, err)
|
||||
"gitspace instance cannot be stopped with ID %s %w", savedGitspace.Identifier, err)
|
||||
}
|
||||
config.GitspaceInstance = savedGitspace
|
||||
config, err = c.gitspaceBusyOperation(ctx, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping
|
||||
if err = c.gitspaceInstanceStore.Update(ctx, config.GitspaceInstance); err != nil {
|
||||
return nil, fmt.Errorf("failed to update gitspace config for stopping %s %w", config.Identifier, err)
|
||||
}
|
||||
if updatedGitspace, stopErr := c.orchestrator.StopGitspace(ctx, config); stopErr != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"failed to stop gitspace instance with ID %s %w", savedGitspace.Identifier, stopErr)
|
||||
|
|
|
@ -19,6 +19,7 @@ import (
|
|||
gitspaceevents "github.com/harness/gitness/app/events/gitspace"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator"
|
||||
"github.com/harness/gitness/app/store"
|
||||
"github.com/harness/gitness/store/database/dbtx"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
|
@ -30,9 +31,11 @@ type Controller struct {
|
|||
eventReporter *gitspaceevents.Reporter
|
||||
orchestrator orchestrator.Orchestrator
|
||||
gitspaceEventStore store.GitspaceEventStore
|
||||
tx dbtx.Transactor
|
||||
}
|
||||
|
||||
func NewController(
|
||||
tx dbtx.Transactor,
|
||||
authorizer authz.Authorizer,
|
||||
infraProviderResourceStore store.InfraProviderResourceStore,
|
||||
gitspaceConfigStore store.GitspaceConfigStore,
|
||||
|
@ -43,6 +46,7 @@ func NewController(
|
|||
gitspaceEventStore store.GitspaceEventStore,
|
||||
) *Controller {
|
||||
return &Controller{
|
||||
tx: tx,
|
||||
authorizer: authorizer,
|
||||
infraProviderResourceStore: infraProviderResourceStore,
|
||||
gitspaceConfigStore: gitspaceConfigStore,
|
||||
|
|
|
@ -80,33 +80,40 @@ func (c *Controller) Create(
|
|||
return nil, fmt.Errorf("invalid input: %w", err)
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
infraProviderResource, err := c.infraProviderResourceStore.FindByIdentifier(
|
||||
ctx,
|
||||
parentSpace.ID,
|
||||
in.ResourceIdentifier)
|
||||
var gitspaceConfig *types.GitspaceConfig
|
||||
err = c.tx.WithTx(ctx, func(ctx context.Context) error {
|
||||
infraProviderResource, err := c.infraProviderResourceStore.FindByIdentifier(
|
||||
ctx,
|
||||
parentSpace.ID,
|
||||
in.ResourceIdentifier)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not find infra provider resource : %q %w", in.ResourceIdentifier, err)
|
||||
}
|
||||
gitspaceConfig = &types.GitspaceConfig{
|
||||
Identifier: identifier,
|
||||
Name: in.Name,
|
||||
IDE: in.IDE,
|
||||
InfraProviderResourceID: infraProviderResource.ID,
|
||||
InfraProviderResourceIdentifier: infraProviderResource.Identifier,
|
||||
CodeRepoType: enum.CodeRepoTypeUnknown, // TODO fix this
|
||||
State: enum.GitspaceStateUninitialized,
|
||||
CodeRepoURL: in.CodeRepoURL,
|
||||
Branch: in.Branch,
|
||||
DevcontainerPath: in.DevcontainerPath,
|
||||
UserID: session.Principal.UID,
|
||||
SpaceID: parentSpace.ID,
|
||||
SpacePath: parentSpace.Path,
|
||||
Created: now,
|
||||
Updated: now,
|
||||
}
|
||||
err = c.gitspaceConfigStore.Create(ctx, gitspaceConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create gitspace config for : %q %w", identifier, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not find infra provider resource : %q %w", in.ResourceIdentifier, err)
|
||||
}
|
||||
gitspaceConfig := &types.GitspaceConfig{
|
||||
Identifier: identifier,
|
||||
Name: in.Name,
|
||||
IDE: in.IDE,
|
||||
InfraProviderResourceID: infraProviderResource.ID,
|
||||
InfraProviderResourceIdentifier: infraProviderResource.Identifier,
|
||||
CodeRepoType: enum.CodeRepoTypeUnknown, // TODO fix this
|
||||
State: enum.GitspaceStateUninitialized,
|
||||
CodeRepoURL: in.CodeRepoURL,
|
||||
Branch: in.Branch,
|
||||
DevcontainerPath: in.DevcontainerPath,
|
||||
UserID: session.Principal.UID,
|
||||
SpaceID: parentSpace.ID,
|
||||
SpacePath: parentSpace.Path,
|
||||
Created: now,
|
||||
Updated: now,
|
||||
}
|
||||
err = c.gitspaceConfigStore.Create(ctx, gitspaceConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create gitspace config for : %q %w", identifier, err)
|
||||
return nil, err
|
||||
}
|
||||
return gitspaceConfig, nil
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ package gitspace
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
apiauth "github.com/harness/gitness/app/api/auth"
|
||||
"github.com/harness/gitness/app/auth"
|
||||
|
@ -58,8 +57,7 @@ func (c *Controller) Delete(
|
|||
}
|
||||
gitspaceConfig.IsDeleted = true
|
||||
if err = c.gitspaceConfigStore.Update(ctx, gitspaceConfig); err != nil {
|
||||
log.Err(err).Msg("Failed to delete gitspace config with ID " + strconv.FormatInt(gitspaceConfig.ID, 10))
|
||||
return err
|
||||
return fmt.Errorf("failed to delete gitspace config with ID: %s %w", gitspaceConfig.Identifier, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ func (c *Controller) Find(
|
|||
identifier string,
|
||||
) (*types.GitspaceConfig, error) {
|
||||
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
|
||||
const resourceNotFoundErr = "Failed to find gitspace: resource not found"
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find space: %w", err)
|
||||
}
|
||||
|
@ -38,28 +39,39 @@ func (c *Controller) Find(
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to authorize: %w", err)
|
||||
}
|
||||
gitspaceConfig, err := c.gitspaceConfigStore.FindByIdentifier(ctx, space.ID, identifier)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find gitspace config: %w", err)
|
||||
}
|
||||
infraProviderResource, err := c.infraProviderResourceStore.Find(
|
||||
ctx,
|
||||
gitspaceConfig.InfraProviderResourceID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find infra provider resource for gitspace config: %w", err)
|
||||
}
|
||||
gitspaceConfig.SpacePath = space.Path
|
||||
gitspaceConfig.InfraProviderResourceIdentifier = infraProviderResource.Identifier
|
||||
instance, _ := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID, gitspaceConfig.SpaceID)
|
||||
if instance != nil {
|
||||
gitspaceConfig.GitspaceInstance = instance
|
||||
gitspaceStateType, err := enum.GetGitspaceStateFromInstance(instance.State)
|
||||
var gitspaceConfig *types.GitspaceConfig
|
||||
err = c.tx.WithTx(ctx, func(ctx context.Context) error {
|
||||
gitspaceConfig, err = c.gitspaceConfigStore.FindByIdentifier(ctx, space.ID, identifier)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return fmt.Errorf("failed to find gitspace config: %w", err)
|
||||
}
|
||||
gitspaceConfig.State = gitspaceStateType
|
||||
} else {
|
||||
gitspaceConfig.State = enum.GitspaceStateUninitialized
|
||||
infraProviderResource, err := c.infraProviderResourceStore.Find(
|
||||
ctx,
|
||||
gitspaceConfig.InfraProviderResourceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find infra provider resource for gitspace config: %w", err)
|
||||
}
|
||||
gitspaceConfig.SpacePath = space.Path
|
||||
gitspaceConfig.InfraProviderResourceIdentifier = infraProviderResource.Identifier
|
||||
instance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID, gitspaceConfig.SpaceID)
|
||||
if err != nil && err.Error() != resourceNotFoundErr { // TODO fix this
|
||||
return fmt.Errorf("failed to find gitspace instance for config ID : %s %w", gitspaceConfig.Identifier, err)
|
||||
}
|
||||
if instance != nil {
|
||||
gitspaceConfig.GitspaceInstance = instance
|
||||
instance.SpacePath = gitspaceConfig.SpacePath
|
||||
gitspaceStateType, err := enum.GetGitspaceStateFromInstance(instance.State)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceConfig.State = gitspaceStateType
|
||||
} else {
|
||||
gitspaceConfig.State = enum.GitspaceStateUninitialized
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gitspaceConfig, nil
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import (
|
|||
gitspaceevents "github.com/harness/gitness/app/events/gitspace"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator"
|
||||
"github.com/harness/gitness/app/store"
|
||||
"github.com/harness/gitness/store/database/dbtx"
|
||||
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
@ -29,6 +30,7 @@ var WireSet = wire.NewSet(
|
|||
)
|
||||
|
||||
func ProvideController(
|
||||
tx dbtx.Transactor,
|
||||
authorizer authz.Authorizer,
|
||||
resourceStore store.InfraProviderResourceStore,
|
||||
configStore store.GitspaceConfigStore,
|
||||
|
@ -39,6 +41,7 @@ func ProvideController(
|
|||
eventStore store.GitspaceEventStore,
|
||||
) *Controller {
|
||||
return NewController(
|
||||
tx,
|
||||
authorizer,
|
||||
resourceStore,
|
||||
configStore,
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"github.com/harness/gitness/app/auth/authz"
|
||||
"github.com/harness/gitness/app/store"
|
||||
"github.com/harness/gitness/infraprovider"
|
||||
"github.com/harness/gitness/store/database/dbtx"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
|
@ -26,9 +27,11 @@ type Controller struct {
|
|||
infraProviderConfigStore store.InfraProviderConfigStore
|
||||
infraProviderFactory infraprovider.Factory
|
||||
spaceStore store.SpaceStore
|
||||
tx dbtx.Transactor
|
||||
}
|
||||
|
||||
func NewController(
|
||||
tx dbtx.Transactor,
|
||||
authorizer authz.Authorizer,
|
||||
infraProviderResourceStore store.InfraProviderResourceStore,
|
||||
infraProviderConfigStore store.InfraProviderConfigStore,
|
||||
|
@ -36,6 +39,7 @@ func NewController(
|
|||
spaceStore store.SpaceStore,
|
||||
) *Controller {
|
||||
return &Controller{
|
||||
tx: tx,
|
||||
authorizer: authorizer,
|
||||
infraProviderResourceStore: infraProviderResourceStore,
|
||||
infraProviderConfigStore: infraProviderConfigStore,
|
||||
|
|
|
@ -84,58 +84,64 @@ func (c *Controller) Create(
|
|||
Created: now,
|
||||
Updated: now,
|
||||
}
|
||||
err = c.infraProviderConfigStore.Create(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create infraprovider config for : %q %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
infraProviderConfiginDB, err := c.infraProviderConfigStore.FindByIdentifier(
|
||||
ctx,
|
||||
parentSpace.ID,
|
||||
infraProviderConfig.Identifier)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
infraProvider, err := c.infraProviderFactory.GetInfraProvider(infraProviderConfiginDB.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(infraProvider.TemplateParams()) > 0 {
|
||||
return nil, fmt.Errorf("failed to fetch templates") // TODO Implement
|
||||
}
|
||||
parameters := []infraprovider.Parameter{}
|
||||
// TODO logic to populate paramteters as per the provider type
|
||||
err = infraProvider.ValidateParams(parameters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, res := range in.Resources {
|
||||
entity := &types.InfraProviderResource{
|
||||
Identifier: res.Identifier,
|
||||
InfraProviderConfigID: infraProviderConfiginDB.ID,
|
||||
InfraProviderType: res.InfraProviderType,
|
||||
Name: res.Name,
|
||||
SpaceID: parentSpace.ID,
|
||||
CPU: res.CPU,
|
||||
Memory: res.Memory,
|
||||
Disk: res.Disk,
|
||||
Network: res.Network,
|
||||
Region: strings.Join(res.Region, " "), // TODO fix
|
||||
Metadata: res.Metadata,
|
||||
GatewayHost: res.GatewayHost,
|
||||
GatewayPort: res.GatewayPort, // No template as of now
|
||||
Created: now,
|
||||
Updated: now,
|
||||
}
|
||||
err = c.infraProviderResourceStore.Create(ctx, infraProviderConfiginDB.ID, entity)
|
||||
err = c.tx.WithTx(ctx, func(ctx context.Context) error {
|
||||
err = c.infraProviderConfigStore.Create(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create infraprovider resource for : %q %w", entity.Identifier, err)
|
||||
return fmt.Errorf("failed to create infraprovider config for : %q %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
}
|
||||
resources, err := c.infraProviderResourceStore.List(ctx, infraProviderConfiginDB.ID, types.ListQueryFilter{})
|
||||
infraProviderConfig.Resources = resources
|
||||
infraProviderConfiginDB, err := c.infraProviderConfigStore.FindByIdentifier(
|
||||
ctx,
|
||||
parentSpace.ID,
|
||||
infraProviderConfig.Identifier)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
infraProvider, err := c.infraProviderFactory.GetInfraProvider(infraProviderConfiginDB.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(infraProvider.TemplateParams()) > 0 {
|
||||
return fmt.Errorf("failed to fetch templates") // TODO Implement
|
||||
}
|
||||
parameters := []infraprovider.Parameter{}
|
||||
// TODO logic to populate paramteters as per the provider type
|
||||
err = infraProvider.ValidateParams(parameters)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to validate infraprovider templates")
|
||||
}
|
||||
for _, res := range in.Resources {
|
||||
entity := &types.InfraProviderResource{
|
||||
Identifier: res.Identifier,
|
||||
InfraProviderConfigID: infraProviderConfiginDB.ID,
|
||||
InfraProviderType: res.InfraProviderType,
|
||||
Name: res.Name,
|
||||
SpaceID: parentSpace.ID,
|
||||
CPU: res.CPU,
|
||||
Memory: res.Memory,
|
||||
Disk: res.Disk,
|
||||
Network: res.Network,
|
||||
Region: strings.Join(res.Region, " "), // TODO fix
|
||||
Metadata: res.Metadata,
|
||||
GatewayHost: res.GatewayHost,
|
||||
GatewayPort: res.GatewayPort, // No template as of now
|
||||
Created: now,
|
||||
Updated: now,
|
||||
}
|
||||
err = c.infraProviderResourceStore.Create(ctx, infraProviderConfiginDB.ID, entity)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create infraprovider resource for : %q %w", entity.Identifier, err)
|
||||
}
|
||||
}
|
||||
resources, err := c.infraProviderResourceStore.List(ctx, infraProviderConfiginDB.ID, types.ListQueryFilter{})
|
||||
infraProviderConfig.Resources = resources
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
"error creating infra provider resource for config : %q %w", infraProviderConfiginDB.Identifier, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"error creating infra provider resource for config : %q %w", infraProviderConfiginDB.Identifier, err)
|
||||
return nil, err
|
||||
}
|
||||
return infraProviderConfig, nil
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"github.com/harness/gitness/app/auth/authz"
|
||||
"github.com/harness/gitness/app/store"
|
||||
"github.com/harness/gitness/infraprovider"
|
||||
"github.com/harness/gitness/store/database/dbtx"
|
||||
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
@ -28,11 +29,12 @@ var WireSet = wire.NewSet(
|
|||
)
|
||||
|
||||
func ProvideController(
|
||||
tx dbtx.Transactor,
|
||||
authorizer authz.Authorizer,
|
||||
resourceStore store.InfraProviderResourceStore,
|
||||
configStore store.InfraProviderConfigStore,
|
||||
spaceStore store.SpaceStore,
|
||||
factory infraprovider.Factory,
|
||||
) *Controller {
|
||||
return NewController(authorizer, resourceStore, configStore, factory, spaceStore)
|
||||
return NewController(tx, authorizer, resourceStore, configStore, factory, spaceStore)
|
||||
}
|
||||
|
|
|
@ -43,35 +43,48 @@ func (c *Controller) ListGitspaces(
|
|||
UserID: session.Principal.UID,
|
||||
SpaceIDs: []int64{space.ID},
|
||||
}
|
||||
gitspaceConfigs, err := c.gitspaceConfigStore.List(ctx, gitspaceFilter)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to list gitspace configs: %w", err)
|
||||
}
|
||||
count, err := c.gitspaceConfigStore.Count(ctx, gitspaceFilter)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to count gitspaces in space: %w", err)
|
||||
}
|
||||
var gitspaceConfigIDs = make([]int64, 0)
|
||||
for idx := 0; idx < len(gitspaceConfigs); idx++ {
|
||||
if gitspaceConfigs[idx].IsDeleted {
|
||||
continue
|
||||
var gitspaceConfigs []*types.GitspaceConfig
|
||||
var count int64
|
||||
err = c.tx.WithTx(ctx, func(ctx context.Context) (err error) {
|
||||
gitspaceConfigs, err = c.gitspaceConfigStore.List(ctx, gitspaceFilter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list gitspace configs: %w", err)
|
||||
}
|
||||
gitspaceConfigs[idx].SpacePath = space.Path // As the API is for a space, this will remain same
|
||||
gitspaceConfigIDs = append(gitspaceConfigIDs, gitspaceConfigs[idx].ID)
|
||||
}
|
||||
gitspaceInstancesMap, err := c.getLatestInstanceMap(ctx, gitspaceConfigIDs)
|
||||
count, err = c.gitspaceConfigStore.Count(ctx, gitspaceFilter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to count gitspaces in space: %w", err)
|
||||
}
|
||||
var gitspaceConfigIDs = make([]int64, 0)
|
||||
for idx := 0; idx < len(gitspaceConfigs); idx++ {
|
||||
if gitspaceConfigs[idx].IsDeleted {
|
||||
continue
|
||||
}
|
||||
gitspaceConfigs[idx].SpacePath = space.Path // As the API is for a space, this will remain same
|
||||
gitspaceConfigIDs = append(gitspaceConfigIDs, gitspaceConfigs[idx].ID)
|
||||
}
|
||||
gitspaceInstancesMap, err := c.getLatestInstanceMap(ctx, gitspaceConfigIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, gitspaceConfig := range gitspaceConfigs {
|
||||
instance := gitspaceInstancesMap[gitspaceConfig.ID]
|
||||
gitspaceConfig.GitspaceInstance = instance
|
||||
if instance != nil {
|
||||
gitspaceStateType, err := enum.GetGitspaceStateFromInstance(instance.State)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceConfig.State = gitspaceStateType
|
||||
instance.SpacePath = gitspaceConfig.SpacePath
|
||||
} else {
|
||||
gitspaceConfig.State = enum.GitspaceStateUninitialized
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
for _, gitspaceConfig := range gitspaceConfigs {
|
||||
gitspaceConfig.GitspaceInstance = gitspaceInstancesMap[gitspaceConfig.ID]
|
||||
if gitspaceConfig.GitspaceInstance != nil {
|
||||
gitspaceConfig.State, _ = enum.GetGitspaceStateFromInstance(gitspaceConfig.GitspaceInstance.State)
|
||||
gitspaceConfig.GitspaceInstance.SpacePath = gitspaceConfig.SpacePath
|
||||
} else {
|
||||
gitspaceConfig.State = enum.GitspaceStateUninitialized
|
||||
}
|
||||
}
|
||||
return gitspaceConfigs, count, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -325,7 +325,7 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
|
|||
dockerClientFactory := infraprovider.ProvideDockerClientFactory(dockerConfig)
|
||||
dockerProvider := infraprovider.ProvideDockerProvider(dockerClientFactory)
|
||||
factory := infraprovider.ProvideFactory(dockerProvider)
|
||||
infraproviderController := infraprovider2.ProvideController(authorizer, infraProviderResourceStore, infraProviderConfigStore, spaceStore, factory)
|
||||
infraproviderController := infraprovider2.ProvideController(transactor, authorizer, infraProviderResourceStore, infraProviderConfigStore, spaceStore, factory)
|
||||
reporter3, err := events5.ProvideReporter(eventsSystem)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -343,7 +343,7 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
|
|||
containerOrchestrator := container.ProvideEmbeddedDockerOrchestrator(dockerClientFactory, vsCode, vsCodeWeb, containerConfig, statefulLogger)
|
||||
orchestratorOrchestrator := orchestrator.ProvideOrchestrator(scmSCM, infraProviderResourceStore, infraProvisioner, containerOrchestrator)
|
||||
gitspaceEventStore := database.ProvideGitspaceEventStore(db)
|
||||
gitspaceController := gitspace.ProvideController(authorizer, infraProviderResourceStore, gitspaceConfigStore, gitspaceInstanceStore, spaceStore, reporter3, orchestratorOrchestrator, gitspaceEventStore)
|
||||
gitspaceController := gitspace.ProvideController(transactor, authorizer, infraProviderResourceStore, gitspaceConfigStore, gitspaceInstanceStore, spaceStore, reporter3, orchestratorOrchestrator, gitspaceEventStore)
|
||||
migrateController := migrate.ProvideController(authorizer, principalStore)
|
||||
apiHandler := router.ProvideAPIHandler(ctx, config, authenticator, repoController, reposettingsController, executionController, logsController, spaceController, pipelineController, secretController, triggerController, connectorController, templateController, pluginController, pullreqController, webhookController, githookController, gitInterface, serviceaccountController, controller, principalController, checkController, systemController, uploadController, keywordsearchController, infraproviderController, gitspaceController, migrateController)
|
||||
gitHandler := router.ProvideGitHandler(provider, authenticator, repoController)
|
||||
|
|
|
@ -26,6 +26,8 @@ var gitspaceInstanceStateTypes = []GitspaceInstanceStateType{
|
|||
GitspaceInstanceStateUnknown,
|
||||
GitspaceInstanceStateError,
|
||||
GitspaceInstanceStateDeleted,
|
||||
GitspaceInstanceStateStarting,
|
||||
GitspaceInstanceStateStopping,
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -34,11 +36,14 @@ const (
|
|||
GitspaceInstanceStateUnknown GitspaceInstanceStateType = "unknown"
|
||||
GitspaceInstanceStateError GitspaceInstanceStateType = "error"
|
||||
GitspaceInstanceStateDeleted GitspaceInstanceStateType = "deleted"
|
||||
|
||||
GitspaceInstanceStateStarting GitspaceInstanceStateType = "starting"
|
||||
GitspaceInstanceStateStopping GitspaceInstanceStateType = "stopping"
|
||||
)
|
||||
|
||||
func (gitspaceInstanceState GitspaceInstanceStateType) IsFinalStatus() bool {
|
||||
func (g GitspaceInstanceStateType) IsFinalStatus() bool {
|
||||
//nolint:exhaustive
|
||||
switch gitspaceInstanceState {
|
||||
switch g {
|
||||
case GitspaceInstanceStateDeleted,
|
||||
GitspaceInstanceStateError:
|
||||
return true
|
||||
|
@ -46,3 +51,14 @@ func (gitspaceInstanceState GitspaceInstanceStateType) IsFinalStatus() bool {
|
|||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (g GitspaceInstanceStateType) IsBusyStatus() bool {
|
||||
//nolint:exhaustive
|
||||
switch g {
|
||||
case GitspaceInstanceStateStarting,
|
||||
GitspaceInstanceStateStopping:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,9 +45,14 @@ func GetGitspaceStateFromInstance(
|
|||
switch instanceState { //nolint:exhaustive
|
||||
case GitspaceInstanceStateRunning:
|
||||
return GitspaceStateRunning, nil
|
||||
case GitspaceInstanceStateUninitialized,
|
||||
GitspaceInstanceStateDeleted:
|
||||
case GitspaceInstanceStateDeleted:
|
||||
return GitspaceStateStopped, nil
|
||||
case GitspaceInstanceStateStarting:
|
||||
return GitspaceStateStarting, nil
|
||||
case GitspaceInstanceStateStopping:
|
||||
return GitspaceStateStopping, nil
|
||||
case GitspaceInstanceStateUninitialized:
|
||||
return GitspaceStateUninitialized, nil
|
||||
default:
|
||||
return GitspaceStateError, fmt.Errorf("unsupported gitspace instance state %s", string(instanceState))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue