Skip to content

Commit

Permalink
Adds support for 'power_state'
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjarobot committed Jun 7, 2024
1 parent 37f20af commit 0c32d0b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
24 changes: 24 additions & 0 deletions FsCloudInit/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ module Builders =

let aptSource = AptSourceBuilder()

type PowerStateBuilder() =
member _.Yield _ = PowerState.Default

[<CustomOperation "delay">]
member _.DelayStateChange(powerState, delay) = { powerState with Delay = delay }

[<CustomOperation "mode">]
member _.Mode(powerState, mode) = { powerState with Mode = mode }

[<CustomOperation "message">]
member _.Message(powerState, message) = { powerState with Message = message }

[<CustomOperation "timeout">]
member _.Timeout(powerState, timeout) = { powerState with Timeout = timeout }

[<CustomOperation "condition">]
member _.Condition(powerState, condition) = { powerState with Condition = condition }

let powerState = PowerStateBuilder()

type UbuntuAdvantageBuilder() =
member _.Yield _ = UbuntuAdvantage.Default

Expand Down Expand Up @@ -245,6 +265,10 @@ module Builders =
{ cloudConfig with
FinalMessage = Some message }

[<CustomOperation "power_state">]
member _.PowerState(cloudConfig: CloudConfig, powerState: PowerState) =
{ cloudConfig with PowerState = Some powerState }

[<CustomOperation "run_commands">]
member _.RunCommands(cloudConfig: CloudConfig, commands: string seq seq) =
let cmdList = commands |> Seq.map List.ofSeq |> List.ofSeq
Expand Down
28 changes: 28 additions & 0 deletions FsCloudInit/CloudConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ type RunCmd =
match this with
| RunCmd commands -> commands |> Seq.map Seq.ofList

type PowerState =
{ Delay: int
Mode: string
Message: string
Timeout: Nullable<int>
Condition: string }

static member Default =
{ Delay = 0
Mode = null
Message = null
Timeout = Nullable()
Condition = null }

module PowerState =

module Mode =
[<Literal>]
let PowerOff = "powerooff"
[<Literal>]
let Reboot = "reboot"
[<Literal>]
let Halt = "halt"


type UbuntuAdvantage =
{ Token: string
Enable: string seq }
Expand Down Expand Up @@ -167,6 +192,7 @@ type CloudConfig =
PackageUpdate: bool option
PackageUpgrade: bool option
PackageRebootIfRequired: bool option
PowerState: PowerState option
RunCmd: RunCmd option
UbuntuAdvantage: UbuntuAdvantage option
Users: User seq
Expand All @@ -179,6 +205,7 @@ type CloudConfig =
PackageUpdate = None
PackageUpgrade = None
PackageRebootIfRequired = None
PowerState = None
RunCmd = None
UbuntuAdvantage = None
Users = []
Expand All @@ -190,6 +217,7 @@ type CloudConfig =
Packages = this.Packages |> Seq.map (fun p -> p.Model) |> Serialization.serializableSeq
PackageUpdate = this.PackageUpdate |> Option.toNullable
PackageUpgrade = this.PackageUpgrade |> Option.toNullable
PowerState = this.PowerState |> Option.defaultValue Unchecked.defaultof<PowerState>
Runcmd = this.RunCmd |> Option.map (fun runCmd -> runCmd.Model) |> Option.toObj
UbuntuAdvantage = this.UbuntuAdvantage |> Option.defaultValue Unchecked.defaultof<UbuntuAdvantage>
Users =
Expand Down
12 changes: 12 additions & 0 deletions FsCloudInitTests/BuilderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ let tests =
|> Writer.write
|> matchExpectedAt "final-message.yaml"
}
test "Set power state to reboot" {
cloudConfig {
power_state (
powerState {
mode PowerState.Mode.Reboot
message "Done with installation. Rebooting now."
}
)
}
|> Writer.write
|> matchExpectedAt "power-state.yaml"
}
test "Run commands with cloudConfig builder" {
cloudConfig {
run_commands
Expand Down
4 changes: 4 additions & 0 deletions FsCloudInitTests/TestContent/power-state.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#cloud-config
power_state:
mode: reboot
message: Done with installation. Rebooting now.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ cloudConfig {
|> Writer.write
```

#### Reboot after initialized

```f#
cloudConfig {
power_state (
powerState {
mode PowerState.Mode.Reboot
message "Done with installation. Rebooting now."
}
)
}
```

#### Attach to Ubuntu Pro Subscription

```f#
Expand Down

0 comments on commit 0c32d0b

Please sign in to comment.