From 2e078af68d4cc2f0556c5a19b468fdfb7bb1e39e Mon Sep 17 00:00:00 2001 From: Michael Goodwin Date: Tue, 20 Feb 2018 10:32:39 -0500 Subject: [PATCH] MongoDB installation Updates (see below): - 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 #40 #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: #40 #50 #54 #71 Mongodb changes from @photoninger (Thanks also for #71 as a reference!) - Add mongodb_service_name to README - Variablize mongodb log to match service name Fixes: #71 --- README.md | 7 +++ defaults/main.yml | 12 +++- handlers/main.yml | 2 +- tasks/main.yml | 17 +----- tasks/mongodb.yml | 100 ++++++++++++++++++++++++++------ templates/mongod.conf.j2 | 4 +- templates/rocketchat.service.j2 | 2 +- templates/rocketchat_upstart.j2 | 2 +- vars/Debian.yml | 22 +++++-- vars/Debian_8.yml | 6 -- vars/Debian_9.yml | 3 + vars/RedHat.yml | 12 +++- vars/Ubuntu.yml | 19 +++++- vars/Ubuntu_14.yml | 1 - vars/Ubuntu_16.yml | 7 --- vars/Ubuntu_18.yml | 2 + vars/main.yml | 22 +++++++ 17 files changed, 177 insertions(+), 63 deletions(-) create mode 100644 vars/Debian_9.yml create mode 100644 vars/Ubuntu_18.yml create mode 100644 vars/main.yml diff --git a/README.md b/README.md index b07b869..93c7b5c 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/defaults/main.yml b/defaults/main.yml index aff2211..208e1a8 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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 diff --git a/handlers/main.yml b/handlers/main.yml index 9ef17ce..3ae80be 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -7,7 +7,7 @@ - name: Restart the MongoDB service service: - name: mongod + name: "{{ rocket_chat_mongodb_service_name }}" state: restarted - name: Upgrade Rocket.Chat diff --git a/tasks/main.yml b/tasks/main.yml index 6987830..db20996 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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" @@ -231,7 +231,6 @@ src: /var/log/upstart/rocketchat.log state: link force: yes - when: - ansible_distribution == "Ubuntu" - ansible_distribution_major_version == "14" @@ -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 diff --git a/tasks/mongodb.yml b/tasks/mongodb.yml index c6ee6e8..ccc435c 100644 --- a/tasks/mongodb.yml +++ b/tasks/mongodb.yml @@ -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) diff --git a/templates/mongod.conf.j2 b/templates/mongod.conf.j2 index 9b3d4ec..a41b420 100644 --- a/templates/mongod.conf.j2 +++ b/templates/mongod.conf.j2 @@ -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 }} @@ -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 }} diff --git a/templates/rocketchat.service.j2 b/templates/rocketchat.service.j2 index ebbfca6..3d92fc6 100644 --- a/templates/rocketchat.service.j2 +++ b/templates/rocketchat.service.j2 @@ -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 }} diff --git a/templates/rocketchat_upstart.j2 b/templates/rocketchat_upstart.j2 index 65c7b00..7b0ed20 100644 --- a/templates/rocketchat_upstart.j2 +++ b/templates/rocketchat_upstart.j2 @@ -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 }}" diff --git a/vars/Debian.yml b/vars/Debian.yml index eafeda2..cf8c5ec 100644 --- a/vars/Debian.yml +++ b/vars/Debian.yml @@ -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 diff --git a/vars/Debian_8.yml b/vars/Debian_8.yml index 5945227..ed97d53 100644 --- a/vars/Debian_8.yml +++ b/vars/Debian_8.yml @@ -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" diff --git a/vars/Debian_9.yml b/vars/Debian_9.yml new file mode 100644 index 0000000..ed7d345 --- /dev/null +++ b/vars/Debian_9.yml @@ -0,0 +1,3 @@ +--- +rocket_chat_mongodb_service_name: mongodb +rocket_chat_mongodb_org_pkgs: false diff --git a/vars/RedHat.yml b/vars/RedHat.yml index 117e863..ced1a49 100644 --- a/vars/RedHat.yml +++ b/vars/RedHat.yml @@ -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 diff --git a/vars/Ubuntu.yml b/vars/Ubuntu.yml index 3e1830a..2ed33b2 100644 --- a/vars/Ubuntu.yml +++ b/vars/Ubuntu.yml @@ -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 diff --git a/vars/Ubuntu_14.yml b/vars/Ubuntu_14.yml index ee583ad..302a5c3 100644 --- a/vars/Ubuntu_14.yml +++ b/vars/Ubuntu_14.yml @@ -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 diff --git a/vars/Ubuntu_16.yml b/vars/Ubuntu_16.yml index becf7e9..ed97d53 100644 --- a/vars/Ubuntu_16.yml +++ b/vars/Ubuntu_16.yml @@ -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 diff --git a/vars/Ubuntu_18.yml b/vars/Ubuntu_18.yml new file mode 100644 index 0000000..14049b7 --- /dev/null +++ b/vars/Ubuntu_18.yml @@ -0,0 +1,2 @@ +--- +rocket_chat_mongodb_org_pkgs: false diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..c485bb2 --- /dev/null +++ b/vars/main.yml @@ -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 }}"