Skip to content

Commit

Permalink
MongoDB installation Updates (see below):
Browse files Browse the repository at this point in the history
 - Explicitly define the mongodb upstart init conf path
 - Mongodb repo updates for 3.4
 - Allow tuning of mongodb service name
   - New vars:
     - `rocket_chat_mongodb_service_name`: mongod (string)
     - `rocket_chat_mongodb_org_pkgs`: false (bool)
     - `rocket_chat_mongodb_org_version`: 3.4 (string)
- Implement suggestions from RocketChat#40 RocketChat#50
  - Allow fine-grained tuning of `MONGO_URL` inside service files
  - Configure `rocket_chat_mongodb_URI` with jinja2 logic
    - New vars:
      - `rocket_chat_mongodb_user`: ~ (string)
      - `rocket_chat_mongodb_password`: ~ (string)
      - `rocket_chat_mongodb_database`: rocketchat (string)
      - `rocket_chat_mongodb_use_tls`: false (bool)
      - `rocket_chat_mongodb_URI`: computed result
- Move replSet task into mongodb.yml
- Change idempotency of replSet command
  - Now actually check on the JSON output of the
- Add MongoDB.org offical package install task for RHEL
- Vaariabilize `rocket_chat_mongodb_config` for debian packages
     - Set the variable explicitly in the org packages task to match the
       official location
- Fixes: RocketChat#40 RocketChat#50 RocketChat#54 RocketChat#71

Mongodb changes from @photoninger (Thanks also for RocketChat#71 as a reference!)

 - Add mongodb_service_name to README
 - Variablize mongodb log to match service name

Fixes: RocketChat#71
  • Loading branch information
xenithorb committed Mar 2, 2018
1 parent 46ffcc3 commit 2e078af
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 63 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,17 @@ All variables have sane defaults set in [`defaults/main.yml`](defaults/main.yml)
| `rocket_chat_include_mongodb` | true | A boolean value that determines whether or not to deploy MongoDB |
| `rocket_chat_mongodb_keyserver` | keyserver.ubuntu.com | The GPG key server to use when importing the MongoDB repo key |
| `rocket_chat_mongodb_gpg_key` | `7F0CEB10` | The GPG key fingerprint to import for the MongoDB repo |
| `rocket_chat_mongodb_user` | not used by default | Username to be used when connecting to MongoDB. If you set this, you should also define `rocket_chat_mongodb_password`, otherwise no user/pass is used to connect to MongoDB |
| `rocket_chat_mongodb_password` | not used by default | Password to be used when connecting to MongoDB. If you set this, you should also define `rocket_chat_mongodb_user`, otherwise no user/pass is used to connect to MongoDB |
| `rocket_chat_mongodb_server` | 127.0.0.1 | The IP/FQDN of the MongoDB host |
| `rocket_chat_mongodb_port` | 27017 | The TCP port to contact the MongoDB host host via |
| `rocket_chat_mongodb_database` | rocketchat | The MongoDB database to be used for Rocket.Chat |
| `rocket_chat_mongodb_use_tls` | false | Whether or not to use TLS to connect to the MongoDB DB |
| `rocket_chat_mongodb_packages` | `mongodb` | The name of the MongoDB package(s) to install (differs for different distros - see `vars/`) |
| `rocket_chat_mongodb_config_template` | [`mongod.conf.j2`](templates/mongod.conf.j2) | The `/etc/mongod.conf` template to deploy |
| `rocket_chat_mongodb_org_pkgs` | false | true (Debian/Ubuntu) | Use official MongoDB.org community edition packages or not |
| `rocket_chat_mongodb_org_version` | 3.4 | Version string of official packages to install |
| `rocket_chat_mongodb_service_name` | `mongod` | The name of the systemd service unit and mongodb config file in /etc |
| `rocket_chat_include_nginx`| true | A boolean value that determines whether or not to deploy Nginx |
| `rocket_chat_ssl_generate_certs` | true | A boolean value that determines whether or not to generate the Nginx SSL certs |
| `rocket_chat_ssl_key_path` | `/etc/nginx/rocket_chat.key` | The destination path for the Nginx SSL private key |
Expand Down
12 changes: 11 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@ rocket_chat_npm_dist: /usr/bin/npm

# MongoDB settings
rocket_chat_mongodb_packages: mongodb
rocket_chat_mongodb_service_name: mongod
rocket_chat_include_mongodb: true
rocket_chat_mongodb_keyserver: hkp://keyserver.ubuntu.com:80
rocket_chat_mongodb_gpg_key: EA312927
rocket_chat_mongodb_gpg_key: 0C49F3730359A14518585931BC711F9BA15703C6
rocket_chat_mongodb_server: 127.0.0.1
rocket_chat_mongodb_port: 27017
rocket_chat_mongodb_config: /etc/mongod.conf
rocket_chat_mongodb_config_template: mongod.conf.j2
# Currently only for Debian/Ubuntu based
rocket_chat_mongodb_org_pkgs: false
rocket_chat_mongodb_org_version: 3.4
# MongoDB connection settings:
rocket_chat_mongodb_database: rocketchat
rocket_chat_mongodb_use_tls: false
rocket_chat_mongodb_user: ~
rocket_chat_mongodb_password: ~

# nginx settings
rocket_chat_include_nginx: true
Expand Down
2 changes: 1 addition & 1 deletion handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

- name: Restart the MongoDB service
service:
name: mongod
name: "{{ rocket_chat_mongodb_service_name }}"
state: restarted

- name: Upgrade Rocket.Chat
Expand Down
17 changes: 2 additions & 15 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@
- name: "Configure /etc/hosts"
lineinfile:
dest: /etc/hosts
line: "127.0.0.1 localhost {{ ansible_fqdn }} {{ ansible_nodename }} {{ ansible_hostname }}"
regexp: '^127\.0\.0\.1'
line: "127.0.1.1 {{ ansible_fqdn }} {{ ansible_nodename }}"
regexp: '^127\.0\.1\.1'
insertbefore: BOF
when: ansible_virtualization_type != "docker"

Expand Down Expand Up @@ -231,7 +231,6 @@
src: /var/log/upstart/rocketchat.log
state: link
force: yes

when:
- ansible_distribution == "Ubuntu"
- ansible_distribution_major_version == "14"
Expand Down Expand Up @@ -263,18 +262,6 @@

- meta: flush_handlers

- name: Ensure the MongoDB replSets have been initiated
shell: >-
mongo --eval 'rs.initiate()' &&
touch .mongo_rs_initialised
become: yes
become_user: mongodb
args:
executable: /bin/bash
chdir: /var/lib/mongodb
creates: /var/lib/mongodb/.mongo_rs_initialised
when: rocket_chat_include_mongodb|bool

- name: Restart the Rocket.Chat service [UPGRADE]
service:
name: rocketchat
Expand Down
100 changes: 82 additions & 18 deletions tasks/mongodb.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,99 @@
---
# tasks/mongodb.yml: MongoDB configuration for RocketChat.Ansible
- name: Install Official MongoDB.org packages
block:

- name: Ensure the MongoDB repository key has been imported
apt_key:
keyserver: "{{ rocket_chat_mongodb_keyserver }}"
id: "{{ rocket_chat_mongodb_gpg_key }}"
when: ansible_os_family == "Debian"
tags: repo

- name: Ensure the MongoDB repository is present
apt_repository:
repo: "{{ rocket_chat_mongodb_apt_repo }}"
state: present
when: ansible_os_family == "Debian"
tags: repo

- name: Ensure MongoDB Server is present
- name: Set official package names
set_fact:
rocket_chat_mongodb_packages:
- mongodb-org
- mongodb-org-server
rocket_chat_mongodb_config: /etc/mongod.conf

- name: Debian/Ubuntu MongoDB.org official pkgs tasks
block:

- name: Ensure the MongoDB repository key has been imported [Debian]
apt_key:
keyserver: "{{ rocket_chat_mongodb_keyserver }}"
id: "{{ rocket_chat_mongodb_gpg_key }}"
tags: repo

- name: Ensure the MongoDB repository is present [Debian]
apt_repository:
repo: "{{ rocket_chat_mongodb_apt_repo }}"
state: present
tags: repo
register: rocket_chat_mongodb_repo_state

when:
- rocket_chat_mongodb_apt_repo is defined
- (ansible_os_family | lower) == "debian"

- name: RHEL-based MongoDB.org official pkgs tasks
block:

- name: Ensure the MongoDB repository key has been imported [RHEL]
rpm_key:
key: "{{ rocket_chat_mongodb_rpm_repo.pgp_key }}"
state: present
tags: repo

- name: Ensure the MongoDB repository is present [RHEL]
yum_repository:
name: "{{ rocket_chat_mongodb_rpm_repo.name }}"
baseurl: "{{ rocket_chat_mongodb_rpm_repo.baseurl }}"
state: present
description: "{{ rocket_chat_mongodb_rpm_repo.desc }}"
gpgcheck: "{{ rocket_chat_mongodb_rpm_repo.gpgcheck }}"
gpgkey: "{{ rocket_chat_mongodb_rpm_repo.pgp_key }}"
tags: repo
register: rocket_chat_mongodb_repo_state

when:
- rocket_chat_mongodb_rpm_repo is defined
- (ansible_os_family | lower) = "redhat"

when:
- rocket_chat_mongodb_org_pkgs

- name: Ensure MongoDB Server is installed
package:
name: "{{ rocket_chat_mongodb_packages }}"
state: present
state: "{{ (rocket_chat_mongodb_repo_state | changed) | ternary('latest','present') }}"

- name: Deploy MongoDB service configuration
template:
src: "{{ rocket_chat_mongodb_config_template }}"
dest: /etc/mongod.conf
dest: "{{ rocket_chat_mongodb_config }}"
notify: Restart the MongoDB service

- meta: flush_handlers

- name: Ensure the MongoDB service is started/enabled
service:
name: mongod
name: "{{ rocket_chat_mongodb_service_name }}"
state: started
enabled: yes

- name: Wait for MongoDB to come online
wait_for:
port: "{{ rocket_chat_mongodb_port }}"
host: "{{ rocket_chat_mongodb_server }}"
state: started

- name: Ensure the MongoDB replSets have been initiated
shell: >-
mongo --quiet --eval
'rs.initiate({_id:"{{ rocket_chat_mongodb_repl_setname }}",
members: [{"_id":1, "host":
"{{ rocket_chat_mongodb_server }}:{{ rocket_chat_mongodb_port }}"}]})'
become: yes
become_user: mongodb
args:
executable: /bin/bash
register: replSet_result
changed_when:
- not (replSet_result.stdout | search(' Object'))
- ('ok' in (replSet_result.stdout | from_json))
- (((replSet_result.stdout | from_json).ok | int) == 1)
4 changes: 2 additions & 2 deletions templates/mongod.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% if rocket_chat_mongodb_fork is defined %}
# whether to fork the process or not
fork = {{ rocket_chat_mongodb_fork }}
{% endif %}
{% endif %}
{% if rocket_chat_mongodb_pidfile_path is defined %}

pidfilepath = {{ rocket_chat_mongodb_pidfile_path }}
Expand Down Expand Up @@ -35,7 +35,7 @@ storage:
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
path: /var/log/mongodb/{{ rocket_chat_mongodb_service_name }}.log
# network interfaces
net:
port: {{ rocket_chat_mongodb_port }}
Expand Down
2 changes: 1 addition & 1 deletion templates/rocketchat.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ StandardOutput=syslog
SyslogIdentifier=RocketChat
User={{ rocket_chat_service_user }}
Group={{ rocket_chat_service_group }}
Environment=MONGO_URL=mongodb://{{ rocket_chat_mongodb_server }}:{{ rocket_chat_mongodb_port }}/rocketchat
Environment=MONGO_URL=mongodb://{{ rocket_chat_mongodb_URI }}
Environment=MONGO_OPLOG_URL=mongodb://{{ rocket_chat_mongodb_server }}:{{ rocket_chat_mongodb_port }}/local
Environment=ROOT_URL=https://{{ rocket_chat_service_host }}
Environment=PORT={{ rocket_chat_service_port }}
Expand Down
2 changes: 1 addition & 1 deletion templates/rocketchat_upstart.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ chdir {{ rocket_chat_application_path }}
setuid {{ rocket_chat_service_user }}
setgid {{ rocket_chat_service_group }}

env MONGO_URL="mongodb://{{ rocket_chat_mongodb_server }}:{{ rocket_chat_mongodb_port }}/rocketchat"
env MONGO_URL="mongodb://{{ rocket_chat_mongodb_URI }}"
env MONGO_OPLOG_URL="mongodb://{{ rocket_chat_mongodb_server }}:{{ rocket_chat_mongodb_port }}/local"
env ROOT_URL="https://{{ rocket_chat_service_host }}"
env PORT="{{ rocket_chat_service_port }}"
Expand Down
22 changes: 17 additions & 5 deletions vars/Debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@
rocket_chat_dep_packages:
- git
- graphicsmagick
- nodejs
- npm
- bash
- make
- wget
# This seems to install something on Docker that causes a failure in the tests
- cron
- gnupg2

rocket_chat_mongodb_org_pkgs: true
rocket_chat_mongodb_apt_repo: "\
deb http://repo.mongodb.org/apt/{{ ansible_distribution | lower }} \
{{ ansible_distribution_release | lower }}/mongodb-org/\
{{ rocket_chat_mongodb_org_version }} main"

rocket_chat_mongodb_packages:
- mongodb-org-server
- mongodb-org-shell
- mongodb
- mongodb-server
rocket_chat_mongodb_config: /etc/mongodb.conf

rocket_chat_mongodb_repl_setname: 001-rs
rocket_chat_mongodb_repl_lines: >-
replication:
replSetName: "001-rs"
replSetName: "{{ rocket_chat_mongodb_repl_setname }}"
rocket_chat_nginx_process_user: www-data

rocket_chat_service_update_command: systemctl daemon-reload
rocket_chat_service_template:
src: rocketchat.service.j2
dest: /etc/systemd/system/rocketchat.service
6 changes: 0 additions & 6 deletions vars/Debian_8.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
---
rocket_chat_service_update_command: systemctl daemon-reload
rocket_chat_service_template:
src: rocketchat.service.j2
dest: /etc/systemd/system/rocketchat.service

rocket_chat_mongodb_apt_repo: "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.2 main"
3 changes: 3 additions & 0 deletions vars/Debian_9.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
rocket_chat_mongodb_service_name: mongodb
rocket_chat_mongodb_org_pkgs: false
12 changes: 10 additions & 2 deletions vars/RedHat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ rocket_chat_dep_packages:
rocket_chat_mongodb_packages:
- mongodb
- mongodb-server

rocket_chat_mongodb_repl_lines: 'replSet=001-rs'
rocket_chat_mongodb_repl_setname: 001-rs
rocket_chat_mongodb_repl_lines: 'replSet={{ rocket_chat_mongodb_repl_setname }}'
rocket_chat_mongodb_fork: true
rocket_chat_mongodb_pidfile_path: /var/run/mongodb/mongodb.pid
rocket_chat_mongodb_logpath: /var/log/mongodb/mongod.log
rocket_chat_mongodb_unixsocketprefix: /var/run/mongodb
rocket_chat_mongodb_dbpath: /var/lib/mongodb
rocket_chat_mongodb_org_pkgs: false

rocket_chat_nginx_process_user: nginx

rocket_chat_mongodb_rpm_repo:
name: mongodb-org-3.4
desc: MongoDB 3.4 Repository
baseurl: https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.4/x86_64/
pgp_key: https://www.mongodb.org/static/pgp/server-3.4.asc
gpgcheck: true
19 changes: 16 additions & 3 deletions vars/Ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ rocket_chat_dep_packages:
- cron
- gnupg2

rocket_chat_mongodb_org_pkgs: true
rocket_chat_mongodb_apt_repo: "\
deb http://repo.mongodb.org/apt/{{ ansible_distribution | lower }} \
{{ ansible_distribution_release | lower }}/mongodb-org/\
{{ rocket_chat_mongodb_org_version }} multiverse"

rocket_chat_mongodb_packages:
- mongodb-org-server
- mongodb-org-shell
- mongodb
- mongodb-server
rocket_chat_mongodb_config: /etc/mongodb.conf

rocket_chat_mongodb_repl_setname: 001-rs
rocket_chat_mongodb_repl_lines: >-
replication:
replSetName: "001-rs"
replSetName: "{{ rocket_chat_mongodb_repl_setname }}"
rocket_chat_nginx_process_user: www-data

rocket_chat_service_update_command: systemctl daemon-reload
rocket_chat_service_template:
src: rocketchat.service.j2
dest: /etc/systemd/system/rocketchat.service
1 change: 0 additions & 1 deletion vars/Ubuntu_14.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ rocket_chat_service_template:
src: rocketchat_upstart.j2
dest: /etc/init/rocketchat.conf

rocket_chat_mongodb_apt_repo: "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse"
rocket_chat_tarball_validate_remote_cert: false
7 changes: 0 additions & 7 deletions vars/Ubuntu_16.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
---
rocket_chat_service_update_command: systemctl daemon-reload
rocket_chat_service_template:
src: rocketchat.service.j2
dest: /etc/systemd/system/rocketchat.service

rocket_chat_mongodb_apt_repo: "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse"
rocket_chat_mongodb_gpg_key: EA312927
2 changes: 2 additions & 0 deletions vars/Ubuntu_18.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
rocket_chat_mongodb_org_pkgs: false
22 changes: 22 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
# rocket_chat_mongodb_repl_result_obj: "\
# {{ replSet_result['stdout_lines'][\
# ((replSet_result['stdout_lines'].index('{'))|int):\
# ((replSet_result['stdout_lines'].index('}'))|int)+1]\
# | join | from_json }}"

rocket_chat_mongodb_URI: "\
{% set temp_out =
rocket_chat_mongodb_server ~ ':' ~ rocket_chat_mongodb_port -%}
{% if rocket_chat_mongodb_user and rocket_chat_mongodb_password -%}
{% set temp_out =
rocket_chat_mongodb_user
~ ':' ~ rocket_chat_mongodb_password
~ '@' ~ temp_out -%}
{% endif -%}
{% set temp_out =
temp_out ~ '/' ~ rocket_chat_mongodb_database -%}
{% if (rocket_chat_mongodb_use_tls | bool) == true -%}
{% set temp_out = temp_out ~ '?ssl=true' -%}
{% endif -%}
{{ temp_out }}"

0 comments on commit 2e078af

Please sign in to comment.