Update satip_facts.py, fix syntax for hardware autoinstall checks

This commit is contained in:
Alexander Grothe 2017-09-04 12:29:53 +02:00
parent d2630e24d3
commit bdda4bc4c0
9 changed files with 170 additions and 76 deletions

View File

@ -3145,13 +3145,13 @@ If a Sat>IP Server responds to a discovery request, the package vdr-plugin-satip
action: satip_facts action: satip_facts
- debug: - debug:
var: satip_detected var: satip_devices
verbosity: 1 verbosity: 1
- name: apt | install vdr-plugin-satip if a Sat>IP server has been detected - name: apt | install vdr-plugin-satip if a Sat>IP server has been detected
apt: apt:
name: vdr-plugin-satip name: vdr-plugin-satip
when: satip_detected when: satip_devices
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]
#+END_SRC #+END_SRC
@ -3164,8 +3164,7 @@ If a Sat>IP Server responds to a discovery request, the package vdr-plugin-satip
- name: apt | install vdr-plugin-targavfd if connected - name: apt | install vdr-plugin-targavfd if connected
apt: apt:
name: vdr-plugin-targavfd name: vdr-plugin-targavfd
when: when: '"19c2:6a11" in usb'
- "19c2:6a11" in usb
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]
#+END_SRC #+END_SRC
** autoinstall-imonlcd ** autoinstall-imonlcd
@ -3177,9 +3176,7 @@ If a Sat>IP Server responds to a discovery request, the package vdr-plugin-satip
- name: apt | install vdr-plugin-imonlcd if connected - name: apt | install vdr-plugin-imonlcd if connected
apt: apt:
name: vdr-plugin-imonlcd name: vdr-plugin-imonlcd
when: when: '"15c2:0038" in usb or "15c2:ffdc" in usb'
- "15c2:0038" in usb
- "15c2:ffdc" in usb
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]
#+END_SRC #+END_SRC
** autoinstall-libcecdaemon ** autoinstall-libcecdaemon
@ -3203,8 +3200,7 @@ If a Sat>IP Server responds to a discovery request, the package vdr-plugin-satip
- name: apt | install vdr-plugin-pvr350 if connected - name: apt | install vdr-plugin-pvr350 if connected
apt: apt:
name: vdr-plugin-pvr350 name: vdr-plugin-pvr350
when: when: '"0070:4000" in pci'
- "0070:4000" in pci
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]
#+END_SRC #+END_SRC
** autoinstall-hauppauge-pvr ** autoinstall-hauppauge-pvr
@ -3215,9 +3211,7 @@ If a Sat>IP Server responds to a discovery request, the package vdr-plugin-satip
- name: apt | install vdr-plugin-pvrinput if a haupauge pvr card is found - name: apt | install vdr-plugin-pvrinput if a haupauge pvr card is found
apt: apt:
name: vdr-plugin-pvrinput name: vdr-plugin-pvrinput
when: when: '"0070:4000" in pci or "4444:0016" in pci'
- "0070:4000" in pci
- "4444:0016" in pci
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]
#+END_SRC #+END_SRC
** TODO autoinstall-dvbhddevice ** TODO autoinstall-dvbhddevice
@ -3230,9 +3224,7 @@ Problem: woher kommt der Treiber (AFAIK noch nicht im Kernel)? Die Firmware soll
- name: apt | install vdr-plugin-dvbhddevice if connected - name: apt | install vdr-plugin-dvbhddevice if connected
apt: apt:
name: vdr-plugin-dvbhddevice name: vdr-plugin-dvbhddevice
when: when: '"13c2:300a" in pci or "13c2:300b" in pci'
- '"13c2:300a" in pci'
- '"13c2:300b" in pci'
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]
#+END_SRC #+END_SRC
@ -3245,8 +3237,7 @@ Problem: woher kommt der Treiber (AFAIK noch nicht im Kernel)? Die Firmware soll
- name: apt | install vdr-plugin-dvbsddevice if module is loaded - name: apt | install vdr-plugin-dvbsddevice if module is loaded
apt: apt:
name: vdr-plugin-dvbsddevice name: vdr-plugin-dvbsddevice
when: when: '"dvb_ttpci" in modules'
- '"dvb_ttpci" in modules'
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]
#+END_SRC #+END_SRC
** kodi ** kodi
@ -4401,23 +4392,26 @@ module: hardware_facts
short_description: "check if at least one SAT>IP server responds on the network" short_description: "check if at least one SAT>IP server responds on the network"
description: description:
- This script sends a multicast message and awaits responses by Sat>IP servers. - This script sends a multicast message and awaits responses by Sat>IP servers.
Returns the boolean variable 'satip_detected' Returns a list of detected SAT>IP servers with their name and capabilites.
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: "detect SAT>IP Server on the network" - name: "detect SAT>IP Server on the network"
action: satip_facts action: satip_facts
- debug: - debug:
var: satip_detected var: satip_devices
''' '''
import json import json
import socket import socket
import sys import sys
import time import time
import xml.etree.ElementTree as ET
import requests
from contextlib import contextmanager
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
SSDP_BIND = "0.0.0.0"
SSDP_ADDR = "239.255.255.250" SSDP_ADDR = "239.255.255.250"
SSDP_PORT = 1900 SSDP_PORT = 1900
# SSDP_MX = max delay for server response # SSDP_MX = max delay for server response
@ -4433,26 +4427,79 @@ ssdpRequest = "\r\n".join((
"ST: %s" % (SSDP_ST), "ST: %s" % (SSDP_ST),
"\r\n")) "\r\n"))
@contextmanager
def socket_manager(*args, **kwargs):
"""provide a context manager for socket"""
sock = socket.socket(*args, **kwargs)
sock.setblocking(False)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error:
pass
sock.settimeout(SSDP_MX + 0.5)
sock.bind((SSDP_BIND, SSDP_PORT))
try:
yield sock
finally:
sock.close()
def parse_satip_xml(data):
""" Parse SAT>IP XML data.
Args:
data (str): XML input data..
Returns:
dict: Parsed SAT>IP device name and frontend information.
"""
result = {'name': '', 'frontends': {}}
if data:
root = ET.fromstring(data)
name = root.find('.//*/{urn:schemas-upnp-org:device-1-0}friendlyName')
result['name'] = name.text
satipcap = root.find('.//*/{urn:ses-com:satip}X_SATIPCAP')
if satipcap is None:
raise ValueError("Invalid SAT>IP device description")
caps = {}
for system in satipcap.text.split(","):
cap = system.split("-")
if cap:
count = int(cap[1])
if cap[0] in caps:
count = count + caps[cap[0]]
caps[cap[0]] = count
result['frontends'] = caps
return result
def main(): def main():
description_urls = []
device_list = []
module = AnsibleModule(argument_spec={}, supports_check_mode=True,) module = AnsibleModule(argument_spec={}, supports_check_mode=True,)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) with socket_manager(socket.AF_INET, socket.SOCK_DGRAM) as sock:
# according to Sat>IP Specification 1.2.2, p. 20 # according to Sat>IP Specification 1.2.2, p. 20
# a client should send three requests within 100 ms with a ttl of 2 # a client should send three requests within 100 ms with a ttl of 2
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.settimeout(SSDP_MX + 0.5)
for _ in range(3): for _ in range(3):
sock.sendto(ssdpRequest, (SSDP_ADDR, SSDP_PORT)) sock.sendto(ssdpRequest, (SSDP_ADDR, SSDP_PORT))
time.sleep(0.03) time.sleep(0.03)
try: try:
response = sock.recv(1000) response = sock.recv(1024)
if response and "SERVER:" in response: if response and "SERVER:" in response:
got_response = True for line in response.splitlines():
if "LOCATION" in line:
url = line.split()[-1].strip()
if url not in description_urls:
description_urls.append(url)
info = requests.get(url, timeout=2)
device_list.append(parse_satip_xml(info.text))
else: else:
raise ValueError('No satip server detected') raise ValueError('No satip server detected')
except (socket.timeout, ValueError): except (socket.timeout, ValueError, ET.ParseError):
got_response = False pass
module.exit_json(changed=False, ansible_facts={'satip_detected': got_response}) module.exit_json(changed=False, ansible_facts={'satip_devices': device_list})
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -5,23 +5,26 @@ module: hardware_facts
short_description: "check if at least one SAT>IP server responds on the network" short_description: "check if at least one SAT>IP server responds on the network"
description: description:
- This script sends a multicast message and awaits responses by Sat>IP servers. - This script sends a multicast message and awaits responses by Sat>IP servers.
Returns the boolean variable 'satip_detected' Returns a list of detected SAT>IP servers with their name and capabilites.
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: "detect SAT>IP Server on the network" - name: "detect SAT>IP Server on the network"
action: satip_facts action: satip_facts
- debug: - debug:
var: satip_detected var: satip_devices
''' '''
import json import json
import socket import socket
import sys import sys
import time import time
import xml.etree.ElementTree as ET
import requests
from contextlib import contextmanager
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
SSDP_BIND = "0.0.0.0"
SSDP_ADDR = "239.255.255.250" SSDP_ADDR = "239.255.255.250"
SSDP_PORT = 1900 SSDP_PORT = 1900
# SSDP_MX = max delay for server response # SSDP_MX = max delay for server response
@ -37,26 +40,79 @@ ssdpRequest = "\r\n".join((
"ST: %s" % (SSDP_ST), "ST: %s" % (SSDP_ST),
"\r\n")) "\r\n"))
@contextmanager
def socket_manager(*args, **kwargs):
"""provide a context manager for socket"""
sock = socket.socket(*args, **kwargs)
sock.setblocking(False)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error:
pass
sock.settimeout(SSDP_MX + 0.5)
sock.bind((SSDP_BIND, SSDP_PORT))
try:
yield sock
finally:
sock.close()
def parse_satip_xml(data):
""" Parse SAT>IP XML data.
Args:
data (str): XML input data..
Returns:
dict: Parsed SAT>IP device name and frontend information.
"""
result = {'name': '', 'frontends': {}}
if data:
root = ET.fromstring(data)
name = root.find('.//*/{urn:schemas-upnp-org:device-1-0}friendlyName')
result['name'] = name.text
satipcap = root.find('.//*/{urn:ses-com:satip}X_SATIPCAP')
if satipcap is None:
raise ValueError("Invalid SAT>IP device description")
caps = {}
for system in satipcap.text.split(","):
cap = system.split("-")
if cap:
count = int(cap[1])
if cap[0] in caps:
count = count + caps[cap[0]]
caps[cap[0]] = count
result['frontends'] = caps
return result
def main(): def main():
description_urls = []
device_list = []
module = AnsibleModule(argument_spec={}, supports_check_mode=True,) module = AnsibleModule(argument_spec={}, supports_check_mode=True,)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) with socket_manager(socket.AF_INET, socket.SOCK_DGRAM) as sock:
# according to Sat>IP Specification 1.2.2, p. 20 # according to Sat>IP Specification 1.2.2, p. 20
# a client should send three requests within 100 ms with a ttl of 2 # a client should send three requests within 100 ms with a ttl of 2
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.settimeout(SSDP_MX + 0.5)
for _ in range(3): for _ in range(3):
sock.sendto(ssdpRequest, (SSDP_ADDR, SSDP_PORT)) sock.sendto(ssdpRequest, (SSDP_ADDR, SSDP_PORT))
time.sleep(0.03) time.sleep(0.03)
try: try:
response = sock.recv(1000) response = sock.recv(1024)
if response and "SERVER:" in response: if response and "SERVER:" in response:
got_response = True for line in response.splitlines():
if "LOCATION" in line:
url = line.split()[-1].strip()
if url not in description_urls:
description_urls.append(url)
info = requests.get(url, timeout=2)
device_list.append(parse_satip_xml(info.text))
else: else:
raise ValueError('No satip server detected') raise ValueError('No satip server detected')
except (socket.timeout, ValueError): except (socket.timeout, ValueError, ET.ParseError):
got_response = False pass
module.exit_json(changed=False, ansible_facts={'satip_detected': got_response}) module.exit_json(changed=False, ansible_facts={'satip_devices': device_list})
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -4,7 +4,5 @@
- name: apt | install vdr-plugin-dvbhddevice if connected - name: apt | install vdr-plugin-dvbhddevice if connected
apt: apt:
name: vdr-plugin-dvbhddevice name: vdr-plugin-dvbhddevice
when: when: '"13c2:300a" in pci or "13c2:300b" in pci'
- '"13c2:300a" in pci'
- '"13c2:300b" in pci'
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]

View File

@ -4,6 +4,5 @@
- name: apt | install vdr-plugin-dvbsddevice if module is loaded - name: apt | install vdr-plugin-dvbsddevice if module is loaded
apt: apt:
name: vdr-plugin-dvbsddevice name: vdr-plugin-dvbsddevice
when: when: '"dvb_ttpci" in modules'
- '"dvb_ttpci" in modules'
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]

View File

@ -4,7 +4,5 @@
- name: apt | install vdr-plugin-pvrinput if a haupauge pvr card is found - name: apt | install vdr-plugin-pvrinput if a haupauge pvr card is found
apt: apt:
name: vdr-plugin-pvrinput name: vdr-plugin-pvrinput
when: when: '"0070:4000" in pci or "4444:0016" in pci'
- "0070:4000" in pci
- "4444:0016" in pci
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]

View File

@ -4,7 +4,5 @@
- name: apt | install vdr-plugin-imonlcd if connected - name: apt | install vdr-plugin-imonlcd if connected
apt: apt:
name: vdr-plugin-imonlcd name: vdr-plugin-imonlcd
when: when: '"15c2:0038" in usb or "15c2:ffdc" in usb'
- "15c2:0038" in usb
- "15c2:ffdc" in usb
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]

View File

@ -4,6 +4,5 @@
- name: apt | install vdr-plugin-pvr350 if connected - name: apt | install vdr-plugin-pvr350 if connected
apt: apt:
name: vdr-plugin-pvr350 name: vdr-plugin-pvr350
when: when: '"0070:4000" in pci'
- "0070:4000" in pci
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]

View File

@ -5,11 +5,11 @@
action: satip_facts action: satip_facts
- debug: - debug:
var: satip_detected var: satip_devices
verbosity: 1 verbosity: 1
- name: apt | install vdr-plugin-satip if a Sat>IP server has been detected - name: apt | install vdr-plugin-satip if a Sat>IP server has been detected
apt: apt:
name: vdr-plugin-satip name: vdr-plugin-satip
when: satip_detected when: satip_devices
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]

View File

@ -4,6 +4,5 @@
- name: apt | install vdr-plugin-targavfd if connected - name: apt | install vdr-plugin-targavfd if connected
apt: apt:
name: vdr-plugin-targavfd name: vdr-plugin-targavfd
when: when: '"19c2:6a11" in usb'
- "19c2:6a11" in usb
notify: [ 'Restart VDR' ] notify: [ 'Restart VDR' ]