/
start_postgres.go
214 lines (192 loc) · 5.05 KB
/
start_postgres.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
package thirdpartyservices
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/go-connections/nat"
"github.com/jinzhu/gorm"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"github.com/docker/docker/client"
)
const (
PostgresPassword = "awesome-palm"
PostgresDatabaseName = "palm"
PostgresHost = "127.0.0.1"
PostgresPort = 5435
PostgresUser = "palm-user"
PostgresContainerName = "palm-postgres"
PostgresImageName = "postgres:12.4"
)
// PALM_POSTGRES_HOST
// PALM_POSTGRES_PORT
// PALM_POSTGRES_DB
// PALM_POSTGRES_USER
// PALM_POSTGRES_PASSWORD
func EnvOr(f, value string) string {
r := os.Getenv(f)
if r == "" {
return value
} else {
return r
}
}
func GetPostgresParams() string {
dbname := EnvOr("PALM_POSTGRES_DB", PostgresDatabaseName)
pwd := EnvOr("PALM_POSTGRES_PASSWORD", PostgresPassword)
host := EnvOr("PALM_POSTGRES_HOST", PostgresHost)
port := EnvOr("PALM_POSTGRES_PORT", fmt.Sprint(PostgresPort))
user := EnvOr("PALM_POSTGRES_USER", PostgresUser)
return fmt.Sprintf("host=%s port=%v user=%s dbname=%s password=%s sslmode=disable",
host, port, user,
dbname, pwd,
)
}
func init() {
/*
switch runtime.GOOS {
case "linux":
dir := "/usr/share/palm/database"
err := os.MkdirAll(dir, 0666)
if err != nil {
panic(fmt.Sprintf("prepare data failed: %s", err))
}
default:
dir, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("get cwd failed: %s", err))
}
}
*/
}
func PullPostgresImage() error {
log.Infof("[POSTGRES] loading image or pulling image")
cmd := exec.Command("docker", "pull", PostgresImageName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return utils.Errorf("docker pull %s failed: %s", PostgresImageName, err)
}
return nil
}
func StartPostgres(pgdir string) error {
if pgdir != "" {
if !filepath.IsAbs(pgdir) {
var err error
pgdir, err = filepath.Abs(pgdir)
if err != nil {
return utils.Errorf("cannot get abs dir: %v", pgdir)
}
}
}
param := GetPostgresParams()
password := PostgresPassword
dbname := PostgresDatabaseName
log.Info("detecting database connecting... pgdir=%v", pgdir)
d, err := gorm.Open("postgres", param)
if err == nil {
log.Info("detected exsited database.")
_ = d.Close()
return nil
} else {
log.Warnf("open database failed: %v", err)
}
log.Info("try to start a database...")
cli, err := client.NewEnvClient()
if err != nil {
return utils.Errorf("docker env is miss: %v", err)
}
defer cli.Close()
err = cli.ContainerKill(utils.TimeoutContext(10*time.Second), PostgresContainerName, "SIGKILL")
if err != nil {
log.Errorf("kill %v failed: %s", PostgresContainerName, err)
}
var startNewContainer bool
_, err = cli.ContainerInspect(utils.TimeoutContext(10*time.Second), PostgresContainerName)
if err != nil {
startNewContainer = true
}
if !startNewContainer {
err = cli.ContainerStart(
utils.TimeoutContext(10*time.Second),
PostgresContainerName, types.ContainerStartOptions{},
)
if err != nil {
return utils.Errorf("start existed postgres container failed: %s", err)
}
} else {
var mounts []mount.Mount
if pgdir != "" {
log.Infof("create bind from %v ===> /var/lib/postgresql/data", pgdir)
mounts = []mount.Mount{
{
Type: mount.TypeBind,
Source: pgdir,
Target: "/var/lib/postgresql/data",
},
}
_ = os.MkdirAll(pgdir, os.ModePerm)
} else {
log.Warnf("postgres data is buildin docker")
}
resp, err := cli.ContainerCreate(
utils.TimeoutContext(10*time.Second),
&container.Config{
ExposedPorts: map[nat.Port]struct{}{
"5432/tcp": {},
},
Env: []string{
fmt.Sprintf("POSTGRES_PASSWORD=%s", password),
fmt.Sprintf("POSTGRES_USER=%s", PostgresUser),
fmt.Sprintf("POSTGRES_DB=%s", dbname),
},
Image: PostgresImageName,
//Volumes: map[string]struct{}{
// fmt.Sprintf("%v:/var/lib/postgresql/data", pgdir): {},
//},
}, &container.HostConfig{
PortBindings: nat.PortMap{
"5432/tcp": []nat.PortBinding{
{
HostIP: PostgresHost,
HostPort: fmt.Sprint(PostgresPort),
},
},
},
Mounts: mounts,
}, nil, nil, PostgresContainerName,
)
if err != nil {
return utils.Errorf("create postgres container failed: %s", err)
}
if len(resp.Warnings) > 0 {
log.Warnf("%#v", resp.Warnings)
}
log.Infof("start to run %v", PostgresContainerName)
err = cli.ContainerStart(utils.TimeoutContext(30*time.Second), resp.ID, types.ContainerStartOptions{})
if err != nil {
return utils.Errorf("start %v failed: %s", PostgresContainerName, err)
}
}
ticker := time.Tick(1 * time.Second)
count := 0
for {
select {
case <-ticker:
count++
conn, err := gorm.Open("postgres", param)
if err != nil {
log.Warningf("try %v times... waiting for the postgres starting up...", err)
continue
}
_ = conn.Close()
return nil
}
}
}