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.
258 lines
9.9 KiB
Go
258 lines
9.9 KiB
Go
package hypervisor
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/netip"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
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 DomainMutationAdmission(uuid string) MutationAdmission {
|
|
return MutationAdmission{LockKeys: []string{domainLockKey(uuid)}}
|
|
}
|
|
|
|
func CreateVMAdmission(request CreateVMRequest) MutationAdmission {
|
|
uuid := strings.ToLower(request.UUID)
|
|
return MutationAdmission{HeavyIO: true, LockKeys: []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)),
|
|
networkIdentityLockKey(request.PublicIPCIDR),
|
|
networkIdentityLockKey(vmMACAddress(uuid)),
|
|
}}
|
|
}
|
|
|
|
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, publicIPCIDR string) MutationAdmission {
|
|
uuid = strings.ToLower(uuid)
|
|
return MutationAdmission{LockKeys: []string{
|
|
domainLockKey(uuid),
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
networkIdentityLockKey(publicIPCIDR),
|
|
}}
|
|
}
|
|
|
|
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 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 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"
|
|
}
|