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

Commit 2378d2f

Browse files
committed
feat(cli): add setup command
1 parent e3bc125 commit 2378d2f

File tree

5 files changed

+118
-4
lines changed

5 files changed

+118
-4
lines changed

ansible/inventory.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ansible
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"text/template"
8+
)
9+
10+
type InventoryOptions struct {
11+
IpAddress string
12+
}
13+
14+
func IpAddress(text string) InventoryOption {
15+
return func(args *InventoryOptions) {
16+
args.IpAddress = text
17+
}
18+
}
19+
20+
type InventoryOption func(*InventoryOptions)
21+
22+
func CreateInventoryFile(options ...InventoryOption) (string, error) {
23+
opts := &InventoryOptions{}
24+
for _, setter := range options {
25+
setter(opts)
26+
}
27+
28+
tmpFile, err := ioutil.TempFile("", "inventory")
29+
if err != nil {
30+
return "", err
31+
}
32+
33+
filePath, err := filepath.Abs(tmpFile.Name())
34+
if err != nil {
35+
os.Remove(tmpFile.Name())
36+
return "", err
37+
}
38+
39+
t, err := template.New("ansible_inventory").Parse(`# This file was generated by StackHead.
40+
---
41+
all:
42+
vars:
43+
ansible_user: root
44+
ansible_connection: ssh
45+
hosts:
46+
mackerel:
47+
ansible_host: {{ .IpAddress }}
48+
`)
49+
50+
if err = t.Execute(tmpFile, opts); err != nil {
51+
return "", err
52+
}
53+
54+
// Close the file
55+
if err := tmpFile.Close(); err != nil {
56+
return "", err
57+
}
58+
59+
return filePath, nil
60+
}

commands/setup.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"sync"
7+
8+
"github.com/spf13/cobra"
9+
10+
"github.com/getstackhead/stackhead/cli/ansible"
11+
"github.com/getstackhead/stackhead/cli/routines"
12+
)
13+
14+
var SetupServer = &cobra.Command{
15+
Use: "setup [ipv4 address]",
16+
Example: "setup 192.168.178.14",
17+
Short: "Prepare a server for deployment",
18+
Long: `setup will install all required software on a server. You are then able to deploy projects onto it.`,
19+
Args: cobra.ExactArgs(1),
20+
Run: func(cmd *cobra.Command, args []string) {
21+
routines.RunTask(
22+
routines.Text(fmt.Sprintf("Deploying to server at IP \"%s\"", args[0])),
23+
routines.Execute(func(wg *sync.WaitGroup, result chan routines.TaskResult) {
24+
defer wg.Done()
25+
26+
// Generate Inventory file
27+
inventoryFile, err := ansible.CreateInventoryFile(ansible.IpAddress(args[0]))
28+
if err == nil {
29+
defer os.Remove(inventoryFile)
30+
err = routines.ExecAnsiblePlaybook("server-provision", inventoryFile)
31+
}
32+
33+
taskResult := routines.TaskResult{
34+
Error: err != nil,
35+
}
36+
if err != nil {
37+
taskResult.Message = err.Error()
38+
}
39+
40+
result <- taskResult
41+
}),
42+
)
43+
},
44+
}

commands/validate.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
)
1111

1212
var Validate = &cobra.Command{
13-
Use: "validate [path to project definition file]",
14-
Short: "Validate a project definition file",
15-
Long: `validate is used to make sure your project definition file meets the StackHead project definition syntax.`,
16-
Args: cobra.ExactArgs(1),
13+
Use: "validate [path to project definition file]",
14+
Example: "validate ./my-project-definition.yml",
15+
Short: "Validate a project definition file",
16+
Long: `validate is used to make sure your project definition file meets the StackHead project definition syntax.`,
17+
Args: cobra.ExactArgs(1),
1718
Run: func(cmd *cobra.Command, args []string) {
1819
result, err := jsonschema.ValidateFile(args[0])
1920

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func init() {
4444

4545
rootCmd.AddCommand(commands.Validate)
4646
rootCmd.AddCommand(commands.Init)
47+
rootCmd.AddCommand(commands.SetupServer)
4748
}
4849

4950
// initConfig reads in config file and ENV variables if set.

routines/exec.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ func ExecAnsibleGalaxy(args ...string) error {
3636
args = append(args, "-p "+collectionDir[0])
3737
return Exec("ansible-galaxy", args...)
3838
}
39+
40+
func ExecAnsiblePlaybook(playbookName string, inventoryPath string) error {
41+
stackHeadLocation, err := ansible.GetStackHeadCollectionLocation()
42+
if err != nil {
43+
return err
44+
}
45+
return Exec("ansible-playbook", stackHeadLocation+"/playbooks/"+playbookName+".yml", "-i", inventoryPath)
46+
}

0 commit comments

Comments
 (0)