@@ -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-
4140func 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
0 commit comments