-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
351 lines (300 loc) · 8.62 KB
/
main.go
File metadata and controls
351 lines (300 loc) · 8.62 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package main
import (
"github.com/moby/moby/client"
"github.com/docker/go-connections/nat"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"context"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/jmoiron/sqlx"
_ "github.com/go-sql-driver/mysql"
"html/template"
"fmt"
//"time"
"io"
"net/http"
//"reflect"
"log"
"database/sql"
"time"
)
/*dokcer関連*/
//func docker() {
// ctx := context.Background()
// cl, err := client.NewClient("http://192.168.111.146:2375", client.DefaultVersion, &http.Client{Transport: http.DefaultTransport}, map[string]string{})
// //cl, err := client.NewEnvClient()
//
// fmt.Println(err)
// fmt.Println(cl.ClientVersion())
// list, err := cl.ImageList(ctx, types.ImageListOptions{All: true})
// if err != nil {
// panic(err)
// }
// for _, image := range list {
// fmt.Println(image.RepoTags)
// }
//
// export := nat.PortSet{"25565/tcp": struct{}{}}
// portbind := nat.PortMap{
// "25565/tcp": []nat.PortBinding{
// {
// HostPort: "25565",
// },
// },
// }
//
// conf := &container.Config{
// Image: "sasenomura/test",
// ExposedPorts: export,
// }
//
// host_conf := &container.HostConfig{
// AutoRemove: true,
// PortBindings: portbind,
// }
//
// net_conf := &network.NetworkingConfig{
// }
//
// resp, err := cl.ContainerCreate(ctx, conf, host_conf, net_conf, "")
// if err := cl.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
// panic(err)
// }
//
// time.Sleep(60 * time.Second)
// cl.ContainerStop(ctx, resp.ID, nil)
// cl.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{
// Force: true,
// })
//
//}
//func docker_list() {
// ctx := context.Background()
//
// //server gave HTTP response to HTTPS client goroutineへの対処
// tr := &http.Transport{}
// cl, err := client.NewClient("http://192.168.111.146:2375", client.DefaultVersion, &http.Client{Transport: tr}, map[string]string{})
//
// //fmt.Println(err)
// //fmt.Println(cl.ClientVersion())
//
// list, err := cl.ImageList(ctx, types.ImageListOptions{All: true})
// if err != nil {
// panic(err)
// }
//
// fmt.Println(list)
// for _, image := range list {
// fmt.Println(image.RepoTags)
// }
//
// cl.Close()
//}
func docker_run(host nat.Port, bind string) (string) {
ctx := context.Background()
//server gave HTTP response to HTTPS client goroutineへの対処
tr := &http.Transport{}
cl, err := client.NewClient("http://192.168.111.146:2375", client.DefaultVersion, &http.Client{Transport: tr}, map[string]string{})
list, err := cl.ImageList(ctx, types.ImageListOptions{All: true})
if err != nil {
panic(err)
}
fmt.Println(list)
for _, image := range list {
fmt.Println(image.RepoTags)
}
export := nat.PortSet{host: struct{}{}}
portbind := nat.PortMap{
host: []nat.PortBinding{
{
HostPort: bind,
},
},
}
conf := &container.Config{
Image: "httpd:latest",
ExposedPorts: export,
}
host_conf := &container.HostConfig{
AutoRemove: true,
PortBindings: portbind,
}
net_conf := &network.NetworkingConfig{
}
resp, err := cl.ContainerCreate(ctx, conf, host_conf, net_conf, "")
if err := cl.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
cl.Close()
return resp.ID
}
func docker_del(docker_id string) {
sttime, _ := time.ParseDuration("60s")
ctx := context.Background()
//server gave HTTP response to HTTPS client goroutineへの対処
tr := &http.Transport{}
cl, _ := client.NewClient("http://192.168.111.146:2375", client.DefaultVersion, &http.Client{Transport: tr}, map[string]string{})
fmt.Println(cl.ContainerStop(ctx, docker_id, &sttime))
remove_conf := types.ContainerRemoveOptions{
Force: true,
}
cl.ContainerRemove(ctx, docker_id, remove_conf)
}
/*テンプレートエンジン関連*/
type TemplateRenderer struct {
templates *template.Template
}
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
// Add global methods if data is a map
if viewContext, isMap := data.(map[string]interface{}); isMap {
viewContext["reverse"] = c.Echo().Reverse
}
return t.templates.ExecuteTemplate(w, name, data)
}
type Item struct {
Id int
Docker_name sql.NullString
Docker_id sql.NullString
Docker_create_user sql.NullString
Docker_password sql.NullString
Host sql.NullString
Bind sql.NullString
Docker_volume sql.NullString
Docker_info sql.NullString
Create_time sql.NullString
Update_time sql.NullString
Delete_flag int
}
type Set struct {
Docler_id string
Docker_name string
Docker_password string
Host string
Bind string
//Docker_volume string
Docker_info string
}
func validation(set Set) bool {
if set.Docker_name == "" || set.Host == "" || set.Bind == "" {
return false
} else {
return true
}
}
//Sql関連
func db() []Item {
db, err := sqlx.Connect("mysql", "root:root@(localhost:3306)/go_docker")
if err != nil {
log.Fatalln(err)
}
//fmt.Println(db)
item := []Item{}
err = db.Select(&item, "SELECT * FROM tbl_docker")
//fmt.Println(reflect.TypeOf(item))
return item
}
func db_insert(docker_name string, docker_id string, docker_create_user string, docker_password string, host string, bind string, docker_volume string, docker_info string, delete_flag int) {
delete_flag = 0;
db, err := sqlx.Connect("mysql", "root:root@(localhost:3306)/go_docker")
if err != nil {
log.Fatalln(err)
}
value := map[string]interface{}{
"docker_name": docker_name,
"docker_id": docker_id,
"docker_create_user": docker_create_user,
"docker_password": docker_password,
"host": host,
"bind": bind,
"docker_volume": docker_volume,
"docker_info": docker_info,
"delete_flag": 0,
}
_, err = db.NamedExec(`INSERT INTO tbl_docker(docker_name,docker_id,docker_create_user,docker_password,host,bind,docker_volume,docker_info,delete_flag)VALUES (:docker_name,:docker_id,:docker_create_user,:docker_password,:host,:bind,:docker_volume,:docker_info,:delete_flag)`, value)
}
func db_del(docker_id string) {
db, err := sqlx.Connect("mysql", "root:root@(localhost:3306)/go_docker")
if err != nil {
log.Fatalln(err)
}
id := map[string]interface{}{"docker_id": docker_id}
_, err = db.NamedExec("DELETE FROM tbl_docker WHERE docker_id = :docker_id", id)
}
func main() {
//docker_stop("94fceeaaaa3d")
renderer := &TemplateRenderer{
templates: template.Must(template.ParseGlob("template/*.html")),
}
//fmt.Println(docker_run(nat.Port("80/tcp"),"8080"))
e := echo.New()
e.Static("/static", "assets")
//e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Renderer = renderer
e.GET("/index", func(i echo.Context) error {
//var item []Item = db()
//docker_list()
return i.Render(http.StatusOK, "index.html", map[string]interface{}{
"name": "Dolly!",
//"docker": item,
})
}).Name = "foobar"
e.GET("/list", func(i echo.Context) error {
var item []Item = db()
return i.JSON(http.StatusOK, item)
}).Name = "list"
//テンプレートを返すだけ
e.GET("/get_new", func(i echo.Context) error {
return i.Render(http.StatusOK, "new.html", map[string]interface{}{
"name": "Dolly!",
//"docker": item,
})
}).Name = "get_new"
//新しく作るとき
e.POST("/post_new", func(i echo.Context) error {
//docker_run("80/tcp", "")
var docker_name string = i.Request().FormValue("name")
var docker_password string = i.Request().FormValue("password")
//var ip = i.Request().FormValue("ip")
var host string = i.Request().FormValue("host")
var bind string = i.Request().FormValue("bind")
var info string = i.Request().FormValue("info")
//構造体のセット
set := Set{}
set.Docker_name = docker_name
set.Docker_password = docker_password
set.Host = host
set.Bind = bind
set.Docker_info = info
//確認用の関数
if validation(set) {
set.Docler_id = docker_run(nat.Port(set.Host), set.Bind)
db_insert(
set.Docker_name,
set.Docler_id,
"",
set.Docker_password,
set.Host,
set.Bind,
"",
set.Docker_info,
0,
)
}
return i.Redirect(http.StatusMovedPermanently, "/get_new")
}).Name = "post_new"
e.GET("/delete/:docker_id", func(i echo.Context) error {
var id string = i.Param("docker_id")
docker_del(id)
db_del(id)
fmt.Println(id)
return i.Redirect(http.StatusMovedPermanently, "/index")
}).Name = "delete"
e.GET("/teapot", func(i echo.Context) error {
return i.String(http.StatusTeapot, "418. I’m a teapot.")
}).Name = "teapot"
// サーバー起動
e.Start(":80")
}