Skip to content

Commit 647a226

Browse files
committed
Add support for cloud environments in settyngs.yaml
1 parent 533b90f commit 647a226

File tree

4 files changed

+110
-42
lines changed

4 files changed

+110
-42
lines changed

Diff for: README.md

+27-13
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,34 @@ The configuration has following structure:
4848
default:
4949
cloud:
5050
openstack:
51-
base_domain: ''
52-
certificate_bundle_file: ''
53-
pull_secret_file: ''
54-
ssh_key_file: ''
55-
osp_cloud: ''
56-
osp_base_flavor: ''
57-
network_list: []
51+
base_env: env1
52+
environments:
53+
- name: env1
54+
base_domain: ''
55+
certificate_bundle_file: ''
56+
pull_secret_file: ''
57+
ssh_key_file: ''
58+
osp_cloud: ''
59+
osp_base_flavor: ''
60+
network_list: []
61+
- name: env2
62+
base_domain: ''
63+
certificate_bundle_file: ''
64+
pull_secret_file: ''
65+
ssh_key_file: ''
66+
osp_cloud: ''
67+
osp_base_flavor: ''
68+
network_list: []
5869
aws:
59-
base_domain: ''
60-
pull_secret_file: ''
61-
certificate_bundle_file: ''
62-
ssh_key_file: ''
63-
worker_flavor: ''
64-
list_of_regions: []
70+
base_env: default
71+
environments:
72+
- name: default
73+
base_domain: ''
74+
pull_secret_file: ''
75+
certificate_bundle_file: ''
76+
ssh_key_file: ''
77+
worker_flavor: ''
78+
list_of_regions: []
6579
dns:
6680
route53:
6781
ttl: 0

Diff for: osia/cli.py

+5-29
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from typing import List
2121
import coloredlogs
2222

23-
from dynaconf import settings
2423
from .installer import install_cluster, delete_cluster, storage, download_installer
24+
from .config import read_config
2525

2626

2727
def _identity(in_attr: str) -> str:
@@ -36,6 +36,7 @@ def _read_list(in_str: str) -> List[str]:
3636
'common': {
3737
'cloud': {'help': 'Cloud provider to be used.', 'type': str,
3838
'choices': ['openstack', 'aws']},
39+
'cloud_environment': {'help': 'Environment of cloud to be used.', 'type': str},
3940
'dns_provider': {'help': 'Provider of dns used with openstack cloud',
4041
'type': str, 'choices': ['nsupdate', 'route53']}
4142
},
@@ -92,36 +93,11 @@ def _resolve_installer(from_args):
9293

9394

9495
def _merge_dictionaries(from_args):
95-
result = {'cloud': None,
96-
'dns': None,
97-
'installer': _resolve_installer(from_args),
98-
'cloud_name': None,
99-
'cluster_name': from_args.cluster_name}
100-
if not from_args.__contains__('cloud'):
101-
return result
102-
defaults = settings.as_dict()
103-
if from_args.dns_provider is not None:
104-
result['dns'] = {'provider': from_args.dns_provider,
105-
'conf': defaults['DNS'][from_args.dns_provider]}
106-
result['dns']['conf'].update(
107-
{j[4:]: i['proc'](vars(from_args)[j])
108-
for j, i in ARGUMENTS['dns'].items()
109-
if vars(from_args)[j] is not None}
110-
)
111-
if from_args.cloud is not None:
112-
result['cloud_name'] = from_args.cloud
113-
result['cloud'] = defaults['CLOUD'][from_args.cloud]
114-
result['cloud'].update(
115-
{j: i['proc'](vars(from_args)[j]) for j, i in ARGUMENTS['install'].items()
116-
if vars(from_args)[j] is not None}
117-
)
96+
result = read_config(from_args, ARGUMENTS)
97+
result["installer"] = _resolve_installer(from_args)
98+
if result.get('cloud'):
11899
# pylint: disable=unsupported-assignment-operation
119100
result['cloud']['installer'] = result['installer']
120-
if result['dns'] is not None:
121-
result['dns']['conf'].update({
122-
'cluster_name': from_args.cluster_name,
123-
'base_domain': result['cloud']['base_domain']
124-
})
125101
return result
126102

127103

Diff for: osia/config/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""Module provides access to configuration via Dynaconf"""
2+
from .config import read_config, settings
3+
__all__ = ['read_config', 'settings']

Diff for: osia/config/config.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Module implements merging of default configuration obtained via Dynaconf
3+
with command line arguments.
4+
"""
5+
import argparse
6+
import logging
7+
import warnings
8+
from typing import Dict, Optional
9+
10+
from dynaconf import Dynaconf
11+
12+
settings = Dynaconf(
13+
environments=True,
14+
lowercase_read=False,
15+
load_dotenv=True,
16+
settings_files=[name + end for name in ["settings", ".secrets"] for end in [".yaml", ".yml"]]
17+
)
18+
19+
20+
def _resolve_cloud_name(args: argparse.Namespace) -> Optional[Dict]:
21+
defaults = settings.as_dict()
22+
23+
if defaults['CLOUD'][args.cloud].get('environments', None) is None:
24+
warnings.warn('[DEPRECATION WARNING] The structure of settings.yaml is changed, '
25+
'please use environments list and default_env option. This behavior will be '
26+
'removed in future releases.')
27+
return defaults['CLOUD'][args.cloud]
28+
default_env = defaults['CLOUD'][args.cloud].get('cloud_env', None) \
29+
if args.cloud_environment is None else \
30+
args.cloud_environment
31+
if default_env is None:
32+
logging.error("Couldn't resolve default environment")
33+
raise Exception("Invalid environment setup, base_env is missing")
34+
for env in defaults['CLOUD'][args.cloud]['environments']:
35+
if env['name'] == default_env:
36+
return env
37+
logging.debug("No environment found, maybe all variables are passed from command line")
38+
return None
39+
40+
41+
def read_config(args: argparse.Namespace, default_args: Dict) -> Dict:
42+
"""
43+
Reads config from Dynaconf and merges it with arguments provided via commandline.
44+
"""
45+
result = {'cloud': None,
46+
'dns': None,
47+
'cloud_name': None,
48+
'cluster_name': args.cluster_name}
49+
if not args.__contains__('cloud'):
50+
return result
51+
defaults = settings.as_dict()
52+
53+
if args.dns_provider is not None:
54+
result['dns'] = {'provider': args.dns_provider,
55+
'conf': defaults['DNS'][args.dns_provider]}
56+
result['dns']['conf'].update(
57+
{j[4:]: i['proc'](vars(args)[j])
58+
for j, i in default_args['dns'].items()
59+
if vars(args)[j] is not None}
60+
)
61+
if args.cloud is not None:
62+
cloud_defaults = _resolve_cloud_name(args)
63+
result['cloud_name'] = args.cloud
64+
result['cloud'] = cloud_defaults
65+
result['cloud'].update(
66+
{j: i['proc'](vars(args)[j]) for j, i in default_args['install'].items()
67+
if vars(args)[j] is not None}
68+
)
69+
70+
if result['dns'] is not None:
71+
result['dns']['conf'].update({
72+
'cluster_name': args.cluster_name,
73+
'base_domain': result['cloud']['base_domain']
74+
})
75+
return result

0 commit comments

Comments
 (0)