/
config.go
95 lines (80 loc) · 2.48 KB
/
config.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
// Copyright (c) 2017 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package server
import (
"context"
"io"
"gate.computer/gate/image"
"gate.computer/gate/runtime"
"gate.computer/gate/server/api"
"gate.computer/gate/server/event"
)
type InstanceConnector interface {
// Connect allocates a new I/O stream. The returned function is to be used
// to transfer data between a network connection and the instance. If it's
// non-nil, a connection was established.
Connect(context.Context) func(context.Context, io.Reader, io.WriteCloser) error
// Close causes currently blocked and future Connect calls to return nil.
// Established connections will not be closed.
Close() error
}
type InstanceServices interface {
InstanceConnector
runtime.ServiceRegistry
}
func NewInstanceServices(c InstanceConnector, r runtime.ServiceRegistry) InstanceServices {
return &struct {
InstanceConnector
runtime.ServiceRegistry
}{c, r}
}
type Inventory interface {
GetSourceModule(ctx context.Context, source string) (module string, err error)
AddModuleSource(ctx context.Context, module, source string) error
}
type Config struct {
ImageStorage image.Storage
Inventory Inventory
ProcessFactory runtime.ProcessFactory
AccessPolicy Authorizer
ModuleSources map[string]Source
Monitor func(*event.Event, error)
OpenDebugLog func(string) io.WriteCloser
}
func (c *Config) Configured() bool {
return c.ProcessFactory != nil && c.AccessPolicy != nil
}
func (c *Config) monitor(ctx context.Context, t event.Type) {
c.Monitor(&event.Event{
Type: t,
Meta: api.ContextMeta(ctx),
}, nil)
}
func (c *Config) monitorFail(ctx context.Context, t event.Type, info *event.Fail, err error) {
c.Monitor(&event.Event{
Type: t,
Meta: api.ContextMeta(ctx),
Info: &event.EventFail{Fail: info},
}, err)
}
func (c *Config) monitorModule(ctx context.Context, t event.Type, info *event.Module) {
c.Monitor(&event.Event{
Type: t,
Meta: api.ContextMeta(ctx),
Info: &event.EventModule{Module: info},
}, nil)
}
func (c *Config) monitorInstance(ctx context.Context, t event.Type, info *event.Instance) {
c.Monitor(&event.Event{
Type: t,
Meta: api.ContextMeta(ctx),
Info: &event.EventInstance{Instance: info},
}, nil)
}
func (c *Config) openDebugLog(opt *api.InvokeOptions) io.WriteCloser {
if c.OpenDebugLog != nil && opt != nil && opt.DebugLog != "" {
return c.OpenDebugLog(opt.DebugLog)
}
return nil
}