Skip to content

Commit

Permalink
[OpenWisp] Avoided repetition of template context #18
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Nov 25, 2015
1 parent ed2f8ef commit a2236ab
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions netjsonconfig/backends/openwisp/openwisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ def _add_unique_file(self, item):
if item not in self.config['files']:
self.config['files'].append(item)

def _add_install(self):
def _get_install_context(self):
"""
generates install.sh and adds it to included files
returns the template context for install.sh and uninstall.sh
"""
config = self.config
# prepare tap VPN list
# layer2 VPN list
l2vpn = []
for vpn in self.config.get('openvpn', []):
if vpn.get('dev_type') != 'tap':
continue
tap = vpn.copy()
tap['name'] = tap['config_value']
l2vpn.append(tap)
# prepare bridge list
# bridge list
bridges = []
for interface in self.config.get('interfaces', []):
if interface['type'] != 'bridge':
Expand All @@ -65,18 +65,24 @@ def _add_install(self):
bridge['proto'] = interface['addresses'][0].get('proto')
bridge['ip'] = interface['addresses'][0].get('address')
bridges.append(bridge)
# crontabs present?
cron = False
for _file in config.get('files', []):
path = _file['path']
if path.startswith('/crontabs') or path.startswith('crontabs'):
cron = True
break
# fill context
context = dict(hostname=config['general']['hostname'], # hostname is required
l2vpn=l2vpn,
bridges=bridges,
radios=config.get('radios', []), # radios might be empty
cron=cron)
# return context
return dict(hostname=config['general']['hostname'], # hostname is required
l2vpn=l2vpn,
bridges=bridges,
radios=config.get('radios', []), # radios might be empty
cron=cron)

def _add_install(self, context):
"""
generates install.sh and adds it to included files
"""
contents = self._render_template('install.sh', context)
self.config.setdefault('files', []) # file list might be empty
# add install.sh to list of included files
Expand All @@ -86,27 +92,10 @@ def _add_install(self):
"mode": "755"
})

def _add_uninstall(self):
def _add_uninstall(self, context):
"""
generates uninstall.sh and adds it to included files
"""
config = self.config
# prepare tap VPN list
l2vpn = []
for vpn in self.config.get('openvpn', []):
if vpn.get('dev_type') != 'tap':
continue
tap = vpn.copy()
tap['name'] = tap['config_value']
l2vpn.append(tap)
cron = False
for _file in config.get('files', []):
path = _file['path']
if path.startswith('/crontabs') or path.startswith('crontabs'):
cron = True
break
# fill context
context = dict(l2vpn=l2vpn, cron=cron)
contents = self._render_template('uninstall.sh', context)
self.config.setdefault('files', []) # file list might be empty
# add uninstall.sh to list of included files
Expand Down Expand Up @@ -179,10 +168,12 @@ def generate(self, name='openwrt-config'):
name='uci/{0}.conf'.format(package_name),
contents=text_contents,
timestamp=timestamp)
# prepare template context for install and uninstall scripts
template_context = self._get_install_context()
# add install.sh to included files
self._add_install()
self._add_install(template_context)
# add uninstall.sh to included files
self._add_uninstall()
self._add_uninstall(template_context)
# add vpn up and down scripts
self._add_openvpn_scripts()
# add tc_script
Expand Down

0 comments on commit a2236ab

Please sign in to comment.