systemd support and separate message queues per room
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
vars:
|
vars:
|
||||||
- botrepo: http://aero2k.de/t/repos/urlbot-native.git
|
- botrepo: http://aero2k.de/t/repos/urlbot-native.git
|
||||||
- pypi_mirror: http://pypi.fcio.net/simple/
|
- pypi_mirror: http://pypi.fcio.net/simple/
|
||||||
|
- systemd: true
|
||||||
tasks:
|
tasks:
|
||||||
- include_vars: credentials.yml
|
- include_vars: credentials.yml
|
||||||
tags: [render_config]
|
tags: [render_config]
|
||||||
@@ -27,6 +28,7 @@
|
|||||||
shell: virtualenv -p python3 --system-site-packages ~/botenv creates=~/botenv/bin/activate
|
shell: virtualenv -p python3 --system-site-packages ~/botenv creates=~/botenv/bin/activate
|
||||||
- name: virtualenv for supervisord
|
- name: virtualenv for supervisord
|
||||||
shell: virtualenv -p python2 ~/svenv creates=~/svenv/bin/activate
|
shell: virtualenv -p python2 ~/svenv creates=~/svenv/bin/activate
|
||||||
|
when: not systemd
|
||||||
- name: clone repository
|
- name: clone repository
|
||||||
git: repo="{{botrepo}}" dest=~/urlbot force=yes update=yes
|
git: repo="{{botrepo}}" dest=~/urlbot force=yes update=yes
|
||||||
register: source_code
|
register: source_code
|
||||||
@@ -34,6 +36,7 @@
|
|||||||
pip: requirements="~/urlbot/requirements.txt" virtualenv=~/botenv extra_args="-i {{pypi_mirror}}"
|
pip: requirements="~/urlbot/requirements.txt" virtualenv=~/botenv extra_args="-i {{pypi_mirror}}"
|
||||||
- name: install supervisor
|
- name: install supervisor
|
||||||
pip: name=supervisor virtualenv=~/svenv extra_args="-i {{pypi_mirror}}"
|
pip: name=supervisor virtualenv=~/svenv extra_args="-i {{pypi_mirror}}"
|
||||||
|
when: not systemd
|
||||||
|
|
||||||
- name: set configuration
|
- name: set configuration
|
||||||
lineinfile: dest=~/urlbot/local_config.ini create=yes line="{{item.key}} = {{item.value}}" regexp="^{{item.key}}.="
|
lineinfile: dest=~/urlbot/local_config.ini create=yes line="{{item.key}} = {{item.value}}" regexp="^{{item.key}}.="
|
||||||
@@ -60,29 +63,94 @@
|
|||||||
- name: create supervisor config
|
- name: create supervisor config
|
||||||
copy: src=supervisord.conf dest=~/supervisord.conf
|
copy: src=supervisord.conf dest=~/supervisord.conf
|
||||||
register: supervisord
|
register: supervisord
|
||||||
|
when: not systemd
|
||||||
|
|
||||||
|
- name: create directory for systemd unit file
|
||||||
|
shell: mkdir -p ~/.config/systemd/user/ creates=~/.config/systemd/user/
|
||||||
|
when: systemd
|
||||||
|
|
||||||
|
- name: create unitfile
|
||||||
|
copy: src=urlbug@.service dest=~/.config/systemd/user/urlbug@.service
|
||||||
|
when: systemd
|
||||||
|
register: unitfile
|
||||||
|
|
||||||
|
# crapshit does not work
|
||||||
|
- name: reload unitfiles
|
||||||
|
become: true
|
||||||
|
shell: systemctl daemon-reload
|
||||||
|
when: unitfile.changed
|
||||||
|
ignore_errors: true
|
||||||
|
|
||||||
|
- name: enable services
|
||||||
|
shell: "systemctl --user enable urlbug@{{item}}.service"
|
||||||
|
with_items:
|
||||||
|
- idlebot
|
||||||
|
- urlbot
|
||||||
|
when: systemd
|
||||||
|
|
||||||
- name: verify supervisor running
|
- name: verify supervisor running
|
||||||
shell: nc -z 127.0.0.1 9004; echo $? executable=/bin/bash
|
shell: nc -z 127.0.0.1 9004; echo $? executable=/bin/bash
|
||||||
register: supervisor_running
|
register: supervisor_running
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
when: not systemd
|
||||||
|
|
||||||
- name: start supervisord
|
- name: start supervisord
|
||||||
shell: source ~/svenv/bin/activate && supervisord executable=/bin/bash
|
shell: source ~/svenv/bin/activate && supervisord executable=/bin/bash
|
||||||
register: start_supervisor
|
register: start_supervisor
|
||||||
when: supervisord.changed or supervisor_running.stdout == "1"
|
when:
|
||||||
|
- not systemd
|
||||||
|
- supervisord.changed or supervisor_running.stdout == "1"
|
||||||
#changed_when: "'already listening' not in start_supervisor.stdout"
|
#changed_when: "'already listening' not in start_supervisor.stdout"
|
||||||
|
|
||||||
- name: activate supervisord changes
|
- name: activate supervisord changes
|
||||||
when: supervisord.changed
|
|
||||||
shell: source ~/svenv/bin/activate && supervisorctl reload executable=/bin/bash
|
shell: source ~/svenv/bin/activate && supervisorctl reload executable=/bin/bash
|
||||||
|
when:
|
||||||
|
- not systemd
|
||||||
|
- supervisord.changed
|
||||||
|
|
||||||
- name: idlebot started
|
- name: idlebot started
|
||||||
supervisorctl: name=idlebot state=restarted supervisorctl_path=~/svenv/bin/supervisorctl
|
supervisorctl: name=idlebot state=restarted supervisorctl_path=~/svenv/bin/supervisorctl
|
||||||
when: (source_code.changed or urlbot_config.changed) and not supervisord.changed
|
when:
|
||||||
|
- not systemd
|
||||||
|
- (source_code.changed or urlbot_config.changed) and not supervisord.changed
|
||||||
|
|
||||||
- pause: seconds=30
|
# following tasks are workaround for missing ansible systemd-user support
|
||||||
when: (source_code.changed or urlbot_config.changed) and not supervisord.changed
|
- name: get systemd unit status
|
||||||
|
shell: systemctl --user status urlbug.slice
|
||||||
|
register: systemd_unit_status
|
||||||
|
|
||||||
|
- debug: var=systemd_unit_status
|
||||||
|
- debug: msg="{{'{{item}}.service' not in systemd_unit_status.stdout}}"
|
||||||
|
with_items:
|
||||||
|
- idlebot
|
||||||
|
- urlbot
|
||||||
|
|
||||||
|
- name: bots started
|
||||||
|
shell: "systemctl --user start urlbug@{{item}}.service && sleep 20"
|
||||||
|
with_items:
|
||||||
|
- idlebot
|
||||||
|
- urlbot
|
||||||
|
when: systemd and '{{item}}.service' not in systemd_unit_status.stdout
|
||||||
|
register: started_bots
|
||||||
|
|
||||||
|
- debug: var=started_bots
|
||||||
|
|
||||||
|
- name: bots restarted
|
||||||
|
shell: "systemctl --user restart urlbug@{{item}}.service && sleep 10"
|
||||||
|
with_items:
|
||||||
|
- idlebot
|
||||||
|
- urlbot
|
||||||
|
when:
|
||||||
|
- systemd
|
||||||
|
- source_code.changed or urlbot_config.changed
|
||||||
|
|
||||||
|
- pause: seconds=20
|
||||||
|
when:
|
||||||
|
- not systemd
|
||||||
|
- (source_code.changed or urlbot_config.changed) and not supervisord.changed
|
||||||
|
|
||||||
- name: urlbot started
|
- name: urlbot started
|
||||||
supervisorctl: name=bot state=restarted supervisorctl_path=~/svenv/bin/supervisorctl
|
supervisorctl: name=bot state=restarted supervisorctl_path=~/svenv/bin/supervisorctl
|
||||||
when: (source_code.changed or urlbot_config.changed) and not supervisord.changed
|
when:
|
||||||
|
- not systemd
|
||||||
|
- (source_code.changed or urlbot_config.changed) and not supervisord.changed
|
||||||
|
|||||||
2
deploy/requirements-deploy.yml
Normal file
2
deploy/requirements-deploy.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
ansible
|
||||||
|
markupsafe
|
||||||
12
deploy/urlbug@.service
Normal file
12
deploy/urlbug@.service
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=jabber bot entertaining and supporting activity on jabber MUCs
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/home/jabberbot/botenv/bin/python3 /home/jabberbot/urlbot/%i.py
|
||||||
|
WorkingDirectory=/home/jabberbot/urlbot/
|
||||||
|
StandardOutput=journal+console
|
||||||
|
StandardError=journal+console
|
||||||
|
Restart=always
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -801,12 +801,12 @@ def raise_an_error(argv, **args):
|
|||||||
raise RuntimeError("Exception for debugging")
|
raise RuntimeError("Exception for debugging")
|
||||||
|
|
||||||
|
|
||||||
@pluginfunction('repeat', 'repeat the last message', ptypes.COMMAND, enabled=False)
|
@pluginfunction('repeat', 'repeat the last message', ptypes.COMMAND)
|
||||||
def repeat_message(argv, **args):
|
def repeat_message(argv, **args):
|
||||||
return {'msg': 'disabled until channel separation'}
|
if args['stack']:
|
||||||
return {
|
return {
|
||||||
'msg': args['stack'][-1]['body']
|
'msg': args['stack'][-1]['body']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pluginfunction('isdown', 'check if a website is reachable', ptypes.COMMAND)
|
@pluginfunction('isdown', 'check if a website is reachable', ptypes.COMMAND)
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ def translate(argv, **args):
|
|||||||
if not api_key:
|
if not api_key:
|
||||||
return
|
return
|
||||||
message_stack = args['stack']
|
message_stack = args['stack']
|
||||||
|
if not message_stack[-1]:
|
||||||
|
return
|
||||||
last_message = message_stack[-1]['body']
|
last_message = message_stack[-1]['body']
|
||||||
data = {
|
data = {
|
||||||
'q': last_message,
|
'q': last_message,
|
||||||
|
|||||||
11
urlbot.py
11
urlbot.py
@@ -7,6 +7,8 @@ import re
|
|||||||
import shlex
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -34,7 +36,7 @@ class UrlBot(IdleBot):
|
|||||||
|
|
||||||
self.hist_ts = {p: [] for p in rate_limit_classes}
|
self.hist_ts = {p: [] for p in rate_limit_classes}
|
||||||
self.hist_flag = {p: True for p in rate_limit_classes}
|
self.hist_flag = {p: True for p in rate_limit_classes}
|
||||||
self.message_stack = []
|
self.message_stack = {str(room): deque(maxlen=5) for room in self.rooms}
|
||||||
|
|
||||||
self.add_event_handler('message', self.message)
|
self.add_event_handler('message', self.message)
|
||||||
self.priority = 100
|
self.priority = 100
|
||||||
@@ -196,9 +198,8 @@ class UrlBot(IdleBot):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.exception(e)
|
self.logger.exception(e)
|
||||||
finally:
|
finally:
|
||||||
if len(self.message_stack) > 4:
|
if msg_obj['from'].bare in self.rooms:
|
||||||
self.message_stack.pop(0)
|
self.message_stack[msg_obj['from'].bare].append(msg_obj)
|
||||||
self.message_stack.append(msg_obj)
|
|
||||||
|
|
||||||
def handle_muc_online(self, msg_obj):
|
def handle_muc_online(self, msg_obj):
|
||||||
"""
|
"""
|
||||||
@@ -268,7 +269,7 @@ class UrlBot(IdleBot):
|
|||||||
reply_user=reply_user,
|
reply_user=reply_user,
|
||||||
msg_obj=msg_obj,
|
msg_obj=msg_obj,
|
||||||
argv=words[2:] if len(words) > 1 else [],
|
argv=words[2:] if len(words) > 1 else [],
|
||||||
stack=self.message_stack
|
stack=self.message_stack.get(msg_obj['from'].bare, [])
|
||||||
)
|
)
|
||||||
|
|
||||||
if ret:
|
if ret:
|
||||||
|
|||||||
Reference in New Issue
Block a user