Pass services through config

This commit is contained in:
Mikhail Gordeev 2019-04-11 01:17:56 +03:00
parent 27ee94a248
commit 7d70c6916f
2 changed files with 72 additions and 13 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
from typing import List, Optional from typing import Dict, List, Optional
import argparse import argparse
import contextlib import contextlib
@ -34,6 +34,7 @@ class CB:
self.system_datadir = system_datadir self.system_datadir = system_datadir
self.date = datetime.date.today().strftime('%Y%m%d') self.date = datetime.date.today().strftime('%Y%m%d')
self.service_default_state = 'enabled'
self.ensure_dirs() self.ensure_dirs()
logging.basicConfig( logging.basicConfig(
@ -64,6 +65,7 @@ class CB:
self.log_level = getattr(logging, cfg.get('log_level', 'INFO').upper()) self.log_level = getattr(logging, cfg.get('log_level', 'INFO').upper())
self._packages = cfg.get('packages', {}) self._packages = cfg.get('packages', {})
self._services = cfg.get('services', {})
try: try:
self._remote = os.path.expanduser(cfg['remote']) self._remote = os.path.expanduser(cfg['remote'])
@ -156,6 +158,9 @@ class CB:
def ensure_mkimage_profiles(self, update: bool = False) -> None: def ensure_mkimage_profiles(self, update: bool = False) -> None:
"""Checks that mkimage-profiles exists or clones it""" """Checks that mkimage-profiles exists or clones it"""
def add_rule(variable: str, value: str) -> str:
return f'\n\t@$(call add,{variable},{value})'
url = self.mkimage_profiles_git url = self.mkimage_profiles_git
if url is None: if url is None:
url = ( url = (
@ -191,9 +196,15 @@ class CB:
rules = [branding] rules = [branding]
for package in self.packages(image, branch): for package in self.packages(image, branch):
rules.append( rules.append(add_rule('BASE_PACKAGES', package))
f'\n\t@$(call add,BASE_PACKAGES,{package})'
) for service in self.enabled_services(image, branch):
rules.append(add_rule('DEFAULT_SERVICES_ENABLE',
service))
for service in self.disabled_services(image, branch):
rules.append(add_rule('DEFAULT_SERVICES_DISABLE',
service))
rules_s = ''.join(rules) rules_s = ''.join(rules)
s = f'{target}_{ebranch}: {requires_s}; @:{rules_s}' s = f'{target}_{ebranch}: {requires_s}; @:{rules_s}'
@ -229,23 +240,63 @@ class CB:
def skip_arch(self, image: str, arch: str) -> bool: def skip_arch(self, image: str, arch: str) -> bool:
return arch in self._images[image].get('skip_arches', []) return arch in self._images[image].get('skip_arches', [])
def packages(self, image: str, branch: str) -> List[str]: def get_items(
packages = [] self,
data: Dict,
image: str,
branch: str,
state_re: str = None,
default_state: str = None,
) -> List[str]:
items = []
for package, constraints in self._packages.items(): if state_re is None:
if image in constraints.get('exclude_images', []): state_re = ''
continue if default_state is None:
if branch in constraints.get('exclude_branches', []): default_state = state_re
for item, constraints in data.items():
if (
image in constraints.get('exclude_images', [])
or branch in constraints.get('exclude_branches', [])
):
continue continue
# Empty means no constraint: e.g. all images # Empty means no constraint: e.g. all images
images = constraints.get('images', [image]) images = constraints.get('images', [image])
branches = constraints.get('branch', [branch]) branches = constraints.get('branch', [branch])
if image in images and branch in branches: state = constraints.get('state', default_state)
packages.append(package)
return packages if (
image in images
and branch in branches
and re.match(state_re, state)
):
items.append(item)
return items
def packages(self, image: str, branch: str) -> List[str]:
return self.get_items(self._packages, image, branch)
def enabled_services(self, image: str, branch: str) -> List[str]:
return self.get_items(
self._services,
image,
branch,
'enabled?',
self.service_default_state,
)
def disabled_services(self, image: str, branch: str) -> List[str]:
return self.get_items(
self._services,
image,
branch,
'disabled?',
self.service_default_state,
)
def build_tarball( def build_tarball(
self, self,

View File

@ -30,6 +30,14 @@ branches:
- x86_64 - x86_64
branding: alt-starterkit branding: alt-starterkit
# services:
# sshd:
# images:
# - opennebula
# exclude_branches:
# - Sisyphus
# state: disable
# packages: # packages:
# gosu: # gosu:
# images: # images: