Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# salt_openwrt
Saltstack openwrt proxy minion

Only works when

multiprocessing: False

is set in /etc/salt/proxy
29 changes: 11 additions & 18 deletions _modules/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def update_pkgs():
'''
Update the list of available packages
'''
out, _, ret = __proxy__['ssh.ssh_check']('opkg update')
out, _, ret = __proxy__['openwrt.ssh_check']('opkg update')
if ret == 0:
return True
else :
Expand All @@ -39,9 +39,11 @@ def list_pkgs():
Retrieve a list of installed packages from the openwrt host
'''
pkgs = {}
for line, _, ret in __proxy__['openwrt.ssh_check']('opkg list-installed').split('\n'):
pkg, version = line.split(' - ')
pkgs[pkg] = version
out, _, ret = __proxy__['openwrt.ssh_check']('opkg list-installed')
if ret == 0:
for line in out.split('\n'):
pkg, version = line.split(' - ')
pkgs[pkg] = version
return pkgs


Expand All @@ -59,22 +61,13 @@ def network_restart():
'''
Restart the network, reconfigures all interfaces
'''
out = __proxy__['openwrt.ubus']('network', 'restart')
if ret == 0:
return True
else :
return False

return __proxy__['openwrt.ubus']('network', 'restart')

def network_reload():
'''
Reload the network, reload interfaces as needed
'''
out = __proxy__['openwrt.ubus']('network', 'reload')
if ret == 0:
return True
else :
return False
return __proxy__['openwrt.ubus']('network', 'reload')


def interface_list():
Expand All @@ -87,6 +80,7 @@ def interface_list():
for line in out.split('\n'):
if line.startswith('network.interface.'):
intfs.append('.'.join(line.split('.')[2:]))
return intfs
else :
return False

Expand Down Expand Up @@ -123,7 +117,7 @@ def _parse_uci(data):
for line in data.split('\n'):
key, value = line.split('=', 1)
path = key.split('.')
uci['key'] = value
uci[key] = value
return uci


Expand All @@ -139,5 +133,4 @@ def reboot():
'''
Reboot openwrt device
'''
__proxy__['openwrt.ubus']('system', 'reboot')
return True
return __proxy__['openwrt.ubus']('system', 'reboot')
35 changes: 21 additions & 14 deletions _proxy/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def init(opts):
password=opts['proxy']['password'],
key_accept=opts['proxy'].get('key_accept', False),
ssh_args=opts['proxy'].get('ssh_args', ''),
prompt='.+[#$]')
prompt='root.+#')
log.info('SSH Connection established.')
out, err = DETAILS['server'].sendline('id')
DETAILS['initialized'] = True
Expand Down Expand Up @@ -103,7 +103,8 @@ def grains(**kwargs):
GRAINS_CACHE['manufacturer'], GRAINS_CACHE['productname'] = board['model'].split(' ', 1)
GRAINS_CACHE['os'] = board['release']['distribution']
GRAINS_CACHE['os_family'] = 'openwrt'
GRAINS_CACHE['oscodename'] = board['release']['codename']
#RVE: wont work
#GRAINS_CACHE['oscodename'] = board['release']['codename']
GRAINS_CACHE['osfullname'] = board['release']['description']
GRAINS_CACHE['osrelease'] = board['release']['version']
GRAINS_CACHE['osmajorrelease'] = board['release']['version'].split('.')[0]
Expand All @@ -119,7 +120,7 @@ def grains(**kwargs):
ip_interfaces = {}
ipv4 = {}
ipv6 = {}
for dev, i in netdev.iteritems():
for dev, i in netdev.items():
hwaddr_interfaces[dev] = i['macaddr']
for i in netif['interface']:
try:
Expand All @@ -131,15 +132,16 @@ def grains(**kwargs):
except KeyError:
pass
for item in [('dns-server', 'nameservers'), ('dns-search', 'search')]:
if len(i[item[0]]) > 0:
if item[0] in i and len(i[item[0]]) > 0:
dns[item[1]].extend(i[item[0]])
for route in i['route']:
if route['target'] == '0.0.0.0':
GRAINS_CACHE['ip4_gw'] = route['nexthop']
GRAINS_CACHE['ip_gw'] = True
if route['target'] == '::/0':
GRAINS_CACHE['ip6_gw'] = route['nexthop']
GRAINS_CACHE['ipv6_gw'] = True
if 'route' in i:
for route in i['route']:
if route['target'] == '0.0.0.0':
GRAINS_CACHE['ip4_gw'] = route['nexthop']
GRAINS_CACHE['ip_gw'] = True
if route['target'] == '::/0':
GRAINS_CACHE['ip6_gw'] = route['nexthop']
GRAINS_CACHE['ipv6_gw'] = True

GRAINS_CACHE['dns'] = dns
GRAINS_CACHE['hwaddr_interfaces'] = hwaddr_interfaces
Expand Down Expand Up @@ -190,8 +192,13 @@ def ubus(path, method, message = {}):
Call a remote ubus method
'''
command = 'ubus call %s %s \'%s\'' % (path, method, salt.utils.json.dumps(message))
out, _ = ssh_cmd(command)
return salt.utils.json.loads(out)
out, _, ret = ssh_check(command)
if ret == 0:
if not out:
return True
else:
return salt.utils.json.loads(out)
return False

def ssh_oneshot(command):
'''
Expand Down Expand Up @@ -219,7 +226,7 @@ def ssh_check(command):
Run cmd on the remote system and fetch exit code
'''
try:
out, err = DETAILS['server'].sendline('%s; echo $?' % (command,))
out, err = DETAILS['server'].sendline('%s; echo $?' % (command))
out = "\n".join(out.split('\n')[1:-1])
out, _, ret = out.rpartition('\n')
return out, err, int(ret)
Expand Down