Skip to content

Commit

Permalink
(feat): Add resize file system capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Nov 24, 2023
1 parent 2c2d015 commit 375c408
Show file tree
Hide file tree
Showing 15 changed files with 600 additions and 304 deletions.
5 changes: 4 additions & 1 deletion cmd/ebs-bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
lds := service.NewLinuxDeviceService(rc)
uos := service.NewUnixOwnerService()
ans := service.NewAwsNitroNVMeService()
fssf := service.NewLinuxFileSystemServiceFactory(rc)

// Warnings
warnings(uos)
Expand All @@ -30,9 +31,10 @@ func main() {
checkError(err)

// Service + Config Consumers
db := backend.NewLinuxDeviceBackend(lds)
db := backend.NewLinuxDeviceBackend(lds, fssf)
fb := backend.NewLinuxFileBackend(ufs)
ub := backend.NewLinuxOwnerBackend(uos)
drb := backend.NewLinuxDeviceResizeBackend(lds, fssf)
ae := action.NewActionExecutor(rc, c)

// Modify Config
Expand All @@ -59,6 +61,7 @@ func main() {
layer.NewUnmountDeviceLayer(db, fb),
layer.NewCreateDirectoryLayer(db, fb),
layer.NewMountDeviceLayer(db, fb),
layer.NewResizeDeviceLayer(drb),
layer.NewChangeOwnerLayer(ub, fb),
layer.NewChangePermissionsLayer(fb),
})
Expand Down
9 changes: 2 additions & 7 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const (
)

type Action interface {
Execute(rc *utils.RunnerCache) error
Preflight(rc *utils.RunnerCache) error
Execute() error
GetDeviceName() string
GetDelay() time.Duration
Success() string
Expand All @@ -56,10 +55,6 @@ func (ae *ActionExecutor) ExecuteAction(action Action) error {
if err != nil {
return err
}
err = action.Preflight(ae.runnerCache)
if err != nil {
return err
}
switch mode {
case config.Prompt:
if !ae.ShouldProceed(action) {
Expand All @@ -75,7 +70,7 @@ func (ae *ActionExecutor) executeAction(action Action) error {
if w := action.Warning(); w != DisabledWarning {
log.Printf("🟠 %s: %s", action.GetDeviceName(), w)
}
if err := action.Execute(ae.runnerCache); err != nil {
if err := action.Execute(); err != nil {
return err
}
log.Printf("⭐ %s: %s", action.GetDeviceName(), action.Success())
Expand Down
14 changes: 3 additions & 11 deletions internal/action/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ func NewCreateDirectoryAction(dn string, p string) *CreateDirectoryAction {
}
}

func (a *CreateDirectoryAction) Execute(rc *utils.RunnerCache) error {
func (a *CreateDirectoryAction) Execute() error {
return os.MkdirAll(a.path, DefaultDirectoryPermissions)
}

func (a *CreateDirectoryAction) Preflight(rc *utils.RunnerCache) error {
return nil
}

func (a *CreateDirectoryAction) GetDeviceName() string {
return a.deviceName
}
Expand Down Expand Up @@ -73,14 +69,10 @@ func NewChangeOwnerAction(dn string, p string, uid int, gid int) *ChangeOwnerAct
}
}

func (a *ChangeOwnerAction) Execute(rc *utils.RunnerCache) error {
func (a *ChangeOwnerAction) Execute() error {
return os.Chown(a.path, a.uid, a.gid)
}

func (a *ChangeOwnerAction) Preflight(rc *utils.RunnerCache) error {
return nil
}

func (a *ChangeOwnerAction) GetDeviceName() string {
return a.deviceName
}
Expand Down Expand Up @@ -119,7 +111,7 @@ func NewChangePermissions(dn string, p string, perms model.Permissions) *ChangeP
}
}

func (a *ChangePermissionsAction) Execute(rc *utils.RunnerCache) error {
func (a *ChangePermissionsAction) Execute() error {
mode, err := a.perms.ToFileMode()
if err != nil {
return err
Expand Down
92 changes: 19 additions & 73 deletions internal/action/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,45 @@ import (
"fmt"
"time"

"github.com/reecetech/ebs-bootstrap/internal/utils"
"github.com/reecetech/ebs-bootstrap/internal/service"
)

type FormatExt4Action struct {
deviceName string
type FormatDeviceAction struct {
deviceName string
fileSystemService service.FileSystemService
}

func NewFormatExt4Action(dn string) *FormatExt4Action {
return &FormatExt4Action{
deviceName: dn,
func NewFormatDeviceAction(dn string, fileSystemService service.FileSystemService) *FormatDeviceAction {
return &FormatDeviceAction{
deviceName: dn,
fileSystemService: fileSystemService,
}
}

func (a *FormatExt4Action) Execute(rc *utils.RunnerCache) error {
r := rc.GetRunner(utils.MkfsExt4)
_, err := r.Command(a.deviceName)
if err != nil {
return err
}
return nil
}

func (a *FormatExt4Action) Preflight(rc *utils.RunnerCache) error {
return nil
func (a *FormatDeviceAction) Execute() error {
return a.fileSystemService.Format(a.deviceName)
}

func (a *FormatExt4Action) GetDeviceName() string {
func (a *FormatDeviceAction) GetDeviceName() string {
return a.deviceName
}

func (a *FormatExt4Action) GetDelay() time.Duration {
func (a *FormatDeviceAction) GetDelay() time.Duration {
return DeviceActionDelay
}

func (a *FormatExt4Action) Prompt() string {
return fmt.Sprintf("Would you like to format %s to ext4", a.deviceName)
}

func (a *FormatExt4Action) Refuse() string {
return "Refused to format to ext4"
}

func (a *FormatExt4Action) Success() string {
return "Successfully formated to ext4"
}

func (a *FormatExt4Action) Warning() string {
return FormatActionWarning
}

type FormatXfsAction struct {
deviceName string
}

func NewFormatXfsAction(dn string) *FormatXfsAction {
return &FormatXfsAction{
deviceName: dn,
}
}

func (a *FormatXfsAction) Execute(rc *utils.RunnerCache) error {
r := rc.GetRunner(utils.MkfsXfs)
_, err := r.Command(a.deviceName)
if err != nil {
return err
}
return nil
}

func (a *FormatXfsAction) Preflight(rc *utils.RunnerCache) error {
return nil
}

func (a *FormatXfsAction) GetDeviceName() string {
return a.deviceName
}

func (a *FormatXfsAction) Prompt() string {
return fmt.Sprintf("Would you like to format %s to XFS?", a.deviceName)
}

func (a *FormatXfsAction) GetDelay() time.Duration {
return DeviceActionDelay
func (a *FormatDeviceAction) Prompt() string {
return fmt.Sprintf("Would you like to format %s to %s", a.deviceName, a.fileSystemService.GetFileSystem())
}

func (a *FormatXfsAction) Refuse() string {
return "Refused to format to XFS"
func (a *FormatDeviceAction) Refuse() string {
return fmt.Sprintf("Refused to format to %s", a.fileSystemService.GetFileSystem())
}

func (a *FormatXfsAction) Success() string {
return "Successfully formated to XFS"
func (a *FormatDeviceAction) Success() string {
return fmt.Sprintf("Successfully formated to %s", a.fileSystemService.GetFileSystem())
}

func (a *FormatXfsAction) Warning() string {
func (a *FormatDeviceAction) Warning() string {
return FormatActionWarning
}
98 changes: 18 additions & 80 deletions internal/action/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,109 +4,47 @@ import (
"fmt"
"time"

"github.com/reecetech/ebs-bootstrap/internal/utils"
"github.com/reecetech/ebs-bootstrap/internal/service"
)

type LabelExt4Action struct {
deviceName string
label string
type LabelDeviceAction struct {
deviceName string
label string
fileSystemService service.FileSystemService
}

func NewLabelExt4Action(dn string, label string) *LabelExt4Action {
return &LabelExt4Action{
deviceName: dn,
label: label,
func NewLabelDeviceAction(dn string, label string, fileSystemService service.FileSystemService) *LabelDeviceAction {
return &LabelDeviceAction{
deviceName: dn,
label: label,
fileSystemService: fileSystemService,
}
}

func (a *LabelExt4Action) Execute(rc *utils.RunnerCache) error {
r := rc.GetRunner(utils.E2Label)
_, err := r.Command(a.deviceName, a.label)
if err != nil {
return err
}
return nil
}

func (a *LabelExt4Action) Preflight(rc *utils.RunnerCache) error {
if len(a.label) > 16 {
return fmt.Errorf("🔴 %s: Label cannot exceed 16 characters for the ext4 file system", a.label)
}
return nil
}

func (a *LabelExt4Action) GetDeviceName() string {
return a.deviceName
}

func (a *LabelExt4Action) GetDelay() time.Duration {
return DeviceActionDelay
}

func (a *LabelExt4Action) Prompt() string {
return fmt.Sprintf("Would you like to label device %s to %s", a.deviceName, a.label)
}

func (a *LabelExt4Action) Refuse() string {
return fmt.Sprintf("Refused to label to %s", a.label)
}

func (a *LabelExt4Action) Success() string {
return fmt.Sprintf("Successfully labelled to %s", a.label)
}

func (a *LabelExt4Action) Warning() string {
return DisabledWarning
}

type LabelXfsAction struct {
deviceName string
label string
}

func NewLabelXfsAction(dn string, label string) *LabelXfsAction {
return &LabelXfsAction{
deviceName: dn,
label: label,
}
}

func (a *LabelXfsAction) Execute(rc *utils.RunnerCache) error {
r := rc.GetRunner(utils.XfsAdmin)
_, err := r.Command("-L", a.label, a.deviceName)
if err != nil {
return err
}
return nil
}

func (a *LabelXfsAction) Preflight(rc *utils.RunnerCache) error {
if len(a.label) > 12 {
return fmt.Errorf("🔴 %s: Label cannot exceed 12 characters for the XFS file system", a.label)
}
return nil
func (a *LabelDeviceAction) Execute() error {
return a.fileSystemService.Label(a.deviceName, a.label)
}

func (a *LabelXfsAction) GetDeviceName() string {
func (a *LabelDeviceAction) GetDeviceName() string {
return a.deviceName
}

func (a *LabelXfsAction) GetDelay() time.Duration {
func (a *LabelDeviceAction) GetDelay() time.Duration {
return DeviceActionDelay
}

func (a *LabelXfsAction) Prompt() string {
func (a *LabelDeviceAction) Prompt() string {
return fmt.Sprintf("Would you like to label device %s to %s", a.deviceName, a.label)
}

func (a *LabelXfsAction) Refuse() string {
func (a *LabelDeviceAction) Refuse() string {
return fmt.Sprintf("Refused to label to %s", a.label)
}

func (a *LabelXfsAction) Success() string {
func (a *LabelDeviceAction) Success() string {
return fmt.Sprintf("Successfully labelled to %s", a.label)
}

func (a *LabelXfsAction) Warning() string {
func (a *LabelDeviceAction) Warning() string {
return DisabledWarning
}
Loading

0 comments on commit 375c408

Please sign in to comment.