Skip to content

Commit

Permalink
Adds support for enabling Ubuntu Pro services, FIPS example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjarobot committed Jun 12, 2024
1 parent 0c32d0b commit 7c784de
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 16 deletions.
24 changes: 17 additions & 7 deletions FsCloudInit/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,23 @@ module Builders =

let powerState = PowerStateBuilder()

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

[<CustomOperation "token">]
member _.Token(ubuntuAdvantage, token) = { ubuntuAdvantage with Token = token }

let ubuntuAdvantage = UbuntuAdvantageBuilder()
member _.Token(ubuntuPro, token) = { ubuntuPro with Token = token }
[<CustomOperation "enable">]
member _.Enable(ubuntuPro, service) =
{ ubuntuPro with Enable = Set.add service (Set.ofSeq ubuntuPro.Enable) }
member _.Enable(ubuntuPro, services) =
{ ubuntuPro with Enable = Set.union (Set.ofSeq services) (Set.ofSeq ubuntuPro.Enable) }
[<CustomOperation "enable_beta">]
member _.EnableBeta(ubuntuPro, service) =
{ ubuntuPro with EnableBeta = Set.add service (Set.ofSeq ubuntuPro.EnableBeta) }
member _.EnableBeta(ubuntuPro, services) =
{ ubuntuPro with EnableBeta = Set.union (Set.ofSeq services) (Set.ofSeq ubuntuPro.EnableBeta) }

let ubuntuAdvantage = UbuntuProBuilder()
let ubuntuPro = ubuntuAdvantage

/// Builder for a User.
Expand Down Expand Up @@ -282,10 +292,10 @@ module Builders =
|> Some }

[<CustomOperation "attach_ubuntu_pro">]
member _.AttachUbuntuPro(cloudConfig: CloudConfig, ubuntuAdvantage: UbuntuAdvantage) =
member _.AttachUbuntuPro(cloudConfig: CloudConfig, ubuntuPro: UbuntuPro) =
{
cloudConfig with
UbuntuAdvantage = Some ubuntuAdvantage
UbuntuPro = Some ubuntuPro
}

[<CustomOperation "users">]
Expand Down
40 changes: 33 additions & 7 deletions FsCloudInit/CloudConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Sudo =

module internal Serialization =
let serializableSeq sequence =
if Seq.isEmpty sequence then null else ResizeArray sequence
if isNull sequence || Seq.isEmpty sequence then null else ResizeArray sequence

let defaultIfTrue b = if b then Unchecked.defaultof<_> else b

Expand Down Expand Up @@ -126,11 +126,37 @@ module PowerState =
let Halt = "halt"


type UbuntuAdvantage =
type UbuntuPro =
{ Token: string
Enable: string seq }
Enable: string seq
EnableBeta: string seq }

static member Default = { Token = null; Enable = [] }
static member Default = { Token = null; Enable = []; EnableBeta = [] }

[<YamlIgnore>]
member this.Model =
{ Token = this.Token
Enable = Serialization.serializableSeq this.Enable
EnableBeta = Serialization.serializableSeq this.EnableBeta }

module UbuntuPro =
module Services =
[<Literal>]
let CcEal = "cc-eal"
[<Literal>]
let Cis = "cis"
[<Literal>]
let EsmApps = "esm-apps"
[<Literal>]
let EsmInfra = "esm-infra"
[<Literal>]
let Fips = "fips"
[<Literal>]
let FipsPreview = "fips-preview"
[<Literal>]
let FipsUpdates = "fips-updates"
[<Literal>]
let Livepatch = "livepatch"

type User =
{ Name: string
Expand Down Expand Up @@ -194,7 +220,7 @@ type CloudConfig =
PackageRebootIfRequired: bool option
PowerState: PowerState option
RunCmd: RunCmd option
UbuntuAdvantage: UbuntuAdvantage option
UbuntuPro: UbuntuPro option
Users: User seq
WriteFiles: WriteFile seq }

Expand All @@ -207,7 +233,7 @@ type CloudConfig =
PackageRebootIfRequired = None
PowerState = None
RunCmd = None
UbuntuAdvantage = None
UbuntuPro = None
Users = []
WriteFiles = [] }

Expand All @@ -219,7 +245,7 @@ type CloudConfig =
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>
UbuntuPro = this.UbuntuPro |> Option.map (fun u -> u.Model) |> Option.defaultValue Unchecked.defaultof<_>
Users =
let users =
this.Users |> Seq.map (fun u -> box u.Model) |> Serialization.serializableSeq
Expand Down
27 changes: 27 additions & 0 deletions FsCloudInitTests/BuilderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,36 @@ let tests =
attach_ubuntu_pro (
ubuntuPro {
token "d6cec6a05314b7c63f251e2c0e238830"
enable [
UbuntuPro.Services.FipsPreview
UbuntuPro.Services.EsmApps
UbuntuPro.Services.EsmInfra
]
}
)
}
|> Writer.write
|> matchExpectedAt "ubuntu-pro.yaml"
}
test "Ubuntu Pro - FIPS" {
cloudConfig {
attach_ubuntu_pro (
ubuntuPro {
token "d6cec6a05314b7c63f251e2c0e238830"
enable [
UbuntuPro.Services.FipsPreview
UbuntuPro.Services.EsmApps
UbuntuPro.Services.EsmInfra
]
}
)
power_state (
powerState {
mode PowerState.Mode.Reboot
message "Rebooting to enable FIPS kernel."
}
)
}
|> Writer.write
|> matchExpectedAt "ubuntu-fips.yaml"
} ]
10 changes: 10 additions & 0 deletions FsCloudInitTests/TestContent/ubuntu-fips.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#cloud-config
power_state:
mode: reboot
message: Rebooting to enable FIPS kernel.
ubuntu_pro:
token: d6cec6a05314b7c63f251e2c0e238830
enable:
- esm-apps
- esm-infra
- fips-preview
7 changes: 5 additions & 2 deletions FsCloudInitTests/TestContent/ubuntu-pro.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#cloud-config
ubuntu_advantage:
ubuntu_pro:
token: d6cec6a05314b7c63f251e2c0e238830
enable: []
enable:
- esm-apps
- esm-infra
- fips-preview
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,29 @@ cloudConfig {
}
```

#### Use Ubuntu 22.04 Pro with FIPS and Reboot to Switch Kernel

```f#
cloudConfig {
attach_ubuntu_pro (
ubuntuPro {
token "d6cec6a05314b7c63f251e2c0e238830"
enable [
UbuntuPro.Services.FipsPreview
UbuntuPro.Services.EsmApps
UbuntuPro.Services.EsmInfra
]
}
)
power_state (
powerState {
mode PowerState.Mode.Reboot
message "Rebooting to enable FIPS kernel."
}
)
}
```

#### Create additional users

```f#
Expand Down

0 comments on commit 7c784de

Please sign in to comment.