diff --git a/internal/backend/lvm.go b/internal/backend/lvm.go index de53671..6422782 100644 --- a/internal/backend/lvm.go +++ b/internal/backend/lvm.go @@ -44,7 +44,7 @@ func (lb *LinuxLvmBackend) GetVolumeGroups(name string) []*model.VolumeGroup { if err != nil { return vgs } - pvn := lb.lvmGraph.GetParents(node, datastructures.PhysicalVolume) + pvn := lb.lvmGraph.GetParents(node, model.PhysicalVolumeKind) for _, pv := range pvn { vgs = append(vgs, &model.VolumeGroup{ Name: node.Name, @@ -59,14 +59,14 @@ func (lb *LinuxLvmBackend) GetLogicalVolume(name string, volumeGroup string) (*m if err != nil { return nil, err } - vgs := lb.lvmGraph.GetParents(node, datastructures.VolumeGroup) + vgs := lb.lvmGraph.GetParents(node, model.VolumeGroupKind) if len(vgs) == 0 { return nil, fmt.Errorf("🔴 %s: Logical volume has no volume group", node.Name) } return &model.LogicalVolume{ Name: node.Name, VolumeGroup: vgs[0].Name, - State: model.LogicalVolumeState(node.State), + State: node.State, }, nil } @@ -76,12 +76,12 @@ func (lb *LinuxLvmBackend) SearchLogicalVolumes(volumeGroup string) ([]*model.Lo if err != nil { return nil, err } - lvn := lb.lvmGraph.GetChildren(node, datastructures.LogicalVolume) + lvn := lb.lvmGraph.GetChildren(node, model.LogicalVolumeKind) for _, lv := range lvn { lvs = append(lvs, &model.LogicalVolume{ Name: lv.Name, VolumeGroup: node.Name, - State: model.LogicalVolumeState(lv.State), + State: lv.State, Size: lv.Size, }) } @@ -93,7 +93,7 @@ func (lb *LinuxLvmBackend) SearchVolumeGroup(physicalVolume string) (*model.Volu if err != nil { return nil, err } - vgn := lb.lvmGraph.GetChildren(node, datastructures.VolumeGroup) + vgn := lb.lvmGraph.GetChildren(node, model.VolumeGroupKind) if len(vgn) == 0 { return nil, fmt.Errorf("🔴 %s: Physical volume has no volume group", physicalVolume) } @@ -125,7 +125,7 @@ func (lb *LinuxLvmBackend) ShouldResizePhysicalVolume(name string, threshold flo if err != nil { return false, nil } - dvn := lb.lvmGraph.GetParents(node, datastructures.Device) + dvn := lb.lvmGraph.GetParents(node, model.DeviceKind) if len(dvn) == 0 { return false, nil } @@ -143,7 +143,7 @@ func (lb *LinuxLvmBackend) ShouldResizeLogicalVolume(name string, volumeGroup st if err != nil { return false, err } - vgn := lb.lvmGraph.GetParents(node, datastructures.VolumeGroup) + vgn := lb.lvmGraph.GetParents(node, model.VolumeGroupKind) if len(vgn) == 0 { return false, fmt.Errorf("🔴 %s: Logical volume has no volume group", name) } @@ -200,7 +200,7 @@ func (db *LinuxLvmBackend) From(config *config.Config) error { return err } for _, lv := range lvs { - err := lvmGraph.AddLogicalVolume(lv.Name, lv.VolumeGroup, datastructures.LvmNodeState(lv.State), lv.Size) + err := lvmGraph.AddLogicalVolume(lv.Name, lv.VolumeGroup, lv.State, lv.Size) if err != nil { return err } diff --git a/internal/datastructures/lvm_graph.go b/internal/datastructures/lvm_graph.go index c1b3cb6..6a68125 100644 --- a/internal/datastructures/lvm_graph.go +++ b/internal/datastructures/lvm_graph.go @@ -2,32 +2,14 @@ package datastructures import ( "fmt" -) - -type LvmNodeState int32 -type LvmNodeCategory int32 - -const ( - DeviceActive LvmNodeState = 0b0000001 - PhysicalVolumeActive LvmNodeState = 0b0000010 - VolumeGroupInactive LvmNodeState = 0b0000100 - VolumeGroupActive LvmNodeState = 0b0001100 - LogicalVolumeInactive LvmNodeState = 0b0010000 - LogicalVolumeActive LvmNodeState = 0b0110000 - LogicalVolumeUnsupported LvmNodeState = 0b1110000 -) -const ( - Device LvmNodeCategory = 0b0000001 - PhysicalVolume LvmNodeCategory = 0b0000010 - VolumeGroup LvmNodeCategory = 0b0000100 - LogicalVolume LvmNodeCategory = 0b0010000 + "github.com/reecetech/ebs-bootstrap/internal/model" ) type LvmNode struct { id string Name string - State LvmNodeState + State model.LvmState Size uint64 children []*LvmNode parents []*LvmNode @@ -37,7 +19,7 @@ func NewDevice(name string, size uint64) *LvmNode { return &LvmNode{ id: fmt.Sprintf("device:%s", name), Name: name, - State: DeviceActive, + State: model.DeviceActive, Size: size, children: []*LvmNode{}, parents: []*LvmNode{}, @@ -48,7 +30,7 @@ func NewPhysicalVolume(name string, size uint64) *LvmNode { return &LvmNode{ id: fmt.Sprintf("pv:%s", name), Name: name, - State: PhysicalVolumeActive, + State: model.PhysicalVolumeActive, Size: size, children: []*LvmNode{}, parents: []*LvmNode{}, @@ -59,14 +41,14 @@ func NewVolumeGroup(name string, size uint64) *LvmNode { return &LvmNode{ id: fmt.Sprintf("vg:%s", name), Name: name, - State: VolumeGroupInactive, + State: model.VolumeGroupInactive, Size: size, children: []*LvmNode{}, parents: []*LvmNode{}, } } -func NewLogicalVolume(name string, vg string, State LvmNodeState, size uint64) *LvmNode { +func NewLogicalVolume(name string, vg string, State model.LvmState, size uint64) *LvmNode { return &LvmNode{ id: fmt.Sprintf("lv:%s:vg:%s", name, vg), Name: name, @@ -143,7 +125,7 @@ func (lg *LvmGraph) AddVolumeGroup(name string, pv string, size uint64) error { return nil } -func (lg *LvmGraph) AddLogicalVolume(name string, vg string, state LvmNodeState, size uint64) error { +func (lg *LvmGraph) AddLogicalVolume(name string, vg string, state model.LvmState, size uint64) error { lv := NewLogicalVolume(name, vg, state, size) _, found := lg.nodes[lv.id] @@ -164,8 +146,8 @@ func (lg *LvmGraph) AddLogicalVolume(name string, vg string, state LvmNodeState, // If at least one of the logical volumes are active, the // volume group is considered active for _, lvn := range vgn.children { - if lvn.State == LogicalVolumeActive { - vgn.State = VolumeGroupActive + if lvn.State == model.LogicalVolumeActive { + vgn.State = model.VolumeGroupActive break } } @@ -199,22 +181,22 @@ func (lg *LvmGraph) GetLogicalVolume(name string, vg string) (*LvmNode, error) { return node, nil } -func (lg *LvmGraph) GetParents(node *LvmNode, state LvmNodeCategory) []*LvmNode { +func (lg *LvmGraph) GetParents(node *LvmNode, kind model.LvmKind) []*LvmNode { parents := []*LvmNode{} for _, p := range node.parents { - // Bitmasking to check if the parent nodes is of the desired category - if int32(p.State)&int32(state) > 0 { + // Bitmasking to check if the parent nodes is of the desired kind + if int32(p.State)&int32(kind) > 0 { parents = append(parents, p) } } return parents } -func (lg *LvmGraph) GetChildren(node *LvmNode, state LvmNodeCategory) []*LvmNode { +func (lg *LvmGraph) GetChildren(node *LvmNode, kind model.LvmKind) []*LvmNode { children := []*LvmNode{} for _, c := range node.children { - // Bitmasking to check if children nodes is of the desired category - if int32(c.State)&int32(state) > 0 { + // Bitmasking to check if children nodes is of the desired kind + if int32(c.State)&int32(kind) > 0 { children = append(children, c) } } diff --git a/internal/layer/lv_activate.go b/internal/layer/lv_activate.go index fb5fffb..256c4c8 100644 --- a/internal/layer/lv_activate.go +++ b/internal/layer/lv_activate.go @@ -31,11 +31,11 @@ func (cvgl *ActivateLogicalVolumeLayer) Modify(c *config.Config) ([]action.Actio return nil, err } - if lv.State == model.Active { + if lv.State == model.LogicalVolumeActive { continue } - if lv.State == model.Unsupported { + if lv.State == model.LogicalVolumeUnsupported { return nil, fmt.Errorf("🔴 %s: Can not activate a logical volume in an unsupported state", lv.Name) } diff --git a/internal/model/lvm.go b/internal/model/lvm.go index 90cea52..98afbb8 100644 --- a/internal/model/lvm.go +++ b/internal/model/lvm.go @@ -1,6 +1,24 @@ package model -import "github.com/reecetech/ebs-bootstrap/internal/datastructures" +type LvmState int32 +type LvmKind int32 + +const ( + DeviceActive LvmState = 0b0000001 + PhysicalVolumeActive LvmState = 0b0000010 + VolumeGroupInactive LvmState = 0b0000100 + VolumeGroupActive LvmState = 0b0001100 + LogicalVolumeInactive LvmState = 0b0010000 + LogicalVolumeActive LvmState = 0b0110000 + LogicalVolumeUnsupported LvmState = 0b1110000 +) + +const ( + DeviceKind LvmKind = 0b0000001 + PhysicalVolumeKind LvmKind = 0b0000010 + VolumeGroupKind LvmKind = 0b0000100 + LogicalVolumeKind LvmKind = 0b0010000 +) type Device struct { Name string @@ -16,19 +34,12 @@ type VolumeGroup struct { Name string PhysicalVolume string Size uint64 + State LvmState } -type LogicalVolumeState int32 - -const ( - Inactive LogicalVolumeState = LogicalVolumeState(datastructures.LogicalVolumeInactive) - Active LogicalVolumeState = LogicalVolumeState(datastructures.LogicalVolumeActive) - Unsupported LogicalVolumeState = LogicalVolumeState(datastructures.LogicalVolumeUnsupported) -) - type LogicalVolume struct { Name string VolumeGroup string - State LogicalVolumeState + State LvmState Size uint64 } diff --git a/internal/service/lvm.go b/internal/service/lvm.go index 6beae5c..c5c1fd0 100644 --- a/internal/service/lvm.go +++ b/internal/service/lvm.go @@ -133,6 +133,7 @@ func (ls *LinuxLvmService) GetVolumeGroups() ([]*model.VolumeGroup, error) { vgs[i] = &model.VolumeGroup{ Name: vg.Name, PhysicalVolume: vg.PhysicalVolume, + State: model.VolumeGroupInactive, Size: size, } } @@ -152,19 +153,23 @@ func (ls *LinuxLvmService) GetLogicalVolumes() ([]*model.LogicalVolume, error) { } lvs := make([]*model.LogicalVolume, len(lr.Report[0].LogicalVolume)) for i, lv := range lr.Report[0].LogicalVolume { - var state model.LogicalVolumeState + // Get Logical Volume State + var state model.LvmState switch lv.Attributes[4] { case 'a': - state = model.Active + state = model.LogicalVolumeActive case '-': - state = model.Inactive + state = model.LogicalVolumeInactive default: - state = model.Unsupported + state = model.LogicalVolumeUnsupported } + + // Get Logical Volume Size size, err := strconv.ParseUint(lv.Size, 10, 64) if err != nil { return nil, fmt.Errorf("🔴 Failed to cast logical volume size to unsigned 64-bit integer") } + lvs[i] = &model.LogicalVolume{ Name: lv.Name, VolumeGroup: lv.VolumeGroup,