-
Notifications
You must be signed in to change notification settings - Fork 656
/
state.py
145 lines (114 loc) · 4.31 KB
/
state.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright (c) 2015-2018 Cisco Systems, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import os
from molecule import logger
from molecule import util
LOG = logger.get_logger(__name__)
VALID_KEYS = ['created', 'converged', 'driver', 'prepared', 'run_uuid', 'is_parallel']
class InvalidState(Exception):
"""
Exception class raised when an error occurs in :class:`.State`.
"""
pass
class State(object):
"""
A class which manages the state file. Intended to be used as a singleton
throughout a given Molecule config. The initial state is serialized to
disk if the file does not exist, otherwise is deserialized from the
existing state file. Changes made to the object are immediately
serialized.
State is not a top level option in Molecule's config. It's purpose is for
bookkeeping, and each :class:`.Config` object has a reference to a State_
object.
.. note::
Currently, it's use is significantly smaller than it was in v1 of
Molecule.
"""
def __init__(self, config):
"""
Initialize a new state class and returns None.
:param config: An instance of a Molecule config.
:returns: None
"""
self._config = config
self._state_file = self._get_state_file()
self._data = self._get_data()
self._write_state_file()
def marshal(func):
def wrapper(self, *args, **kwargs):
func(self, *args, **kwargs)
self._write_state_file()
return wrapper
@property
def state_file(self):
return self._state_file
@property
def converged(self):
return self._data.get('converged')
@property
def created(self):
return self._data.get('created')
@property
def driver(self):
return self._data.get('driver')
@property
def prepared(self):
return self._data.get('prepared')
@property
def run_uuid(self):
return self._data.get('run_uuid')
@property
def is_parallel(self):
return self._data.get('is_parallel')
@marshal
def reset(self):
self._data = self._default_data()
@marshal
def change_state(self, key, value):
"""
Changes the state of the instance data with the given
``key`` and the provided ``value``.
Wrapping with a decorator is probably not necessary.
:param key: A ``str`` containing the key to update
:param value: A value to change the ``key`` to
:return: None
"""
if key not in VALID_KEYS:
raise InvalidState
self._data[key] = value
def _get_data(self):
if os.path.isfile(self.state_file):
return self._load_file()
return self._default_data()
def _default_data(self):
return {
'converged': False,
'created': False,
'driver': None,
'prepared': None,
'run_uuid': self._config._run_uuid,
'is_parallel': self._config.is_parallel,
}
def _load_file(self):
return util.safe_load_file(self.state_file)
def _write_state_file(self):
util.write_file(self.state_file, util.safe_dump(self._data))
def _get_state_file(self):
return os.path.join(self._config.scenario.ephemeral_directory, 'state.yml')