Skip to content

Commit

Permalink
Add support for cloud environments in settyngs.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
mijaros committed May 11, 2022
1 parent 533b90f commit 647a226
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 42 deletions.
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,34 @@ The configuration has following structure:
default:
cloud:
openstack:
base_domain: ''
certificate_bundle_file: ''
pull_secret_file: ''
ssh_key_file: ''
osp_cloud: ''
osp_base_flavor: ''
network_list: []
base_env: env1
environments:
- name: env1
base_domain: ''
certificate_bundle_file: ''
pull_secret_file: ''
ssh_key_file: ''
osp_cloud: ''
osp_base_flavor: ''
network_list: []
- name: env2
base_domain: ''
certificate_bundle_file: ''
pull_secret_file: ''
ssh_key_file: ''
osp_cloud: ''
osp_base_flavor: ''
network_list: []
aws:
base_domain: ''
pull_secret_file: ''
certificate_bundle_file: ''
ssh_key_file: ''
worker_flavor: ''
list_of_regions: []
base_env: default
environments:
- name: default
base_domain: ''
pull_secret_file: ''
certificate_bundle_file: ''
ssh_key_file: ''
worker_flavor: ''
list_of_regions: []
dns:
route53:
ttl: 0
Expand Down
34 changes: 5 additions & 29 deletions osia/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from typing import List
import coloredlogs

from dynaconf import settings
from .installer import install_cluster, delete_cluster, storage, download_installer
from .config import read_config


def _identity(in_attr: str) -> str:
Expand All @@ -36,6 +36,7 @@ def _read_list(in_str: str) -> List[str]:
'common': {
'cloud': {'help': 'Cloud provider to be used.', 'type': str,
'choices': ['openstack', 'aws']},
'cloud_environment': {'help': 'Environment of cloud to be used.', 'type': str},
'dns_provider': {'help': 'Provider of dns used with openstack cloud',
'type': str, 'choices': ['nsupdate', 'route53']}
},
Expand Down Expand Up @@ -92,36 +93,11 @@ def _resolve_installer(from_args):


def _merge_dictionaries(from_args):
result = {'cloud': None,
'dns': None,
'installer': _resolve_installer(from_args),
'cloud_name': None,
'cluster_name': from_args.cluster_name}
if not from_args.__contains__('cloud'):
return result
defaults = settings.as_dict()
if from_args.dns_provider is not None:
result['dns'] = {'provider': from_args.dns_provider,
'conf': defaults['DNS'][from_args.dns_provider]}
result['dns']['conf'].update(
{j[4:]: i['proc'](vars(from_args)[j])
for j, i in ARGUMENTS['dns'].items()
if vars(from_args)[j] is not None}
)
if from_args.cloud is not None:
result['cloud_name'] = from_args.cloud
result['cloud'] = defaults['CLOUD'][from_args.cloud]
result['cloud'].update(
{j: i['proc'](vars(from_args)[j]) for j, i in ARGUMENTS['install'].items()
if vars(from_args)[j] is not None}
)
result = read_config(from_args, ARGUMENTS)
result["installer"] = _resolve_installer(from_args)
if result.get('cloud'):
# pylint: disable=unsupported-assignment-operation
result['cloud']['installer'] = result['installer']
if result['dns'] is not None:
result['dns']['conf'].update({
'cluster_name': from_args.cluster_name,
'base_domain': result['cloud']['base_domain']
})
return result


Expand Down
3 changes: 3 additions & 0 deletions osia/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Module provides access to configuration via Dynaconf"""
from .config import read_config, settings
__all__ = ['read_config', 'settings']
75 changes: 75 additions & 0 deletions osia/config/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Module implements merging of default configuration obtained via Dynaconf
with command line arguments.
"""
import argparse
import logging
import warnings
from typing import Dict, Optional

from dynaconf import Dynaconf

settings = Dynaconf(
environments=True,
lowercase_read=False,
load_dotenv=True,
settings_files=[name + end for name in ["settings", ".secrets"] for end in [".yaml", ".yml"]]
)


def _resolve_cloud_name(args: argparse.Namespace) -> Optional[Dict]:
defaults = settings.as_dict()

if defaults['CLOUD'][args.cloud].get('environments', None) is None:
warnings.warn('[DEPRECATION WARNING] The structure of settings.yaml is changed, '
'please use environments list and default_env option. This behavior will be '
'removed in future releases.')
return defaults['CLOUD'][args.cloud]
default_env = defaults['CLOUD'][args.cloud].get('cloud_env', None) \
if args.cloud_environment is None else \
args.cloud_environment
if default_env is None:
logging.error("Couldn't resolve default environment")
raise Exception("Invalid environment setup, base_env is missing")
for env in defaults['CLOUD'][args.cloud]['environments']:
if env['name'] == default_env:
return env
logging.debug("No environment found, maybe all variables are passed from command line")
return None


def read_config(args: argparse.Namespace, default_args: Dict) -> Dict:
"""
Reads config from Dynaconf and merges it with arguments provided via commandline.
"""
result = {'cloud': None,
'dns': None,
'cloud_name': None,
'cluster_name': args.cluster_name}
if not args.__contains__('cloud'):
return result
defaults = settings.as_dict()

if args.dns_provider is not None:
result['dns'] = {'provider': args.dns_provider,
'conf': defaults['DNS'][args.dns_provider]}
result['dns']['conf'].update(
{j[4:]: i['proc'](vars(args)[j])
for j, i in default_args['dns'].items()
if vars(args)[j] is not None}
)
if args.cloud is not None:
cloud_defaults = _resolve_cloud_name(args)
result['cloud_name'] = args.cloud
result['cloud'] = cloud_defaults
result['cloud'].update(
{j: i['proc'](vars(args)[j]) for j, i in default_args['install'].items()
if vars(args)[j] is not None}
)

if result['dns'] is not None:
result['dns']['conf'].update({
'cluster_name': args.cluster_name,
'base_domain': result['cloud']['base_domain']
})
return result

0 comments on commit 647a226

Please sign in to comment.