# Load example system
load-example --nsleds 3 --ndisks-per-sled 3

# Print the initial configuration
show

# Create TUF repos
generate-fake-repo 1.0.0

set target-release repo-1.0.0.zip

# Update the install dataset on all sleds to the target release.
# This will cause zones to be noop converted over to Artifact,
# unblocking upgrades.
sled-update-install-dataset serial0 --to-target-release
sled-update-install-dataset serial1 --to-target-release
sled-update-install-dataset serial2 --to-target-release

# I wish there was a less painful way of doing this!
# Manually update the:
# - RT Bootloader
# - ROT
# - SP
# - Host Phase 1
# - Host Phase 2
#
# To match what is requested by the blueprint.

sled-update-rot-bootloader serial0 --stage0 1.0.0
sled-update-rot-bootloader serial1 --stage0 1.0.0
sled-update-rot-bootloader serial2 --stage0 1.0.0

sled-update-rot serial0 --slot-a 1.0.0
sled-update-rot serial1 --slot-a 1.0.0
sled-update-rot serial2 --slot-a 1.0.0

sled-update-rot serial0 --slot-b 1.0.0
sled-update-rot serial1 --slot-b 1.0.0
sled-update-rot serial2 --slot-b 1.0.0

sled-update-sp serial0 --active 1.0.0
sled-update-sp serial1 --active 1.0.0
sled-update-sp serial2 --active 1.0.0

sled-update-host-phase1 serial0 --slot-a 2902cb5932fa703ed62fc6a0a330898fe6fd4bde68c74f6398a6aa916cb7e79c
sled-update-host-phase1 serial1 --slot-a 2902cb5932fa703ed62fc6a0a330898fe6fd4bde68c74f6398a6aa916cb7e79c
sled-update-host-phase1 serial2 --slot-a 2902cb5932fa703ed62fc6a0a330898fe6fd4bde68c74f6398a6aa916cb7e79c

sled-update-host-phase2 serial0 --boot-disk A --slot-a c2a8af75d6907ef138d033dc3be1fcbf7d7a78a87aee04796caf283a6bb0952d
sled-update-host-phase2 serial1 --boot-disk A --slot-a c2a8af75d6907ef138d033dc3be1fcbf7d7a78a87aee04796caf283a6bb0952d
sled-update-host-phase2 serial2 --boot-disk A --slot-a c2a8af75d6907ef138d033dc3be1fcbf7d7a78a87aee04796caf283a6bb0952d

# Migrate from Install -> Artifact
inventory-generate
blueprint-plan latest latest
blueprint-diff latest

# Also set the Omicron config for all sleds to reflect the
# corresponding image sources.
sled-set serial0 omicron-config latest
sled-set serial1 omicron-config latest
sled-set serial2 omicron-config latest

# Generate inventory once more to reflect the omicron config changes.
inventory-generate
inventory-show latest

blueprint-plan latest latest
blueprint-show latest

# Intentionally downgrade Nexus
#
# Note that the artifact hash here is totally synthetic -- we just want to push
# the system into a state where it wants to upgrade ONLY Nexus.

blueprint-edit latest set-zone-image 466a9f29-62bf-4e63-924a-b9efdb86afec artifact 0.0.1 5d835e8666bd5cf65be66efbca6847b24e41fd85993448a211ad7be5fea559ab
blueprint-edit latest set-zone-image 0c71b3b2-6ceb-4e8f-b020-b08675e83038 artifact 0.0.1 5d835e8666bd5cf65be66efbca6847b24e41fd85993448a211ad7be5fea559ab
blueprint-edit latest set-zone-image 3eeb8d49-eb1a-43f8-bb64-c2338421c2c6 artifact 0.0.1 5d835e8666bd5cf65be66efbca6847b24e41fd85993448a211ad7be5fea559ab

# Make sure the "not-upgraded-yet" Nexuses are appearing in inventory
sled-set serial0 omicron-config latest
sled-set serial1 omicron-config latest
sled-set serial2 omicron-config latest
inventory-generate

# The test effectively starts here:
#
# - Nexus is running at version 0.0.1
# - The rest of the system is running at 1.0.0
# - Blueprint planning should try to move from 0.0.1 -> 1.0.0 for Nexus

# Should add three Nexuses at the new version
blueprint-plan latest latest
blueprint-diff latest

# If we try to plan again, nothing happens.
# The report should say why: These zones haven't propagated to inventory yet.
blueprint-plan latest latest

# Show the list of inventories - the one at the bottom is the "most recent"
# one, and we're going to go back-in-time to it later.
inventory-list

sled-set serial0 omicron-config latest
sled-set serial1 omicron-config latest
sled-set serial2 omicron-config latest

inventory-generate

# If we have the Database records, but we use an old inventory,
# we won't see the new Nexus zones yet.
#
# This will refuse to bump the Nexus generation ("new Nexus zones
# are not in inventory yet")
blueprint-plan latest b1bda47d-2c19-4fba-96e3-d9df28db7436

# If we're missing the "Active" records, we won't be able to lookup the
# current Nexus image, which will cause planning to fail
set nexus-zones --active --not-yet-inferred
blueprint-plan latest latest
# Reset the set of active Nexuses.
set nexus-zones --active-inferred --not-yet-inferred

# If we're missing the "NotYet" records, we'll refuse to bump the Nexus
# generation ("new Nexus zones do not have database records yet")
set nexus-zones --active-inferred --not-yet
blueprint-plan latest latest

# Even if we have records for one of the zones, we need all of them.
set nexus-zones --active-inferred --not-yet a40e7821-3ec7-4aa9-995d-7d26ecec7d0f
blueprint-plan latest latest

# This is equivalent to explicitly setting the "not-yet-nexus-zones" to
# all the newly created Nexuses.
set nexus-zones --active-inferred --not-yet-inferred

# Bump the nexus generation to "2".
# This will cause the "0.0.1" zones to be considered out-of-date
blueprint-plan latest latest
blueprint-diff latest

# If we try to plan again, we still keep these old Nexus zones around.
# This is expected! The old Nexuses are still part of the "active" set.
blueprint-plan latest latest
blueprint-diff latest

# Now: Pretend we're one of the new Nexuses, planning a blueprint.
set active-nexus-gen 2

# Now expungement can complete
# (Three Nexuses, expunged one at a time)
blueprint-plan latest latest
blueprint-diff latest
blueprint-plan latest latest
blueprint-diff latest
blueprint-plan latest latest
blueprint-diff latest

# Should be a no-op
blueprint-plan latest latest
blueprint-diff latest
