Skip to content

Commit

Permalink
Improve UID reset (#610)
Browse files Browse the repository at this point in the history
* Allow resetting UID/GID of the aegir user on ansible play runs.
* Kill all tasks running of the user and chown everything to the new UID.
* Separate user create and UID reset plays

#610
  • Loading branch information
jonpugh authored Nov 12, 2020
1 parent 25f3376 commit d2afe55
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 26 deletions.
1 change: 0 additions & 1 deletion roles/opendevshop.devmaster/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ ansible_become_method_aegir: sudo
ansible_share_path: /usr/share/ansible

# Server Options
php_memory_limit: "512M"

# Get a support license at https://devshop.support
devshop_support_license_key: ""
Expand Down
2 changes: 1 addition & 1 deletion roles/opendevshop.devmaster/tasks/config-devmaster.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: DevShop Control | Clear all caches
- name: DevShop Control | Clear drush caches
command: "{{ drush_bin_path }} cc drush"
become: true
become_user: "{{ aegir_user_name }}"
Expand Down
8 changes: 8 additions & 0 deletions roles/opendevshop.users/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
# defaults file for aegir.user
aegir_user_uid: 12345
aegir_user_gid: 12345

# Set to TRUE to always incude the 'reset-uid.yml' task list, forcing a gid/uid reset each time.
aegir_user_force_set_uid: false

aegir_user_name: aegir
aegir_user_home: /var/aegir
aegir_user_authorized_keys: ReplaceAtRuntime
aegir_logs_path: /var/log/aegir

# Populated in prepare-user.yml
aegir_user_uid_current: "{{ aegir_user_uid }}"
aegir_user_gid_current: "{{ aegir_user_gid }}"

# Used for the secondary install scripts for fix-perms and fix-ownership so far
aegir_hosting_version: "7.x-3.170"

Expand Down
27 changes: 3 additions & 24 deletions roles/opendevshop.users/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,9 @@
state: present
enablerepo: "{{ devshop_prerequisite_enablerepo }}"

- name: Populate service facts
service_facts:

- name: Create Aegir Group (so we can set the GID)
group:
name: "{{ aegir_user_name }}"
state: present
gid: "{{ aegir_user_gid }}"

- name: Stop all processes running as aegir.
command: killall -u "{{ aegir_user_name }}"
ignore_errors: true

- name: Create Aegir user
when: aegir_create_user
ignore_errors: true
user:
name: "{{ aegir_user_name }}"
shell: /bin/bash
group: "{{ aegir_user_name }}"
system: true
home: "{{ aegir_user_home }}"
generate_ssh_key: true
uid: "{{ aegir_user_uid }}"
- name: "Prepare System User"
include_tasks: "prepare-user.yml"
tags: [always]

- name: Ensure /var/aegir is owned by aegir user.
file:
Expand Down
60 changes: 60 additions & 0 deletions roles/opendevshop.users/tasks/prepare-user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
- name: Detect current Aegir user UID
getent:
database: passwd
key: "{{ aegir_user_name }}"
failed_when: false

- name: Detect current Aegir user GID
getent:
database: group
key: "{{ aegir_user_name }}"
failed_when: false

- name: Save variables for current user UID and GID
set_fact:
aegir_user_uid_current: "{{ getent_passwd[aegir_user_name][1] }}"
aegir_user_gid_current: "{{ getent_group[aegir_user_name][1] }}"
when:
- getent_passwd is defined

- name: Current Aegir User UID & GID
debug:
msg: "UID: {{ aegir_user_uid_current }} GID: {{ aegir_user_gid_current }}"
when:
- getent_passwd is defined

- name: Desired Aegir User UID & GID
debug:
msg: "UID: {{ aegir_user_uid }} GID: {{ aegir_user_gid }}"
when:
- getent_passwd is defined

- name: Include Reset Aegir User tasks
include_tasks: "reset-uid.yml"
when:
(aegir_user_uid != aegir_user_uid_current)
or (aegir_user_gid != aegir_user_gid_current)
or (aegir_user_force_set_uid)
tags: [always]

- name: Create Aegir Group (so we can set the GID)
group:
name: "{{ aegir_user_name }}"
state: present
gid: "{{ aegir_user_gid }}"

- name: Populate service facts
service_facts:

- name: Create Aegir user
when:
- aegir_create_user
user:
name: "{{ aegir_user_name }}"
shell: /bin/bash
group: "{{ aegir_user_name }}"
system: true
home: "{{ aegir_user_home }}"
generate_ssh_key: true
uid: "{{ aegir_user_uid }}"
31 changes: 31 additions & 0 deletions roles/opendevshop.users/tasks/reset-uid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---

- name: "User ID Change: Stop supervisord"
service:
name: supervisord
state: stopped

- name: "User ID Change: Find all user processes"
shell: "ps -ef | grep -v grep | grep -w {{ aegir_user_name }} | awk '{print $2}'"
register: running_processes

- name: Aegir User Processes
debug:
var: running_processes

- name: "User ID Change: Kill all user processes"
command: "kill {{ item }}"
ignore_errors: true
with_items: "{{ running_processes.stdout_lines }}"

- name: "User ID Change: Wait for all processes to end"
wait_for:
path: "/proc/{{ item }}/status"
state: absent
with_items: "{{ running_processes.stdout_lines }}"
ignore_errors: true
register: killed_processes

- name: "User ID Change: Force stop stuck processes"
command: "kill -9 {{ item }}"
with_items: "{{ killed_processes.results | select('failed') | map(attribute='item') | list }}"

0 comments on commit d2afe55

Please sign in to comment.