/
deployment.go
52 lines (42 loc) · 1.43 KB
/
deployment.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
package model
import (
"path/filepath"
"regexp"
"github.com/pkg/errors"
)
var (
refnameRegexp = regexp.MustCompile(`^refs/heads/`)
)
type Deployment struct {
App *Application
Branch string
ComposeFilePath string
ProjectName string
Revision string
Timestamp string
}
// args:
// user/app, 19fb23cd71a4cf2eab00ad1a393e40de4ed61531, user, 4c:1f:92:b9:43:2b:23:0b:c0:e8:ab:12:cd:34:ef:56, refs/heads/branch-name
func DeploymentFromArgs(app *Application, args []string, timestamp, repositoryDir string) (*Deployment, error) {
if len(args) < 5 {
return nil, errors.Errorf("5 arguments (repository, revision, username, fingerprint, refname) must be passed. got: %d", len(args))
}
revision := args[1]
branch := refnameRegexp.ReplaceAllString(args[4], "")
return NewDeployment(app, branch, revision, timestamp, repositoryDir), nil
}
func NewDeployment(app *Application, branch, revision, timestamp, repositoryDir string) *Deployment {
projectName := app.Repository + "-" + revision[0:8]
composeFilePath := filepath.Join(repositoryDir, app.Username, projectName, "docker-compose-"+timestamp+".yml")
return &Deployment{
App: app,
Branch: branch,
ComposeFilePath: composeFilePath,
ProjectName: projectName,
Revision: revision,
Timestamp: timestamp,
}
}
func (d *Deployment) Register() error {
return d.App.RegisterMetadata(d.Revision, d.Timestamp)
}