-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
219 lines (182 loc) · 5.1 KB
/
main.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/lxc/lxd/client"
"gopkg.in/fsnotify.v0"
"gopkg.in/yaml.v2"
)
// Global variables
var lxdDaemon lxd.ContainerServer
var config serverConfig
type serverConfig struct {
Container string `yaml:"container"`
Image string `yaml:"image"`
Profiles []string `yaml:"profiles"`
Command []string `yaml:"command"`
Feedback bool `yaml:"feedback"`
FeedbackTimeout int `yaml:"feedback_timeout"`
QuotaCPU int `yaml:"quota_cpu"`
QuotaDisk int `yaml:"quota_disk"`
QuotaProcesses int `yaml:"quota_processes"`
QuotaRAM int `yaml:"quota_ram"`
QuotaSessions int `yaml:"quota_sessions"`
QuotaTime int `yaml:"quota_time"`
ServerAddr string `yaml:"server_addr"`
ServerBannedIPs []string `yaml:"server_banned_ips"`
ServerConsoleOnly bool `yaml:"server_console_only"`
ServerContainersMax int `yaml:"server_containers_max"`
ServerIPv6Only bool `yaml:"server_ipv6_only"`
ServerMaintenance bool `yaml:"server_maintenance"`
ServerStatisticsKeys []string `yaml:"server_statistics_keys"`
ServerTerms string `yaml:"server_terms"`
serverTermsHash string
}
type statusCode int
const (
serverOperational statusCode = 0
serverMaintenance statusCode = 1
containerStarted statusCode = 0
containerInvalidTerms statusCode = 1
containerServerFull statusCode = 2
containerQuotaReached statusCode = 3
containerUserBanned statusCode = 4
containerUnknownError statusCode = 5
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
err := run()
if err != nil {
fmt.Printf("error: %s\n", err)
os.Exit(1)
}
}
func parseConfig() error {
data, err := ioutil.ReadFile("lxd-demo.yaml")
if os.IsNotExist(err) {
return fmt.Errorf("The configuration file (lxd-demo.yaml) doesn't exist.")
} else if err != nil {
return fmt.Errorf("Unable to read the configuration: %s", err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return fmt.Errorf("Unable to parse the configuration: %s", err)
}
if config.ServerAddr == "" {
config.ServerAddr = ":8080"
}
if config.Command == nil {
config.Command = []string{"bash"}
}
config.ServerTerms = strings.TrimRight(config.ServerTerms, "\n")
hash := sha256.New()
io.WriteString(hash, config.ServerTerms)
config.serverTermsHash = fmt.Sprintf("%x", hash.Sum(nil))
if config.Container == "" && config.Image == "" {
return fmt.Errorf("No container or image specified in configuration")
}
return nil
}
func run() error {
var err error
// Setup configuration
err = parseConfig()
if err != nil {
return err
}
// Watch for configuration changes
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("Unable to setup fsnotify: %s", err)
}
err = watcher.Watch(".")
if err != nil {
return fmt.Errorf("Unable to setup fsnotify watch: %s", err)
}
go func() {
for {
select {
case ev := <-watcher.Event:
if ev.Name != "./lxd-demo.yaml" {
continue
}
if !ev.IsModify() {
continue
}
fmt.Printf("Reloading configuration\n")
err := parseConfig()
if err != nil {
fmt.Printf("Failed to parse configuration: %s\n", err)
}
case err := <-watcher.Error:
fmt.Printf("Inotify error: %s\n", err)
}
}
}()
// Connect to the LXD daemon
warning := false
for {
lxdDaemon, err = lxd.ConnectLXDUnix("", nil)
if err == nil {
break
}
if !warning {
fmt.Printf("Waiting for the LXD server to come online.\n")
warning = true
}
time.Sleep(time.Second)
}
if warning {
fmt.Printf("LXD is now available. Daemon starting.\n")
}
// Setup the database
err = dbSetup()
if err != nil {
return fmt.Errorf("Failed to setup the database: %s", err)
}
// Restore cleanup handler for existing containers
containers, err := dbActive()
if err != nil {
return fmt.Errorf("Unable to read current containers: %s", err)
}
for _, entry := range containers {
containerID := int64(entry[0].(int))
containerName := entry[1].(string)
containerExpiry := int64(entry[2].(int))
duration := containerExpiry - time.Now().Unix()
timeDuration, err := time.ParseDuration(fmt.Sprintf("%ds", duration))
if err != nil || duration <= 0 {
lxdForceDelete(lxdDaemon, containerName)
dbExpire(containerID)
continue
}
time.AfterFunc(timeDuration, func() {
lxdForceDelete(lxdDaemon, containerName)
dbExpire(containerID)
})
}
// Setup the HTTP server
r := mux.NewRouter()
r.Handle("/", http.RedirectHandler("/static", http.StatusMovedPermanently))
r.PathPrefix("/static").Handler(http.StripPrefix("/static", http.FileServer(http.Dir("static/"))))
r.HandleFunc("/1.0", restStatusHandler)
r.HandleFunc("/1.0/console", restConsoleHandler)
r.HandleFunc("/1.0/feedback", restFeedbackHandler)
r.HandleFunc("/1.0/info", restInfoHandler)
r.HandleFunc("/1.0/start", restStartHandler)
r.HandleFunc("/1.0/statistics", restStatisticsHandler)
r.HandleFunc("/1.0/terms", restTermsHandler)
err = http.ListenAndServe(config.ServerAddr, r)
if err != nil {
return err
}
return nil
}