Skip to content

Commit

Permalink
feat: [CDE-163]: Moving dir creation and deletion to infra provider. …
Browse files Browse the repository at this point in the history
…Refactoring gitspace env variables. Renaming unprovisioning to deprovisioning. (harness#2231)

* feat: [CDE-163]: Moving dir creation and deletion to infra provider. Refactoring gitspace env variables. Renaming unprovisioning to deprovisioning.
  • Loading branch information
dhruv-harness authored and Harness committed Jul 16, 2024
1 parent 1711ee0 commit 6e51618
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ENV GITNESS_DATABASE_DATASOURCE /data/database.sqlite
ENV GITNESS_METRIC_ENABLED=true
ENV GITNESS_METRIC_ENDPOINT=https://stats.drone.ci/api/v1/gitness
ENV GITNESS_TOKEN_COOKIE_NAME=token
ENV GITNESS_GITSPACE_DEFAULT_BIND_MOUNT_SOURCE_BASE_PATH /data
ENV GITNESS_GITSPACE_ROOT /data
ENV GITNESS_DOCKER_HOST unix:///var/run/docker.sock

COPY --from=builder /app/gitness /app/gitness
Expand Down
4 changes: 3 additions & 1 deletion app/api/controller/gitspace/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (c *Controller) Delete(
}
instance, _ := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID, gitspaceConfig.SpaceID)
gitspaceConfig.GitspaceInstance = instance
gitspaceConfig.SpacePath = space.Path
if instance != nil {
if stopErr := c.stopRunningGitspace(ctx, gitspaceConfig); stopErr != nil {
return stopErr
Expand All @@ -64,7 +65,8 @@ func (c *Controller) Delete(

func (c *Controller) stopRunningGitspace(
ctx context.Context,
config *types.GitspaceConfig) error {
config *types.GitspaceConfig,
) error {
if instanceUpdated, err := c.orchestrator.DeleteGitspace(ctx, config); err != nil {
return err
} else if err = c.gitspaceInstanceStore.Update(ctx, instanceUpdated); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions app/gitspace/infrastructure/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type InfraProvisioner interface {
gitspaceConfig *types.GitspaceConfig,
) (*infraprovider.Infrastructure, error)

// Unprovision unprovisions all the resources created for the gitspace.
Unprovision(
// Deprovision removes all the resources created for the gitspace.
Deprovision(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
gitspaceConfig *types.GitspaceConfig,
Expand Down
7 changes: 4 additions & 3 deletions app/gitspace/infrastructure/provisioner_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (i infraProvisioner) Provision(
return nil, fmt.Errorf("invalid provisioning params %v: %w", infraProviderResource.Metadata, err)
}

provisionedInfra, err := infraProvider.Provision(ctx, gitspaceConfig.Identifier, allParams)
provisionedInfra, err := infraProvider.Provision(ctx, gitspaceConfig.SpacePath, gitspaceConfig.Identifier, allParams)
if err != nil {
return nil, fmt.Errorf(
"unable to provision infrastructure for gitspaceConfigIdentifier %v: %w",
Expand Down Expand Up @@ -156,7 +156,7 @@ func (i infraProvisioner) Stop(
return &stoppedInfra, err
}

func (i infraProvisioner) Unprovision(
func (i infraProvisioner) Deprovision(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
gitspaceConfig *types.GitspaceConfig,
Expand Down Expand Up @@ -199,12 +199,13 @@ func (i infraProvisioner) Unprovision(
} else {
provisionedInfra = infraprovider.Infrastructure{
ResourceKey: gitspaceConfig.Identifier,
SpacePath: gitspaceConfig.SpacePath,
ProviderType: infraProviderEntity.Type,
Parameters: allParams,
}
}

destroyedInfra, err := infraProvider.Destroy(ctx, provisionedInfra)
destroyedInfra, err := infraProvider.Deprovision(ctx, provisionedInfra)
if err != nil {
return nil, fmt.Errorf("unable to stop provisioned infra %+v: %w", provisionedInfra, err)
}
Expand Down
57 changes: 11 additions & 46 deletions app/gitspace/orchestrator/container/embedded_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -51,10 +50,9 @@ const (
)

type Config struct {
DefaultBaseImage string
DefaultBindMountTargetPath string
DefaultBindMountSourceBasePath string
DefaultBindMountSourceBasePathAbsolute string
DefaultBaseImage string
WorkingDirectory string
RootSource string
}

type EmbeddedDockerOrchestrator struct {
Expand Down Expand Up @@ -174,7 +172,7 @@ func (e *EmbeddedDockerOrchestrator) StartGitspace(
usedPorts = ports

// TODO: Add gitspace status reporting.
log.Debug().Msgf("started gitspace: %s", gitspaceConfig.Identifier)
log.Debug().Msg("started gitspace")

default:
return nil, fmt.Errorf("gitspace %s is in a bad state: %s", containerName, state)
Expand All @@ -183,7 +181,7 @@ func (e *EmbeddedDockerOrchestrator) StartGitspace(
return &StartResponse{
ContainerID: containerID,
ContainerName: containerName,
WorkingDirectory: strings.TrimPrefix(e.config.DefaultBindMountTargetPath, "/"),
WorkingDirectory: strings.TrimPrefix(e.config.WorkingDirectory, "/"),
PortsUsed: usedPorts,
}, nil
}
Expand Down Expand Up @@ -215,7 +213,7 @@ func (e *EmbeddedDockerOrchestrator) startGitspace(
var devcontainer = &Devcontainer{
ContainerName: containerName,
DockerClient: dockerClient,
WorkingDir: e.config.DefaultBindMountTargetPath,
WorkingDir: e.config.WorkingDirectory,
}

err = e.executePostCreateCommand(ctx, devcontainerConfig, devcontainer, logStreamInstance)
Expand Down Expand Up @@ -444,48 +442,15 @@ func (e *EmbeddedDockerOrchestrator) createContainer(
commands := make(strslice.StrSlice, 0)
commands = append(commands, "infinity")

bindMountSourcePath :=
mountSource :=
filepath.Join(
e.config.DefaultBindMountSourceBasePath,
e.config.RootSource,
gitspacesDir,
gitspaceConfig.SpacePath,
gitspaceConfig.Identifier,
)

absoluteBindMountSourcePath :=
filepath.Join(
e.config.DefaultBindMountSourceBasePathAbsolute,
gitspacesDir,
gitspaceConfig.SpacePath,
gitspaceConfig.Identifier,
)

loggingErr := logStreamInstance.Write(
"Creating bind mount source directory: " + bindMountSourcePath + " (" + absoluteBindMountSourcePath + ")")
if loggingErr != nil {
return fmt.Errorf("logging error: %w", loggingErr)
}

err := os.MkdirAll(bindMountSourcePath, os.ModePerm)
if err != nil {
loggingErr = logStreamInstance.Write("Error while creating bind mount source directory: " + err.Error())

err = fmt.Errorf(
"could not create bind mount source path %s: %w", bindMountSourcePath, err)

if loggingErr != nil {
err = fmt.Errorf("original error: %w; logging error: %w", err, loggingErr)
}

return err
}

loggingErr = logStreamInstance.Write("Successfully created bind mount source directory")
if loggingErr != nil {
return fmt.Errorf("logging error: %w", loggingErr)
}

loggingErr = logStreamInstance.Write("Creating container: " + containerName)
loggingErr := logStreamInstance.Write("Creating container: " + containerName)
if loggingErr != nil {
return fmt.Errorf("logging error: %w", loggingErr)
}
Expand All @@ -500,8 +465,8 @@ func (e *EmbeddedDockerOrchestrator) createContainer(
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: absoluteBindMountSourcePath,
Target: e.config.DefaultBindMountTargetPath,
Source: mountSource,
Target: e.config.WorkingDirectory,
},
},
}, nil, containerName)
Expand Down
2 changes: 1 addition & 1 deletion app/gitspace/orchestrator/orchestrator_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (o orchestrator) DeleteGitspace(

o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraUnprovisioningStart)

_, err = o.infraProvisioner.Unprovision(ctx, infraProviderResource, gitspaceConfig)
_, err = o.infraProvisioner.Deprovision(ctx, infraProviderResource, gitspaceConfig)
if err != nil {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraUnprovisioningFailed)

Expand Down
47 changes: 28 additions & 19 deletions cli/operations/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,28 +405,19 @@ func ProvideIDEVSCodeWebConfig(config *types.Config) *container.VSCodeWebConfig
}

// ProvideGitspaceContainerOrchestratorConfig loads the Gitspace container orchestrator config from the main config.
func ProvideGitspaceContainerOrchestratorConfig(config *types.Config) (*container.Config, error) {
if config.Gitspace.DefaultBindMountSourceBasePath == "" {
var homedir string

homedir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("unable to determine home directory: %w", err)
}

config.Gitspace.DefaultBindMountSourceBasePath = filepath.Join(homedir, gitnessHomeDir)
}

if config.Gitspace.DefaultBindMountSourceBasePathAbsolute == "" {
config.Gitspace.DefaultBindMountSourceBasePathAbsolute = config.Gitspace.DefaultBindMountSourceBasePath
func ProvideGitspaceContainerOrchestratorConfig(
config *types.Config,
dockerProviderConfig *infraprovider.DockerProviderConfig,
) *container.Config {
if config.Gitspace.RootSource == "" {
config.Gitspace.RootSource = dockerProviderConfig.MountSourceBasePath
}

return &container.Config{
DefaultBaseImage: config.Gitspace.DefaultBaseImage,
DefaultBindMountTargetPath: config.Gitspace.DefaultBindMountTargetPath,
DefaultBindMountSourceBasePath: config.Gitspace.DefaultBindMountSourceBasePath,
DefaultBindMountSourceBasePathAbsolute: config.Gitspace.DefaultBindMountSourceBasePathAbsolute,
}, nil
DefaultBaseImage: config.Gitspace.DefaultBaseImage,
WorkingDirectory: config.Gitspace.WorkingDirectory,
RootSource: config.Gitspace.RootSource,
}
}

// ProvideGitspaceEventConfig loads the gitspace event service config from the main config.
Expand All @@ -437,3 +428,21 @@ func ProvideGitspaceEventConfig(config *types.Config) gitspaceevent.Config {
MaxRetries: config.Gitspace.Events.MaxRetries,
}
}

// ProvideDockerProviderConfig loads the Docker provider config from the main config.
func ProvideDockerProviderConfig(config *types.Config) (*infraprovider.DockerProviderConfig, error) {
if config.Gitspace.Root == "" {
var homedir string

homedir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("unable to determine home directory: %w", err)
}

config.Gitspace.Root = filepath.Join(homedir, gitnessHomeDir)
}

return &infraprovider.DockerProviderConfig{
MountSourceBasePath: config.Gitspace.Root,
}, nil
}
1 change: 1 addition & 0 deletions cmd/gitness/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e
cliserver.ProvideGitspaceContainerOrchestratorConfig,
cliserver.ProvideGitspaceEventConfig,
logutil.WireSet,
cliserver.ProvideDockerProviderConfig,
)
return &cliserver.System{}, nil
}
11 changes: 6 additions & 5 deletions cmd/gitness/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6e51618

Please sign in to comment.