From 7bf57d10b697949893cbd75c9e2aa02764fce5e1 Mon Sep 17 00:00:00 2001 From: Vojtech Sodoma Date: Thu, 23 Feb 2017 11:44:55 +0100 Subject: [PATCH 1/6] 747 Add xuc-request agi script --- agi-bin/xuc-request.py | 153 +++++++++++++++++++++++++++++++++++++++++ debian/install | 1 + 2 files changed, 154 insertions(+) create mode 100755 agi-bin/xuc-request.py diff --git a/agi-bin/xuc-request.py b/agi-bin/xuc-request.py new file mode 100755 index 0000000..09eb786 --- /dev/null +++ b/agi-bin/xuc-request.py @@ -0,0 +1,153 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +__version__ = '$Revision$' +__date__ = '$Date$' +__copyright__ = 'Copyright (C) 2014-2015 Avencall' +__author__ = 'AV' + + +import sys +import requests +import logging +import traceback +import json +from xivo.agi import AGI + +agi = AGI() + +# To be synchronised with your design +# Set XUC_SERVER to 'xivocc_ip:xuc_port' Default XUC port is 8090 +XUC_SERVER='' +TogglePauseURL="http://" + XUC_SERVER + "/xuc/api/1.0/togglePause/" +AgentLoginURL="http://" + XUC_SERVER + "/xuc/api/1.0/agentLogin/" +AgentLogoutURL="http://" + XUC_SERVER + "/xuc/api/1.0/agentLogout/" + +# User configuration +DEBUG_MODE = False +LOGFILE = '/var/log/xivo-xuc-agent-webservices.log' +# timeout of the tcp connection to the webserver (seconds) +CONNECTION_TIMEOUT = 2 + +# Factory configuration +URL_HEADERS = { 'User-Agent' : 'XiVO Webservice AGI' } + +logger = logging.getLogger() + + +class Syslogger(object): + + def write(self, data): + global logger + logger.error(data) + +def init_logging(debug_mode): + if debug_mode: + logger.setLevel(logging.DEBUG) + else: + logger.setLevel(logging.INFO) + + logfilehandler = logging.FileHandler(LOGFILE) + + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + logfilehandler.setFormatter(formatter) + logger.addHandler(logfilehandler) + + syslogger = Syslogger() + sys.stderr = syslogger + +def logAgiFile(message, error = False): + agi.verbose(message) + if error == False: + logger.info(message) + else: + logger.error(message) + +def send_request(url, customHeaders, params): + try: + result = requests.post(url, + headers = customHeaders, + data = json.dumps(params), + timeout=CONNECTION_TIMEOUT) + agi.verbose("Request sent to server") + + except requests.exceptions.ConnectionError: + logAgiFile("Server XUC refused connection") + + except requests.exceptions.Timeout: + logAgiFile("Timeout when connecting to the XUC server.") + + except: + logAgiFile("Unknown problem during connection to the XUC server.") + exc_type, exc_value, exc_traceback = sys.exc_info() + logger.debug(repr(traceback.format_exception(exc_type, exc_value, + exc_traceback))) + finally: + agi.set_variable("WS_RESULT_CODE",str(result.status_code)) + +def toggle_pause(phonenumber): + params = {'phoneNumber': phonenumber} + headers = {'content-type': 'application/json'} + logAgiFile("Toggle Pause request for agent at phone: " + str(phonenumber)) + send_request(TogglePauseURL, headers, params) + +def log_agent(phonenumber, agentnumber): + params = {'agentphonenumber': phonenumber, 'agentnumber': agentnumber} + headers = {'content-type': 'application/json'} + logAgiFile("Agent login request for agentnumber: " + str(agentnumber) + " at phone: " + str(phonenumber)) + send_request(AgentLoginURL, headers, params) + +def logout_agent(phonenumber): + params = {'phoneNumber': phonenumber} + headers = {'content-type': 'application/json'} + logAgiFile("Agent logout request for agent logged on phone: " + str(phonenumber)) + send_request(AgentLogoutURL, headers, params) + +def main(): + agi.verbose('---------------------------------------------------') + init_logging(DEBUG_MODE) + + if(len(XUC_SERVER) <= 0): + logAgiFile('XUC address is not defined, the agent login/pause can\'t be used') + logAgiFile('Please configure the address in the /var/lib/asterisk/agi-bin/xuc-request.py file') + sys.exit(2) + + try: + if len(sys.argv) < 2: + logAgiFile("Wrong number of arguments", error=True) + sys.exit(1) + agi.verbose('Request: '+sys.argv[1]) + if sys.argv[1] == 'togglePause': + toggle_pause(sys.argv[2]) + elif sys.argv[1] == 'agentLogin': + if len(sys.argv) < 3: + logAgiFile('Wrong number of arguments', error=True) + sys.exit(1) + log_agent(sys.argv[2], sys.argv[3]) + elif sys.argv[1] == 'agentLogout': + logout_agent(sys.argv[2]) + else: + logAgiFile('Undefined request', error=True) + except Exception: + exc_type, exc_value, exc_traceback = sys.exc_info() + logger.error(repr(traceback.format_exception(exc_type, exc_value, + exc_traceback))) + sys.exit(1) + + sys.exit(1) + + +if __name__ == '__main__': + main() diff --git a/debian/install b/debian/install index e2d532b..01a939e 100644 --- a/debian/install +++ b/debian/install @@ -1,3 +1,4 @@ bin/* usr/bin/ sbin/* usr/sbin/ asterisk-bin/* usr/share/asterisk/bin/ +agi-bin/* var/lib/asterisk/agi-bin/ -- GitLab From 15fb1cc2158f15bdbd6c2c8e3a4f4247ccb931da Mon Sep 17 00:00:00 2001 From: Vojtech Sodoma Date: Thu, 23 Feb 2017 11:55:36 +0100 Subject: [PATCH 2/6] 747 Edit changelog --- debian/changelog | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/debian/changelog b/debian/changelog index 15b0451..37127bd 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +xivo-utils (13.06~20170223.114757) xivo-dev; urgency=medium + + * Add xuc-request agi script + + -- Vojtech Sodoma Thu, 23 Feb 2017 11:47:57 +0100 + xivo-utils (13.05~20130312.124101.6eedb0e) xivo-dev; urgency=low * add dependency to xivo-lib-python -- GitLab From 6a4203db75197e5ace2db3f19585770a490fdf73 Mon Sep 17 00:00:00 2001 From: Vojtech Sodoma Date: Thu, 23 Feb 2017 13:51:07 +0100 Subject: [PATCH 3/6] 747 Change script owner to asterisk in preinst and postinst --- debian/postinst | 29 +++++++++++++++++++++++++++++ debian/preinst | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 debian/postinst create mode 100644 debian/preinst diff --git a/debian/postinst b/debian/postinst new file mode 100644 index 0000000..eaef7ca --- /dev/null +++ b/debian/postinst @@ -0,0 +1,29 @@ +#!/bin/bash +# postinst script for xivo-utils +# +# see: dh_installdeb(1) + +set -e + +case "$1" in + configure) + chown -R asterisk:asterisk /var/lib/asterisk/agi-bin/xuc-request.py + ;; + + abort-upgrade|abort-remove|abort-deconfigure) + + ;; + + *) + echo "postinst called with unknown argument '$1'" >&2 + exit 1 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 + diff --git a/debian/preinst b/debian/preinst new file mode 100644 index 0000000..c226fe1 --- /dev/null +++ b/debian/preinst @@ -0,0 +1,34 @@ +#!/bin/sh +# preinst script for xivo-utils +# +# see: dh_installdeb(1) + +set -e + +SCRIPT="/var/lib/asterisk/agi-bin/xuc-request.py" + +case "$1" in + install) + ;; + + upgrade) + if [ -f ${SCRIPT} ]; then + chown -R asterisk:asterisk ${SCRIPT} + fi + ;; + + abort-upgrade) + ;; + + *) + echo "preinst called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 -- GitLab From 461deaeb99f6b7c6947b7167bc06bbe163730998 Mon Sep 17 00:00:00 2001 From: Vojtech Sodoma Date: Thu, 23 Feb 2017 14:02:44 +0100 Subject: [PATCH 4/6] 747 Edit changelog --- debian/changelog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index 37127bd..eec7301 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -xivo-utils (13.06~20170223.114757) xivo-dev; urgency=medium +xivo-utils (2017.02~20170223.114757) xivo-dev; urgency=low * Add xuc-request agi script -- GitLab From e66d15d0617e227ba03f04dd3e0c1a3ed154666d Mon Sep 17 00:00:00 2001 From: Vojtech Sodoma Date: Thu, 23 Feb 2017 16:09:08 +0100 Subject: [PATCH 5/6] 747 Add log file and remove preinst --- debian/postinst | 11 ++++++++++- debian/preinst | 34 ---------------------------------- debian/xivo-utils.logrotate | 7 +++++++ 3 files changed, 17 insertions(+), 35 deletions(-) delete mode 100644 debian/preinst create mode 100644 debian/xivo-utils.logrotate diff --git a/debian/postinst b/debian/postinst index eaef7ca..a417b36 100644 --- a/debian/postinst +++ b/debian/postinst @@ -5,9 +5,18 @@ set -e +LOG_FILENAME="/var/log/xivo-xuc-agent-webservices.log" +SCRIPT="/var/lib/asterisk/agi-bin/xuc-request.py" + case "$1" in configure) - chown -R asterisk:asterisk /var/lib/asterisk/agi-bin/xuc-request.py + if [ ! -e "$LOG_FILENAME" ]; then + touch "$LOG_FILENAME" + fi + + chown asterisk:asterisk "$LOG_FILENAME" + + chown -R asterisk:asterisk "$SCRIPT" ;; abort-upgrade|abort-remove|abort-deconfigure) diff --git a/debian/preinst b/debian/preinst deleted file mode 100644 index c226fe1..0000000 --- a/debian/preinst +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh -# preinst script for xivo-utils -# -# see: dh_installdeb(1) - -set -e - -SCRIPT="/var/lib/asterisk/agi-bin/xuc-request.py" - -case "$1" in - install) - ;; - - upgrade) - if [ -f ${SCRIPT} ]; then - chown -R asterisk:asterisk ${SCRIPT} - fi - ;; - - abort-upgrade) - ;; - - *) - echo "preinst called with unknown argument \`$1'" >&2 - exit 1 - ;; -esac - -# dh_installdeb will replace this with shell code automatically -# generated by other debhelper scripts. - -#DEBHELPER# - -exit 0 diff --git a/debian/xivo-utils.logrotate b/debian/xivo-utils.logrotate new file mode 100644 index 0000000..b93353d --- /dev/null +++ b/debian/xivo-utils.logrotate @@ -0,0 +1,7 @@ +/var/log/xivo-xuc-agent-webservices.log { + daily + rotate 15 + compress + copytruncate + missingok +} -- GitLab From 47ec3356e91eb25380a9d999147edd5f4409ac26 Mon Sep 17 00:00:00 2001 From: Stepan Kabele Date: Mon, 27 Feb 2017 14:11:24 +0100 Subject: [PATCH 6/6] 747 extract xuc server IP an port to config file --- agi-bin/xuc-request.py | 10 +++++++--- debian/install | 1 + etc/xivo-xuc.conf | 3 +++ 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 etc/xivo-xuc.conf diff --git a/agi-bin/xuc-request.py b/agi-bin/xuc-request.py index 09eb786..a142214 100755 --- a/agi-bin/xuc-request.py +++ b/agi-bin/xuc-request.py @@ -24,13 +24,17 @@ import requests import logging import traceback import json +import ConfigParser from xivo.agi import AGI agi = AGI() -# To be synchronised with your design -# Set XUC_SERVER to 'xivocc_ip:xuc_port' Default XUC port is 8090 -XUC_SERVER='' +xuc_config = ConfigParser.ConfigParser() +xuc_config.readfp(open('/etc/xivo-xuc.conf')) +XUC_SERVER_IP = xuc_config.get('XUC', 'XUC_SERVER_IP') +XUC_SERVER_PORT = xuc_config.get('XUC', 'XUC_SERVER_PORT') +XUC_SERVER = XUC_SERVER_IP + ':' + XUC_SERVER_PORT + TogglePauseURL="http://" + XUC_SERVER + "/xuc/api/1.0/togglePause/" AgentLoginURL="http://" + XUC_SERVER + "/xuc/api/1.0/agentLogin/" AgentLogoutURL="http://" + XUC_SERVER + "/xuc/api/1.0/agentLogout/" diff --git a/debian/install b/debian/install index 01a939e..90cb197 100644 --- a/debian/install +++ b/debian/install @@ -1,4 +1,5 @@ bin/* usr/bin/ sbin/* usr/sbin/ +etc/* etc/ asterisk-bin/* usr/share/asterisk/bin/ agi-bin/* var/lib/asterisk/agi-bin/ diff --git a/etc/xivo-xuc.conf b/etc/xivo-xuc.conf new file mode 100644 index 0000000..43430db --- /dev/null +++ b/etc/xivo-xuc.conf @@ -0,0 +1,3 @@ +[XUC] +XUC_SERVER_IP= +XUC_SERVER_PORT=8090 \ No newline at end of file -- GitLab