-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
148 lines (127 loc) · 4.17 KB
/
database.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
package main
import (
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
)
type dbContainerConfig struct {
name string
image string
port string
}
type dbPost struct {
DbDescription string `json:"dbDescription,omitemtpy"`
DbName string `json:"databaseName"`
DbType string `json:"databaseType"`
DbVersion string `json:"databaseVersion"`
DbTableCollection string `json:"databaseTable"`
}
// TODO: ADD DB NAME
// create a new database container given the db type, image, port, enviroment variables and volume
// it returns the container id and an error
func (c ContainerController) CreateNewDB(conf dbContainerConfig, env []string) (string, error) {
//container config (image and environment variables)
config := &container.Config{
Image: conf.image,
Env: env,
}
//host config
hostBinding := nat.PortBinding{
HostIP: "0.0.0.0",
//HostPort is the port that the host will listen to, since it's not set
//the docker engine will assign a random open port
// HostPort: "8080",
}
//set the port for the container (internal one)
containerPort, err := nat.NewPort("tcp", conf.port)
fmt.Println("container port" + containerPort)
if err != nil {
return "", err
}
//set a slice of possible port bindings
//since it's a db container we need just one
portBinding := nat.PortMap{containerPort: []nat.PortBinding{hostBinding}}
//set the configuration of the host
//set the port bindings and the restart policy
//!choose a restart policy
hostConfig := &container.HostConfig{
PortBindings: portBinding,
RestartPolicy: container.RestartPolicy{
Name: "on-failure",
MaximumRetryCount: 5,
},
// Mounts: []mount.Mount{
// {
// Type: "volume",
// Source: volumePath,
// Target: "/var/lib/mysql",
// },
// },
}
// networkConf := &network.NetworkingConfig{
// EndpointsConfig: map[string]*network.EndpointSettings{
// "ipaas-network": {
// NetworkID: "cf8bc28f9dcd413ece744e799e24c9be15cecae17e8225dc7e3b25db97644e10",
// },
// },
// }
//create the container
//!set a name to identify the container (<student-name>.<registration_number>-<db-name>)
resp, err := c.cli.ContainerCreate(c.ctx, config, hostConfig, nil, nil, "")
if err != nil {
return "", err
}
//start the container
if err := c.cli.ContainerStart(c.ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return "", err
}
// statusCh, errCh := c.cli.ContainerWait(c.ctx, resp.ID, container.WaitConditionNotRunning)
// select {
// case err := <-errCh:
// if err != nil {
// return "", err
// }
// case <-statusCh:
// }
return resp.ID, nil
}
//!BACKLOG, UNDER DEVELOPMENT
// func (c ContainerController) ExportDBData(containerID string, dbData dbPost) (string, error) {
// //docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > /tmp/DATABASE.sql
// //check if the container id is an exadecimal string
// _, err := strconv.ParseUint(containerID, 16, 64)
// if err != nil {
// return "", errors.New("not a valid container id")
// }
// //get the container
// container, err := c.cli.ContainerInspect(c.ctx, containerID)
// if err != nil {
// return "", err
// }
// //get the database password
// switch dbData.DbType {
// case "mysql", "mariadb":
// //get the database password from enviroment variables
// dbPassword := container.Config.Env[1]
// //create the command
// cmd := fmt.Sprintf("docker exec %s /usr/bin/mysqldump -u root --password=%s %s > /tmp/%s.sql", dbPassword, dbName, containerID)
// //execute the command
// out, err := exec.Command("sh", "-c", cmd).Output()
// if err != nil {
// return "", err
// }
// log.Println("output", out)
// case "mongodb":
// //get the database password from enviroment variables
// dbPassword := container.Config.Env[2]
// //create the command
// cmd := fmt.Sprintf("docker exec %s /usr/bin/mysqldump -u root --password=%s %s > /tmp/%s.sql", dbPassword, dbName, containerID)
// //execute the command
// out, err := exec.Command("sh", "-c", cmd).Output()
// if err != nil {
// return "", err
// }
// log.Println("output", out)
// }
// }