You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
398 lines
15 KiB
Go
398 lines
15 KiB
Go
package hypervisor
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/netip"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"git.luoli.live/root/virt/agent/publicnetwork"
|
|
)
|
|
|
|
const (
|
|
managedStoragePoolName = "virt-cloud-managed"
|
|
templateStoragePoolName = "virt-cloud-templates"
|
|
seedStoragePoolName = "virt-cloud-seeds"
|
|
userISOStoragePoolName = "virt-cloud-user-isos"
|
|
rescueStoragePoolName = "virt-cloud-rescue-isos"
|
|
)
|
|
|
|
// MutationAdmission describes every host resource that must be leased before
|
|
// the Journal crosses its durable dispatch boundary.
|
|
type MutationAdmission struct {
|
|
LockKeys []string
|
|
HeavyIO bool
|
|
}
|
|
|
|
func StorageConfigurationAdmission() MutationAdmission {
|
|
return MutationAdmission{LockKeys: []string{"storage-config"}, HeavyIO: true}
|
|
}
|
|
|
|
func VPCNetworkAdmission(vpcKey string) MutationAdmission {
|
|
return MutationAdmission{LockKeys: []string{
|
|
"catalog:vpc-host/state",
|
|
catalogLockKey("vpc", vpcKey),
|
|
}}
|
|
}
|
|
|
|
func VPCPortAdmission(uuid string, request VPCPortRequest) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{LockKeys: []string{
|
|
catalogLockKey("vpc-port", request.PortKey),
|
|
domainLockKey(uuid),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
networkIdentityLockKey(request.PrivateIP),
|
|
networkIdentityLockKey(request.MAC),
|
|
}}
|
|
}
|
|
|
|
func DomainMutationAdmission(uuid string) MutationAdmission {
|
|
return MutationAdmission{LockKeys: []string{domainLockKey(uuid)}}
|
|
}
|
|
|
|
func ResizeResourcesAdmission(uuid string, request ResizeResourcesRequest) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{
|
|
HeavyIO: request.TargetDiskGiB > request.FromDiskGiB,
|
|
LockKeys: []string{domainLockKey(uuid), vmManagedVolumesLockKey(uuid)},
|
|
}
|
|
}
|
|
|
|
func CreateVMAdmission(request CreateVMRequest) MutationAdmission {
|
|
uuid := strings.ToLower(request.UUID)
|
|
locks := []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, rootVolumeName(uuid)),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
nwfilterLockKey(networkFilterName(uuid)),
|
|
catalogLockKey("template", request.TemplateID),
|
|
volumeLockKey(templateStoragePoolName, templateVolumeName(request.TemplateID)),
|
|
}
|
|
locks = appendPublicNetworkIdentityLocks(locks, request.PublicNetwork)
|
|
locks = append(locks, networkIdentityLockKey(vmMACAddress(uuid)))
|
|
return MutationAdmission{HeavyIO: true, LockKeys: locks}
|
|
}
|
|
|
|
func DeleteVMAdmission(uuid string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{LockKeys: []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, rootVolumeName(uuid)),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
nwfilterLockKey(networkFilterName(uuid)),
|
|
}}
|
|
}
|
|
|
|
func SeedMutationAdmission(uuid string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{LockKeys: []string{
|
|
domainLockKey(uuid), volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
}}
|
|
}
|
|
|
|
func ChangeNetworkAdmission(uuid string, public publicnetwork.Intent) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
locks := []string{
|
|
domainLockKey(uuid),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
}
|
|
locks = appendPublicNetworkIdentityLocks(locks, public)
|
|
return MutationAdmission{LockKeys: locks}
|
|
}
|
|
|
|
func FirewallMutationAdmission(uuid string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{LockKeys: []string{
|
|
domainLockKey(uuid), nwfilterLockKey(networkFilterName(uuid)),
|
|
}}
|
|
}
|
|
|
|
func ReinstallVMAdmission(uuid string, request ReinstallRequest) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, reinstallVolumeName(uuid, request.OperationID)),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
catalogLockKey("template", request.TemplateID),
|
|
volumeLockKey(templateStoragePoolName, templateVolumeName(request.TemplateID)),
|
|
}}
|
|
}
|
|
|
|
func CreateSnapshotAdmission(uuid, snapshotKey string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, snapshotVolumeName(uuid, snapshotKey)),
|
|
}}
|
|
}
|
|
|
|
func CreateBackupAdmission(uuid string, request BackupRequest) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
locks := []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, backupWorkspaceVolumeName(uuid, request.BackupKey)),
|
|
catalogLockKey("backup", request.BackupKey),
|
|
}
|
|
return MutationAdmission{HeavyIO: true, LockKeys: locks}
|
|
}
|
|
|
|
func CleanupBackupWorkspaceAdmission(uuid, backupKey string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, backupWorkspaceVolumeName(uuid, backupKey)),
|
|
// Legacy backups created before the workspace split used the snapshot
|
|
// namespace. Keep that one exact lock until their residue is gone.
|
|
volumeLockKey(managedStoragePoolName, snapshotVolumeName(uuid, backupKey)),
|
|
catalogLockKey("backup", backupKey),
|
|
}}
|
|
}
|
|
|
|
func RestoreBackupAdmission(request RestoreBackupRequest) MutationAdmission {
|
|
uuid := strings.ToLower(request.UUID)
|
|
locks := []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, rootVolumeName(uuid)),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
nwfilterLockKey(networkFilterName(uuid)),
|
|
catalogLockKey("backup", request.BackupKey),
|
|
}
|
|
locks = appendPublicNetworkIdentityLocks(locks, request.PublicNetwork)
|
|
locks = append(locks, networkIdentityLockKey(vmMACAddress(uuid)))
|
|
return MutationAdmission{HeavyIO: true, LockKeys: locks}
|
|
}
|
|
|
|
func RecoverBackupAdmission(uuid string, request RecoverBackupRequest) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName,
|
|
snapshotRestoreVolumeName(uuid, request.OperationID)),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
catalogLockKey("backup", request.BackupKey),
|
|
}}
|
|
}
|
|
|
|
func PreparePeerMigrationAdmission(uuid string, request PreparePeerMigrationRequest) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
locks := []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, rootVolumeName(uuid)),
|
|
catalogLockKey("peer-migration", request.MigrationID),
|
|
}
|
|
for _, artifact := range request.Artifacts {
|
|
if artifact.Role == "snapshot" {
|
|
locks = append(locks, volumeLockKey(
|
|
managedStoragePoolName, snapshotVolumeName(uuid, artifact.SnapshotKey),
|
|
))
|
|
}
|
|
}
|
|
return MutationAdmission{HeavyIO: true, LockKeys: locks}
|
|
}
|
|
|
|
func ReceivePeerMigrationAdmission(request ReceivePeerMigrationRequest) MutationAdmission {
|
|
uuid := strings.ToLower(request.UUID)
|
|
locks := []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, rootVolumeName(uuid)),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
nwfilterLockKey(networkFilterName(uuid)),
|
|
catalogLockKey("peer-migration", request.MigrationID),
|
|
}
|
|
locks = appendPublicNetworkIdentityLocks(locks, request.PublicNetwork)
|
|
locks = append(locks, networkIdentityLockKey(vmMACAddress(uuid)))
|
|
if request.VPCPort != nil {
|
|
locks = append(locks,
|
|
"catalog:vpc-host/state",
|
|
catalogLockKey("vpc", request.VPCPort.VPCKey),
|
|
catalogLockKey("vpc-port", request.VPCPort.PortKey),
|
|
networkIdentityLockKey(request.VPCPort.PrivateIP),
|
|
networkIdentityLockKey(request.VPCPort.MAC),
|
|
)
|
|
}
|
|
return MutationAdmission{HeavyIO: true, LockKeys: locks}
|
|
}
|
|
|
|
func appendPublicNetworkIdentityLocks(locks []string, public publicnetwork.Intent) []string {
|
|
for _, cidr := range public.IdentityCIDRs() {
|
|
locks = append(locks, networkIdentityLockKey(cidr))
|
|
}
|
|
return locks
|
|
}
|
|
|
|
func CleanupPeerMigrationTargetAdmission(uuid, migrationID string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, rootVolumeName(uuid)),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
nwfilterLockKey(networkFilterName(uuid)),
|
|
catalogLockKey("peer-migration", migrationID),
|
|
}}
|
|
}
|
|
|
|
func DeleteSnapshotAdmission(uuid, snapshotKey string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{LockKeys: []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, snapshotVolumeName(uuid, snapshotKey)),
|
|
}}
|
|
}
|
|
|
|
func RestoreSnapshotAdmission(uuid, snapshotKey, operationID string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
domainLockKey(uuid), vmManagedVolumesLockKey(uuid),
|
|
volumeLockKey(managedStoragePoolName, snapshotVolumeName(uuid, snapshotKey)),
|
|
volumeLockKey(managedStoragePoolName, snapshotRestoreVolumeName(uuid, operationID)),
|
|
}}
|
|
}
|
|
|
|
func EnterRescueAdmission(uuid, templateID string) MutationAdmission {
|
|
return MutationAdmission{LockKeys: []string{
|
|
domainLockKey(uuid), catalogLockKey("rescue-template", templateID),
|
|
volumeLockKey(rescueStoragePoolName, rescueImageVolumeName(templateID)),
|
|
}}
|
|
}
|
|
|
|
func ImportTemplateAdmission(request ImportTemplateRequest) MutationAdmission {
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
catalogLockKey("template", request.ID), catalogNameLockKey("template", request.Name),
|
|
volumeLockKey(templateStoragePoolName, templateVolumeName(request.ID)),
|
|
volumeLockKey(templateStoragePoolName, templateMetadataVolumeName(request.ID)),
|
|
}}
|
|
}
|
|
|
|
func UpdateTemplateAdmission(id string, request UpdateTemplateRequest) MutationAdmission {
|
|
return MutationAdmission{LockKeys: []string{
|
|
catalogLockKey("template", id), catalogNameLockKey("template", request.Name),
|
|
volumeLockKey(templateStoragePoolName, templateMetadataVolumeName(id)),
|
|
}}
|
|
}
|
|
|
|
func DeleteTemplateAdmission(id string) MutationAdmission {
|
|
return MutationAdmission{LockKeys: []string{
|
|
catalogLockKey("template", id),
|
|
volumeLockKey(templateStoragePoolName, templateVolumeName(id)),
|
|
volumeLockKey(templateStoragePoolName, templateMetadataVolumeName(id)),
|
|
}}
|
|
}
|
|
|
|
func ImportRescueTemplateAdmission(request ImportRescueTemplateRequest) MutationAdmission {
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
catalogLockKey("rescue-template", request.ID), catalogNameLockKey("rescue-template", request.Name),
|
|
volumeLockKey(rescueStoragePoolName, rescueImageVolumeName(request.ID)),
|
|
volumeLockKey(rescueStoragePoolName, rescueMetadataVolumeName(request.ID)),
|
|
}}
|
|
}
|
|
|
|
func DeleteRescueTemplateAdmission(id string) MutationAdmission {
|
|
return MutationAdmission{LockKeys: []string{
|
|
catalogLockKey("rescue-template", id),
|
|
volumeLockKey(rescueStoragePoolName, rescueImageVolumeName(id)),
|
|
volumeLockKey(rescueStoragePoolName, rescueMetadataVolumeName(id)),
|
|
}}
|
|
}
|
|
|
|
func ImportUserISOAdmission(request ImportUserISORequest) MutationAdmission {
|
|
catalogID := strconv.FormatInt(request.OwnerID, 10) + ":" + request.ID
|
|
nameKind := "user-iso-" + strconv.FormatInt(request.OwnerID, 10)
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []string{
|
|
catalogLockKey("user-iso", catalogID), catalogNameLockKey(nameKind, request.Name),
|
|
volumeLockKey(userISOStoragePoolName, userISOImageVolumeName(request.OwnerID, request.ID)),
|
|
volumeLockKey(userISOStoragePoolName, userISOMetadataVolumeName(request.OwnerID, request.ID)),
|
|
}}
|
|
}
|
|
|
|
func DeleteUserISOAdmission(ownerID int64, id string) MutationAdmission {
|
|
catalogID := strconv.FormatInt(ownerID, 10) + ":" + id
|
|
return MutationAdmission{LockKeys: []string{
|
|
catalogLockKey("user-iso", catalogID),
|
|
volumeLockKey(userISOStoragePoolName, userISOImageVolumeName(ownerID, id)),
|
|
volumeLockKey(userISOStoragePoolName, userISOMetadataVolumeName(ownerID, id)),
|
|
}}
|
|
}
|
|
|
|
func domainLockKey(uuid string) string { return "domain:" + strings.ToLower(uuid) }
|
|
func volumeLockKey(pool, name string) string { return "volume:" + pool + "/" + name }
|
|
func nwfilterLockKey(name string) string { return "nwfilter:" + name }
|
|
func catalogLockKey(kind, id string) string { return "catalog:" + kind + "/" + id }
|
|
|
|
func vmManagedVolumesLockKey(uuid string) string {
|
|
return volumeLockKey(managedStoragePoolName, "vm-"+strings.ToLower(uuid))
|
|
}
|
|
|
|
func catalogNameLockKey(kind, name string) string {
|
|
digest := sha256.Sum256([]byte(equalFoldCanonical(strings.TrimSpace(name))))
|
|
return catalogLockKey(kind+"-name", hex.EncodeToString(digest[:]))
|
|
}
|
|
|
|
func equalFoldCanonical(value string) string {
|
|
var result strings.Builder
|
|
result.Grow(len(value))
|
|
for _, current := range value {
|
|
canonical := current
|
|
for candidate := unicode.SimpleFold(current); candidate != current; candidate = unicode.SimpleFold(candidate) {
|
|
if candidate < canonical {
|
|
canonical = candidate
|
|
}
|
|
}
|
|
result.WriteRune(canonical)
|
|
}
|
|
return result.String()
|
|
}
|
|
|
|
func networkIdentityLockKey(publicIPCIDR string) string {
|
|
identity := strings.ToLower(strings.TrimSpace(publicIPCIDR))
|
|
if prefix, err := netip.ParsePrefix(identity); err == nil {
|
|
identity = prefix.Addr().String()
|
|
}
|
|
digest := sha256.Sum256([]byte(identity))
|
|
return "network-identity:" + hex.EncodeToString(digest[:])
|
|
}
|
|
|
|
func vmMACAddress(uuid string) string {
|
|
digest := sha256.Sum256([]byte("virt-cloud-mac-v1:" + strings.ToLower(uuid)))
|
|
return fmt.Sprintf("02:%02x:%02x:%02x:%02x:%02x", digest[0], digest[1], digest[2], digest[3], digest[4])
|
|
}
|
|
|
|
func templateVolumeName(id string) string { return "template-" + id + ".qcow2" }
|
|
func rootVolumeName(uuid string) string { return strings.ToLower(uuid) + "-root.qcow2" }
|
|
func seedVolumeName(uuid string) string { return strings.ToLower(uuid) + "-seed.iso" }
|
|
func networkFilterName(uuid string) string { return "virt-" + strings.ToLower(uuid) }
|
|
|
|
func templateMetadataVolumeName(id string) string { return "virt-template-" + id + ".json" }
|
|
func rescueImageVolumeName(id string) string { return "rescue-" + id + ".iso" }
|
|
func rescueMetadataVolumeName(id string) string { return "virt-rescue-" + id + ".json" }
|
|
func userISOImageVolumeName(ownerID int64, id string) string {
|
|
return "useriso-" + strconv.FormatInt(ownerID, 10) + "-" + id + ".iso"
|
|
}
|
|
func userISOMetadataVolumeName(ownerID int64, id string) string {
|
|
return "virt-useriso-" + strconv.FormatInt(ownerID, 10) + "-" + id + ".json"
|
|
}
|
|
|
|
func snapshotVolumeName(uuid, snapshotKey string) string {
|
|
compact := strings.ReplaceAll(strings.ToLower(uuid), "-", "")
|
|
return "vsnap-" + compact + "-" + snapshotKey + ".qcow2"
|
|
}
|
|
|
|
func backupWorkspaceVolumeName(uuid, backupKey string) string {
|
|
compact := strings.ReplaceAll(strings.ToLower(uuid), "-", "")
|
|
return "vbackup-" + compact + "-" + backupKey + ".qcow2"
|
|
}
|
|
|
|
func snapshotRestoreVolumeName(uuid, operationID string) string {
|
|
sum := sha256.Sum256([]byte(operationID))
|
|
compact := strings.ReplaceAll(strings.ToLower(uuid), "-", "")
|
|
return "vrestore-" + compact + "-" + hex.EncodeToString(sum[:8]) + ".qcow2"
|
|
}
|
|
|
|
func reinstallVolumeName(uuid, operationID string) string {
|
|
sum := sha256.Sum256([]byte(operationID))
|
|
compact := strings.ReplaceAll(strings.ToLower(uuid), "-", "")
|
|
return "vreinstall-" + compact + "-" + hex.EncodeToString(sum[:8]) + ".qcow2"
|
|
}
|