-
Notifications
You must be signed in to change notification settings - Fork 100
/
env_config_parser.py
76 lines (60 loc) · 3.04 KB
/
env_config_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
This file defines a data structure containing the environment variables that have been written to a file
(`config/pulumi/environment`). The values stored there are used to specify the environment when executing
operations using the Pulumi Automation API.
"""
import os
from typing import Optional, Mapping
from configparser import ConfigParser
import stack_config_parser
# Directory in which script is located
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
# Default path to the MARA environment file
DEFAULT_PATH = os.path.abspath(os.path.sep.join([SCRIPT_DIR, '..', '..', '..', 'config', 'pulumi', 'environment']))
# Default environment variables set for all Pulumi executions invoked by the Automation API
DEFAULT_ENV_VARS = {
'PULUMI_SKIP_UPDATE_CHECK': 'true'
}
class EnvConfig(dict):
"""Object containing environment variables used when executing operations with the Pulumi Automation API"""
_stack_config: Optional[stack_config_parser.PulumiStackConfig] = None
config_path: Optional[str] = None
def __init__(self,
env_vars: Mapping[str, str],
file_vars: Mapping[str, str],
stack_config: Optional[stack_config_parser.PulumiStackConfig] = None,
config_path: Optional[str] = None) -> None:
super().__init__()
self.update(DEFAULT_ENV_VARS)
self.update(env_vars)
self.update(file_vars)
self._stack_config = stack_config
self.config_path = config_path
def stack_name(self) -> str:
"""Returns the stack name used in the environment"""
return self.get('PULUMI_STACK')
def no_color(self) -> bool:
"""Returns a flag if color in the console is supported"""
return self.get('NO_COLOR') is not None
def pulumi_color_settings(self):
"""Returns a string indicating if console colors should be auto-detected or just disabled"""
if self.no_color():
return 'never'
else:
return 'auto'
def read(config_file_path: str = DEFAULT_PATH) -> EnvConfig:
"""Reads the contents of the specified file path into a new instance of `EnvConfig`.
:param config_file_path: path to environment variable file
:return: new instance of EnvConfig
"""
config_parser = ConfigParser()
config_parser.optionxform = lambda option: option
with open(config_file_path, 'r') as f:
# The Python configparser library is used to parse the file because it supports the KEY=VALUE syntax of the
# environment file. However, there is one exception; it requires the presence of a [main] section using the
# ini format style. In order avoid having to add a "[main]" string to the environment file, we spoof the
# presence of that section with this line below. It just prepends the string "[main]" before the contents of
# the environment file.
content = f'[main]{os.linesep}{f.read()}'
config_parser.read_string(content)
return EnvConfig(env_vars=os.environ, file_vars=config_parser['main'], config_path=config_file_path)