Fixes to get example running. Start adding test.

This commit is contained in:
James Grogan 2024-03-29 12:39:44 +00:00
parent 4843413883
commit bda8084172
9 changed files with 47 additions and 16 deletions

View file

@ -2,6 +2,8 @@ import logging
from .util import run_op
logger = logging.getLogger(__name__)
class UfwInterface:
def __init__(self):
@ -9,12 +11,12 @@ class UfwInterface:
def enable(self):
op = "ufw enable"
logging.info(f"Enabling ufw: {op}")
logger.info(f"Enabling ufw: {op}")
run_op(op)
def allow_app(self, app_name: str):
op = f"ufw allow {app_name}"
logging.info(f"Allowing ufw app: {op}")
logger.info(f"Allowing ufw app: {op}")
run_op(op)
class Firewall:

View file

@ -21,7 +21,7 @@ class Machine:
self.ssh_config.restart_service()
def setup(self):
self.package_manager.update()
self.package_manager.upgrade()
self.user_manager.setup_user(self.user)
self.enable_firewall()
self.secure_ssh_config()

View file

@ -2,6 +2,8 @@ import logging
from .util import run_op
logger = logging.getLogger(__name__)
class AptInterface:
def __init__(self):
@ -9,18 +11,18 @@ class AptInterface:
def update(self):
op = "apt-get update"
logging.info(f"Updating apt: {op}")
logger.info(f"Updating apt: {op}")
run_op(op)
def upgrade(self):
op = "apt-get -y upgrade"
logging.info(f"Upgrading via apt: {op}")
logger.info(f"Upgrading via apt: {op}")
run_op(op)
def install_packages(self, packages: list):
packages_str = "".join(packages)
packages_str = " ".join(packages)
op = f"apt-get install -y {packages_str}"
logging.info(f"Installing packages: {op}")
logger.info(f"Installing packages: {op}")
run_op(op)

View file

@ -3,6 +3,8 @@ import logging
from .util import run_op
logger = logging.getLogger(__name__)
class SshConfig:
def __init__(self):
@ -13,14 +15,16 @@ class SshConfig:
"UsePAM": "No"}
def sync_target_values(self):
logging.info(f"Updating ssh config in: {self.config_path}")
logger.info(f"Updating ssh config in: {self.config_path}")
pass
def restart_service(self):
op = "systemctl restart ssh"
logging.info(f"Restarting ssh service: {op}")
logger.info(f"Restarting ssh service: {op}")
run_op(op)
def copy_ssh_dir_to_user(self, username:str):
op = f"rsync --archive --chown={username}:{username} ~/.ssh /home/{username}"
logger.info(f"Copying ssh dir to user: {op}")
run_op(op)

View file

@ -1,6 +1,8 @@
import logging
from .util import run_op
logger = logging.getLogger(__name__)
class User:
def __init__(self, name, has_sudo=False):
self.name = name
@ -8,20 +10,20 @@ class User:
class UserManager:
def __init__():
def __init__(self):
pass
def setup_user(self, user: User):
add_user(user)
self.add_user(user)
if user.has_sudo:
add_user_to_sudo(user)
self.add_user_to_sudo(user)
def add_user(self, user: User):
op = f"adduser {user.name}"
logging.info(f"Adding user: {op}")
logger.info(f"Adding user: {op}")
run_op(op)
def add_user_to_sudo(self, user: User):
op = f"usermod -aG sudo {user.name}"
logging.info(f"Adding user to sudo: {op}")
logger.info(f"Adding user to sudo: {op}")
run_op(op)

View file

@ -1,6 +1,8 @@
import subprocess
import logging
logger = logging.getLogger(__name__)
_DRY_RUN = False
def set_is_dry_run(is_dry_run: bool):
@ -10,4 +12,4 @@ def run_op(op: str):
if _DRY_RUN:
return subprocess.run(op, shell=True)
else:
logging.info(f"Dry Run | {op}")
logger.info(f"Dry Run | {op}")

View file

@ -3,6 +3,9 @@ import logging
from machine_admin.user import User
from machine_admin.machine import Machine
from machine_admin.util import set_is_dry_run
logger = logging.getLogger(__name__)
if __name__ == "__main__":
@ -10,14 +13,18 @@ if __name__ == "__main__":
prog='MachineSetup',
description='Scripts for machine provisioning')
parser.add_argument('--username',
parser.add_argument('username',
help="Name of the default non-root user")
parser.add_argument('--dry_run',
help="If set then don't change the system state - used for testing.",
default = False)
args = parser.parse_args()
set_is_dry_run(args.dry_run)
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
user = User(args.username, has_sudo=True)
machine = Machine(user)
machine.setup()

1
test/requirements.txt Normal file
View file

@ -0,0 +1 @@
pytest

View file

@ -0,0 +1,11 @@
from machine_admin.util import set_is_dry_run
from machine_admin.user import User
from machine_admin.machine import Machine
set_is_dry_run(True)
def test_machine_setup():
user = User("test_user", has_sudo=True)
machine = Machine(user)
machine.setup()