Skip to content

Commit

Permalink
Adds support for 'defer' when writing files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjarobot committed Jun 17, 2022
1 parent ac1ba33 commit 209987e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions FsCloudInit/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ module Builders =
[<CustomOperation "append">]
member _.Append (writeFile:WriteFile, append:bool) =
{ writeFile with Append = append }
[<CustomOperation "defer">]
member _.Defer (writeFile:WriteFile, defer:bool) =
{ writeFile with Defer = defer }

let writeFile = WriteFileBuilder ()

Expand Down
2 changes: 2 additions & 0 deletions FsCloudInit/CloudConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type WriteFile =
[<YamlDotNet.Serialization.YamlMember(ScalarStyle = YamlDotNet.Core.ScalarStyle.SingleQuoted)>]
Permissions : string
Append : bool
Defer : bool
}
static member Default =
{
Expand All @@ -58,6 +59,7 @@ type WriteFile =
Path = null
Permissions = null
Append = false
Defer = false
}

type AptSource =
Expand Down
14 changes: 14 additions & 0 deletions FsCloudInitTests/BuilderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ let tests =
|> Writer.write
|> matchExpectedAt "file-embedding-readonly.yaml"
}
test "Embed file with defer builder" {
cloudConfig {
write_files [
writeFile {
path "/var/lib/data/hello"
content "hello world"
owner "azureuser:azureuser"
defer true
}
]
}
|> Writer.write
|> matchExpectedAt "file-embedding-defer.yaml"
}
testAsync "Install dotnet with aptSource builders" {
let! aptSourceRes = http.GetAsync "https://packages.microsoft.com/config/ubuntu/20.04/prod.list" |> Async.AwaitTask
let! aptSourceVal = aptSourceRes.Content.ReadAsStringAsync () |> Async.AwaitTask
Expand Down
7 changes: 7 additions & 0 deletions FsCloudInitTests/TestContent/file-embedding-defer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#cloud-config
write_files:
- encoding: b64
content: aGVsbG8gd29ybGQ=
owner: azureuser:azureuser
path: /var/lib/data/hello
defer: true
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ cloudConfig {
|> Writer.write
```

If you are writing files that should be owned by users other than `root`, you often need to use `defer` so they are not written until after that user exists. When not deferred, depending on the version of cloud-init, the file may not be written or it may end up owned by `root`.

```f#
cloudConfig {
write_files [
writeFile {
path "/var/lib/data/hello"
content "hello world"
owner "myuser:myuser"
defer true
}
]
}
|> Writer.write
```

#### Install packages

```f#
Expand Down

0 comments on commit 209987e

Please sign in to comment.