Skip to content

Commit ce7939b

Browse files
Code Generation, Pt. 2: Module code generation (#258)
* add module templates * add kraken module generate functionality * add kraken module update functionality * update README with info on module generation * update app/module generation help
1 parent 593c821 commit ce7939b

File tree

10 files changed

+1099
-26
lines changed

10 files changed

+1099
-26
lines changed

README.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ $ mkdir tester
8383
$ cd tester
8484
<create kraken.yaml>
8585
$ kraken app generate
86-
2021/04/15 10:39:02 INFO: app "tester" generated at "."
86+
INFO[0000] app "tester" generated at "."
8787
```
8888

8989
Now you can build your application:
@@ -99,7 +99,88 @@ this kraken is built with modules:
9999
github.com/kraken-hpc/kraken/modules/websocket
100100
```
101101

102-
Generating modules and extensions is not yet supported, but will be very soon!
102+
### Generating a module
103+
104+
You also need a definition to create a module using the `kraken` command. The default name for this definition is `module.yaml` in the module directory. Canonically, we use `<project>/modules/<module>` for module directories.
105+
106+
Module defintions are a more complicated than app definitions. They need to define all of the mutations and discoveries that a module can do. Here's an annotated example:
107+
108+
```yaml
109+
---
110+
# The package_url is the Go-style path to the module. This must be a full url.
111+
package_url: "github.com/kraken-hpc/kraken/modules/test"
112+
# If with_polling is true, the module will be generated with a polling loop.
113+
# The timer for the polling loop uses a config, so with_config is implied.
114+
with_polling: true
115+
# If with_config is specified, a stub for a protobuf config will be generated.
116+
with_config: true
117+
# This list declares any URLs we descover.
118+
# The state of our own service and any URLs used in `mutates` sections of mutations below are automatically added.
119+
# In this case, we're letting Kraken know that we discover things about `/RunState`, even though it's not part
120+
# of our mutations.
121+
discoveries:
122+
- "/RunState"
123+
# This section declares mutations. The key can be anything, but it must be unique. Ideally, it should be something descriptive.
124+
# In this example we declare two mutations, one discovers /PhysState when it's unknown, the other mutates power from OFF to ON.
125+
mutations:
126+
"Discover":
127+
mutates:
128+
"/PhysState":
129+
from: "PHYS_UNKNOWN"
130+
to: "POWER_OFF"
131+
requires:
132+
"/Platoform": "test"
133+
timeout: "10s"
134+
fail_to:
135+
url: "/PhysState"
136+
value: "PHYS_ERROR"
137+
"PowerON":
138+
mutates:
139+
"/PhysState":
140+
from: "POWER_OFF"
141+
to: "POWER_ON"
142+
requires:
143+
"/Platform": "test"
144+
timeout: "10s"
145+
fail_to:
146+
url: "/PhysState"
147+
value: "PHYS_ERROR"
148+
```
149+
150+
Once we have the definition in place, we can generate the module with:
151+
152+
```bash
153+
$ mkdir -p modules/test
154+
$ cd modules/test
155+
<create module.yaml>
156+
$ kraken module generate
157+
INFO[0000] module "test" generated at modules/test
158+
```
159+
160+
If we selected `with_config: true`, we will need to generate the protobuf code from the provided `proto` file. You can add some variables to `test.config.proto` first, then:
161+
162+
```bash
163+
$ cd modules/test
164+
$ go generate
165+
```
166+
167+
This will create `test.config.pb.go` (note: you need `protoc` and `gogo-proto` installed).
168+
169+
At this point, the module can be built and run, though it won't really do anything. It will add to the generated graph, so this is a good point to make sure the graph output of an app that links this module is sane.
170+
171+
Finally, you'll want to edit `test.go`. Unlike `test.mod.go`, which shouldn't be edited by hand, `test.go` is a stub to get you started and you'll need to edit it. You'll need to add real function hanlders for your mutations in `func Init()`. If you made a polling loop you probably want to put some stuff in `func Poll()`. In general, look for comments starting with `// TODO:` for areas where you might want to alter things.
172+
173+
If you change your module definition, you can update your module without overwriting `test.go`.
174+
175+
```bash
176+
$ kraken module update
177+
```
178+
179+
This will *only* update `test.mod.go`. Note: you may need to make manual changes to make `test.go` match, e.g. if you changed your list of mutations.
180+
181+
### Generating an extension
182+
183+
Extension generation is not yet supported.
103184

104185
## I want to get involved...
105186

generators/app.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ package generators
1212
import (
1313
"flag"
1414
"fmt"
15-
"html/template"
1615
"io/ioutil"
1716
"os"
1817
"path/filepath"
1918
"strings"
19+
"text/template"
2020

2121
"github.com/kraken-hpc/kraken/generators/templates"
2222
"gopkg.in/yaml.v2"
@@ -25,11 +25,11 @@ import (
2525
// App generation
2626
type AppConfig struct {
2727
Global *GlobalConfigType
28-
Name string
29-
Version string
30-
Pprof bool
31-
Extensions []string
32-
Modules []string
28+
Name string // Application name
29+
Version string // Application version (not Kraken version)
30+
Pprof bool // Build with pprof (default: false)?
31+
Extensions []string // List of extensions to include (url paths)
32+
Modules []string // List of modules to include (url paths)
3333
}
3434

3535
func appCompileTemplate(tplFile, tmpDir string, cfg *AppConfig) (target string, e error) {
@@ -66,9 +66,8 @@ func AppGenerate(global *GlobalConfigType, args []string) {
6666
fs.StringVar(&outDir, "o", ".", "output directory for app")
6767
fs.BoolVar(&help, "h", false, "print this usage")
6868
fs.Usage = func() {
69-
fmt.Println("Usage: kraken <opts> app [-h] [command] [opts]")
70-
fmt.Println("Commands:")
71-
fmt.Println("\tgenerate")
69+
fmt.Println("[app]lication generate will generate a kraken entry point based on an app config.")
70+
fmt.Println("Usage: kraken <opts> app generate [-h] [-c <config_file>] [-o <out_dir>]")
7271
fs.PrintDefaults()
7372
}
7473
fs.Parse(args)

generators/global.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
//go:generate go-bindata -o templates/templates.go -fs -pkg templates -prefix templates templates/app
1+
/* global.go: global config for generators
2+
*
3+
* Author: J. Lowell Wofford <lowell@lanl.gov>
4+
*
5+
* This software is open source software available under the BSD-3 license.
6+
* Copyright (c) 2021, Triad National Security, LLC
7+
* See LICENSE file for details.
8+
*/
9+
10+
//go:generate go-bindata -o templates/templates.go -fs -pkg templates -prefix templates templates/app templates/module
211

312
package generators
413

0 commit comments

Comments
 (0)