-
Notifications
You must be signed in to change notification settings - Fork 32
/
daemon.go
181 lines (149 loc) · 4.33 KB
/
daemon.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
package daemon
import (
"context"
"fmt"
"log"
"os"
"github.com/nightlyone/lockfile"
"github.com/manifoldco/go-base64"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/config"
"github.com/manifoldco/torus-cli/identity"
"github.com/manifoldco/torus-cli/registry"
"github.com/manifoldco/torus-cli/daemon/crypto"
"github.com/manifoldco/torus-cli/daemon/db"
"github.com/manifoldco/torus-cli/daemon/logic"
"github.com/manifoldco/torus-cli/daemon/session"
"github.com/manifoldco/torus-cli/daemon/socket"
"github.com/manifoldco/torus-cli/daemon/updates"
)
// Daemon is the torus coprocess that contains session secrets, handles
// cryptographic operations, and communication with the registry.
type Daemon struct {
proxy *socket.AuthProxy
lock lockfile.Lockfile // actually a string
session session.Session
config *config.Config
db *db.DB
logic *logic.Engine
updates *updates.Engine
hasShutdown bool
}
// New creates a new Daemon.
func New(cfg *config.Config, groupShared bool) (*Daemon, error) {
lock, err := lockfile.New(cfg.PidPath)
if err != nil {
return nil, fmt.Errorf("Failed to create lockfile object: %s", err)
}
err = lock.TryLock()
if err != nil {
return nil, fmt.Errorf(
"Failed to create lockfile[%s]: %s", cfg.PidPath, err)
}
// Recover from the panic and return the error; this way we can
// delete the lockfile!
defer func() {
if r := recover(); r != nil {
err, _ = r.(error)
}
}()
if groupShared {
if err = os.Chmod(string(lock), 0640); err != nil {
return nil, err
}
}
db, err := db.NewDB(cfg.DBPath)
if err != nil {
return nil, err
}
session := session.NewSession()
cryptoEngine := crypto.NewEngine(session)
transport := socket.CreateHTTPTransport(cfg)
client := registry.NewClient(cfg.RegistryURI.String(), cfg.APIVersion,
cfg.Version, session, transport)
logic := logic.NewEngine(session, db, cryptoEngine, client)
updates := updates.NewEngine(cfg)
proxy, err := socket.NewAuthProxy(cfg, session, db, transport, client, logic, updates, groupShared)
if err != nil {
return nil, fmt.Errorf("Failed to create auth proxy: %s", err)
}
daemon := &Daemon{
proxy: proxy,
lock: lock,
session: session,
config: cfg,
db: db,
logic: logic,
hasShutdown: false,
updates: updates,
}
return daemon, nil
}
// Addr returns the domain socket the Daemon is listening on.
func (d *Daemon) Addr() string {
return d.proxy.Addr()
}
// Run starts the daemon main loop. It returns on failure, or when the daemon
// has been gracefully shut down.
func (d *Daemon) Run() error {
email, hasEmail := os.LookupEnv("TORUS_EMAIL")
password, hasPassword := os.LookupEnv("TORUS_PASSWORD")
tokenID, hasTokenID := os.LookupEnv("TORUS_TOKEN_ID")
tokenSecret, hasTokenSecret := os.LookupEnv("TORUS_TOKEN_SECRET")
if hasEmail && hasPassword {
log.Printf("Attempting to login as: %s", email)
userLogin := &apitypes.UserLogin{
Email: email,
Password: password,
}
err := d.logic.Session.Login(context.Background(), userLogin)
if err != nil {
return err
}
}
if hasTokenID && hasTokenSecret {
log.Printf("Attempting to login as machine token id: %s", tokenID)
ID, err := identity.DecodeFromString(tokenID)
if err != nil {
log.Printf("Could not parse TORUS_TOKEN_ID")
return err
}
secret, err := base64.NewFromString(tokenSecret)
if err != nil {
log.Printf("Could not parse TORUS_TOKEN_SECRET")
return err
}
machineLogin := &apitypes.MachineLogin{
TokenID: &ID,
Secret: secret,
}
err = d.logic.Session.Login(context.Background(), machineLogin)
if err != nil {
return err
}
}
if err := d.updates.Start(); err != nil {
log.Printf("cannot start updates checker: %s", err)
}
return d.proxy.Listen()
}
// Shutdown gracefully shuts down the daemon.
func (d *Daemon) Shutdown() error {
if d.hasShutdown {
return nil
}
d.hasShutdown = true
if err := d.lock.Unlock(); err != nil {
return fmt.Errorf("Could not unlock: %s", err)
}
if err := d.proxy.Close(); err != nil {
return fmt.Errorf("Could not stop http proxy: %s", err)
}
if err := d.db.Close(); err != nil {
return fmt.Errorf("Could not close db: %s", err)
}
if err := d.updates.Stop(); err != nil {
return fmt.Errorf("Could not stop update checker: %s", err)
}
return nil
}