/
setup.go
94 lines (83 loc) · 1.98 KB
/
setup.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
package cmd
import (
"errors"
"fmt"
"github.com/LastCallMedia/vagabond/config"
"github.com/LastCallMedia/vagabond/step"
"github.com/LastCallMedia/vagabond/util"
"github.com/Songmu/prompter"
"github.com/codegangsta/cli"
"github.com/mitchellh/go-homedir"
"os"
)
// Sets up the vagabond environment
var CmdSetup = cli.Command{
Name: "setup",
Usage: "Prepare the environment for use",
Description: "Prepare the environment for the first use",
Action: runSetup,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force",
Usage: "Force the setup actions",
},
},
}
func runSetup(ctx *cli.Context) {
var err error
env := config.NewEnvironment()
force := ctx.Bool("force")
env.Tz = prompter.Prompt("Timezone", env.Tz)
env.SitesDir, err = promptForDir("Sites directory", env.SitesDir)
if err != nil {
util.Fatal(err)
}
env.DataDir, err = promptForDir("Database storage directory", env.DataDir)
if err != nil {
util.Fatal(err)
}
err = env.Check()
if err != nil {
util.Fatal(err)
}
acts := []step.ConfigStep{
step.VariablesStep,
}
if env.RequiresMachine() {
acts = append(acts, step.MachineStep)
acts = append(acts, step.NfsServerStep)
acts = append(acts, step.NfsClientStep)
}
acts = append(acts, step.ServicesStep)
acts = append(acts, step.NewDnsAction())
for _, act := range acts {
needs := act.NeedsRun(env)
if needs || force {
util.Successf("Running %s", act.GetName())
err := act.Run(env)
if err != nil {
util.Fatal(err)
}
} else {
util.Successf("Skipping %s", act.GetName())
}
}
util.Success("Setup complete")
}
func promptForDir(name string, def string) (dir string, err error) {
dir = prompter.Prompt(name, def)
dir, err = homedir.Expand(dir)
if err != nil {
return
}
exists, err := util.DirExists(dir)
if !exists {
create := prompter.YN(fmt.Sprintf("%s does not exist. Create?", dir), false)
if create {
err = os.MkdirAll(dir, 0755)
return
}
err = errors.New("Did not create directory")
}
return
}