-
Notifications
You must be signed in to change notification settings - Fork 106
/
commands.go
168 lines (132 loc) · 3.06 KB
/
commands.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
package codetainer
import (
"fmt"
"io/ioutil"
"log"
)
func CodetainerRemove(id string) {
db, err := GlobalConfig.GetDatabase()
if err != nil {
Log.Fatal(err)
}
codetainer := Codetainer{}
if err = codetainer.LookupByNameOrId(id, db); err != nil {
Log.Fatal(err)
}
err = codetainer.Remove(db)
if err != nil {
Log.Fatal(err)
}
fmt.Println("[-] Codetainer removed.")
}
func RegisterCodetainerProfile(pathToProfile string, name string) {
db, err := GlobalConfig.GetDatabase()
if err != nil {
Log.Fatal(err)
}
var c CodetainerConfig
prof, err := ioutil.ReadFile(pathToProfile)
if err != nil {
Log.Error("Unable to read file " + pathToProfile)
Log.Fatal(err)
}
c.Profile = string(prof)
c.Name = name
err = c.Validate()
if err != nil {
Log.Error("Unable to parse " + pathToProfile)
Log.Fatal(err)
}
err = c.Save(db)
if err != nil {
Log.Fatal(err)
}
log.Printf("Created profile with id=%s: \n", c.Id)
log.Println("--")
log.Printf(c.Profile)
}
func ListCodetainerImages() {
db, err := GlobalConfig.GetDatabase()
if err != nil {
Log.Fatal(err)
}
var cl []CodetainerImage = make([]CodetainerImage, 0)
err = db.engine.Find(&cl, &CodetainerImage{})
if err != nil {
Log.Fatal("Unable to list images: ", err)
}
if len(cl) > 0 {
fmt.Printf("Found %d images:\n", len(cl))
} else {
fmt.Println("No images found.")
}
for _, c := range cl {
fmt.Printf("-- [%s] %+v\n", c.Id, c.Tags)
}
}
func ListCodetainerProfiles() {
db, err := GlobalConfig.GetDatabase()
if err != nil {
Log.Fatal(err)
}
var cl []CodetainerConfig = make([]CodetainerConfig, 0)
err = db.engine.Find(&cl, &CodetainerConfig{})
if err != nil {
Log.Fatal("Unable to list profiles: ", err)
}
if len(cl) > 0 {
fmt.Printf("Found %d profiles:\n", len(cl))
} else {
fmt.Println("No profiles found.")
}
for _, c := range cl {
fmt.Printf("-- [%s] %s\n", c.Id, c.Name)
}
}
func RegisterCodetainerImage(id string, command string) {
db, err := GlobalConfig.GetDatabase()
if err != nil {
Log.Fatal(err)
}
image := CodetainerImage{Id: id, DefaultStartCommand: command}
err = image.Register(db)
if err != nil {
Log.Fatal("Unable to register container image: ", err)
}
Log.Info("Registration succeeded.")
}
func CodetainerList() {
db, err := GlobalConfig.GetDatabase()
if err != nil {
Log.Fatal(err)
}
cl, err := db.ListCodetainers()
if err != nil {
Log.Fatal(err)
}
if len(*cl) > 0 {
fmt.Printf("Found %d codetainers.\n", len(*cl))
} else {
fmt.Println("No codetainers found.")
}
for _, c := range *cl {
fmt.Printf("-- [%s] %s", c.Id, c.Name)
if c.Running {
fmt.Printf(" (Running)")
}
fmt.Println()
}
}
func CreateCodetainer(imageId string, name string) {
db, err := GlobalConfig.GetDatabase()
if err != nil {
Log.Fatal(err)
}
c := Codetainer{ImageId: imageId, Name: name}
err = c.Create(db)
if err != nil {
Log.Fatal("Unable to create codetainer: ", err)
}
fmt.Printf("Codetainer %s creation succeeded!\n", c.Name)
fmt.Printf("You can interact with it here: "+GlobalConfig.Url()+"/api/v1/codetainer/%s/view\n", c.Id)
}