diff --git a/FsCloudInit/Builders.fs b/FsCloudInit/Builders.fs index 4965c04..3d45c8c 100644 --- a/FsCloudInit/Builders.fs +++ b/FsCloudInit/Builders.fs @@ -46,6 +46,9 @@ module Builders = [] member _.Append (writeFile:WriteFile, append:bool) = { writeFile with Append = append } + [] + member _.Defer (writeFile:WriteFile, defer:bool) = + { writeFile with Defer = defer } let writeFile = WriteFileBuilder () diff --git a/FsCloudInit/CloudConfig.fs b/FsCloudInit/CloudConfig.fs index c5dbb96..5edfaab 100644 --- a/FsCloudInit/CloudConfig.fs +++ b/FsCloudInit/CloudConfig.fs @@ -49,6 +49,7 @@ type WriteFile = [] Permissions : string Append : bool + Defer : bool } static member Default = { @@ -58,6 +59,7 @@ type WriteFile = Path = null Permissions = null Append = false + Defer = false } type AptSource = diff --git a/FsCloudInitTests/BuilderTests.fs b/FsCloudInitTests/BuilderTests.fs index 825ed78..5731d98 100644 --- a/FsCloudInitTests/BuilderTests.fs +++ b/FsCloudInitTests/BuilderTests.fs @@ -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 diff --git a/FsCloudInitTests/TestContent/file-embedding-defer.yaml b/FsCloudInitTests/TestContent/file-embedding-defer.yaml new file mode 100644 index 0000000..a90005d --- /dev/null +++ b/FsCloudInitTests/TestContent/file-embedding-defer.yaml @@ -0,0 +1,7 @@ +#cloud-config +write_files: +- encoding: b64 + content: aGVsbG8gd29ybGQ= + owner: azureuser:azureuser + path: /var/lib/data/hello + defer: true diff --git a/README.md b/README.md index deb9ccd..d46931b 100644 --- a/README.md +++ b/README.md @@ -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#