mirror of https://github.com/harness/drone.git
feat: [CDE-461]: abstract logger for docker, gitspace setup steps (#2966)
* feat: [CDE-461]: abstract logger for docker, gitspace setup steps * feat: [CDE-461]: abstract logger for docker, gitspace setup steps * feat: [CDE-461]: abstract logger for docker, gitspace setup steps * feat: [CDE-461]: abstract logger for docker, gitspace setup steps * feat: [CDE-461]: abstract logger for docker, gitspace setup steps * feat: [CDE-461]: abstract logger for docker, gitspace setup steps * feat: [CDE-461]: abstract logger for docker, gitspace setup steps * feat: [CDE-461]: abstract logger for docker, gitspace setup stepspull/3586/head
parent
3cdbf05237
commit
d4c7f6930c
|
@ -115,3 +115,15 @@ func (l *LogStreamInstance) Flush() error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LogStreamInstance) Info(msg string) {
|
||||
l.Write("INFO: " + msg) //nolint:errcheck
|
||||
}
|
||||
|
||||
func (l *LogStreamInstance) Debug(msg string) {
|
||||
l.Write("DEBUG: " + msg) //nolint:errcheck
|
||||
}
|
||||
|
||||
func (l *LogStreamInstance) Error(msg string, err error) {
|
||||
l.Write("ERROR: " + msg + ": " + err.Error()) //nolint:errcheck
|
||||
}
|
||||
|
|
|
@ -22,9 +22,9 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/harness/gitness/app/gitspace/logutil"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/devcontainer"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/ide"
|
||||
orchestratorTypes "github.com/harness/gitness/app/gitspace/orchestrator/types"
|
||||
"github.com/harness/gitness/app/gitspace/scm"
|
||||
"github.com/harness/gitness/types"
|
||||
|
||||
|
@ -43,53 +43,44 @@ const (
|
|||
)
|
||||
|
||||
// Helper function to log messages and handle error wrapping.
|
||||
func logStreamWrapError(logStreamInstance *logutil.LogStreamInstance, msg string, err error) error {
|
||||
if loggingErr := logStreamInstance.Write(msg + ": " + err.Error()); loggingErr != nil {
|
||||
return fmt.Errorf("original error: %w; logging error: %w", err, loggingErr)
|
||||
}
|
||||
func logStreamWrapError(gitspaceLogger orchestratorTypes.GitspaceLogger, msg string, err error) error {
|
||||
gitspaceLogger.Error(msg, err)
|
||||
return fmt.Errorf("%s: %w", msg, err)
|
||||
}
|
||||
|
||||
// Helper function to log success messages.
|
||||
func logStreamSuccess(logStreamInstance *logutil.LogStreamInstance, message string) error {
|
||||
if loggingErr := logStreamInstance.Write(message); loggingErr != nil {
|
||||
return fmt.Errorf("logging error: %w", loggingErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Generalized Docker Container Management.
|
||||
func (e *EmbeddedDockerOrchestrator) manageContainer(
|
||||
ctx context.Context,
|
||||
action Action,
|
||||
containerName string,
|
||||
dockerClient *client.Client,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
var err error
|
||||
switch action {
|
||||
case ContainerActionStop:
|
||||
err = dockerClient.ContainerStop(ctx, containerName, container.StopOptions{})
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while stopping container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while stopping container", err)
|
||||
}
|
||||
return logStreamSuccess(logStreamInstance, "Successfully stopped container")
|
||||
gitspaceLogger.Info("Successfully stopped container")
|
||||
|
||||
case ContainerActionStart:
|
||||
err = dockerClient.ContainerStart(ctx, containerName, container.StartOptions{})
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while starting container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while starting container", err)
|
||||
}
|
||||
return logStreamSuccess(logStreamInstance, "Successfully started container")
|
||||
gitspaceLogger.Info("Successfully started container")
|
||||
case ContainerActionRemove:
|
||||
err = dockerClient.ContainerRemove(ctx, containerName, container.RemoveOptions{Force: true})
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while removing container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while removing container", err)
|
||||
}
|
||||
return logStreamSuccess(logStreamInstance, "Successfully removed container")
|
||||
gitspaceLogger.Info("Successfully removed container")
|
||||
default:
|
||||
return fmt.Errorf("unknown action: %s", action)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EmbeddedDockerOrchestrator) containerState(
|
||||
|
@ -118,7 +109,7 @@ func (e *EmbeddedDockerOrchestrator) createContainer(
|
|||
dockerClient *client.Client,
|
||||
imageName string,
|
||||
containerName string,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
volumeName string,
|
||||
homeDir string,
|
||||
portMappings map[int]*types.PortMapping,
|
||||
|
@ -126,10 +117,7 @@ func (e *EmbeddedDockerOrchestrator) createContainer(
|
|||
) error {
|
||||
exposedPorts, portBindings := applyPortMappings(portMappings)
|
||||
|
||||
// Log the creation process
|
||||
if err := logStreamInstance.Write("Creating container: " + containerName); err != nil {
|
||||
return fmt.Errorf("logging error: %w", err)
|
||||
}
|
||||
gitspaceLogger.Info("Creating container: " + containerName)
|
||||
|
||||
hostConfig := prepareHostConfig(volumeName, homeDir, portBindings)
|
||||
|
||||
|
@ -143,7 +131,7 @@ func (e *EmbeddedDockerOrchestrator) createContainer(
|
|||
}
|
||||
_, err := dockerClient.ContainerCreate(ctx, containerConfig, hostConfig, nil, nil, containerName)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while creating container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while creating container", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -217,11 +205,9 @@ func (e *EmbeddedDockerOrchestrator) pullImage(
|
|||
ctx context.Context,
|
||||
imageName string,
|
||||
dockerClient *client.Client,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
if err := logStreamSuccess(logStreamInstance, "Pulling image: "+imageName); err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceLogger.Info("Pulling image: " + imageName)
|
||||
|
||||
pullResponse, err := dockerClient.ImagePull(ctx, imageName, image.PullOptions{})
|
||||
defer func() {
|
||||
|
@ -231,16 +217,17 @@ func (e *EmbeddedDockerOrchestrator) pullImage(
|
|||
}()
|
||||
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while pulling image", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while pulling image", err)
|
||||
}
|
||||
|
||||
// Ensure the image has been fully pulled by reading the response
|
||||
output, err := io.ReadAll(pullResponse)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while parsing image pull response", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while parsing image pull response", err)
|
||||
}
|
||||
|
||||
return logStreamSuccess(logStreamInstance, string(output))
|
||||
gitspaceLogger.Info(string(output))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EmbeddedDockerOrchestrator) runGitspaceSetupSteps(
|
||||
|
@ -251,7 +238,7 @@ func (e *EmbeddedDockerOrchestrator) runGitspaceSetupSteps(
|
|||
infrastructure types.Infrastructure,
|
||||
resolvedRepoDetails scm.ResolvedDetails,
|
||||
defaultBaseImage string,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
homeDir := GetUserHomeDir(gitspaceConfig.GitspaceUser.Identifier)
|
||||
containerName := GetGitspaceContainerName(gitspaceConfig)
|
||||
|
@ -263,27 +250,33 @@ func (e *EmbeddedDockerOrchestrator) runGitspaceSetupSteps(
|
|||
}
|
||||
|
||||
// Pull the required image
|
||||
if err := e.pullImage(ctx, imageName, dockerClient, logStreamInstance); err != nil {
|
||||
if err := e.pullImage(ctx, imageName, dockerClient, gitspaceLogger); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
forwardPorts := ExtractForwardPorts(devcontainerConfig)
|
||||
portMappings := infrastructure.GitspacePortMappings
|
||||
for _, port := range forwardPorts {
|
||||
portMappings[port] = &types.PortMapping{
|
||||
PublishedPort: port,
|
||||
ForwardedPort: port,
|
||||
forwardPorts := ExtractForwardPorts(devcontainerConfig)
|
||||
if len(forwardPorts) > 0 {
|
||||
for _, port := range forwardPorts {
|
||||
portMappings[port] = &types.PortMapping{
|
||||
PublishedPort: port,
|
||||
ForwardedPort: port,
|
||||
}
|
||||
}
|
||||
gitspaceLogger.Info(fmt.Sprintf("Forwarding ports : %v", forwardPorts))
|
||||
}
|
||||
|
||||
storage := infrastructure.Storage
|
||||
environment := ExtractEnv(devcontainerConfig)
|
||||
if len(environment) > 0 {
|
||||
gitspaceLogger.Info(fmt.Sprintf("Setting Environment : %v", environment))
|
||||
}
|
||||
// Create the container
|
||||
err := e.createContainer(
|
||||
ctx,
|
||||
dockerClient,
|
||||
imageName,
|
||||
containerName,
|
||||
logStreamInstance,
|
||||
gitspaceLogger,
|
||||
storage,
|
||||
homeDir,
|
||||
portMappings,
|
||||
|
@ -294,7 +287,7 @@ func (e *EmbeddedDockerOrchestrator) runGitspaceSetupSteps(
|
|||
}
|
||||
|
||||
// Start the container
|
||||
if err := e.manageContainer(ctx, ContainerActionStart, containerName, dockerClient, logStreamInstance); err != nil {
|
||||
if err := e.manageContainer(ctx, ContainerActionStart, containerName, dockerClient, gitspaceLogger); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -311,7 +304,7 @@ func (e *EmbeddedDockerOrchestrator) runGitspaceSetupSteps(
|
|||
if err := e.setupGitspaceAndIDE(
|
||||
ctx,
|
||||
exec,
|
||||
logStreamInstance,
|
||||
gitspaceLogger,
|
||||
ideService,
|
||||
gitspaceConfig,
|
||||
resolvedRepoDetails,
|
||||
|
|
|
@ -23,6 +23,7 @@ import (
|
|||
"github.com/harness/gitness/app/gitspace/orchestrator/devcontainer"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/git"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/ide"
|
||||
orchestratorTypes "github.com/harness/gitness/app/gitspace/orchestrator/types"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/user"
|
||||
"github.com/harness/gitness/app/gitspace/scm"
|
||||
"github.com/harness/gitness/infraprovider"
|
||||
|
@ -38,15 +39,8 @@ const (
|
|||
loggingKey = "gitspace.container"
|
||||
)
|
||||
|
||||
// Step represents a single setup action.
|
||||
type Step struct {
|
||||
Name string
|
||||
Execute func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error
|
||||
StopOnFailure bool // Flag to control whether execution should stop on failure
|
||||
}
|
||||
|
||||
type EmbeddedDockerOrchestrator struct {
|
||||
steps []Step // Steps registry
|
||||
steps []orchestratorTypes.Step // Steps registry
|
||||
dockerClientFactory *infraprovider.DockerClientFactory
|
||||
statefulLogger *logutil.StatefulLogger
|
||||
gitService git.Service
|
||||
|
@ -56,10 +50,10 @@ type EmbeddedDockerOrchestrator struct {
|
|||
// RegisterStep registers a new setup step with an option to stop or continue on failure.
|
||||
func (e *EmbeddedDockerOrchestrator) RegisterStep(
|
||||
name string,
|
||||
execute func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error,
|
||||
execute func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger orchestratorTypes.GitspaceLogger) error,
|
||||
stopOnFailure bool,
|
||||
) {
|
||||
step := Step{
|
||||
step := orchestratorTypes.Step{
|
||||
Name: name,
|
||||
Execute: execute,
|
||||
StopOnFailure: stopOnFailure,
|
||||
|
@ -71,20 +65,17 @@ func (e *EmbeddedDockerOrchestrator) RegisterStep(
|
|||
func (e *EmbeddedDockerOrchestrator) ExecuteSteps(
|
||||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
for _, step := range e.steps {
|
||||
// Execute the step
|
||||
if err := step.Execute(ctx, exec, logStreamInstance); err != nil {
|
||||
if err := step.Execute(ctx, exec, gitspaceLogger); err != nil {
|
||||
// Log the error and decide whether to stop or continue based on stopOnFailure flag
|
||||
if step.StopOnFailure {
|
||||
return fmt.Errorf("error executing step %s: %w (stopping due to failure)", step.Name, err)
|
||||
}
|
||||
// Log that we continue despite the failure
|
||||
if err := logStreamSuccess(logStreamInstance,
|
||||
fmt.Sprintf("Step %s failed:", step.Name)); err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceLogger.Info(fmt.Sprintf("Step %s failed:", step.Name))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -19,10 +19,10 @@ import (
|
|||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/harness/gitness/app/gitspace/logutil"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/common"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/devcontainer"
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/ide"
|
||||
orchestratorTypes "github.com/harness/gitness/app/gitspace/orchestrator/types"
|
||||
"github.com/harness/gitness/app/gitspace/scm"
|
||||
"github.com/harness/gitness/types"
|
||||
"github.com/harness/gitness/types/enum"
|
||||
|
@ -32,7 +32,7 @@ import (
|
|||
func (e *EmbeddedDockerOrchestrator) setupGitspaceAndIDE(
|
||||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
ideService ide.IDE,
|
||||
gitspaceConfig types.GitspaceConfig,
|
||||
resolvedRepoDetails scm.ResolvedDetails,
|
||||
|
@ -46,44 +46,44 @@ func (e *EmbeddedDockerOrchestrator) setupGitspaceAndIDE(
|
|||
e.RegisterStep("Validate Supported OS", e.validateSupportedOS, true)
|
||||
e.RegisterStep("Manage User", e.manageUser, true)
|
||||
e.RegisterStep("Install Tools",
|
||||
func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error {
|
||||
return e.installTools(ctx, exec, logStreamInstance, gitspaceConfig.IDE)
|
||||
func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger orchestratorTypes.GitspaceLogger) error {
|
||||
return e.installTools(ctx, exec, gitspaceLogger, gitspaceConfig.IDE)
|
||||
}, true)
|
||||
e.RegisterStep("Setup IDE",
|
||||
func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error {
|
||||
return e.setupIDE(ctx, exec, ideService, logStreamInstance)
|
||||
func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger orchestratorTypes.GitspaceLogger) error {
|
||||
return e.setupIDE(ctx, exec, ideService, gitspaceLogger)
|
||||
}, true)
|
||||
e.RegisterStep("Run IDE",
|
||||
func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error {
|
||||
return e.runIDE(ctx, exec, ideService, logStreamInstance)
|
||||
func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger orchestratorTypes.GitspaceLogger) error {
|
||||
return e.runIDE(ctx, exec, ideService, gitspaceLogger)
|
||||
}, true)
|
||||
e.RegisterStep("Install Git", e.installGit, true)
|
||||
e.RegisterStep("Setup Git Credentials",
|
||||
func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error {
|
||||
func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger orchestratorTypes.GitspaceLogger) error {
|
||||
if resolvedRepoDetails.ResolvedCredentials.Credentials != nil {
|
||||
return e.setupGitCredentials(ctx, exec, resolvedRepoDetails, logStreamInstance)
|
||||
return e.setupGitCredentials(ctx, exec, resolvedRepoDetails, gitspaceLogger)
|
||||
}
|
||||
return nil
|
||||
}, true)
|
||||
e.RegisterStep("Clone Code",
|
||||
func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error {
|
||||
return e.cloneCode(ctx, exec, defaultBaseImage, resolvedRepoDetails, logStreamInstance)
|
||||
func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger orchestratorTypes.GitspaceLogger) error {
|
||||
return e.cloneCode(ctx, exec, defaultBaseImage, resolvedRepoDetails, gitspaceLogger)
|
||||
}, true)
|
||||
|
||||
// Register the Execute Command steps (PostCreate and PostStart)
|
||||
e.RegisterStep("Execute PostCreate Command",
|
||||
func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error {
|
||||
func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger orchestratorTypes.GitspaceLogger) error {
|
||||
command := ExtractCommand(PostCreateAction, devcontainerConfig)
|
||||
return e.executeCommand(ctx, exec, codeRepoDir, logStreamInstance, command, PostCreateAction)
|
||||
return e.executeCommand(ctx, exec, codeRepoDir, gitspaceLogger, command, PostCreateAction)
|
||||
}, false)
|
||||
e.RegisterStep("Execute PostStart Command",
|
||||
func(ctx context.Context, exec *devcontainer.Exec, logStreamInstance *logutil.LogStreamInstance) error {
|
||||
func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger orchestratorTypes.GitspaceLogger) error {
|
||||
command := ExtractCommand(PostStartAction, devcontainerConfig)
|
||||
return e.executeCommand(ctx, exec, codeRepoDir, logStreamInstance, command, PostStartAction)
|
||||
return e.executeCommand(ctx, exec, codeRepoDir, gitspaceLogger, command, PostStartAction)
|
||||
}, false)
|
||||
|
||||
// Execute the registered steps
|
||||
if err := e.ExecuteSteps(ctx, exec, logStreamInstance); err != nil {
|
||||
if err := e.ExecuteSteps(ctx, exec, gitspaceLogger); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -92,17 +92,15 @@ func (e *EmbeddedDockerOrchestrator) setupGitspaceAndIDE(
|
|||
func (e *EmbeddedDockerOrchestrator) installTools(
|
||||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
ideType enum.IDEType,
|
||||
) error {
|
||||
output, err := common.InstallTools(ctx, exec, ideType)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while installing tools inside container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while installing tools inside container", err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(logStreamInstance, "Tools installation output...\n"+string(output)); err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceLogger.Info("Tools installation output...\n" + string(output))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -110,16 +108,14 @@ func (e *EmbeddedDockerOrchestrator) installTools(
|
|||
func (e *EmbeddedDockerOrchestrator) validateSupportedOS(
|
||||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
output, err := common.ValidateSupportedOS(ctx, exec)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while detecting OS inside container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while detecting OS inside container", err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(logStreamInstance, "Validate supported OSes...\n"+string(output)); err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceLogger.Info("Validate supported OSes...\n" + string(output))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -128,37 +124,24 @@ func (e *EmbeddedDockerOrchestrator) executeCommand(
|
|||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
codeRepoDir string,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
command string,
|
||||
actionType PostAction,
|
||||
) error {
|
||||
if command == "" {
|
||||
if err := logStreamSuccess(
|
||||
logStreamInstance,
|
||||
fmt.Sprintf("No %s command provided, skipping execution", actionType)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
gitspaceLogger.Info(fmt.Sprintf("No %s command provided, skipping execution", actionType))
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(
|
||||
logStreamInstance, fmt.Sprintf("Executing %s command: %s", actionType, command)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gitspaceLogger.Info(fmt.Sprintf("Executing %s command: %s", actionType, command))
|
||||
output, err := exec.ExecuteCommand(ctx, command, true, false, codeRepoDir)
|
||||
if err != nil {
|
||||
return logStreamWrapError(
|
||||
logStreamInstance, fmt.Sprintf("Error while executing %s command", actionType), err)
|
||||
gitspaceLogger, fmt.Sprintf("Error while executing %s command", actionType), err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(
|
||||
logStreamInstance, "Post create command execution output...\n"+string(output)); err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceLogger.Info("Post create command execution output...\n" + string(output))
|
||||
|
||||
return logStreamSuccess(
|
||||
logStreamInstance, "Successfully executed postCreate command")
|
||||
gitspaceLogger.Info(fmt.Sprintf("Successfully executed %s command", actionType))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EmbeddedDockerOrchestrator) cloneCode(
|
||||
|
@ -166,34 +149,28 @@ func (e *EmbeddedDockerOrchestrator) cloneCode(
|
|||
exec *devcontainer.Exec,
|
||||
defaultBaseImage string,
|
||||
resolvedRepoDetails scm.ResolvedDetails,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
output, err := e.gitService.CloneCode(ctx, exec, resolvedRepoDetails, defaultBaseImage)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while cloning code inside container", err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(logStreamInstance, "Clone output...\n"+string(output)); err != nil {
|
||||
return err
|
||||
return logStreamWrapError(gitspaceLogger, "Error while cloning code inside container", err)
|
||||
}
|
||||
|
||||
gitspaceLogger.Info("Clone output...\n" + string(output))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EmbeddedDockerOrchestrator) installGit(
|
||||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
output, err := e.gitService.Install(ctx, exec)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while installing git inside container", err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(logStreamInstance, "Install git output...\n"+string(output)); err != nil {
|
||||
return err
|
||||
return logStreamWrapError(gitspaceLogger, "Error while installing git inside container", err)
|
||||
}
|
||||
|
||||
gitspaceLogger.Info("Install git output...\n" + string(output))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -201,35 +178,28 @@ func (e *EmbeddedDockerOrchestrator) setupGitCredentials(
|
|||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
resolvedRepoDetails scm.ResolvedDetails,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
output, err := e.gitService.SetupCredentials(ctx, exec, resolvedRepoDetails)
|
||||
if err != nil {
|
||||
return logStreamWrapError(
|
||||
logStreamInstance, "Error while setting up git credentials inside container", err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(logStreamInstance, "Setting up git credentials output...\n"+string(output)); err != nil {
|
||||
return err
|
||||
gitspaceLogger, "Error while setting up git credentials inside container", err)
|
||||
}
|
||||
|
||||
gitspaceLogger.Info("Setting up git credentials output...\n" + string(output))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EmbeddedDockerOrchestrator) manageUser(
|
||||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
output, err := e.userService.Manage(ctx, exec)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while creating user inside container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while creating user inside container", err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(logStreamInstance, "Managing user output...\n"+string(output)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gitspaceLogger.Info("Managing user output...\n" + string(output))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -237,44 +207,35 @@ func (e *EmbeddedDockerOrchestrator) setupIDE(
|
|||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
ideService ide.IDE,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
if err := logStreamSuccess(
|
||||
logStreamInstance, "Setting up IDE inside container: "+string(ideService.Type())); err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceLogger.Info("Setting up IDE inside container: " + string(ideService.Type()))
|
||||
|
||||
output, err := ideService.Setup(ctx, exec)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while setting up IDE inside container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while setting up IDE inside container", err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(logStreamInstance, "IDE setup output...\n"+string(output)); err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceLogger.Info("IDE setup output...\n" + string(output))
|
||||
|
||||
return logStreamSuccess(logStreamInstance, "Successfully set up IDE inside container")
|
||||
gitspaceLogger.Info("Successfully set up IDE inside container")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EmbeddedDockerOrchestrator) runIDE(
|
||||
ctx context.Context,
|
||||
exec *devcontainer.Exec,
|
||||
ideService ide.IDE,
|
||||
logStreamInstance *logutil.LogStreamInstance,
|
||||
gitspaceLogger orchestratorTypes.GitspaceLogger,
|
||||
) error {
|
||||
if err := logStreamSuccess(
|
||||
logStreamInstance, "Running the IDE inside container: "+string(ideService.Type())); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gitspaceLogger.Info("Running the IDE inside container: " + string(ideService.Type()))
|
||||
output, err := ideService.Run(ctx, exec)
|
||||
if err != nil {
|
||||
return logStreamWrapError(logStreamInstance, "Error while running IDE inside container", err)
|
||||
return logStreamWrapError(gitspaceLogger, "Error while running IDE inside container", err)
|
||||
}
|
||||
|
||||
if err := logStreamSuccess(logStreamInstance, "IDE run output...\n"+string(output)); err != nil {
|
||||
return err
|
||||
}
|
||||
gitspaceLogger.Info("IDE run output...\n" + string(output))
|
||||
|
||||
return logStreamSuccess(logStreamInstance, "Successfully run the IDE inside container")
|
||||
gitspaceLogger.Info("Successfully run the IDE inside container")
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2023 Harness, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/harness/gitness/app/gitspace/orchestrator/devcontainer"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type GitspaceLogger interface {
|
||||
Info(msg string)
|
||||
Debug(msg string)
|
||||
Error(msg string, err error)
|
||||
}
|
||||
|
||||
// Step represents a single setup action.
|
||||
type Step struct {
|
||||
Name string
|
||||
Execute func(ctx context.Context, exec *devcontainer.Exec, gitspaceLogger GitspaceLogger) error
|
||||
StopOnFailure bool // Flag to control whether execution should stop on failure
|
||||
}
|
||||
|
||||
type ZerologAdapter struct {
|
||||
logger *zerolog.Logger
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2023 Harness, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package types
|
||||
|
||||
import "github.com/rs/zerolog"
|
||||
|
||||
// NewZerologAdapter creates a new adapter from a zerolog.Logger.
|
||||
func NewZerologAdapter(logger *zerolog.Logger) *ZerologAdapter {
|
||||
return &ZerologAdapter{logger: logger}
|
||||
}
|
||||
|
||||
// Implement the Logger interface for ZerologAdapter.
|
||||
func (z *ZerologAdapter) Info(msg string) {
|
||||
z.logger.Info().Msg(msg)
|
||||
}
|
||||
|
||||
func (z *ZerologAdapter) Debug(msg string) {
|
||||
z.logger.Debug().Msg(msg)
|
||||
}
|
||||
|
||||
func (z *ZerologAdapter) Error(msg string, err error) {
|
||||
z.logger.Err(err).Msg(msg)
|
||||
}
|
Loading…
Reference in New Issue