diff --git a/defaults/main.yml b/defaults/main.yml index b02279a..58f5b83 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,6 +1,8 @@ --- #HUBOT DEFAULT VARIABLES +# used for hubot bots + hubot_nodejs_version: '14' hubot_apt: - nodejs @@ -33,5 +35,28 @@ hubot_list: xmpp_password: test123test123 xmpp_room: 'myroom@example.org' # you can add multiple rooms, just separate them with a coma + +#BOT DEFAULT VARIABLES +# used for feed-to-muc bots + +feedbot_list: + - bot_name: mybot1 + xmpp_server: 'example.com:5222' + xmpp_jid: 'feedbot@example.com' + xmpp_password: 'ChangeThis' + xmpp_muc: 'muc-to-feed@conference.example.com' + xmpp_botusername: 'My feed bot' + xmpp_contact: 'xmpp:botadmin@example.com' + xmpp_MaxArticles: 5 + xmpp_RefreshTime: 30 + xmpp_NoExcerpt: 'false' + xmpp_Filter: '"submitted by", "[link]"' + xmpp_FilterMessage: '"Block me!", "Block me too!"' + xmpp_Quiet: 'false' + xmpp_Feeds: '"https://www.debian.org/News/news", + "https://www.debian.org/security/dsa-long", + "https://www.reddit.com/r/FDroidUpdates/new.rss"' + + # List the name of the bots to remove, separeted by a comma -hubot_delete_list: [] +bot_delete_list: [] \ No newline at end of file diff --git a/tasks/addmodules.yml b/tasks/addmodules.yml index b9b3b22..56b74fb 100644 --- a/tasks/addmodules.yml +++ b/tasks/addmodules.yml @@ -11,7 +11,6 @@ become: yes become_user: '{{ item.0.hubot_name }}' - - name: '[MODULES] - Deploy external-scripts.json config' template: src: hubot/external-scripts.json.j2 diff --git a/tasks/createfeedbot.yml b/tasks/createfeedbot.yml new file mode 100644 index 0000000..6426164 --- /dev/null +++ b/tasks/createfeedbot.yml @@ -0,0 +1,25 @@ +--- + +- name: "[CREATE] - Generate each bot in its path" + shell: /usr/local/go/bin/go install salsa.debian.org/mdosch/feed-to-muc@latest + args: + chdir: '/home/{{ item.bot_name }}' + loop: '{{ feedbot_list }}' + become: yes + become_user: '{{ item.bot_name }}' + +- name: '[CREATE] - Create feed-to-muc directory' + file: + path: '/home/{{ item.bot_name }}/.config/feed-to-muc/' + state: directory + mode: '0755' + loop: '{{ feedbot_list }}' + +- name: '[CREATE] - Deploy config.json' + template: + src: home/botname/config.json.j2 + dest: '/home/{{ item.bot_name }}/.config/feed-to-muc/config.json' + owner: '{{ item.bot_name }}' + group: '{{ item.bot_name }}' + mode: 0644 + loop: '{{ feedbot_list }}' diff --git a/tasks/deletebot.yml b/tasks/deletebot.yml new file mode 100644 index 0000000..5ea92b5 --- /dev/null +++ b/tasks/deletebot.yml @@ -0,0 +1,24 @@ +--- + +- name: '[DELETE] - Stop bot service' + systemd: + name: "{{ item }}" + state: stopped + loop: '{{ bot_delete_list }}' + +- name: '[DELETE] - Delete bot service' + file: + path: "/etc/systemd/system/{{ item }}.service" + state: absent + loop: '{{ bot_delete_list }}' + +- name: '[DELETE] - Daemon reload' + systemd: + daemon_reload: yes + +- name: '[DELETE] - Delete bot user' + user: + name: "{{ item }}" + state: absent + remove: yes + loop: '{{ bot_delete_list }}' diff --git a/tasks/deletehubot.yml b/tasks/deletehubot.yml deleted file mode 100644 index c79339a..0000000 --- a/tasks/deletehubot.yml +++ /dev/null @@ -1,24 +0,0 @@ ---- - -- name: '[DELETE] - Stop hubot service' - systemd: - name: "{{ item }}" - state: stopped - loop: '{{ hubot_delete_list }}' - -- name: '[DELETE] - Delete hubot service' - file: - path: "/etc/systemd/system/{{ item }}.service" - state: absent - loop: '{{ hubot_delete_list }}' - -- name: '[DELETE] - Daemon reload' - systemd: - daemon_reload: yes - -- name: '[DELETE] - Delete {{ item }} user' - user: - name: "{{ item}}" - state: absent - remove: yes - loop: '{{ hubot_delete_list }}' diff --git a/tasks/installgo.yml b/tasks/installgo.yml new file mode 100644 index 0000000..f64c199 --- /dev/null +++ b/tasks/installgo.yml @@ -0,0 +1,20 @@ +--- + +- name: '[INSTALLDEPS] - Remove any previous Go installation' + shell: rm -rf /usr/local/go + +- name: '[INSTALLDEPS] - Get latest go' + unarchive: + src: https://go.dev/dl/go1.19.4.linux-amd64.tar.gz + dest: /usr/local/ + remote_src: yes + +- name: '[INSTALLDEPS] - Add /usr/local/go/bin to the PATH environment variable' + template: + src: 'home/botname/profile.j2' + dest: '/home/{{ item.bot_name }}/.profile' + loop: '{{ feedbot_list }}' + +- name: '[INSTALLDEPS] - Source PATH environment variable' + shell: "/bin/bash -c 'source /home/{{ item.bot_name }}/.profile ;'" + loop: '{{ feedbot_list }}' diff --git a/tasks/main.yml b/tasks/main.yml index 83d6426..06a73b8 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -12,9 +12,15 @@ - name: Install hubot modules include: addmodules.yml -- name: Delete hubot - include: deletehubot.yml - when: hubot_delete_list is defined and (hubot_delete_list | length > 0) +- name: Install go + include: installgo.yml + +- name: Generate feedbot instance + include: createfeedbot.yml + +- name: Delete bot + include: deletebot.yml + when: bot_delete_list is defined and (bot_delete_list | length > 0) - name: Deploy Systemd - include: systemd.yml + include: systemd.yml \ No newline at end of file diff --git a/tasks/systemd.yml b/tasks/systemd.yml index 9042b6a..d2c5bb2 100644 --- a/tasks/systemd.yml +++ b/tasks/systemd.yml @@ -1,5 +1,6 @@ --- +# HUBOT - name: '[SystemD] - Deploy Systemd config' template: src: 'etc/systemd/system/hubot.service.j2' @@ -22,3 +23,28 @@ name: "{{ item.hubot_name }}" state: restarted loop: '{{ hubot_list }}' + + +# BOT +- name: '[SystemD] - Deploy Systemd config' + template: + src: 'etc/systemd/system/bot.service.j2' + dest: '/etc/systemd/system/{{ item.bot_name }}.service' + owner: root + group: root + mode: 0644 + loop: '{{ feedbot_list }}' + register: config + +- name: '[SystemD] - Enable systemd' + systemd: + name: "{{ item.bot_name }}" + enabled: yes + daemon_reload: yes + loop: '{{ feedbot_list }}' + +- name: '[SystemD] - Restart bot' + systemd: + name: "{{ item.bot_name }}" + state: restarted + loop: '{{ feedbot_list }}' \ No newline at end of file diff --git a/tasks/user.yml b/tasks/user.yml index 3c042f3..e966f07 100644 --- a/tasks/user.yml +++ b/tasks/user.yml @@ -1,15 +1,32 @@ --- -- name: '[User] - Add group {{ item.hubot_name }}' +# HUBOT +- name: '[User] - Add hubot group' group: name: '{{ item.hubot_name }}' state: present loop: '{{ hubot_list }}' -- name: '[User] - Add linux user {{ item.hubot_name }}' +- name: '[User] - Add hubot linux user' user: name: '{{ item.hubot_name }}' shell: /bin/bash group: '{{ item.hubot_name }}' loop: '{{ hubot_list }}' + + +# BOT +- name: '[User] - Add bot group' + group: + name: '{{ item.bot_name }}' + state: present + loop: '{{ feedbot_list }}' + + +- name: '[User] - Addubot linux user' + user: + name: '{{ item.bot_name }}' + shell: /bin/bash + group: '{{ item.bot_name }}' + loop: '{{ feedbot_list }}' \ No newline at end of file diff --git a/templates/etc/systemd/system/bot.service.j2 b/templates/etc/systemd/system/bot.service.j2 new file mode 100644 index 0000000..93b5b63 --- /dev/null +++ b/templates/etc/systemd/system/bot.service.j2 @@ -0,0 +1,19 @@ +[Unit] +Description=feed-to-muc robot +Documentation=https://salsa.debian.org/mdosch/feed-to-muc +Requires=network.target +After=network.target + +[Service] +Type=simple +WorkingDirectory=/home/{{ item.bot_name }} +RemainAfterExit=yes +User={{ item.bot_name }} +Restart=always +RestartSec=10 +StandardOutput=syslog +SyslogIdentifier={{ item.bot_name }} +ExecStart=/home/{{ item.bot_name}}/go/bin/feed-to-muc + +[Install] +WantedBy=multi-user.target diff --git a/templates/home/botname/config.json.j2 b/templates/home/botname/config.json.j2 new file mode 100644 index 0000000..6c915ae --- /dev/null +++ b/templates/home/botname/config.json.j2 @@ -0,0 +1,15 @@ +{ +"ServerAddress": "{{ item.xmpp_server }}", +"BotJid": "{{ item.xmpp_jid }}", +"Password": "{{ item.xmpp_password }}", +"Muc": "{{ item.xmpp_muc }}", +"MucNick": "{{ item.xmpp_botusername }}", +"Contact": "{{ item.xmpp_contact }}", +"MaxArticles": {{ item.xmpp_MaxArticles }}, +"RefreshTime": {{ item.xmpp_RefreshTime }}, +"NoExcerpt": {{ item.xmpp_NoExcerpt }}, +"Filter": [ {{ item.xmpp_Filter }} ], +"FilterMessage": [ {{ item.xmpp_FilterMessage }} ], +"Quiet": {{ item.xmpp_Quiet }}, +"Feeds": [ {{ item.xmpp_Feeds }} ] +} \ No newline at end of file diff --git a/templates/home/botname/profile.j2 b/templates/home/botname/profile.j2 new file mode 100644 index 0000000..1127df2 --- /dev/null +++ b/templates/home/botname/profile.j2 @@ -0,0 +1,2 @@ +export PATH=$PATH:/usr/local/go/bin +export GOPATH=$HOME/go \ No newline at end of file