|
1 | 1 | import six
|
2 | 2 |
|
3 | 3 |
|
4 |
| -class Config(object): |
5 |
| - ''' |
6 |
| - The default/base configuration options for a pyinfra deploy. |
7 |
| - ''' |
8 |
| - |
9 |
| - state = None |
10 |
| - |
| 4 | +config_defaults = { |
11 | 5 | # % of hosts which have to fail for all operations to stop
|
12 |
| - FAIL_PERCENT = None |
| 6 | + 'FAIL_PERCENT': None, |
13 | 7 |
|
14 | 8 | # Seconds to timeout SSH connections
|
15 |
| - CONNECT_TIMEOUT = 10 |
| 9 | + 'CONNECT_TIMEOUT': 10, |
16 | 10 |
|
17 | 11 | # Temporary directory (on the remote side) to use for caching any files/downloads
|
18 |
| - TEMP_DIR = '/tmp' |
| 12 | + 'TEMP_DIR': '/tmp', |
19 | 13 |
|
20 | 14 | # Gevent pool size (defaults to #of target hosts)
|
21 |
| - PARALLEL = None |
| 15 | + 'PARALLEL': None, |
22 | 16 |
|
23 | 17 | # Specify the required pyinfra version (using PEP 440 setuptools specifier)
|
24 |
| - REQUIRE_PYINFRA_VERSION = None |
| 18 | + 'REQUIRE_PYINFRA_VERSION': None, |
25 | 19 | # Specify any required packages (either using PEP 440 or a requirements file)
|
26 | 20 | # Note: this can also include pyinfra, potentially replacing REQUIRE_PYINFRA_VERSION
|
27 |
| - REQUIRE_PACKAGES = None |
| 21 | + 'REQUIRE_PACKAGES': None, |
28 | 22 |
|
29 | 23 | # COMPAT w/<1.1
|
30 | 24 | # TODO: remove this in favour of above at v2
|
31 | 25 | # Specify a minimum required pyinfra version for a deploy
|
32 |
| - MIN_PYINFRA_VERSION = None |
| 26 | + 'MIN_PYINFRA_VERSION': None, |
33 | 27 |
|
34 | 28 | # All these can be overridden inside individual operation calls:
|
35 | 29 |
|
36 | 30 | # Switch to this user (from ssh_user) using su before executing operations
|
37 |
| - SU_USER = None |
38 |
| - USE_SU_LOGIN = False |
39 |
| - SU_SHELL = None |
40 |
| - PRESERVE_SU_ENV = False |
| 31 | + 'SU_USER': None, |
| 32 | + 'USE_SU_LOGIN': False, |
| 33 | + 'SU_SHELL': None, |
| 34 | + 'PRESERVE_SU_ENV': False, |
41 | 35 |
|
42 | 36 | # Use sudo and optional user
|
43 |
| - SUDO = False |
44 |
| - SUDO_USER = None |
45 |
| - PRESERVE_SUDO_ENV = False |
46 |
| - USE_SUDO_LOGIN = False |
47 |
| - USE_SUDO_PASSWORD = False |
| 37 | + 'SUDO': False, |
| 38 | + 'SUDO_USER': None, |
| 39 | + 'PRESERVE_SUDO_ENV': False, |
| 40 | + 'USE_SUDO_LOGIN': False, |
| 41 | + 'USE_SUDO_PASSWORD': False, |
48 | 42 |
|
49 | 43 | # Use doas and optional user
|
50 |
| - DOAS = False |
51 |
| - DOAS_USER = None |
| 44 | + 'DOAS': False, |
| 45 | + 'DOAS_USER': None, |
52 | 46 |
|
53 | 47 | # Only show errors, but don't count as failure
|
54 |
| - IGNORE_ERRORS = False |
| 48 | + 'IGNORE_ERRORS': False, |
55 | 49 |
|
56 | 50 | # Shell to use to execute commands
|
57 |
| - SHELL = None |
| 51 | + 'SHELL': None, |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +class Config(object): |
| 56 | + ''' |
| 57 | + The default/base configuration options for a pyinfra deploy. |
| 58 | + ''' |
| 59 | + |
| 60 | + state = None |
58 | 61 |
|
59 | 62 | def __init__(self, **kwargs):
|
60 | 63 | # Always apply some env
|
61 | 64 | env = kwargs.pop('ENV', {})
|
62 | 65 | self.ENV = env
|
63 | 66 |
|
64 |
| - # Apply kwargs |
65 |
| - for key, value in six.iteritems(kwargs): |
| 67 | + config = config_defaults.copy() |
| 68 | + config.update(kwargs) |
| 69 | + |
| 70 | + for key, value in six.iteritems(config): |
66 | 71 | setattr(self, key, value)
|
| 72 | + |
| 73 | + def get_current_state(self): |
| 74 | + return [ |
| 75 | + (key, getattr(self, key)) |
| 76 | + for key in config_defaults.keys() |
| 77 | + ] |
| 78 | + |
| 79 | + def set_current_state(self, config_state): |
| 80 | + for key, value in config_state: |
| 81 | + setattr(self, key, value) |
| 82 | + |
| 83 | + def lock_current_sate(self): |
| 84 | + self._locked_config = self.get_current_state() |
| 85 | + |
| 86 | + def reset_locked_state(self): |
| 87 | + self.set_current_state(self._locked_config) |
0 commit comments