From 23e93bfdb9e02836745f917737151309f7c8b60b Mon Sep 17 00:00:00 2001 From: lasith-kg Date: Thu, 23 May 2024 02:45:08 +0000 Subject: [PATCH] (feat): Rename variables for clarity --- configs/ubuntu.yml | 2 +- internal/backend/device.go | 6 +- internal/backend/lvm.go | 54 +++++++++-------- internal/datastructures/lvm_graph.go | 86 ++++++++++++++-------------- 4 files changed, 78 insertions(+), 70 deletions(-) diff --git a/configs/ubuntu.yml b/configs/ubuntu.yml index 9a36076..e305eb2 100644 --- a/configs/ubuntu.yml +++ b/configs/ubuntu.yml @@ -17,4 +17,4 @@ devices: user: ubuntu group: ubuntu permissions: 755 - lvmConsumption: 20 + lvmConsumption: 30 diff --git a/internal/backend/device.go b/internal/backend/device.go index 726b80b..ea05ea9 100644 --- a/internal/backend/device.go +++ b/internal/backend/device.go @@ -117,10 +117,12 @@ func (db *LinuxDeviceBackend) Umount(bd *model.BlockDevice) action.Action { } func (db *LinuxDeviceBackend) From(config *config.Config) error { - // Clear representation of devices + // We populate a temporary map and then assign it to the backend + // after all objects have been successfully added. This avoids a partial + // state in the event of failure during one of intermediate steps. db.blockDevices = nil - blockDevices := map[string]*model.BlockDevice{} + for name := range config.Devices { d, err := db.deviceService.GetBlockDevice(name) if err != nil { diff --git a/internal/backend/lvm.go b/internal/backend/lvm.go index 6422782..6145bf0 100644 --- a/internal/backend/lvm.go +++ b/internal/backend/lvm.go @@ -40,66 +40,70 @@ func NewLinuxLvmBackend(ls service.LvmService) *LinuxLvmBackend { func (lb *LinuxLvmBackend) GetVolumeGroups(name string) []*model.VolumeGroup { vgs := []*model.VolumeGroup{} - node, err := lb.lvmGraph.GetVolumeGroup(name) + vgn, err := lb.lvmGraph.GetVolumeGroup(name) if err != nil { return vgs } - pvn := lb.lvmGraph.GetParents(node, model.PhysicalVolumeKind) + pvn := lb.lvmGraph.GetParents(vgn, model.PhysicalVolumeKind) for _, pv := range pvn { vgs = append(vgs, &model.VolumeGroup{ - Name: node.Name, + Name: vgn.Name, PhysicalVolume: pv.Name, + State: vgn.State, + Size: vgn.Size, }) } return vgs } func (lb *LinuxLvmBackend) GetLogicalVolume(name string, volumeGroup string) (*model.LogicalVolume, error) { - node, err := lb.lvmGraph.GetLogicalVolume(name, volumeGroup) + lvn, err := lb.lvmGraph.GetLogicalVolume(name, volumeGroup) if err != nil { return nil, err } - vgs := lb.lvmGraph.GetParents(node, model.VolumeGroupKind) + vgs := lb.lvmGraph.GetParents(lvn, model.VolumeGroupKind) if len(vgs) == 0 { - return nil, fmt.Errorf("🔴 %s: Logical volume has no volume group", node.Name) + return nil, fmt.Errorf("🔴 %s: Logical volume has no volume group", lvn.Name) } return &model.LogicalVolume{ - Name: node.Name, + Name: lvn.Name, VolumeGroup: vgs[0].Name, - State: node.State, + State: lvn.State, + Size: lvn.Size, }, nil } func (lb *LinuxLvmBackend) SearchLogicalVolumes(volumeGroup string) ([]*model.LogicalVolume, error) { lvs := []*model.LogicalVolume{} - node, err := lb.lvmGraph.GetVolumeGroup(volumeGroup) + vgn, err := lb.lvmGraph.GetVolumeGroup(volumeGroup) if err != nil { return nil, err } - lvn := lb.lvmGraph.GetChildren(node, model.LogicalVolumeKind) - for _, lv := range lvn { + lvns := lb.lvmGraph.GetChildren(vgn, model.LogicalVolumeKind) + for _, lvn := range lvns { lvs = append(lvs, &model.LogicalVolume{ - Name: lv.Name, - VolumeGroup: node.Name, - State: lv.State, - Size: lv.Size, + Name: lvn.Name, + VolumeGroup: vgn.Name, + State: lvn.State, + Size: lvn.Size, }) } return lvs, nil } func (lb *LinuxLvmBackend) SearchVolumeGroup(physicalVolume string) (*model.VolumeGroup, error) { - node, err := lb.lvmGraph.GetPhysicalVolume(physicalVolume) + pvn, err := lb.lvmGraph.GetPhysicalVolume(physicalVolume) if err != nil { return nil, err } - vgn := lb.lvmGraph.GetChildren(node, model.VolumeGroupKind) + vgn := lb.lvmGraph.GetChildren(pvn, model.VolumeGroupKind) if len(vgn) == 0 { return nil, fmt.Errorf("🔴 %s: Physical volume has no volume group", physicalVolume) } return &model.VolumeGroup{ Name: vgn[0].Name, - PhysicalVolume: node.Name, + PhysicalVolume: pvn.Name, + State: vgn[0].State, Size: vgn[0].Size, }, nil } @@ -121,15 +125,15 @@ func (lb *LinuxLvmBackend) ActivateLogicalVolume(name string, volumeGroup string } func (lb *LinuxLvmBackend) ShouldResizePhysicalVolume(name string, threshold float64) (bool, error) { - node, err := lb.lvmGraph.GetPhysicalVolume(name) + pvn, err := lb.lvmGraph.GetPhysicalVolume(name) if err != nil { return false, nil } - dvn := lb.lvmGraph.GetParents(node, model.DeviceKind) - if len(dvn) == 0 { + dn := lb.lvmGraph.GetParents(pvn, model.DeviceKind) + if len(dn) == 0 { return false, nil } - return (float64(node.Size) / float64(dvn[0].Size) * 100) < threshold, nil + return (float64(pvn.Size) / float64(dn[0].Size) * 100) < threshold, nil } func (lb *LinuxLvmBackend) ResizePhysicalVolume(name string) action.Action { @@ -139,15 +143,15 @@ func (lb *LinuxLvmBackend) ResizePhysicalVolume(name string) action.Action { func (lb *LinuxLvmBackend) ShouldResizeLogicalVolume(name string, volumeGroup string, volumeGroupPercent int, tolerance float64) (bool, error) { left := float64(volumeGroupPercent) - tolerance right := float64(volumeGroupPercent) + tolerance - node, err := lb.lvmGraph.GetLogicalVolume(name, volumeGroup) + lvn, err := lb.lvmGraph.GetLogicalVolume(name, volumeGroup) if err != nil { return false, err } - vgn := lb.lvmGraph.GetParents(node, model.VolumeGroupKind) + vgn := lb.lvmGraph.GetParents(lvn, model.VolumeGroupKind) if len(vgn) == 0 { return false, fmt.Errorf("🔴 %s: Logical volume has no volume group", name) } - usedPerecent := (float64(node.Size) / float64(vgn[0].Size)) * 100 + usedPerecent := (float64(lvn.Size) / float64(vgn[0].Size)) * 100 if usedPerecent > right { return false, fmt.Errorf("🔴 %s: Logical volume %s is using %.0f%% of volume group %s, which exceeds the expected usage of %d%%", volumeGroup, name, usedPerecent, volumeGroup, volumeGroupPercent) } diff --git a/internal/datastructures/lvm_graph.go b/internal/datastructures/lvm_graph.go index 6a68125..8f90bcc 100644 --- a/internal/datastructures/lvm_graph.go +++ b/internal/datastructures/lvm_graph.go @@ -60,7 +60,7 @@ func NewLogicalVolume(name string, vg string, State model.LvmState, size uint64) } type LvmGraph struct { - nodes map[string]*LvmNode // A map that stores all the nodes by their Id + nodes map[string]*LvmNode } func NewLvmGraph() *LvmGraph { @@ -74,7 +74,7 @@ func (lg *LvmGraph) AddDevice(name string, size uint64) error { _, found := lg.nodes[bd.id] if found { - return fmt.Errorf("block device %s already exists", name) + return fmt.Errorf("🔴 %s: Device already exists", name) } lg.nodes[bd.id] = bd @@ -86,62 +86,59 @@ func (lg *LvmGraph) AddPhysicalVolume(name string, size uint64) error { _, found := lg.nodes[pv.id] if found { - return fmt.Errorf("physical volume %s already exists", name) + return fmt.Errorf("🔴 %s: Physical volume already exists", name) } - bdId := fmt.Sprintf("device:%s", name) - bdn, found := lg.nodes[bdId] - if !found { - return fmt.Errorf("block device %s does not exist", name) + dn, err := lg.GetDevice(name) + if err != nil { + return err } lg.nodes[pv.id] = pv - bdn.children = append(bdn.children, pv) - pv.parents = append(pv.parents, bdn) + dn.children = append(dn.children, pv) + pv.parents = append(pv.parents, dn) return nil } func (lg *LvmGraph) AddVolumeGroup(name string, pv string, size uint64) error { id := fmt.Sprintf("vg:%s", name) - vg, found := lg.nodes[id] + vgn, found := lg.nodes[id] if !found { - vg = NewVolumeGroup(name, size) + vgn = NewVolumeGroup(name, size) } - pvId := fmt.Sprintf("pv:%s", pv) - pvn, found := lg.nodes[pvId] - if !found { - return fmt.Errorf("physical volume %s does not exist", pv) + pvn, err := lg.GetPhysicalVolume(pv) + if err != nil { + return err } if len(pvn.children) > 0 { - return fmt.Errorf("%s is already assigned to volume group %s", pv, pvn.children[0].Name) + return fmt.Errorf("🔴 %s: Physical volume is already assigned to volume group %s", pv, pvn.children[0].Name) } - lg.nodes[vg.id] = vg - pvn.children = append(pvn.children, vg) - vg.parents = append(vg.parents, pvn) + lg.nodes[vgn.id] = vgn + pvn.children = append(pvn.children, vgn) + vgn.parents = append(vgn.parents, pvn) return nil } func (lg *LvmGraph) AddLogicalVolume(name string, vg string, state model.LvmState, size uint64) error { - lv := NewLogicalVolume(name, vg, state, size) + lvn := NewLogicalVolume(name, vg, state, size) - _, found := lg.nodes[lv.id] + _, found := lg.nodes[lvn.id] if found { - return fmt.Errorf("logical volume %s already exists", name) + return fmt.Errorf("🔴 %s/%s: Logical volume already exists", name, vg) } - vgId := fmt.Sprintf("vg:%s", vg) - vgn, found := lg.nodes[vgId] - if !found { - return fmt.Errorf("volume group %s does not exist", vg) + vgn, err := lg.GetVolumeGroup(vg) + if err != nil { + return err } - lg.nodes[lv.id] = lv - vgn.children = append(vgn.children, lv) - lv.parents = append(lv.parents, vgn) + lg.nodes[lvn.id] = lvn + vgn.children = append(vgn.children, lvn) + lvn.parents = append(lvn.parents, vgn) // If at least one of the logical volumes are active, the // volume group is considered active @@ -154,31 +151,40 @@ func (lg *LvmGraph) AddLogicalVolume(name string, vg string, state model.LvmStat return nil } +func (lg *LvmGraph) GetDevice(name string) (*LvmNode, error) { + id := fmt.Sprintf("device:%s", name) + dn, found := lg.nodes[id] + if !found { + return nil, fmt.Errorf("🔴 %s: Block device does not exist", name) + } + return dn, nil +} + func (lg *LvmGraph) GetPhysicalVolume(name string) (*LvmNode, error) { id := fmt.Sprintf("pv:%s", name) - node, found := lg.nodes[id] + pvn, found := lg.nodes[id] if !found { - return nil, fmt.Errorf("physical volume %s does not exist", name) + return nil, fmt.Errorf("🔴 %s: Physical volume does not exist", name) } - return node, nil + return pvn, nil } func (lg *LvmGraph) GetVolumeGroup(name string) (*LvmNode, error) { id := fmt.Sprintf("vg:%s", name) - node, found := lg.nodes[id] + vgn, found := lg.nodes[id] if !found { - return nil, fmt.Errorf("volume group %s does not exist", name) + return nil, fmt.Errorf("🔴 %s: Volume group does not exist", name) } - return node, nil + return vgn, nil } func (lg *LvmGraph) GetLogicalVolume(name string, vg string) (*LvmNode, error) { id := fmt.Sprintf("lv:%s:vg:%s", name, vg) - node, found := lg.nodes[id] + lvn, found := lg.nodes[id] if !found { - return nil, fmt.Errorf("logical volume %s/%s does not exist", vg, name) + return nil, fmt.Errorf("🔴 %s/%s: Logical volume does not exist", vg, name) } - return node, nil + return lvn, nil } func (lg *LvmGraph) GetParents(node *LvmNode, kind model.LvmKind) []*LvmNode { @@ -202,7 +208,3 @@ func (lg *LvmGraph) GetChildren(node *LvmNode, kind model.LvmKind) []*LvmNode { } return children } - -func (lg *LvmGraph) Clear() { - lg.nodes = map[string]*LvmNode{} -}