Skip to content

Commit b03bed6

Browse files
committedMar 7, 2017
Change autorep to be a proper module
1 parent d57dbfe commit b03bed6

File tree

10 files changed

+222
-228
lines changed

10 files changed

+222
-228
lines changed
 

‎Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ COPY requirements.txt ./
66
RUN apt update && apt upgrade -y --force-yes && apt install -y --force-yes libsasl2-dev libldap2-dev libssl-dev && rm -rf /var/cache/apt/archives
77
RUN pip install --no-cache-dir -r requirements.txt
88

9-
COPY ./autorep/ ./
9+
COPY ./autorep/ ./autorep
10+
COPY ./envconfig.py /usr/local/lib/python3.6/site-packages/envconfig.py
1011

1112
ENTRYPOINT [ "python3" ]
1213

13-
CMD [ "./autorep.py" ]
14+
CMD [ "-m", "autorep" ]

‎autorep/__init__.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
""" Module
2+
"""
3+
import time
4+
import sys
5+
import enum
6+
7+
8+
class ReplicationMode(enum.Enum):
9+
""" Class
10+
"""
11+
12+
OneWay = "oneway"
13+
Remove = "remove"
14+
15+
16+
class ReplicationDefinition(object):
17+
""" Class
18+
"""
19+
20+
def __init__(self, source, target, mode=ReplicationMode.OneWay):
21+
if not isinstance(mode, ReplicationMode):
22+
raise ValueError("Argument mode must be instance of ReplicationMode")
23+
if not isinstance(source, str):
24+
raise ValueError("Argument source must be a string")
25+
if not isinstance(target, str):
26+
raise ValueError("Argument target must be a string")
27+
28+
self.source = source
29+
self.target = target
30+
self.mode = mode
31+
32+
def __str__(self):
33+
return "%s from: \"%s\" to \"%s\" " % (self.mode, self.source, self.target)
34+
35+
36+
class ReplicationList(object):
37+
""" Class
38+
"""
39+
40+
def __init__(self, *args):
41+
pos = 0
42+
if len(args) == 1 and isinstance(args, tuple):
43+
args = args[0]
44+
self.replications = []
45+
for arg in args:
46+
pos += 1
47+
try:
48+
self.add(arg)
49+
except ValueError as exce:
50+
raise ValueError("At position %i: %s" % (pos, exce))
51+
def __str__(self):
52+
ret = "ReplicationList("
53+
54+
for each in self.replications:
55+
ret = ret + ("(%s), " % str(each))
56+
57+
ret = ret + ")"
58+
return ret
59+
60+
def __iter__(self):
61+
return self.replications.__iter__()
62+
63+
def add(self, repdef):
64+
""" pack
65+
"""
66+
if not isinstance(repdef, ReplicationDefinition):
67+
raise ValueError("Argument is not a ReplicationDefinition instance")
68+
69+
self.replications.append(repdef)
70+
71+
def pack(self):
72+
""" pack
73+
"""
74+
pass
75+
76+
77+
def main_loop(conf, sourcedriver, targetdriver, graphdriver, clusteringdriver):
78+
""" pack
79+
"""
80+
while True:
81+
try:
82+
hostlist = sourcedriver.get_hostlist()
83+
currentstate = targetdriver.get_state(hostlist)
84+
localchanges = graphdriver.get_changes(hostlist, currentstate)
85+
clusterchanges = clusteringdriver.get_cluster_changes(localchanges)
86+
targetdriver.apply_changes(hostlist, currentstate, clusterchanges)
87+
except Exception as exce:
88+
print("Failed to update state: " + str(exce))
89+
raise exce
90+
91+
sys.stdout.flush()
92+
time.sleep(conf['AUTOREP_UPDATE_PERIOD'])
93+

‎autorep/__main__.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import sys
2+
import envconfig
3+
import importlib
4+
import inspect
5+
import autorep
6+
7+
from autorep import target, source, clustering, replication
8+
9+
def get_driver_class(modulename, baseclass):
10+
""" pack
11+
"""
12+
mod = importlib.import_module(modulename)
13+
klass = None
14+
15+
for klassname in dir(mod):
16+
tmpklass = getattr(mod, klassname)
17+
if not inspect.isclass(tmpklass):
18+
continue
19+
if issubclass(tmpklass, baseclass) and not tmpklass == baseclass:
20+
if klass is not None:
21+
raise envconfig.InvalidConfig("Multiple candidate classes available in %s" %
22+
modulename)
23+
klass = tmpklass
24+
25+
return klass
26+
27+
env_vars_def = {
28+
'AUTOREP_TARGET_DRIVER_MODULE' : {
29+
envconfig.Options.parser: envconfig.Parser.String,
30+
envconfig.Options.required: lambda conf: True,
31+
},
32+
'AUTOREP_SOURCE_DRIVER_MODULE' : {
33+
envconfig.Options.parser: envconfig.Parser.String,
34+
envconfig.Options.required: lambda conf: True,
35+
},
36+
'AUTOREP_CLUSTERING_DRIVER_MODULE' : {
37+
envconfig.Options.parser: envconfig.Parser.String,
38+
envconfig.Options.required: lambda conf: True,
39+
},
40+
'AUTOREP_REPLICATION_GRAPH_DRIVER_MODULE' : {
41+
envconfig.Options.parser: envconfig.Parser.String,
42+
envconfig.Options.required: lambda conf: True,
43+
},
44+
'AUTOREP_UPDATE_PERIOD' : {
45+
envconfig.Options.parser: envconfig.Parser.Integer,
46+
envconfig.Options.required: lambda conf: True,
47+
},
48+
}
49+
50+
try:
51+
mainconf = envconfig.Parser.parse(env_vars_def)
52+
except envconfig.InvalidConfig as exce:
53+
print(exce)
54+
sys.exit(1)
55+
56+
try:
57+
driverclass = get_driver_class('autorep.target.' + mainconf['AUTOREP_TARGET_DRIVER_MODULE'],
58+
target.AbstractTargetDriver)
59+
if driverclass is None:
60+
raise envconfig.InvalidConfig("No valid driver found in %s" % 'target.' +
61+
mainconf['AUTOREP_TARGET_DRIVER_MODULE'])
62+
targetdriver = driverclass()
63+
except ImportError:
64+
print("Target Driver \"%s\" does not exist" % mainconf['AUTOREP_TARGET_DRIVER_MODULE'])
65+
sys.exit(2)
66+
except envconfig.InvalidConfig as exce:
67+
print(exce)
68+
exit(3)
69+
70+
try:
71+
driverclass = get_driver_class('autorep.source.' + mainconf['AUTOREP_SOURCE_DRIVER_MODULE'],
72+
source.AbstractSourceDriver)
73+
if driverclass is None:
74+
raise envconfig.InvalidConfig("No valid driver found in %s" % 'source.' +
75+
mainconf['AUTOREP_SOURCE_DRIVER_MODULE'])
76+
sourcedriver = driverclass()
77+
except ImportError as exce:
78+
print("Source Driver \"%s\" does not exist" % mainconf['AUTOREP_SOURCE_DRIVER_MODULE'])
79+
print(exce)
80+
sys.exit(4)
81+
except envconfig.InvalidConfig as exce:
82+
print(exce)
83+
exit(5)
84+
85+
try:
86+
driverclass = get_driver_class('autorep.clustering.' + mainconf['AUTOREP_CLUSTERING_DRIVER_MODULE'],
87+
clustering.AbstractClusteringDecider)
88+
if driverclass is None:
89+
raise envconfig.InvalidConfig("No valid driver found in %s" % 'clustering.' +
90+
mainconf['AUTOREP_CLUSTERING_DRIVER_MODULE'])
91+
clusteringdriver = driverclass()
92+
except ImportError:
93+
print("Clustering Driver \"%s\" does not exist" %
94+
mainconf['AUTOREP_CLUSTERING_DRIVER_MODULE'])
95+
sys.exit(6)
96+
except envconfig.InvalidConfig as exce:
97+
print(exce)
98+
exit(7)
99+
100+
try:
101+
driverclass = get_driver_class('autorep.replication.' +
102+
mainconf['AUTOREP_REPLICATION_GRAPH_DRIVER_MODULE'],
103+
replication.AbstractReplicationGraphBuilder)
104+
if driverclass is None:
105+
raise envconfig.InvalidConfig("No valid driver found in %s" % 'replication.' +
106+
mainconf['AUTOREP_REPLICATION_GRAPH_DRIVER_MODULE'])
107+
graphdriver = driverclass()
108+
except ImportError as exce:
109+
print("Replication Graph Driver \"%s\" does not exist" %
110+
mainconf['AUTOREP_REPLICATION_GRAPH_DRIVER_MODULE'])
111+
print(exce)
112+
sys.exit(8)
113+
except envconfig.InvalidConfig as exce:
114+
print(exce)
115+
exit(9)
116+
117+
autorep.main_loop(mainconf, sourcedriver, targetdriver, graphdriver, clusteringdriver)
118+

‎autorep/autorep.py

-213
This file was deleted.

‎autorep/clustering/standalone.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import clustering
2-
import autorep
1+
import autorep.clustering
32

43

5-
class StandaloneDecider(clustering.AbstractClusteringDecider):
4+
class StandaloneDecider(autorep.clustering.AbstractClusteringDecider):
65

76
def get_cluster_changes(self, localchanges):
87
return localchanges

‎autorep/replication/fullmesh.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import replication
2-
import autorep
31
import networkx as nx
42

3+
import autorep
4+
import autorep.replication
5+
56

6-
class AllMasterGraphBuilder(replication.AbstractReplicationGraphBuilder):
7+
class AllMasterGraphBuilder(autorep.replication.AbstractReplicationGraphBuilder):
78

89
def get_changes(self, hostlist, currentstate):
910
dg=nx.DiGraph()

‎autorep/source/rancher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from . import AbstractSourceDriver
2+
import envconfig
23

3-
from autorep import envconfig
44
import urllib.request
55
import json
66

‎autorep/target/389ds.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from autorep import envconfig
1+
import envconfig
22
import ldap
33
import ldap.modlist as modlist
44
import tempfile

‎autorep/target/test.py

-5
This file was deleted.

‎autorep/envconfig.py ‎envconfig.py

File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.