Skip to content

Commit

Permalink
(feat): Format, label, resize and mount devices through configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Jan 3, 2024
1 parent 2ea24ac commit 184b1c5
Show file tree
Hide file tree
Showing 85 changed files with 11,746 additions and 1,564 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/ebs-bootstrap*
coverage.*
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# syntax=docker/dockerfile:1
FROM golang:1.21

# Build a static binary
ENV CGO_ENABLED=0

# Set destination for COPY
WORKDIR /app

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
# Specific Architecture
./build/docker.sh --architecture arm64
ls -la
... ebs-bootstrap_linux-arm64
... ebs-bootstrap-linux-aarch64

# All Architectures
./build/docker.sh
ls -la
... ebs-bootstrap_linux-arm64
... ebs-bootstrap_linux-x86_64
... ebs-bootstrap-linux-aarch64
... ebs-bootstrap-linux-x86_64
```

## Recommended Setup

### `systemd`
Expand Down
276 changes: 276 additions & 0 deletions assets/uml.drawio

Large diffs are not rendered by default.

Binary file added assets/uml.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions build/coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -euo pipefail

# https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "${SCRIPT_DIR}/.."

go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
rm -rf coverage.out

# The HTML file can either be opened locally or served through HTTP using a CLI tool like miniserve
# https://github.com/svenstaro/miniserve. The latter is great if you are work
# e.g `miniserve coverage.html`
102 changes: 74 additions & 28 deletions cmd/ebs-bootstrap.go
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()

// 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, layer.DefaultExponentialBackoffParameters())
layers := []layer.Layer{
layer.NewFormatDeviceLayer(db),
layer.NewLabelDeviceLayer(db),
layer.NewCreateDirectoryLayer(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(owns service.OwnerService) {
user, err := owns.GetCurrentUser()
if err != nil {
return
}
if user.Id != 0 {
log.Println("🚧 Not running as root user. Device operations might yield unexpected results")
}
}
11 changes: 0 additions & 11 deletions configs/ebs-bootstrap.yml

This file was deleted.

9 changes: 9 additions & 0 deletions configs/ubuntu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
devices:
/dev/vdb:
fs: xfs
label: external-vol
mountPoint: /mnt/app
group: ubuntu
user: ubuntu
permissions: 0644
resizeFs: true
Loading

0 comments on commit 184b1c5

Please sign in to comment.