Skip to content

Commit

Permalink
Merge pull request #139 from cybozu-go/remote-files
Browse files Browse the repository at this point in the history
[ignition] add "remote_files" to template.
  • Loading branch information
binoue committed Feb 19, 2019
2 parents 767996e + 9ac1bb8 commit 7f149e2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 20 deletions.
43 changes: 37 additions & 6 deletions client/ignition_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ type SystemdUnit struct {
Mask bool `json:"mask"`
}

// RemoteFile represents a remote file.
type RemoteFile struct {
Name string `json:"name"`
URL string `json:"url"`
}

// TemplateSource represents YAML/JSON source file of Ignition template.
type TemplateSource struct {
Version IgnitionVersion `json:"version"`
Include string `json:"include"`
Passwd string `json:"passwd"`
Files []string `json:"files"`
Systemd []SystemdUnit `json:"systemd"`
Networkd []string `json:"networkd"`
Version IgnitionVersion `json:"version"`
Include string `json:"include"`
Passwd string `json:"passwd"`
Files []string `json:"files"`
RemoteFiles []RemoteFile `json:"remote_files"`
Systemd []SystemdUnit `json:"systemd"`
Networkd []string `json:"networkd"`
}

func loadSource(sourceFile, baseDir string) (*TemplateSource, string, error) {
Expand Down Expand Up @@ -153,6 +160,18 @@ func buildTemplate2_2(src *TemplateSource, baseDir string) (*ign22.Config, error
cfg.Storage.Files = append(cfg.Storage.Files, file)
}

for _, remoteFile := range src.RemoteFiles {
fname := remoteFile.Name
// filepath.IsAbs is intentionally avoided to allow running clients on Windows.
if !strings.HasPrefix(fname, "/") {
return nil, errors.New("non-absolute filename: " + fname)
}
var file ign22.File
file.Path = fname
file.Contents.Source = remoteFile.URL
cfg.Storage.Files = append(cfg.Storage.Files, file)
}

for _, netunit := range src.Networkd {
target := filepath.Join(baseDir, "networkd", netunit)
data, err := ioutil.ReadFile(target)
Expand Down Expand Up @@ -242,6 +261,18 @@ func buildTemplate2_3(src *TemplateSource, baseDir string) (*ign23.Config, error
cfg.Storage.Files = append(cfg.Storage.Files, file)
}

for _, remoteFile := range src.RemoteFiles {
fname := remoteFile.Name
// filepath.IsAbs is intentionally avoided to allow running clients on Windows.
if !strings.HasPrefix(fname, "/") {
return nil, errors.New("non-absolute filename: " + fname)
}
var file ign23.File
file.Path = fname
file.Contents.Source = remoteFile.URL
cfg.Storage.Files = append(cfg.Storage.Files, file)
}

for _, netunit := range src.Networkd {
target := filepath.Join(baseDir, "networkd", netunit)
data, err := ioutil.ReadFile(target)
Expand Down
12 changes: 7 additions & 5 deletions client/ignition_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func testBuildIgnitionTemplate2_3(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if tmpl.Version != Ignition2_2 {
t.Error(`tmpl.Version != Ignition2_2:`, tmpl.Version)
if tmpl.Version != Ignition2_3 {
t.Error(`tmpl.Version != Ignition2_3:`, tmpl.Version)
}
if !cmp.Equal(meta, tmpl.Metadata) {
t.Error("wrong meta data:", cmp.Diff(meta, tmpl.Metadata))
Expand Down Expand Up @@ -54,11 +54,13 @@ func testBuildIgnitionTemplate2_3(t *testing.T) {
SSHAuthorizedKeys: []ign23.SSHAuthorizedKey{"key1"},
},
}
expectedFiles := make([]ign23.File, 2)
expectedFiles := make([]ign23.File, 3)
expectedFiles[0].Path = "/etc/rack"
expectedFiles[0].Contents.Source = "data:," + dataurl.EscapeString("{{ .Spec.Rack }}\n")
expectedFiles[1].Path = "/etc/hostname"
expectedFiles[1].Contents.Source = "data:," + dataurl.EscapeString("{{ .Spec.Serial }}\n")
expectedFiles[1].Path = "/tmp/foo.img"
expectedFiles[1].Contents.Source = "{{ MyURL }}/api/v1/assets/foo.img"
expectedFiles[2].Path = "/etc/hostname"
expectedFiles[2].Contents.Source = "data:," + dataurl.EscapeString("{{ .Spec.Serial }}\n")
expected.Storage.Files = expectedFiles
expected.Networkd.Units = []ign23.Networkdunit{
{
Expand Down
10 changes: 5 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -762,11 +762,11 @@ Upload an Ignition template for `<role>` with `<id>`.

The request body must be a JSON object with these fields:

| Name | Type | Description |
| ---------- | ------ | ----------------------------------------------------------------------------------- |
| `version` | string | Ignition configuration specification as `major.minor`. |
| `template` | object | An ignition configuration to be rendered as desribed in [ignition.md](ignition.md). |
| `meta` | object | Meta data associated with this template. |
| Name | Type | Description |
| ---------- | ------ | ----------------------------------------------------------------------------------------------------- |
| `version` | string | Ignition configuration specification as `major.minor`. |
| `template` | object | An ignition configuration to be rendered as desribed in [ignition_template.md](ignition_template.md). |
| `meta` | object | Meta data associated with this template. |

The currently supported ignition specifications are: `2.3`.

Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ CoreOS Container Linux using [UEFI HTTP Boot][HTTPBoot].

CoreOS can be initialized at first boot by [ignition][].
Sabakan can generate ignition configuration from templates.
Read [ignition.md](ignition.md) for details.
Read [ignition_template.md](ignition_template.md) for details.

### <a name="kernelparams" />Register kernel parameters

Expand Down
8 changes: 6 additions & 2 deletions docs/ignition.md → docs/ignition_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ include: ../base/base.yml
passwd: passwd.yml
files:
- /etc/hostname
remote_files:
- name: /tmp/some.img
url: "{{ MyURL }}/v1/assets/some.img"
systemd:
- name: chronyd.service
enabled: true
Expand All @@ -32,10 +35,10 @@ Field descriptions:
* "2.2" (default)
* "2.3"
* `include`: Another ignition template to be included.
* `passwd`: A filename in the same directory of the template.
The contents should be a YAML encoded ignition's [`passwd` object](https://coreos.com/ignition/docs/latest/configuration-v2_3.html).
* `passwd`: A YAML filename that contains YAML encoded ignition's [`passwd` object](https://coreos.com/ignition/docs/latest/configuration-v2_3.html).
* `files`: List of filenames to be provisioned.
The file contents are read from files under `files/` sub directory.
* `remote_files`: List of remote files to be provisioned.
* `systemd`: List of systemd unit files to be provisioned.
The unit contents are read from files under `systemd/` sub directory.
If `enabled` is true, the unit will be enabled.
Expand All @@ -46,6 +49,7 @@ Field descriptions:
### Rendering specifications

Files pointed by the template YAML are rendered by [text/template][].
Strings in `passwd` YAML and `url` for `remote_files` are also rendered as templates.

`.` in the template is set to the [`Machine`](machine.md#machine-struct) struct of the target machine.
For example, `{{ .Spec.Serial }}` will be replaced with the serial number of the target machine.
Expand Down
2 changes: 1 addition & 1 deletion docs/sabactl.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ If `ID` is specified, the command outputs a JSON object like this:
Register a new ignition template for `ROLE` with `ID`.
For details, see .

`FILE` is either a template YAML described in [Ignition Templates](ignition.md), or
`FILE` is either a template YAML described in [Ignition Templates](ignition_template.md), or
a JSON got by `sabactl ignitions get ROLE ID` if `--json` is given.

A template can be associated with meta data if `--meta FILENAME` is given.
Expand Down
4 changes: 4 additions & 0 deletions testdata/base/base.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
version: "2.3"
passwd: passwd.yml
files:
- /etc/rack
remote_files:
- name: /tmp/foo.img
url: "{{ MyURL }}/api/v1/assets/foo.img"
systemd:
- name: chronyd.service
enabled: true
1 change: 1 addition & 0 deletions testdata/test/test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: "2.3"
include: ../base/base.yml
files:
- /etc/hostname
Expand Down

0 comments on commit 7f149e2

Please sign in to comment.