Skip to content

Commit

Permalink
partsetup container added
Browse files Browse the repository at this point in the history
  • Loading branch information
krobertson committed Apr 7, 2024
1 parent 42b1fbe commit 5d119e9
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
16 changes: 16 additions & 0 deletions apps/partsetup/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM debian:12-slim AS base

ARG TARGETPLATFORM
ARG VERSION
ARG CHANNEL

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
vim-tiny python3-parted udev xfsprogs python3-yaml \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

CMD ["/app/partsetup.py" ]

COPY ./apps/partsetup/scripts/partsetup.py .
2 changes: 2 additions & 0 deletions apps/partsetup/ci/latest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
printf "2024.04.07"
10 changes: 10 additions & 0 deletions apps/partsetup/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
app: partsetup
base: true
semantic_versioning: false
channels:
- name: master
platforms: ["linux/amd64"]
stable: true
tests:
enabled: false
103 changes: 103 additions & 0 deletions apps/partsetup/scripts/partsetup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env python3
#
# coding=utf-8
#

from glob import glob
from logging import getLogger
import logging
import sys
import os
import os.path
import parted
from pathlib import Path
import yaml


class _ConsoleHandler(logging.StreamHandler):
def __init__(self):
super().__init__()
self.setFormatter(logging.Formatter("{levelname} - {message}", style="{"))


class Device(object):
def __init__(self, dev):
self.givenDev = dev
self.device = parted.getDevice(dev)
self.path = self.device.path
self.logger = getLogger(__name__)

@property
def partition_names(self):
names = glob("{}[0-9]*".format(self.path))
return names

def partition(self):
disk = parted.freshDisk(self.device, "gpt")
geometry = parted.Geometry(
device=self.device, start=1, length=self.device.getLength() - 1
)
filesystem = parted.FileSystem(type="xfs", geometry=geometry)
partition = parted.Partition(
disk=disk, type=parted.PARTITION_NORMAL, fs=filesystem, geometry=geometry
)
disk.addPartition(
partition=partition, constraint=self.device.optimalAlignedConstraint
)
partition.setFlag(parted.PARTITION_BOOT)
disk.commit()

def format(self):
for partition in self.partition_names:
os.system("mkfs.xfs {}".format(partition))

def mount(self, dest):
src = self.partition_names[0]
os.makedirs(dest, exist_ok=True)

if not os.path.ismount(dest):
Path(os.path.join(dest, ".not_mounted")).touch()
self.logger.info("mounting %s to %s", src, dest)
os.system("mount {} {}".format(src, dest))
else:
self.logger.info("filesystem at %s already mounted", dest)

def wipe_dev(self, dev_path):
self.logger.debug("wiping %s", dev_path)
with open(dev_path, "wb") as p:
p.write(bytearray(1024))

def wipe(self):
self.logger.info("wiping partitions and other meta-data")
for partition in self.partition_names:
self.wipe_dev(partition)
self.wipe_dev(self.path)


if __name__ == "__main__":

# Set up a logger for nice visibility.
logger = getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(_ConsoleHandler())

# load config
cfgFile = "/config/{}.yaml".format(os.environ["NODE_NAME"])
with open(cfgFile, 'r') as stream:
data = yaml.safe_load(stream)

# loop each source
for source in data["source"].keys():
for idx, devpath in enumerate(data["source"][source]):
if not os.path.exists(devpath):
logger.warning("disk %s not found", devpath)
continue

disk = Device(devpath)
if len(disk.partition_names) == 0:
logger.info("no partitions on %s, partitioning", disk.path)
disk.partition()
disk.format()
mntname = "-".join((source, "hdd%02d" % (idx+1)))
mntpath = os.path.join(data["destination"], mntname)
disk.mount(mntpath)

0 comments on commit 5d119e9

Please sign in to comment.