Skip to content

Commit 32376d6

Browse files
authored
Merge pull request #32 from liquidweb/plan-sys-vars
expand plan template variables
2 parents a4abecf + 0a5648f commit 32376d6

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,77 @@ If you end up wanting to modify an auth context later on, you can do so with `au
5858

5959
## LiquidWeb Cloud
6060
The Cloud features you can use in manage.liquidweb.com on your Cloud Servers you can do with this command line tool. See `help cloud` for a full list of features and capabilities.
61+
62+
## Plans
63+
64+
A plan is a pre-defined yaml with optional template variables that can be used to
65+
repeate specific tasks.
66+
67+
Currently only "lw cloud server create" is implemented.
68+
69+
Example:
70+
71+
`lw plan --file plan.yaml`
72+
73+
```
74+
---
75+
cloud:
76+
server:
77+
create:
78+
- type: "SS.VPS"
79+
password: "{{- generatePassword 25 -}}"
80+
template: "UBUNTU_1804_UNMANAGED"
81+
zone: 27
82+
hostname: "web1.somehost.org"
83+
ips: 1
84+
public-ssh-key: "your public ssh key here
85+
config-id: 88
86+
backup-plan: "None"
87+
bandwidth: "SS.5000"
88+
```
89+
90+
### Plan Variables
91+
92+
Plan yaml can make use of golang's template variables. Allows variables to be passed on the
93+
command line and it can access environment variables.
94+
95+
#### Environment Variables
96+
Envonrment variables are defined as `.Env.VARNAME`. On most linux systems and shells you can
97+
get the logged in user with `{{ .Env.USER }}`.
98+
99+
#### User Defined Variables
100+
If you wanted to pass user defined variables on the command line you would use the `--var` flag
101+
(multiple `--var` flags can be passed). For example, if you wanted to generate the hostname of
102+
`web3.somehost.org` you would use the following command and yaml:
103+
104+
`lw plan --file play.yaml --var node=3 --var role=web`
105+
106+
```
107+
hostname: "{{- .Var.role -}}{{- .Var.node -}}.somehost.org"
108+
```
109+
110+
111+
#### Functions
112+
113+
The following functions are defined to be called from a plan template:
114+
115+
- generatePassword <length>
116+
117+
For example, to generate a random 25 character password:
118+
119+
```password: "{{- generatePassword 25 -}}"```
120+
121+
- now
122+
123+
Gives access to a Golang `time` object using your local machine's clock.
124+
125+
Simple example:
126+
127+
```
128+
hostname: "web1.{{- now.Year -}}{{- now.Month -}}{{- now.Day -}}.somehost.org"
129+
```
130+
131+
- hex
132+
133+
Convert a number to hexidecimal.
134+

cmd/plan.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import (
2222
"io/ioutil"
2323
"os"
2424
"strings"
25+
"time"
2526

2627
"github.com/spf13/cobra"
2728
"gopkg.in/yaml.v2"
2829

2930
"github.com/liquidweb/liquidweb-cli/instance"
31+
"github.com/liquidweb/liquidweb-cli/utils"
3032
)
3133

3234
var planCmd = &cobra.Command{
@@ -135,7 +137,16 @@ func processTemplate(varSliceFlag []string, planYaml []byte) ([]byte, error) {
135137
}
136138

137139
var tmplBytes bytes.Buffer
138-
tmpl, err := template.New("plan.yaml").Parse(string(planYaml))
140+
tmpl, err := template.New("plan.yaml").Funcs(template.FuncMap{
141+
"generatePassword": func(length int) string {
142+
return utils.RandomString(length)
143+
},
144+
"now": time.Now,
145+
"hex": func(number int64) string {
146+
return fmt.Sprintf("%X", number)
147+
},
148+
}).
149+
Parse(string(planYaml))
139150
if err != nil {
140151
return nil, err
141152
}

0 commit comments

Comments
 (0)