forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slugs.go
74 lines (59 loc) · 1.98 KB
/
slugs.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
package empire
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/remind101/empire/pkg/image"
"github.com/remind101/empire/procfile"
"golang.org/x/net/context"
)
// Slug represents a container image with the extracted ProcessType.
type Slug struct {
// A unique uuid that identifies this slug.
ID string
// The Docker image that this slug is for.
Image image.Image
// The raw Procfile that was extracted from the Docker image.
Procfile []byte
}
// ParsedProcfile returns the parsed Procfile.
func (s *Slug) ParsedProcfile() (procfile.Procfile, error) {
return procfile.ParseProcfile(s.Procfile)
}
// Formation returns a new Formation built from the extracted Procfile.
func (s *Slug) Formation() (Formation, error) {
p, err := s.ParsedProcfile()
if err != nil {
return nil, err
}
return formationFromProcfile(p)
}
// slugsService provides convenience methods for creating slugs.
type slugsService struct {
*Empire
}
// SlugsCreateByImage creates a Slug for the given image.
func (s *slugsService) Create(ctx context.Context, db *gorm.DB, img image.Image, w *DeploymentStream) (*Slug, error) {
return slugsCreateByImage(ctx, db, s.ImageRegistry, img, w)
}
// slugsCreate inserts a Slug into the database.
func slugsCreate(db *gorm.DB, slug *Slug) (*Slug, error) {
return slug, db.Create(slug).Error
}
// SlugsCreateByImage first attempts to find a matching slug for the image. If
// it's not found, it will fallback to extracting the process types using the
// provided extractor, then create a slug.
func slugsCreateByImage(ctx context.Context, db *gorm.DB, r ImageRegistry, img image.Image, w *DeploymentStream) (*Slug, error) {
var (
slug Slug
err error
)
slug.Image, err = r.Resolve(ctx, img, w.Stream)
if err != nil {
return nil, fmt.Errorf("resolving %s: %v", img, err)
}
slug.Procfile, err = r.ExtractProcfile(ctx, slug.Image, w.Stream)
if err != nil {
return nil, fmt.Errorf("extracting Procfile from %s: %v", slug.Image, err)
}
return slugsCreate(db, &slug)
}