Skip to content

Commit a52fecd

Browse files
authored
Merge pull request #18 from roots/update-config-files
Generate configs during project creation
2 parents 91fe396 + 037b896 commit a52fecd

File tree

14 files changed

+534
-41
lines changed

14 files changed

+534
-41
lines changed

cmd/info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ func (c *InfoCommand) Run(args []string) int {
1919
return 1
2020
}
2121

22-
for name, sites := range c.Trellis.Environments {
22+
for name, config := range c.Trellis.Environments {
2323
var siteNames []string
2424

25-
for _, site := range sites {
26-
siteNames = append(siteNames, site.Name)
25+
for name, _ := range config.WordPressSites {
26+
siteNames = append(siteNames, name)
2727
}
2828

2929
c.UI.Info(fmt.Sprintf("%s => %s", name, strings.Join(siteNames, ", ")))

cmd/new.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ import (
1414

1515
"github.com/mholt/archiver"
1616
"github.com/mitchellh/cli"
17+
"trellis-cli/trellis"
1718
)
1819

1920
type NewCommand struct {
20-
UI cli.Ui
21-
flags *flag.FlagSet
22-
force bool
21+
UI cli.Ui
22+
flags *flag.FlagSet
23+
force bool
24+
trellis *trellis.Trellis
2325
}
26+
2427
type Release struct {
2528
Version string `json:"tag_name"`
2629
ZipUrl string `json:"zipball_url"`
2730
}
2831

29-
func NewNewCommand(ui cli.Ui) *NewCommand {
30-
c := &NewCommand{UI: ui}
32+
func NewNewCommand(ui cli.Ui, trellis *trellis.Trellis) *NewCommand {
33+
c := &NewCommand{UI: ui, trellis: trellis}
3134
c.init()
3235
return c
3336
}
@@ -88,15 +91,42 @@ func (c *NewCommand) Run(args []string) int {
8891
}
8992
}
9093

94+
var host string
95+
host, err = c.UI.Ask(fmt.Sprintf("Enter main site host [default: %s]", name))
96+
97+
if err == nil {
98+
if len(host) == 0 {
99+
host = name
100+
}
101+
}
102+
91103
fmt.Println("Fetching latest versions of Trellis and Bedrock...")
104+
92105
trellisPath := filepath.Join(path, "trellis")
93106
trellisVersion := downloadLatestRelease("roots/trellis", path, trellisPath)
94-
bedrockVersion := downloadLatestRelease("roots/bedrock", path, filepath.Join(path, "bedrock"))
107+
bedrockVersion := downloadLatestRelease("roots/bedrock", path, filepath.Join(path, "site"))
95108

96109
if addTrellisFile(trellisPath) != nil {
97110
log.Fatal("Error writing .trellis.yml file")
98111
}
99112

113+
os.Chdir(path)
114+
115+
if err := c.trellis.LoadProject(); err != nil {
116+
c.UI.Error(err.Error())
117+
return 1
118+
}
119+
120+
// Update default configs
121+
for env, config := range c.trellis.Environments {
122+
c.trellis.UpdateDefaultConfig(config, name, host, env)
123+
c.trellis.WriteConfigYaml(config, filepath.Join("group_vars", env, "wordpress_sites.yml"))
124+
125+
stringGenerator := trellis.RandomStringGenerator{Length: 64}
126+
vault := c.trellis.GenerateVaultConfig(name, env, &stringGenerator)
127+
c.trellis.WriteVaultYaml(vault, filepath.Join("group_vars", env, "vault.yml"))
128+
}
129+
100130
fmt.Printf("\n%s project created with versions:\n", name)
101131
fmt.Printf(" Trellis v%s\n", trellisVersion)
102132
fmt.Printf(" Bedrock v%s\n", bedrockVersion)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ require (
1414
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
1515
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 // indirect
1616
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
17+
golang.org/x/net v0.0.0-20190119204137-ed066c81e75e
1718
gopkg.in/yaml.v2 v2.2.2
1819
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 h1:Pn8fQdvx+z1avAi7fdM2kRYW
3838
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8=
3939
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M=
4040
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
41+
golang.org/x/net v0.0.0-20190119204137-ed066c81e75e h1:MDa3fSUp6MdYHouVmCCNz/zaH2a6CRcxY3VhT/K3C5Q=
42+
golang.org/x/net v0.0.0-20190119204137-ed066c81e75e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
4143
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc h1:MeuS1UDyZyFH++6vVy44PuufTeFF0d0nfI6XB87YGSk=
4244
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4345
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func main() {
3737
return &cmd.InfoCommand{UI: ui, Trellis: trellis}, nil
3838
},
3939
"new": func() (cli.Command, error) {
40-
return cmd.NewNewCommand(ui), nil
40+
return cmd.NewNewCommand(ui, trellis), nil
4141
},
4242
"provision": func() (cli.Command, error) {
4343
return cmd.NewProvisionCommand(ui, trellis), nil
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Documentation: https://roots.io/trellis/docs/vault/
2+
vault_mysql_root_password: devpw
3+
4+
# Variables to accompany `group_vars/development/wordpress_sites.yml`
5+
# Note: the site name (`example.com`) must match up with the site name in the above file.
6+
vault_wordpress_sites:
7+
example.com:
8+
admin_password: admin
9+
env:
10+
db_password: example_dbpassword
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Documentation: https://roots.io/trellis/docs/local-development-setup/
2+
# `wordpress_sites` options: https://roots.io/trellis/docs/wordpress-sites
3+
# Define accompanying passwords/secrets in group_vars/development/vault.yml
4+
5+
wordpress_sites:
6+
example.com:
7+
site_hosts:
8+
- canonical: example.test
9+
redirects:
10+
- www.example.test
11+
local_path: ../site # path targeting local Bedrock site directory (relative to Ansible root)
12+
admin_email: admin@example.test
13+
multisite:
14+
enabled: false
15+
ssl:
16+
enabled: false
17+
provider: self-signed
18+
cache:
19+
enabled: false
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Documentation: https://roots.io/trellis/docs/vault/
2+
vault_mysql_root_password: productionpw
3+
4+
# Documentation: https://roots.io/trellis/docs/security/
5+
vault_users:
6+
- name: "{{ admin_user }}"
7+
password: example_password
8+
salt: "generateme"
9+
10+
# Variables to accompany `group_vars/production/wordpress_sites.yml`
11+
# Note: the site name (`example.com`) must match up with the site name in the above file.
12+
vault_wordpress_sites:
13+
example.com:
14+
env:
15+
db_password: example_dbpassword
16+
# Generate your keys here: https://roots.io/salts.html
17+
auth_key: "generateme"
18+
secure_auth_key: "generateme"
19+
logged_in_key: "generateme"
20+
nonce_key: "generateme"
21+
auth_salt: "generateme"
22+
secure_auth_salt: "generateme"
23+
logged_in_salt: "generateme"
24+
nonce_salt: "generateme"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Documentation: https://roots.io/trellis/docs/remote-server-setup/
2+
# `wordpress_sites` options: https://roots.io/trellis/docs/wordpress-sites
3+
# Define accompanying passwords/secrets in group_vars/production/vault.yml
4+
5+
wordpress_sites:
6+
example.com:
7+
site_hosts:
8+
- canonical: example.com
9+
redirects:
10+
- www.example.com
11+
local_path: ../site # path targeting local Bedrock site directory (relative to Ansible root)
12+
repo: git@github.com:example/example.com.git # replace with your Git repo URL
13+
repo_subtree_path: site # relative path to your Bedrock/WP directory in your repo
14+
branch: master
15+
multisite:
16+
enabled: false
17+
ssl:
18+
enabled: false
19+
provider: letsencrypt
20+
cache:
21+
enabled: false

trellis/config.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package trellis
2+
3+
import (
4+
"fmt"
5+
suffix "golang.org/x/net/publicsuffix"
6+
"gopkg.in/yaml.v2"
7+
"io/ioutil"
8+
"log"
9+
"strings"
10+
)
11+
12+
const DefaultSiteName = "example.com"
13+
14+
type Site struct {
15+
SiteHosts []SiteHost `yaml:"site_hosts"`
16+
LocalPath string `yaml:"local_path"`
17+
AdminEmail string `yaml:"admin_email,omitempty"`
18+
Branch string `yaml:"branch,omitempty"`
19+
Repo string `yaml:"repo,omitempty"`
20+
RepoSubtreePath string `yaml:"repo_subtree_path,omitempty"`
21+
Multisite map[string]interface{} `yaml:"multisite"`
22+
Ssl map[string]interface{} `yaml:"ssl"`
23+
Cache map[string]interface{} `yaml:"cache"`
24+
}
25+
26+
type SiteHost struct {
27+
Canonical string `yaml:"canonical"`
28+
Redirects []string `yaml:"redirects"`
29+
}
30+
31+
type Config struct {
32+
WordPressSites map[string]*Site `yaml:"wordpress_sites"`
33+
}
34+
35+
func (t *Trellis) ParseConfig(path string) *Config {
36+
configYaml, err := ioutil.ReadFile(path)
37+
38+
if err != nil {
39+
log.Fatalln(err)
40+
}
41+
42+
config := &Config{}
43+
44+
if err = yaml.Unmarshal(configYaml, &config); err != nil {
45+
log.Fatalln(err)
46+
}
47+
48+
return config
49+
}
50+
51+
func (t *Trellis) GenerateSite(site *Site, name string, host string, env string) {
52+
var redirect string
53+
54+
if env == "development" {
55+
tld, _ := suffix.PublicSuffix(host)
56+
host = strings.Replace(host, tld, "test", 1)
57+
58+
site.AdminEmail = fmt.Sprintf("admin@%s", host)
59+
site.Branch = ""
60+
site.Repo = ""
61+
site.RepoSubtreePath = ""
62+
} else {
63+
site.AdminEmail = ""
64+
}
65+
66+
if host[:4] == "www." {
67+
redirect = strings.Replace(host, "www.", "", 1)
68+
} else {
69+
redirect = fmt.Sprintf("www.%s", host)
70+
}
71+
72+
siteHost := SiteHost{
73+
Canonical: host,
74+
Redirects: []string{redirect},
75+
}
76+
77+
site.SiteHosts = []SiteHost{siteHost}
78+
}
79+
80+
func (t *Trellis) UpdateDefaultConfig(config *Config, name string, host string, env string) {
81+
config.WordPressSites[name] = config.WordPressSites[DefaultSiteName]
82+
delete(config.WordPressSites, DefaultSiteName)
83+
t.GenerateSite(config.WordPressSites[name], name, host, env)
84+
}
85+
86+
func (t *Trellis) WriteConfigYaml(config *Config, path string) error {
87+
configYaml, err := yaml.Marshal(config)
88+
89+
if err != nil {
90+
log.Fatal(err)
91+
}
92+
93+
return t.WriteYamlFile(path, configYaml)
94+
}

0 commit comments

Comments
 (0)