-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_cluster_create.go
73 lines (59 loc) · 1.58 KB
/
command_cluster_create.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
package main
import (
"fmt"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
// DoClusterCreate is an implementation of cluster create command
func DoClusterCreate(c *cli.Context) {
args := c.Args()
if len(args) == 0 {
showHelpAndExit(c, "<version> must be specified")
}
pg, err := NewPostgres(args[0])
if err != nil {
log.WithField("err", err).Fatal("a specified version is not installed")
}
name := c.String("name")
if name == "" {
name = pg.Version().Name
}
path := filepath.Join(baseDir.clusterDir(), name)
if exists(path) {
log.WithField("name", name).Fatal("cluster already exists")
}
initdbArgs := []string{"-D", path}
if len(args) > 1 {
for _, a := range args[1:] {
initdbArgs = append(initdbArgs, a)
}
}
log.WithField("initdb options", initdbArgs).Debug("initdb")
if err := pg.Initdb(initdbArgs); err != nil {
log.WithField("err", err).Fatal("failed to execute initdb")
}
writeClusterExtraInfoFile(name, pg)
}
// ClusterCreateCompletion provides cli completion of cluster create command
func ClusterCreateCompletion(c *cli.Context) {
args := c.Args()
if len(args) == 0 {
versions := AllVersions()
for _, v := range versions {
fmt.Println(v.Name)
}
}
}
func writeClusterExtraInfoFile(name string, pg *Postgres) {
cluster := &Cluster{
Pg: pg,
Name: name,
}
log.WithFields(log.Fields{
"version name": pg.Version().Name,
}).Debug("write a cluster extra info file")
if err := cluster.WriteExtraInfoFile(); err != nil {
log.WithField("err", err).Fatal("failed to write a cluster extra info file")
}
}