Skip to content

Commit 098985e

Browse files
committed
Update writing packs doc
1 parent 6b504bd commit 098985e

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

docs/writing-packs.md

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ the [detailed usage documentation](./detailed-usage.md).
99

1010
First, make a pack registry. This will be a repository that provides the structure, templates, and metadata that define your custom packs.
1111

12-
<!-- TODO: MAKE THE EXAMPLE REPO -->
13-
1412
Cloning [hashicorp/example-nomad-pack-registry](https://github.com/hashicorp/example-nomad-pack-registry) can give you a head start.
1513

1614
Each registry should have a README.md file that describes the packs in it, and
@@ -19,7 +17,7 @@ pack. Conventionally, the pack subdirectory name matches the pack name.
1917

2018
The top level of a pack registry looks like the following:
2119

22-
```
20+
```plaintext
2321
.
2422
└── README.md
2523
└── packs
@@ -41,22 +39,25 @@ The directory should have the following contents:
4139
- A `variables.hcl` file that defines the variables in a pack.
4240
- An optional, but _highly encouraged_ `CHANGELOG.md` file that lists changes for each version of the pack.
4341
- An optional `outputs.tpl` file that defines an output to be printed when a pack is deployed.
44-
- A `templates` subdirectory containing the HCL templates used to render the jobspec.
42+
- A `templates` subdirectory containing the HCL templates used to render the output files.
4543

4644
#### metadata.hcl
4745

48-
The `metadata.hcl` file contains important key value information regarding the pack. It contains the following blocks and their associated fields:
46+
The `metadata.hcl` file contains important key value information about the pack. It contains the following blocks and their associated fields:
4947

50-
- "app {url}" - The HTTP(S) url to the homepage of the application to provide a quick reference to the documentation and help pages.
51-
- "pack {name}" - The name of the pack.
52-
- "pack {description}" - A small overview of the application that is deployed by the pack.
53-
- "pack {version}" - The version of the pack.
54-
- "dependency {name}" - The dependencies that the pack has on other packs. Multiple dependencies can be supplied.
55-
- "dependency {source}" - The source URL for this dependency.
48+
- `app`
49+
- `url` - The HTTP(S) URL to the homepage of the application to provide a quick reference to the documentation and help pages.
50+
- `pack`
51+
- `name` - The name of the pack.
52+
- `description` - A small overview of the application that is deployed by the pack.
53+
- `version` - The version of the pack.
54+
- `dependency` - The dependencies that the pack has on other packs. Multiple dependencies can be supplied.
55+
- `name` - The dependencies that the pack has on other packs. Multiple dependencies can be supplied.
56+
- `source` - The source URL for this dependency.
5657

5758
An example `metadata.hcl` file:
5859

59-
```
60+
```hcl
6061
app {
6162
url = "https://github.com/mikenomitch/hello_world_server"
6263
}
@@ -72,11 +73,11 @@ pack {
7273

7374
The `variables.hcl` file defines the variables required to fully render and deploy all the templates found within the "templates" directory.
7475

75-
These variables are defined using [HCL](https://github.com/hashicorp/hcl).
76+
These variables are defined using [HCL][].
7677

7778
An example `variables.hcl` file:
7879

79-
```
80+
```hcl
8081
variable "datacenters" {
8182
description = "A list of datacenters in the region which are eligible for task placement."
8283
type = list(string)
@@ -114,7 +115,7 @@ The `outputs.tpl` is an optional file that defines an output to be printed when
114115

115116
Output files have access to pack variables and template helper functions. A simple example:
116117

117-
```
118+
```go
118119
Congrats on deploying [[ .nomad_pack.pack.name ]].
119120

120121
There are [[ .hello_world.app_count ]] instances of your job now running on Nomad.
@@ -138,9 +139,9 @@ Templates are written using [Go Template Syntax](https://learn.hashicorp.com/tut
138139

139140
Unlike default Go Template syntax, Nomad Pack uses "[[" and "]]" as delimiters.
140141

141-
An example template using variables values from above:
142+
The following is an example template using variables values from earlier.
142143

143-
```
144+
```hcl
144145
job "hello_world" {
145146
region = "[[ .hello_world.region ]]"
146147
datacenters = [ [[ range $idx, $dc := .hello_world.datacenters ]][[if $idx]],[[end]][[ $dc | quote ]][[ end ]] ]
@@ -176,11 +177,12 @@ job "hello_world" {
176177

177178
This is a relatively simple template that mostly sets variables.
178179

179-
The `datacenters` value shows slightly more complex Go Template, which allows for [control structures](https://learn.hashicorp.com/tutorials/nomad/go-template-syntax#control-structure-list) like `range` and [pipelines](https://learn.hashicorp.com/tutorials/nomad/go-template-syntax#pipelines).
180+
The `datacenters` value demonstrates slightly more complex Go Templating, which
181+
allows for [control structures](https://learn.hashicorp.com/tutorials/nomad/go-template-syntax#control-structure-list) (like `range`) and [pipelines](https://learn.hashicorp.com/tutorials/nomad/go-template-syntax#pipelines).
180182

181183
#### Template Functions
182184

183-
To supplement the standard Go Template set of template functions, the (masterminds/sprig)[https://github.com/Masterminds/sprig] library is used. This adds helpers for various use cases such as string manipulation, cryptographics, and data conversion (for instance to and from JSON).
185+
To supplement the standard Go Template set of template functions, the [Masterminds/sprig](https://github.com/Masterminds/sprig) library is used. This adds helpers for various use cases such as string manipulation, cryptography, and data conversion (for instance to and from JSON).
184186

185187
Custom Nomad-specific and debugging functions are also provided:
186188

@@ -193,7 +195,7 @@ Custom Nomad-specific and debugging functions are also provided:
193195

194196
A custom function within a template is called like any other:
195197

196-
```
198+
```go
197199
[[ nomadRegions ]]
198200
[[ nomadRegions | spewDump ]]
199201
```
@@ -202,22 +204,25 @@ A custom function within a template is called like any other:
202204

203205
For complex packs, authors may want to reuse template snippets across multiple resources.
204206

205-
For instance, if we had two jobs defined in a pack, and we knew both would re-use the same `region`
206-
logic for both, we could use a helper template to consolidate logic.
207+
For instance, suppose you have two jobs defined in your pack and both jobs reuse
208+
the same `region` logic. You can create a helper template to centralize that
209+
logic.
207210

208-
Helper template names are prepended with an underscore "\_" and end in ".tpl". So we could define a helper called "\_region.tpl":
211+
Helper template names are prepended with an underscore `_` and end in `.tpl`.
212+
You could define a helper called "\_region.tpl" with the following code.
209213

210-
```
214+
```hcl
211215
[[- define "region" -]]
212216
[[- if not (eq .hello_world.region "") -]]
213217
region = [[ .hello_world.region | quote]]
214218
[[- end -]]
215219
[[- end -]]
216220
```
217221

218-
Then in the parent templates, "job_a.nomad.tpl" and "job_b.nomad.tpl", we would render to the helper template using its name:
222+
In the parent templates, "job_a.nomad.tpl" and "job_b.nomad.tpl", you use
223+
the template's name to render it in place.
219224

220-
```
225+
```hcl
221226
job "job_a" {
222227
type = "service"
223228
@@ -231,9 +236,9 @@ job "job_a" {
231236

232237
Packs can depend on content from other packs.
233238

234-
First, packs must define their dependencies in `metadata.hcl`. A pack block with a dependency would look like the following:
239+
First, packs must define their dependencies in `metadata.hcl`. A pack block with a dependency would look like the following.
235240

236-
```
241+
```hcl
237242
app {
238243
url = "https://some-url-for-the-application.dev"
239244
}
@@ -249,11 +254,17 @@ dependency "demo_dep" {
249254
}
250255
```
251256

252-
The dependency name label *must* match the `name` property of the dependant pack, as specified in its `metadata.hcl`.
257+
**Note**: The dependency block's label, "demo_dep" in the previous example,
258+
*must* match the `name` property of the dependency pack, as specified in its
259+
`metadata.hcl`.
253260

254-
This would allow templates of "simple_service" to use "demo_dep"'s helper templates in the following way:
261+
For packs that reuse a dependency pack, add a unique `alias` attribute inside
262+
each dependency block to disambiguate them.
255263

256-
```
264+
This allows templates of "simple_service" to use "demo_dep"'s helper templates
265+
in the following way.
266+
267+
```hcl
257268
[[ template "demo_dep.data" . ]]
258269
```
259270

@@ -266,7 +277,7 @@ directory path as the name of the pack to the `run`, `plan`, `render`, `info`,
266277
For instance, if your current working directory is the directory where you are
267278
developing your pack, and `nomad-pack` is on your path, you can run this command.
268279

269-
```
280+
```shell
270281
nomad-pack run .
271282
```
272283

@@ -278,23 +289,28 @@ To use your new pack, you will likely want to publish it to the internet. Push t
278289
accessible by your command line tool.
279290

280291
If you wish to share your packs, please consider adding them to the
281-
[Nomad Pack Community Registry](https://github.com/hashicorp/nomad-pack-community-registry)
292+
[Nomad Pack Community Registry][].
282293

283294
If you don't want to host a pack registry in version control, you can work locally
284-
using the filesystem method.
295+
using the file system method.
285296

286-
## Step Six: Deploy your Custom Pack!
297+
## Step Six: Deploy your Custom Pack
287298

288299
Add your custom repository using the `nomad-pack registry add` command.
289300

290-
```
301+
```shell
291302
nomad-pack registry add my_packs github.com/<YOUR_ORG>/<YOUR_REPO>
292303
```
293304

294305
Deploy your custom packs.
295306

296-
```
307+
```shell
297308
nomad-pack run hello_world --var app_count=1 --registry=my_packs
298309
```
299310

300-
Congrats! You can now write custom packs for Nomad!
311+
Your custom pack should now be running in your Nomad cluster.
312+
313+
## Learn More
314+
315+
[Nomad Pack community registry]: https://github.com/hashicorp/nomad-pack-community-registry
316+
[HCL]: https://github.com/hashicorp/hcl

0 commit comments

Comments
 (0)