Skip to content
Snippets Groups Projects
Commit 483239d0 authored by Michael Loipführer's avatar Michael Loipführer
Browse files

fix errors

parent b0dafd48
No related branches found
No related tags found
No related merge requests found
Pipeline #344 passed
import click import click
from lustmolch.config import DEFAULT_CONF_FILE, init_config from lustmolch.config import DEFAULT_CONF_FILE, init_config
from lustmolch.lustmolch import ( from lustmolch import lustmolch
list_containers,
add_user,
create_container,
remove_container,
remove_user,
update_containers
)
@click.group() @click.group()
...@@ -22,14 +15,14 @@ def cli(config_file: str): ...@@ -22,14 +15,14 @@ def cli(config_file: str):
@cli.command() @cli.command()
def list_containers(): def list_containers():
list_containers() lustmolch.list_containers()
@cli.command() @cli.command()
@click.option('--dry-run', is_flag=True, default=False) @click.option('--dry-run', is_flag=True, default=False)
@click.argument('name') @click.argument('name')
def create_container(dry_run: bool, name: str): def create_container(dry_run: bool, name: str):
create_container(dry_run, name) lustmolch.create_container(dry_run, name)
@cli.command() @cli.command()
...@@ -37,23 +30,23 @@ def create_container(dry_run: bool, name: str): ...@@ -37,23 +30,23 @@ def create_container(dry_run: bool, name: str):
@click.argument('name') @click.argument('name')
@click.argument('key') @click.argument('key')
def add_user(key_string: bool, name: str, key: str) -> None: def add_user(key_string: bool, name: str, key: str) -> None:
add_user(key_string, name, key) lustmolch.add_user(key_string, name, key)
@cli.command() @cli.command()
@click.argument('name') @click.argument('name')
def remove_user(name: str) -> None: def remove_user(name: str) -> None:
remove_user(name) lustmolch.remove_user(name)
@cli.command() @cli.command()
@click.option('--dry-run', is_flag=True, default=False) @click.option('--dry-run', is_flag=True, default=False)
def update_containers(dry_run: bool) -> None: def update_containers(dry_run: bool) -> None:
update_containers(dry_run) lustmolch.update_containers(dry_run)
@cli.command() @cli.command()
@click.option('--dry-run', is_flag=True, default=False) @click.option('--dry-run', is_flag=True, default=False)
@click.argument('name') @click.argument('name')
def remove_container(dry_run: bool, name: str) -> None: def remove_container(dry_run: bool, name: str) -> None:
remove_container(dry_run, name) lustmolch.remove_container(dry_run, name)
...@@ -14,7 +14,7 @@ DEFAULTS = { ...@@ -14,7 +14,7 @@ DEFAULTS = {
'ssh_start_port': 10022, 'ssh_start_port': 10022,
'ssh_port_increment': 1000, 'ssh_port_increment': 1000,
'host_ip': '141.84.69.235', 'host_ip': '141.84.69.235',
'ip_start_host': (192, 168, 0, 1), 'ip_start_host': '192.168.0.1',
'ip_subnet_length': 30, 'ip_subnet_length': 30,
'log_level': logging.INFO 'log_level': logging.INFO
} }
...@@ -35,15 +35,18 @@ class Config: ...@@ -35,15 +35,18 @@ class Config:
self.config[key] = value self.config[key] = value
def __getitem__(self, key: str) -> Any: def __getitem__(self, key: str) -> Any:
return self.config[key] return self.get(key)
def __setitem__(self, key: str, value: Any) -> Any: def __setitem__(self, key: str, value: Any) -> Any:
self.config[key] = value self.set(key, value)
def __delitem__(self, key): def __delitem__(self, key):
del self.config[key] del self.config[key]
def save(self): def save(self):
if not self.file_path.exists():
self.file_path.parent.mkdir(parents=True, exist_ok=True)
with self.file_path.open('w+') as f: with self.file_path.open('w+') as f:
json.dump(self.config, f, indent=2) json.dump(self.config, f, indent=2)
......
...@@ -10,7 +10,7 @@ from jinja2 import Environment, PackageLoader ...@@ -10,7 +10,7 @@ from jinja2 import Environment, PackageLoader
from .config import config, DEFAULT_TEMPLATE_DIR from .config import config, DEFAULT_TEMPLATE_DIR
logging.basicConfig(level=config['log_level']) logging.basicConfig(format='%(levelname)s:%(message)s', level=config['log_level'])
env = Environment(loader=PackageLoader('lustmolch', str(Path(__file__).parent.parent / 'templates'))) env = Environment(loader=PackageLoader('lustmolch', str(Path(__file__).parent.parent / 'templates')))
cfg_template = namedtuple('cfg_template', ['source', 'path', 'filename']) cfg_template = namedtuple('cfg_template', ['source', 'path', 'filename'])
...@@ -66,13 +66,13 @@ def next_ip_address(name: str) -> Tuple[str, str]: ...@@ -66,13 +66,13 @@ def next_ip_address(name: str) -> Tuple[str, str]:
return (c.get('ip_address_host').split('/')[0], return (c.get('ip_address_host').split('/')[0],
c.get('ip_address_container').split('/')[0]) c.get('ip_address_container').split('/')[0])
ip_host = list(config['ip_start_host']) ip_host = [int(ip) for ip in config['ip_start_host'].split('.')]
container_ips = [container['ip_address_host'].split('/')[0].split('.') container_ips = [container['ip_address_host'].split('/')[0].split('.')
for container in config['containers'].values()] for container in config['containers'].values()]
ip_host[2] = max([int(ip[2]) for ip in container_ips]) ip_host[2] = max([int(ip[2]) for ip in container_ips] + [ip_host[2]])
ip_host[3] = max([int(ip[3]) for ip in container_ips]) ip_host[3] = max([int(ip[3]) for ip in container_ips] + [ip_host[3]])
ip_host[3] += 4 ip_host[3] += 4
if ip_host[3] >= 254: if ip_host[3] >= 254:
...@@ -85,9 +85,9 @@ def next_ip_address(name: str) -> Tuple[str, str]: ...@@ -85,9 +85,9 @@ def next_ip_address(name: str) -> Tuple[str, str]:
ip_container = list(ip_host) ip_container = list(ip_host)
ip_container[3] += 1 ip_container[3] += 1
return ( return (
'.'.join( '.'.join(str(x) for x in ip_host),
str(x) for x in ip_host), '.'.join( '.'.join(str(x) for x in ip_container)
str(x) for x in ip_container)) )
def list_containers(): def list_containers():
...@@ -117,7 +117,7 @@ def create_container(dry_run, name): ...@@ -117,7 +117,7 @@ def create_container(dry_run, name):
'ssh_port': ssh_port, 'ssh_port': ssh_port,
'ip_address_host': ip_address_host, 'ip_address_host': ip_address_host,
'ip_address_container': ip_address_container, 'ip_address_container': ip_address_container,
'ip_subnet_length': config['ip_subnet_lenght'], 'ip_subnet_length': config['ip_subnet_length'],
'url': f'{name}.stusta.de', 'url': f'{name}.stusta.de',
'users': [] 'users': []
} }
...@@ -209,9 +209,13 @@ def add_user(key_string: bool, name: str, key: str) -> None: ...@@ -209,9 +209,13 @@ def add_user(key_string: bool, name: str, key: str) -> None:
def remove_user(name: str) -> None: def remove_user(name: str) -> None:
"""remove a user, doesn't remove the user from all containers""" """remove a user, doesn't remove the user from all containers"""
logging.info(f'Removing user {name}')
if name in config['users']: if name in config['users']:
del config['users'][name] del config['users'][name]
for container in config['containers']:
container['users'] = [user for user in container['users'] if user != name]
config.save() config.save()
......
from setuptools import setup from setuptools import setup
VERSION = '1.0.0' VERSION = '1.0.1'
AUTHOR = 'Michael Loipführer' AUTHOR = 'Michael Loipführer'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment