-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat): Format, label, resize and mount devices through configuration
- Loading branch information
Showing
52 changed files
with
3,118 additions
and
1,737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"log" | ||
"ebs-bootstrap/internal/config" | ||
"ebs-bootstrap/internal/service" | ||
"ebs-bootstrap/internal/utils" | ||
"ebs-bootstrap/internal/state" | ||
"os" | ||
|
||
"github.com/reecetech/ebs-bootstrap/internal/action" | ||
"github.com/reecetech/ebs-bootstrap/internal/backend" | ||
"github.com/reecetech/ebs-bootstrap/internal/config" | ||
"github.com/reecetech/ebs-bootstrap/internal/layer" | ||
"github.com/reecetech/ebs-bootstrap/internal/service" | ||
"github.com/reecetech/ebs-bootstrap/internal/utils" | ||
) | ||
|
||
func main() { | ||
// Disable Timetamp | ||
log.SetFlags(0) | ||
e := utils.NewExecRunner() | ||
ds := &service.LinuxDeviceService{Runner: e} | ||
ns := &service.AwsNVMeService{} | ||
fs := &service.UnixFileService{} | ||
dts := &service.EbsDeviceTranslator{DeviceService: ds, NVMeService: ns} | ||
|
||
dt, err := dts.GetTranslator() | ||
if err != nil { | ||
log.Fatal(err) | ||
// Services | ||
erf := utils.NewExecRunnerFactory() | ||
ufs := service.NewUnixFileService() | ||
lds := service.NewLinuxDeviceService(erf) | ||
uos := service.NewUnixOwnerService() | ||
ans := service.NewAwsNitroNVMeService() | ||
fssf := service.NewLinuxFileSystemServiceFactory(erf) | ||
|
||
// Warnings | ||
warnings(uos) | ||
|
||
// Config + Flags | ||
c, err := config.New(os.Args) | ||
checkError(err) | ||
|
||
// Service + Config Consumers | ||
db := backend.NewLinuxDeviceBackend(lds, fssf) | ||
fb := backend.NewLinuxFileBackend(ufs) | ||
ub := backend.NewLinuxOwnerBackend(uos) | ||
dmb := backend.NewLinuxDeviceMetricsBackend(lds, fssf) | ||
dae := action.NewDefaultActionExecutor(c) | ||
|
||
// Modify Config | ||
modifiers := []config.Modifier{ | ||
config.NewAwsNVMeDriverModifier(ans, lds), | ||
} | ||
for _, m := range modifiers { | ||
checkError(m.Modify(c)) | ||
} | ||
config, err := config.New(os.Args, dt, fs) | ||
|
||
// Validate Config | ||
validators := []config.Validator{ | ||
config.NewFileSystemValidator(), | ||
config.NewModeValidator(), | ||
config.NewResizeThresholdValidator(), | ||
config.NewDeviceValidator(lds), | ||
config.NewMountPointValidator(), | ||
config.NewMountOptionsValidator(), | ||
config.NewOwnerValidator(uos), | ||
} | ||
for _, v := range validators { | ||
checkError(v.Validate(c)) | ||
} | ||
|
||
// Layers | ||
le := layer.NewExponentialBackoffLayerExecutor(c, dae) | ||
layers := []layer.Layer{ | ||
layer.NewFormatDeviceLayer(db), | ||
layer.NewLabelDeviceLayer(db), | ||
layer.NewCreateDirectoryLayer(db, fb), | ||
layer.NewMountDeviceLayer(db, fb), | ||
layer.NewResizeDeviceLayer(db, dmb), | ||
layer.NewChangeOwnerLayer(ub, fb), | ||
layer.NewChangePermissionsLayer(fb), | ||
} | ||
checkError(le.Execute(layers)) | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
for name, device := range config.Devices { | ||
d, err := state.NewDevice(name, ds, fs) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = d.Diff(config) | ||
if err == nil { | ||
log.Printf("🟢 %s: No changes detected", name) | ||
continue | ||
} | ||
if device.Mode == "healthcheck" { | ||
log.Fatal(err) | ||
} | ||
func warnings(us service.OwnerService) { | ||
cu, err := us.GetCurrentUser() | ||
if err != nil { | ||
return | ||
} | ||
if cu.Uid != 0 { | ||
log.Println("🚧 Not running as root user. Operations that query and modify block devices will likely be restricted") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
devices: | ||
/dev/vdb: | ||
fs: ext4 | ||
label: external-vol | ||
mountPoint: /mnt/app | ||
group: ubuntu | ||
user: ubuntu | ||
permissions: 0644 | ||
resizeFs: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
module ebs-bootstrap | ||
module github.com/reecetech/ebs-bootstrap | ||
|
||
go 1.21 | ||
|
||
require gopkg.in/yaml.v2 v2.4.0 | ||
|
||
require github.com/google/go-cmp v0.6.0 // indirect | ||
require ( | ||
github.com/cenkalti/backoff/v4 v4.2.1 // indirect | ||
github.com/google/go-cmp v0.6.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package action | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/reecetech/ebs-bootstrap/internal/config" | ||
"github.com/reecetech/ebs-bootstrap/internal/model" | ||
) | ||
|
||
type Action interface { | ||
Execute() error | ||
Success() string | ||
Prompt() string | ||
Refuse() string | ||
GetMode() model.Mode | ||
SetMode(mode model.Mode) Action | ||
} | ||
|
||
type ActionExecutor interface { | ||
Execute(actions []Action) error | ||
} | ||
|
||
type DefaultActionExecutor struct { | ||
config *config.Config | ||
read func(buffer *string) error | ||
} | ||
|
||
func NewDefaultActionExecutor(c *config.Config) *DefaultActionExecutor { | ||
return &DefaultActionExecutor{ | ||
config: c, | ||
read: func(buffer *string) error { | ||
_, err := fmt.Scanln(buffer) | ||
return err | ||
}, | ||
} | ||
} | ||
|
||
func (dae *DefaultActionExecutor) Execute(actions []Action) error { | ||
for _, a := range actions { | ||
err := dae.execute(a) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (dae *DefaultActionExecutor) execute(action Action) error { | ||
switch action.GetMode() { | ||
case model.Force: | ||
break | ||
case model.Prompt: | ||
if !dae.shouldProceed(action) { | ||
return fmt.Errorf("🔴 Action rejected. %s", action.Refuse()) | ||
} | ||
case model.Healthcheck: | ||
return fmt.Errorf("🔴 Healthcheck mode enabled. %s", action.Refuse()) | ||
default: | ||
return fmt.Errorf("🔴 Unexpected mode detected. %s", action.Refuse()) | ||
} | ||
|
||
if err := action.Execute(); err != nil { | ||
return err | ||
} | ||
log.Printf("⭐ %s", action.Success()) | ||
return nil | ||
} | ||
|
||
func (dae *DefaultActionExecutor) shouldProceed(action Action) bool { | ||
prompt := action.Prompt() | ||
|
||
fmt.Printf("🟣 %s? (y/n): ", prompt) | ||
var response string | ||
err := dae.read(&response) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
response = strings.ToLower(response) | ||
if response == "y" || response == "yes" { | ||
return true | ||
} | ||
return false | ||
} |
Oops, something went wrong.