Skip to content
This repository was archived by the owner on Sep 18, 2021. It is now read-only.

Commit ab5c8b0

Browse files
committed
chore(cli): generate inventory file via YAML module
1 parent c41908e commit ab5c8b0

5 files changed

Lines changed: 61 additions & 80 deletions

File tree

ansible/inventory.go

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,38 @@ import (
55
"os"
66
"path/filepath"
77
"strings"
8-
"text/template"
8+
9+
"github.com/spf13/viper"
10+
"gopkg.in/yaml.v2"
911

1012
"github.com/getstackhead/stackhead/cli/stackhead"
1113
)
1214

13-
// InventoryOptions is a configuration used during creation of the temporary inventory file
14-
type InventoryOptions struct {
15-
IPAddress string
16-
ProjectDefinitionFile string
17-
ProjectDefinitionFileName string
18-
TmpProjectDefinitionFolder string
19-
Webserver string
20-
Container string
21-
Plugins []string
22-
}
23-
24-
// IPAddress sets the corresponding inventory option
25-
func IPAddress(ipAddress string) InventoryOption {
26-
return func(args *InventoryOptions) {
27-
args.IPAddress = ipAddress
28-
}
29-
}
30-
31-
// ProjectDefinitionFile sets the corresponding inventory option
32-
func ProjectDefinitionFile(projectDefinitionFile string) InventoryOption {
33-
return func(args *InventoryOptions) {
34-
args.ProjectDefinitionFile = projectDefinitionFile
15+
type Inventory struct {
16+
All struct {
17+
Vars struct {
18+
AnsibleUser string `yaml:"ansible_user"`
19+
AnsibleConnection string `yaml:"ansible_connection"`
20+
AnsiblePythonInterpreter string `yaml:"ansible_python_interpreter"`
21+
StackHeadConfigFolder string `yaml:"stackhead__config_folder"`
22+
StackHeadWebserver string `yaml:"stackhead__webserver"`
23+
StackHeadContainer string `yaml:"stackhead__container"`
24+
StackHeadPlugins []string `yaml:"stackhead__plugins"`
25+
StackHeadConfigSetup map[string]interface{} `yaml:"stackhead__config_setup"`
26+
StackHeadConfigDeployment map[string]interface{} `yaml:"stackhead__config_deployment"`
27+
StackHeadConfigDestroy map[string]interface{} `yaml:"stackhead__config_destroy"`
28+
}
29+
Hosts struct {
30+
Mackerel struct {
31+
AnsibleHost string `yaml:"ansible_host"`
32+
Stackhead struct {
33+
Applications []string
34+
}
35+
}
36+
}
3537
}
3638
}
3739

38-
// InventoryOption is a single inventory setting
39-
type InventoryOption func(*InventoryOptions)
40-
4140
func copyFile(srcPath string, destPath string) error {
4241
input, err := ioutil.ReadFile(srcPath)
4342
if err != nil {
@@ -52,83 +51,70 @@ func copyFile(srcPath string, destPath string) error {
5251

5352
// CreateInventoryFile creates a temporary inventory file with the given settings and returns an absolute file path.
5453
// Note: make sure to remove the file when you don't need it anymore!
55-
func CreateInventoryFile(options ...InventoryOption) (string, error) {
56-
opts := &InventoryOptions{}
57-
for _, setter := range options {
58-
setter(opts)
59-
}
60-
61-
tmpFile, err := ioutil.TempFile("", "inventory")
54+
func CreateInventoryFile(ipAddress string, projectDefinitionFile string) (string, error) {
55+
conf := Inventory{}
56+
conf.All.Vars.AnsibleUser = "root"
57+
conf.All.Vars.AnsibleConnection = "ssh"
58+
conf.All.Vars.AnsiblePythonInterpreter = "/usr/bin/python3"
59+
conf.All.Hosts.Mackerel.AnsibleHost = ipAddress
60+
61+
webserverModule, err := stackhead.GetWebserverModule()
6262
if err != nil {
6363
return "", err
6464
}
65+
conf.All.Vars.StackHeadWebserver = webserverModule
6566

66-
filePath, err := filepath.Abs(tmpFile.Name())
67+
containerModule, err := stackhead.GetContainerModule()
6768
if err != nil {
68-
os.Remove(tmpFile.Name())
6969
return "", err
7070
}
71+
conf.All.Vars.StackHeadContainer = containerModule
7172

72-
opts.Webserver, err = stackhead.GetWebserverModule()
73+
pluginModules, err := stackhead.GetPluginModules()
7374
if err != nil {
7475
return "", err
7576
}
76-
opts.Container, err = stackhead.GetContainerModule()
77+
conf.All.Vars.StackHeadPlugins = pluginModules
78+
79+
tmpFile, err := ioutil.TempFile("", "inventory")
7780
if err != nil {
7881
return "", err
7982
}
80-
opts.Plugins, err = stackhead.GetPluginModules()
81-
for i := range opts.Plugins {
82-
opts.Plugins[i] = "\"" + opts.Plugins[i] + "\""
83-
}
83+
84+
filePath, err := filepath.Abs(tmpFile.Name())
8485
if err != nil {
86+
os.Remove(tmpFile.Name())
8587
return "", err
8688
}
8789

88-
if len(opts.ProjectDefinitionFile) > 0 {
89-
opts.TmpProjectDefinitionFolder, err = ioutil.TempDir("", "project_definitions")
90+
if len(projectDefinitionFile) > 0 {
91+
conf.All.Vars.StackHeadConfigFolder, err = ioutil.TempDir("", "project_definitions")
9092
if err != nil {
9193
return "", err
9294
}
93-
projectDefinitionFile := filepath.Base(opts.ProjectDefinitionFile)
95+
projectDefinitionFilePath := filepath.Base(projectDefinitionFile)
9496

9597
// Copy project definition file
96-
err = copyFile(opts.ProjectDefinitionFile, filepath.Join(opts.TmpProjectDefinitionFolder, projectDefinitionFile))
98+
err = copyFile(projectDefinitionFile, filepath.Join(conf.All.Vars.StackHeadConfigFolder, projectDefinitionFilePath))
9799
if err != nil {
98100
return "", err
99101
}
100102

101-
opts.ProjectDefinitionFileName = projectDefinitionFile
102-
opts.ProjectDefinitionFileName = strings.TrimSuffix(opts.ProjectDefinitionFileName, ".yml")
103-
opts.ProjectDefinitionFileName = strings.TrimSuffix(opts.ProjectDefinitionFileName, ".yaml")
103+
projectDefinitionFilePath = strings.TrimSuffix(projectDefinitionFilePath, ".yml")
104+
projectDefinitionFilePath = strings.TrimSuffix(projectDefinitionFilePath, ".yaml")
105+
conf.All.Hosts.Mackerel.Stackhead.Applications = append(conf.All.Hosts.Mackerel.Stackhead.Applications, projectDefinitionFilePath)
104106
}
105107

106-
t := template.New("ansible_inventory")
107-
t = t.Funcs(template.FuncMap{"StringsJoin": strings.Join})
108-
109-
t, err = t.Parse(`# This file was generated by StackHead.
110-
---
111-
all:
112-
vars:
113-
ansible_user: root
114-
ansible_connection: ssh
115-
stackhead__config_folder: "{{ .TmpProjectDefinitionFolder }}"
116-
stackhead__webserver: "{{ .Webserver }}"
117-
stackhead__container: "{{ .Container }}"
118-
stackhead__plugins: [{{ StringsJoin .Plugins "," }}]
119-
ansible_python_interpreter: /usr/bin/python3
120-
hosts:
121-
mackerel:
122-
ansible_host: {{ .IPAddress }}
123-
stackhead:
124-
applications:
125-
- {{ .ProjectDefinitionFileName }}
126-
`)
108+
conf.All.Vars.StackHeadConfigSetup = viper.GetStringMap("modules.config.setup")
109+
conf.All.Vars.StackHeadConfigDeployment = viper.GetStringMap("modules.config.deployment")
110+
conf.All.Vars.StackHeadConfigDestroy = viper.GetStringMap("modules.config.destroy")
111+
112+
d, err := yaml.Marshal(&conf)
127113
if err != nil {
128114
return "", err
129115
}
130116

131-
if err = t.Execute(tmpFile, opts); err != nil {
117+
if _, err = tmpFile.Write(d); err != nil {
132118
return "", err
133119
}
134120

commands/project/deploy.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ var DeployApplication = &cobra.Command{
2525
defer wg.Done()
2626

2727
// Generate Inventory file
28-
inventoryFile, err := ansible.CreateInventoryFile(
29-
ansible.IPAddress(args[1]),
30-
ansible.ProjectDefinitionFile(args[0]),
31-
)
28+
inventoryFile, err := ansible.CreateInventoryFile(args[1], args[0])
3229
if err == nil {
3330
defer os.Remove(inventoryFile)
3431
err = routines.ExecAnsiblePlaybook("application-deploy", inventoryFile, nil)

commands/project/destroy.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ var DestroyApplication = &cobra.Command{
2727
defer wg.Done()
2828

2929
// Generate Inventory file
30-
inventoryFile, err := ansible.CreateInventoryFile(
31-
ansible.IPAddress(args[1]),
32-
ansible.ProjectDefinitionFile(args[0]),
33-
)
30+
inventoryFile, err := ansible.CreateInventoryFile(args[1], args[0])
3431

3532
if err == nil {
3633
defer os.Remove(inventoryFile)

commands/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var SetupServer = &cobra.Command{
2525
defer wg.Done()
2626

2727
// Generate Inventory file
28-
inventoryFile, err := ansible.CreateInventoryFile(ansible.IPAddress(args[0]))
28+
inventoryFile, err := ansible.CreateInventoryFile(args[0], "")
2929
if err == nil {
3030
defer os.Remove(inventoryFile)
3131
err = routines.ExecAnsiblePlaybook("server-provision", inventoryFile, nil)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ go 1.13
55
require (
66
github.com/briandowns/spinner v1.11.1
77
github.com/mitchellh/go-homedir v1.1.0
8+
github.com/saitho/jsonschema-validator v1.0.0
89
github.com/smartystreets/goconvey v1.6.4
910
github.com/spf13/cobra v1.0.0
1011
github.com/spf13/viper v1.7.1
11-
github.com/saitho/jsonschema-validator v1.0.0
12+
gopkg.in/yaml.v2 v2.2.8
1213
)

0 commit comments

Comments
 (0)