-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstances.go
124 lines (113 loc) · 3.47 KB
/
instances.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
package getaredis
import (
"errors"
"strconv"
"github.com/fsouza/go-dockerclient"
)
import "time"
type Instance struct {
ID int `sql:"AUTO_INCREMENT"`
Name string
CreatorIP string
CreatedAt time.Time
HostedAtIP string
HostedAtPort string
Password string `sql:"-"` // Don't Store passwords in the database
Running bool
ContainerID string
}
func generateRedisConfig(ctx *context, name, password string) docker.CreateContainerOptions {
return docker.CreateContainerOptions{
Name: name,
Config: &docker.Config{
Image: "redis",
Memory: int64(ctx.config.MaxInstanceSize) * 1024 * 1024,
MemorySwap: -1,
Cmd: []string{"redis-server", "--requirepass", password, "--maxclients", strconv.Itoa(ctx.config.MaxRedisConnections)},
},
}
}
func startRedisInstance(ctx *context, dockerAdderss, name, password string) (*docker.Container, error) {
dockerClient, err := docker.NewClient(dockerAdderss)
if err != nil {
return nil, err
}
container, err := dockerClient.CreateContainer(generateRedisConfig(ctx, name, password))
if err != nil {
return nil, err
}
err = dockerClient.StartContainer(container.ID, &docker.HostConfig{PublishAllPorts: true})
if err != nil {
return nil, err
}
time.Sleep(time.Second)
container, err = dockerClient.InspectContainer(container.ID)
if err != nil || !container.State.Running {
if err != nil {
return nil, err
}
return nil, errors.New("Container Failed to start")
}
return container, nil
}
func CheckInstanceLimit(ctx *context, ip string) error {
var count int
ctx.db.Model(&Instance{}).Where("running = 1 AND creator_ip = ?", ip).Count(&count)
if count >= ctx.config.MaxInstancesPerIP {
return errors.New("Instances limit per IP reached")
}
return nil
}
// Creates a new docker instance with a random name, and returns the instance details back
func (ctx *context) NewInstance(creatorIP string) (*Instance, error) {
dockerHostPublicIP, dockerHostPrivateIP, err := ctx.scheduleNewContainer()
if err != nil {
return nil, err
}
dockerAddress := generateDockerAddress(dockerHostPrivateIP, ctx.config.Docker["user"], ctx.config.Docker["password"])
name := generateRandomString(20)
password := generateRandomString(20)
var count int
for ctx.db.Model(&Instance{}).Where(&Instance{Name: name}).Count(&count); count != 0; name = generateRandomString(20) {
// Keep Trying!
}
container, err := startRedisInstance(ctx, dockerAddress, name, password)
if err != nil {
return nil, err
}
instance := &Instance{
Name: name,
CreatorIP: creatorIP,
CreatedAt: time.Now(),
HostedAtIP: dockerHostPublicIP,
HostedAtPort: container.NetworkSettings.Ports["6379/tcp"][0].HostPort,
Password: password,
Running: true,
ContainerID: container.ID,
}
ctx.db.Create(instance)
if ctx.db.NewRecord(instance) {
return nil, errors.New("Failed to write to the database")
}
return instance, nil
}
func (ctx *context) RemoveContainer(hostIP, id string) error {
dockerClient, err := docker.NewClient(generateDockerAddress(hostIP, ctx.config.Docker["user"], ctx.config.Docker["password"]))
if err != nil {
return err
}
err = dockerClient.RemoveContainer(docker.RemoveContainerOptions{
ID: id,
Force: true,
})
return err
}
func (ctx *context) CountContainers(includeNotRunning bool) (int, error) {
var count = -1
var where string
if !includeNotRunning {
where = "running = 1"
}
err := ctx.db.Model(&Instance{}).Where(where).Count(&count)
return count, err.Error
}