From f0185d9d8891daf369357e5dbd8b56e2fc629f6a Mon Sep 17 00:00:00 2001 From: Matt Thompson Date: Fri, 12 Feb 2016 15:37:27 +0000 Subject: [PATCH] Updated role using the Multi-Distro framework This commit updates the memcached_server role to work on Trusty, Xenial, and CentOS 7. NOTES: 1. This role no longer creates the memcache user since both Ubuntu and CentOS already install a suitable user 2. We have temporarily disabled testing of the log file since CentOS and Xenial do not log to file 3. On Ubuntu we drop ulimits into /etc/defaults/memcached, we need to figure out how to do the equivalent on CentOS 4. We update tasks/memcached_config.yml to use the correct memcached user in limits.conf, however neither these limits or the ones in templates/memcached.debian.j2 actually seem to be taking effect. More work in an additional review will need to be done to clean this all up. Implements: blueprint multi-platform-host Change-Id: I4c32f3d60939615c5d0c6fb202e96aacb35ab9b4 Signed-off-by: Kevin Carter --- defaults/main.yml | 4 -- meta/main.yml | 4 +- other-requirements.txt | 9 +++- tasks/install-apt.yml | 54 +++++++++++++++++++ tasks/install-yum.yml | 44 +++++++++++++++ tasks/main.yml | 11 +++- tasks/memcached_config.yml | 19 +++---- tasks/memcached_install.yml | 33 +++--------- ...emcached.conf => memcached.conf.debian.j2} | 0 .../{memcached.j2 => memcached.debian.j2} | 0 templates/memcached.redhat.j2 | 13 +++++ tests/test.yml | 31 +++++------ tox.ini | 3 +- .../debian.yml | 22 ++++---- vars/redhat.yml | 26 +++++++++ 15 files changed, 205 insertions(+), 68 deletions(-) create mode 100644 tasks/install-apt.yml create mode 100644 tasks/install-yum.yml rename templates/{memcached.conf => memcached.conf.debian.j2} (100%) rename templates/{memcached.j2 => memcached.debian.j2} (100%) create mode 100644 templates/memcached.redhat.j2 rename tasks/memcached_pre_install.yml => vars/debian.yml (69%) create mode 100644 vars/redhat.yml diff --git a/defaults/main.yml b/defaults/main.yml index 455b069..a21a058 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -29,12 +29,8 @@ base_memcached_memory: "{{ ansible_memtotal_mb | default(4096) }}" memcached_memory: "{{ base_memcached_memory | int // 4 if base_memcached_memory | int // 4 < 8192 else 8192 }}" memcached_port: 11211 -memcached_user: memcache memcached_listen: "127.0.0.1" memcached_log: /var/log/memcached/memcached.log memcached_connections: 1024 memcached_threads: 4 memcached_file_limits: "{{ memcached_connections | int + 1024 }}" - -memcached_apt_packages: - - memcached diff --git a/meta/main.yml b/meta/main.yml index 42203d6..8e3ada4 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -29,4 +29,6 @@ galaxy_info: - development - openstack dependencies: - - apt_package_pinning + - role: apt_package_pinning + when: + - ansible_pkg_mgr == 'apt' diff --git a/other-requirements.txt b/other-requirements.txt index 67cd643..b8f2f67 100644 --- a/other-requirements.txt +++ b/other-requirements.txt @@ -16,5 +16,10 @@ curl # Requirements for Paramiko 2.0 -libssl-dev -libffi-dev +libssl-dev [platform:dpkg] +libffi-dev [platform:dpkg] +libffi-devel [platform:rpm] +openssl-devel [platform:rpm] + +# For selinux +libselinux-python [platform:rpm] diff --git a/tasks/install-apt.yml b/tasks/install-apt.yml new file mode 100644 index 0000000..4f45e99 --- /dev/null +++ b/tasks/install-apt.yml @@ -0,0 +1,54 @@ +--- +# Copyright 2016, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#TODO(evrardjp): Replace the next 2 tasks by a standard apt with cache +#when https://github.com/ansible/ansible-modules-core/pull/1517 is merged +#in 1.9.x or we move to 2.0 (if tested working) +- name: Check apt last update file + stat: + path: /var/cache/apt + register: apt_cache_stat + tags: + - memcached-apt-packages + +- name: Update apt if needed + apt: + update_cache: yes + when: "ansible_date_time.epoch|float - apt_cache_stat.stat.mtime > {{cache_timeout}}" + tags: + - memcached-apt-packages + +- name: Install apt packages + apt: + pkg: "{{ item }}" + state: present + register: install_packages + until: install_packages|success + retries: 5 + delay: 2 + with_items: "{{ memcached_packages }}" + tags: + - memcached-apt-packages + +- name: Install apt packages for testing + apt: + pkg: "{{ item }}" + state: present + register: install_test_packages + until: install_test_packages|success + retries: 5 + delay: 2 + with_items: "{{ memcached_test_packages }}" + when: install_test_packages|bool diff --git a/tasks/install-yum.yml b/tasks/install-yum.yml new file mode 100644 index 0000000..84be099 --- /dev/null +++ b/tasks/install-yum.yml @@ -0,0 +1,44 @@ +--- +# Copyright 2016, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: yum clean all + command: "/usr/bin/yum clean all" + register: yum_clean + until: yum_clean | success + tags: + - memcache-yum-packages + +- name: Install yum packages + yum: + pkg: "{{ item }}" + state: latest + register: install_yum_packages + until: install_yum_packages | success + retries: 5 + delay: 2 + with_items: "{{ memcached_packages }}" + tags: + - memcache-yum-packages + +- name: Install yum packages for testing + yum: + pkg: "{{ item }}" + state: latest + register: install_yum_test_packages + until: install_yum_test_packages | success + retries: 5 + delay: 2 + with_items: "{{ memcached_test_packages }}" + when: install_test_packages|bool diff --git a/tasks/main.yml b/tasks/main.yml index 3aac544..6aede66 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -13,7 +13,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -- include: memcached_pre_install.yml +- name: Gather variables for each operating system + include_vars: "{{ item }}" + with_first_found: + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" + - "{{ ansible_distribution | lower }}.yml" + - "{{ ansible_os_family | lower }}-{{ ansible_distribution_version.split('.')[0] }}.yml" + - "{{ ansible_os_family | lower }}.yml" + tags: + - always + - include: memcached_install.yml - include: memcached_config.yml diff --git a/tasks/memcached_config.yml b/tasks/memcached_config.yml index 7a01421..ad2d8d1 100644 --- a/tasks/memcached_config.yml +++ b/tasks/memcached_config.yml @@ -20,8 +20,8 @@ - name: Apply memcached config template: - src: "memcached.conf" - dest: "/etc/memcached.conf" + src: "{{ memcached_conf_template }}" + dest: "{{ memcached_conf_dest }}" owner: "root" group: "root" mode: "0644" @@ -31,13 +31,14 @@ - name: Apply resource limits template: - src: "memcached.j2" + src: "memcached.debian.j2" dest: "/etc/default/memcached" owner: "root" group: "root" mode: "0644" - when: > - memcached_connections > 1024 + when: + - ansible_pkg_mgr == 'apt' + - memcached_connections > 1024 notify: Restart memcached tags: - memcached-config @@ -45,15 +46,15 @@ - name: Configure soft file limits lineinfile: dest: "/etc/security/limits.conf" - regexp: "^memcache.*soft.*nofile.*" + regexp: "^{{ memcached_user }}.*soft.*nofile.*" state: "present" - line: "memcache\tsoft\tnofile\t\t{{ memcached_file_limits }}" + line: "{{ memcached_user }}\tsoft\tnofile\t\t{{ memcached_file_limits }}" insertbefore: "^# End of file" - name: Configure hard file limits lineinfile: dest: "/etc/security/limits.conf" - regexp: "^memcache.*hard.*nofile.*" + regexp: "^{{ memcached_user }}.*hard.*nofile.*" state: "present" - line: "memcache\thard\tnofile\t\t{{ memcached_file_limits }}" + line: "{{ memcached_user }}\thard\tnofile\t\t{{ memcached_file_limits }}" insertbefore: "^# End of file" diff --git a/tasks/memcached_install.yml b/tasks/memcached_install.yml index 8079b17..594122b 100644 --- a/tasks/memcached_install.yml +++ b/tasks/memcached_install.yml @@ -13,31 +13,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -#TODO(evrardjp): Replace the next 2 tasks by a standard apt with cache -#when https://github.com/ansible/ansible-modules-core/pull/1517 is merged -#in 1.9.x or we move to 2.0 (if tested working) -- name: Check apt last update file - stat: - path: /var/cache/apt - register: apt_cache_stat +- include: install-apt.yml + when: + - ansible_pkg_mgr == 'apt' tags: - - memcached-apt-packages + - install-apt -- name: Update apt if needed - apt: - update_cache: yes - when: "ansible_date_time.epoch|float - apt_cache_stat.stat.mtime > {{cache_timeout}}" +- include: install-yum.yml + when: + - ansible_pkg_mgr == 'yum' tags: - - memcached-apt-packages - -- name: Install apt packages - apt: - pkg: "{{ item }}" - state: present - register: install_packages - until: install_packages|success - retries: 5 - delay: 2 - with_items: "{{ memcached_apt_packages }}" - tags: - - memcached-apt-packages + - install-yum diff --git a/templates/memcached.conf b/templates/memcached.conf.debian.j2 similarity index 100% rename from templates/memcached.conf rename to templates/memcached.conf.debian.j2 diff --git a/templates/memcached.j2 b/templates/memcached.debian.j2 similarity index 100% rename from templates/memcached.j2 rename to templates/memcached.debian.j2 diff --git a/templates/memcached.redhat.j2 b/templates/memcached.redhat.j2 new file mode 100644 index 0000000..95a0b52 --- /dev/null +++ b/templates/memcached.redhat.j2 @@ -0,0 +1,13 @@ +# {{ ansible_managed }} + +{% if debug | bool %} + {% set _verbosity = '-vvv' %} +{% else %} + {% set _verbosity = '-vv' %} +{% endif %} + +PORT="{{ memcached_port }}" +USER="{{ memcached_user }}" +MAXCONN="{{ memcached_connections }}" +CACHESIZE="{{ memcached_memory }}" +OPTIONS="-l {{ memcached_listen }} -t {{ memcached_threads }} {{ _verbosity }}" diff --git a/tests/test.yml b/tests/test.yml index 1c1226b..c1fe9b0 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -19,24 +19,25 @@ roles: - role: "{{ rolename | basename }}" post_tasks: - - name: Open memcached file - slurp: - src: /etc/memcached.conf - register: memcached_file - - name: Read files - set_fact: - memcached_file_content: "{{ memcached_file.content | b64decode }}" - name: Open memcached log file stat: - path: /var/log/memcached/memcached.log - register: memcached_log + path: "{{ memcached_log }}" + register: memcached_log_stat - name: Check memcache is running - shell: ps auxfww | grep 'memcache' + command: pgrep -a memcached + register: memcached_proc + - name: Test connecting to memcache + shell: echo stats | nc -w5 127.0.0.1 11211 + register: memcached_stats - name: Check role functions assert: that: - - "memcached_log.stat.exists" - - "'logfile /var/log/memcached/memcached.log' in memcached_file_content" - - "'-l 127.0.0.1' in memcached_file_content" - - "'-c 1024' in memcached_file_content" - - "'-t 4' in memcached_file_content" + # TODO(mattt): Uncomment these once we have figured out systemd logging + #- "memcached_log_stat.stat.exists" + #- "'logfile /var/log/memcached/memcached.log' in memcached_file_content" + - "'-p 11211' in memcached_proc.stdout" + - "'-u {{ memcached_user }}' in memcached_proc.stdout" + - "'-l 127.0.0.1' in memcached_proc.stdout" + - "'-c 1024' in memcached_proc.stdout" + - "'-t 4' in memcached_proc.stdout" + - "'STAT pid' in memcached_stats.stdout" diff --git a/tox.ini b/tox.ini index 34ce164..6f09ba0 100644 --- a/tox.ini +++ b/tox.ini @@ -114,7 +114,8 @@ commands = --force ansible-playbook -i {toxinidir}/tests/inventory \ -e "rolename={toxinidir}" \ - {toxinidir}/tests/test.yml + -e "install_test_packages=True" \ + {toxinidir}/tests/test.yml -vvvv [testenv:linters] diff --git a/tasks/memcached_pre_install.yml b/vars/debian.yml similarity index 69% rename from tasks/memcached_pre_install.yml rename to vars/debian.yml index 56f38cb..2c2b7ab 100644 --- a/tasks/memcached_pre_install.yml +++ b/vars/debian.yml @@ -1,5 +1,5 @@ --- -# Copyright 2014, Rackspace US, Inc. +# Copyright 2016, Rackspace US, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,12 +13,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -- name: Ensure the memcache user exists - user: - name: "{{ memcached_user }}" - comment: "memcached user" - system: "yes" - shell: "/bin/false" - createhome: "no" - tags: - - memcached-user +memcached_user: memcache + +memcached_packages: + - memcached + +memcached_test_packages: + - netcat + +memcached_conf_template: memcached.conf.debian.j2 + +memcached_conf_dest: /etc/memcached.conf diff --git a/vars/redhat.yml b/vars/redhat.yml new file mode 100644 index 0000000..14e1b97 --- /dev/null +++ b/vars/redhat.yml @@ -0,0 +1,26 @@ +--- +# Copyright 2016, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +memcached_user: memcached + +memcached_packages: + - memcached + +memcached_test_packages: + - nc + +memcached_conf_template: memcached.redhat.j2 + +memcached_conf_dest: /etc/sysconfig/memcached