-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
local_artifact_manager.go
128 lines (104 loc) · 3.29 KB
/
local_artifact_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package artifact
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/pkg/log"
"github.com/YuukanOO/seelf/pkg/ostools"
)
var (
ErrArtifactOpenLoggerFailed = errors.New("artifact_open_logger_failed")
ErrArtifactPrepareBuildDirectoryFailed = errors.New("artifact_prepare_build_directory_failed")
)
const (
logsDir = "logs"
appsDir = "apps"
)
type (
LocalOptions interface {
DeploymentDirTemplate() *template.Template
DataDir() string
}
localArtifactManager struct {
options LocalOptions
appsDirectory string
logsDirectory string
logger log.Logger
}
deploymentTemplateData struct {
Number domain.DeploymentNumber
Environment domain.Environment
}
)
// Instantiate a new ArtifactManager which will store all the artifacts locally.
func NewLocal(options LocalOptions, logger log.Logger) domain.ArtifactManager {
return &localArtifactManager{
options: options,
appsDirectory: filepath.Join(options.DataDir(), appsDir),
logsDirectory: filepath.Join(options.DataDir(), logsDir),
logger: logger,
}
}
func (a *localArtifactManager) PrepareBuild(
ctx context.Context,
depl domain.Deployment,
) (buildDirectory string, logger domain.DeploymentLogger, err error) {
logfile, err := ostools.OpenAppend(a.LogPath(ctx, depl))
if err != nil {
a.logger.Error(err)
return "", nil, ErrArtifactOpenLoggerFailed
}
logger = newLogger(logfile)
defer func() {
if err == nil {
return
}
logger.Error(err) // Log the error in the deployment log file
err = ErrArtifactPrepareBuildDirectoryFailed // Returns a generic error for the job to fail with it
logger.Close() // And close the logger right now
}()
if buildDirectory, err = a.deploymentPath(depl); err != nil {
return
}
logger.Infof("preparing build directory %s", buildDirectory)
if err = ostools.EmptyDir(buildDirectory); err != nil {
return
}
return
}
func (a *localArtifactManager) Cleanup(ctx context.Context, app domain.App) error {
// Remove all app directory
appDir := a.appPath(app.ID())
a.logger.Debugw("removing app directory", "path", appDir)
if err := os.RemoveAll(appDir); err != nil {
return err
}
// Remove all logs for this app
logsPattern := filepath.Join(a.logsDirectory, fmt.Sprintf("*%s*.deployment.log", app.ID()))
a.logger.Debugw("removing app logs", "pattern", logsPattern)
return ostools.RemovePattern(logsPattern)
}
func (a *localArtifactManager) LogPath(ctx context.Context, depl domain.Deployment) string {
return filepath.Join(
a.logsDirectory,
fmt.Sprintf("%d-%s-%d.deployment.log",
depl.Requested().At().Unix(), depl.ID().AppID(), depl.ID().DeploymentNumber()))
}
func (a *localArtifactManager) appPath(appID domain.AppID) string {
return filepath.Join(a.appsDirectory, string(appID))
}
func (a *localArtifactManager) deploymentPath(depl domain.Deployment) (string, error) {
var w strings.Builder
if err := a.options.DeploymentDirTemplate().Execute(&w, deploymentTemplateData{
Number: depl.ID().DeploymentNumber(),
Environment: depl.Config().Environment(),
}); err != nil {
return "", err
}
return filepath.Join(a.appPath(depl.ID().AppID()), w.String()), nil
}