Last active
August 29, 2015 14:11
-
-
Save dshulyak/013a62126f5ae63d7ee2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Expression parser based on some templater | |
Pros: | |
- it is not a new language | |
- it has very strict api, so atleast we can try to guarantee its stability | |
- complex abstract logic can be hidden in simple python methods | |
Stements will be expressed in the form of: | |
api.cluster_status == 'operational' | |
api.role_in_deployment('ceph-osd') | |
api.role_in_cluster('zabbix-server') | |
api.cluster_status == 'new' and api.nodes_count() > 10 | |
""" | |
import mock | |
import jinja2 | |
class ExpressionApi(object): | |
def __init__(self, cluster, nodes): | |
self.cluster = cluster | |
self.nodes = nodes | |
def role_in_deployment(self, role): | |
for node in self.nodes: | |
if role in node.roles: | |
return True | |
return False | |
def role_in_cluster(self, role): | |
for node in self.cluser.nodes: | |
if role in node.roles: | |
return True | |
return False | |
def nodes_count(self): | |
return len(self.nodes) | |
@property | |
def cluster_status(self): | |
return self.cluster.status | |
cluster = mock.Mock() | |
cluster.status = 'operational' | |
nodes = [mock.Mock()]*8 | |
API = ExpressionApi(cluster, nodes) | |
env = jinja2.Environment() | |
expr = env.compile_expression("api.cluster_status == 'operational' and api.nodes_count() < 9") | |
print expr(api=API) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
api.cluster_status == 'operational' and api.role_in_deployment('compute')