This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
builder.go
205 lines (159 loc) · 5.48 KB
/
builder.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2019] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package distribution
import (
"context"
"github.com/lastbackend/lastbackend/pkg/distribution/errors"
"github.com/lastbackend/lastbackend/pkg/log"
"github.com/lastbackend/registry/pkg/distribution/types"
"github.com/lastbackend/registry/pkg/storage"
)
const (
logBuilderPrefix = "distribution:builder"
)
type IBuilder interface {
Get(hostname string) (*types.Builder, error)
List() ([]*types.Builder, error)
Create(opts *types.BuilderCreateOptions) (*types.Builder, error)
Update(builder *types.Builder, opts *types.BuilderUpdateOptions) error
FindBuild(builder *types.Builder) (*types.Build, error)
MarkOffline() error
}
type Builder struct {
context context.Context
storage storage.IStorage
}
func (b *Builder) Get(hostname string) (*types.Builder, error) {
log.V(logLevel).Debugf("%s:get:> get by hostname %s", logBuilderPrefix, hostname)
builder, err := b.storage.Builder().Get(b.context, hostname)
if err != nil {
log.V(logLevel).Errorf("%s:get:> get builder %s err: %v", logBuilderPrefix, hostname, err)
return nil, err
}
return builder, nil
}
func (b *Builder) List() ([]*types.Builder, error) {
log.V(logLevel).Debugf("%s:list:> get builders list", logBuilderPrefix)
builder, err := b.storage.Builder().List(b.context, nil)
if err != nil {
log.V(logLevel).Errorf("%s:list:> get builders list err: %v", logBuilderPrefix, err)
return nil, err
}
return builder, nil
}
func (b *Builder) Create(opts *types.BuilderCreateOptions) (*types.Builder, error) {
log.V(logLevel).Debugf("%s:create:> create new builder %#v", logBuilderPrefix, opts)
if opts == nil {
opts = new(types.BuilderCreateOptions)
}
builder := new(types.Builder)
builder.Meta.Hostname = opts.Hostname
builder.Status.Online = true
builder.Spec.Network.IP = opts.IP
builder.Spec.Network.Port = opts.Port
builder.Spec.Network.TLS = opts.TLS
if opts.SSL != nil {
builder.Spec.Network.SSL = new(types.SSL)
builder.Spec.Network.SSL = opts.SSL
}
if err := b.storage.Builder().Insert(b.context, builder); err != nil {
log.V(logLevel).Errorf("%s:create:> insert builder %s err: %v", logBuilderPrefix, builder.Meta.Hostname, err)
return nil, err
}
return builder, nil
}
func (b *Builder) Update(builder *types.Builder, opts *types.BuilderUpdateOptions) error {
if builder == nil {
return errors.New("invalid argument")
}
if opts == nil {
opts = new(types.BuilderUpdateOptions)
}
log.V(logLevel).Debugf("%s:update:> update builder %s -> %#v", logBuilderPrefix, builder.Meta.Hostname, opts)
if opts.Hostname != nil {
builder.Meta.Hostname = *opts.Hostname
}
if opts.IP != nil {
builder.Spec.Network.IP = *opts.IP
}
if opts.Port != nil {
builder.Spec.Network.Port = *opts.Port
}
if opts.Online != nil {
builder.Status.Online = *opts.Online
}
if opts.TLS != nil {
builder.Spec.Network.TLS = *opts.TLS
}
if opts.SSL != nil {
builder.Spec.Network.SSL = new(types.SSL)
builder.Spec.Network.SSL = opts.SSL
}
if opts.Allocated != nil {
builder.Status.Allocated.Workers = opts.Allocated.Workers
builder.Status.Allocated.RAM = opts.Allocated.RAM
builder.Status.Allocated.CPU = opts.Allocated.CPU
builder.Status.Allocated.Storage = opts.Allocated.Storage
}
if opts.Capacity != nil {
builder.Status.Capacity.Workers = opts.Capacity.Workers
builder.Status.Capacity.RAM = opts.Capacity.RAM
builder.Status.Capacity.CPU = opts.Capacity.CPU
builder.Status.Capacity.Storage = opts.Capacity.Storage
}
if opts.Usage != nil {
builder.Status.Usage.Workers = opts.Usage.Workers
builder.Status.Usage.RAM = opts.Usage.RAM
builder.Status.Usage.CPU = opts.Usage.CPU
builder.Status.Usage.Storage = opts.Usage.Storage
}
if err := b.storage.Builder().Update(b.context, builder); err != nil {
log.V(logLevel).Errorf("%s:update:> update builder %s err: %v", logBuilderPrefix, builder.Meta.Hostname, err)
return err
}
return nil
}
func (b *Builder) FindBuild(builder *types.Builder) (*types.Build, error) {
if builder == nil {
return nil, errors.New("invalid argument")
}
log.V(logLevel).Debugf("%s:find_build:> get build for builder %s", logBuilderPrefix, builder.Meta.Hostname)
build, err := b.storage.Build().Attach(b.context, builder)
if err != nil {
log.V(logLevel).Errorf("%s:find_build:> get build for builder %s err: %v", logBuilderPrefix, builder.Meta.Hostname, err)
return nil, err
}
if build == nil {
return nil, nil
}
return build, nil
}
func (b *Builder) MarkOffline() error {
log.V(logLevel).Debugf("%s:mark_offline:> find and mark offline builders", logBuilderPrefix)
if err := b.storage.Builder().MarkOffline(b.context); err != nil {
log.V(logLevel).Errorf("%s:mark_offline:> find and mark offline builders err: %v", logBuilderPrefix, err)
return err
}
return nil
}
func NewBuilderModel(ctx context.Context, stg storage.IStorage) IBuilder {
return &Builder{
context: ctx,
storage: stg,
}
}