forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployments.go
More file actions
81 lines (67 loc) · 1.98 KB
/
deployments.go
File metadata and controls
81 lines (67 loc) · 1.98 KB
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
package empire
import (
"encoding/json"
"fmt"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/jinzhu/gorm"
"golang.org/x/net/context"
)
// deployerService is an implementation of the deployer interface that performs
// the core business logic to deploy.
type deployerService struct {
*Empire
}
// deploy does the actual deployment
func (s *deployerService) deploy(ctx context.Context, db *gorm.DB, opts DeploymentsCreateOpts) (*Release, error) {
app, img := opts.App, opts.Image
// If no app is specified, attempt to find the app that relates to this
// images repository, or create it if not found.
if app == nil {
var err error
app, err = appsFindOrCreateByRepo(db, img.Repository)
if err != nil {
return nil, err
}
} else {
// If the app doesn't already have a repo attached to it, we'll attach
// this image's repo.
if err := appsEnsureRepo(db, app, img.Repository); err != nil {
return nil, err
}
}
// Grab the latest config.
config, err := s.configs.Config(db, app)
if err != nil {
return nil, err
}
// Create a new slug for the docker image.
slug, err := s.slugs.Create(ctx, db, img, opts.Output)
if err != nil {
return nil, err
}
// Create a new release for the Config
// and Slug.
desc := fmt.Sprintf("Deploy %s", img.String())
r, err := s.releases.Create(ctx, db, &Release{
App: app,
Config: config,
Slug: slug,
Description: desc,
})
return r, err
}
// Deploy is a thin wrapper around deploy to that adds the error to the
// jsonmessage stream.
func (s *deployerService) Deploy(ctx context.Context, db *gorm.DB, opts DeploymentsCreateOpts) (*Release, error) {
var msg jsonmessage.JSONMessage
r, err := s.deploy(ctx, db, opts)
if err != nil {
msg = newJSONMessageError(err)
} else {
msg = jsonmessage.JSONMessage{Status: fmt.Sprintf("Status: Created new release v%d for %s", r.Version, r.App.Name)}
}
if err := json.NewEncoder(opts.Output).Encode(&msg); err != nil {
return r, err
}
return r, err
}