This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
forked from heyaaron/openmesher
-
Notifications
You must be signed in to change notification settings - Fork 2
/
openmesher.py
executable file
·192 lines (156 loc) · 6.36 KB
/
openmesher.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
import logging
import argparse
import random
from distutils.sysconfig import get_python_lib
from OpenMesher.interfaces import IOpenMesherConfigPlugin, IOpenMesherPackagePlugin, IOpenMesherDeployPlugin
from OpenMesher.lib import nested_dict_merge
from OpenMesher.linkmesh import create_link_mesh
from OpenMesher.tunnelobjects import Mesh
from yapsy.PluginManager import PluginManager
def main():
# Find and load plugins
pm = PluginManager()
libpath = '%s/OpenMesher/plugins' % (get_python_lib())
pm.setPluginPlaces([
'/usr/share/openmesher/plugins',
'~/.openmesher/plugins',
'./OpenMesher/plugins',
'./plugins',
libpath,
])
pm.setPluginInfoExtension('plugin')
pm.setCategoriesFilter({
'config': IOpenMesherConfigPlugin,
'package': IOpenMesherPackagePlugin,
'deploy': IOpenMesherDeployPlugin,
})
pm.collectPlugins()
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Configure, package, and deploy an OpenVPN mesh"
)
parser.add_argument('-r', '--router', action='append', help='Adds a router that can be a client and server')
parser.add_argument(
'-s',
'--server',
action='append',
help='Adds a router that can only act as a server, not a client.'
)
parser.add_argument(
'-c',
'--client',
action='append',
help='Adds a router than can only act as a client. For example, a router that is behind NAT and not '
'accessible by a public IP'
)
# BUG: Stupid argparse appends your switches to the default.
# parser.add_argument('-n', '--network', action='append', default=['10.99.99.0/24'])
parser.add_argument('-n', '--network', action='append', required=True)
portgroup = parser.add_mutually_exclusive_group()
portgroup.add_argument('-p', '--ports', action='append', default=['7000-7999'])
portgroup.add_argument('-a', '--random', action='store_true')
parser.add_argument(
'-v',
'--verbose',
action='append_const',
const='verbose',
help='Specify multiple times to make things more verbose'
)
parser.add_argument('--version', action='version', version='v0.6.4')
pluginargsgroup = parser.add_argument_group('plugins')
for plugin in pm.getAllPlugins():
plugin.plugin_object.setupargs(pluginargsgroup)
arg = parser.parse_args()
for plugin in pm.getAllPlugins():
if plugin.plugin_object.__class__.__name__.lower() in arg:
if eval('arg.%s' % (plugin.plugin_object.__class__.__name__.lower())):
logging.debug('Plugin enabled: %s' % (plugin.name))
pm.activatePluginByName(plugin.name)
else:
logging.debug('Plugin disabled: %s' % (plugin.name))
pm.deactivatePluginByName(plugin.name)
else:
logging.debug('Plugin disabled: %s' % (plugin.name))
pm.deactivatePluginByName(plugin.name)
l = logging.getLogger()
if arg.verbose:
if len(arg.verbose) == 1:
l.setLevel(logging.INFO)
if len(arg.verbose) >= 2:
l.setLevel(logging.DEBUG)
# Call activate() on all plugins so they prep themselves for use
for plugin in pm.getAllPlugins():
if eval('arg.%s' % (plugin.plugin_object.__class__.__name__.lower())):
logging.info('Enabled plugin: %s' % (plugin.name))
pm.activatePluginByName(plugin.name)
plugin.plugin_object.activate()
plugin.plugin_object._enabled = True
if len(arg.ports) > 1:
arg.ports.reverse()
arg.ports.pop()
port_list = []
if arg.random:
numdevs = 0
if arg.router:
numdevs += len(arg.router)
if arg.server:
numdevs += len(arg.server)
if arg.client:
numdevs += len(arg.client)
ports_needed = numdevs * (numdevs - 1) / 2
for i in range(0, ports_needed):
port_list.append(random.randrange(1025, 32767))
try:
if not arg.random:
# If we're not using random ports, pull whatever is in arg.ports
for portrange in arg.ports:
portstart, portstop = portrange.split('-')
port_list += range(int(portstart), int(portstop))
except ValueError as e:
print 'Invalid port range: %s' % (portrange)
raise ValueError('Invalid port range: %s' % (portrange))
linkmesh = create_link_mesh(routers=arg.router, servers=arg.server, clients=arg.client)
m = Mesh(linkmesh, port_list, arg.network)
files = None
# Run through config plugins
configPlugins = []
for plugin in pm.getPluginsOfCategory('config'):
if plugin.plugin_object._enabled:
plugin.plugin_object.process(m, arg)
configPlugins.append(plugin.plugin_object)
if files:
files = nested_dict_merge(files, plugin.plugin_object.files())
else:
files = plugin.plugin_object.files()
# Grab list of folders that need to be in the package root
includedirs = []
for f in files:
for fldr in files[f]:
rootfldr = fldr.split('/')[1]
if rootfldr not in includedirs:
includedirs.append(rootfldr)
logging.debug('The following folders will be packaged: %s' % (includedirs))
# Run through packaging plugins
packagePlugins = []
for plugin in pm.getPluginsOfCategory('package'):
if plugin.plugin_object._enabled:
# BUG: Services to restart may not necessarily be the same name as their config dir...
plugin.plugin_object.process(
m,
include_dirs=includedirs,
restart_services=includedirs,
configPlugins=configPlugins,
cliargs=arg
)
packagePlugins.append(plugin.plugin_object)
# Run through deployment plugins
for plugin in pm.getPluginsOfCategory('deploy'):
if plugin.plugin_object._enabled:
try:
plugin.plugin_object.deploy(packagePlugins=packagePlugins, cliargs=arg, stoponfailure=False)
except Exception as e:
print "Unable to deploy due to error: %s" % (e)
logging.info('OpenMesher run complete')
if __name__ == "__main__":
main()