Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(users/init.sls): dedup groups and directories creation #223

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 40 additions & 22 deletions users/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
{% set used_googleauth = [] %}
{% set used_user_files = [] %}
{% set used_polkit = [] %}
{%- set create_dirs = [] %}
{%- set created_groups = [] %}

{% for group, setting in salt['pillar.get']('groups', {}).items() %}
{% if setting.absent is defined and setting.absent or setting.get('state', "present") == 'absent' %}
users_group_absent_{{ group }}:
group.absent:
- name: {{ group }}
{% else %}
{%- do created_groups.append(group) %}
users_group_present_{{ group }}:
group.present:
- name: {{ group }}
Expand Down Expand Up @@ -75,26 +78,40 @@ include:
{%- set user_group = name -%}
{%- endif %}

{%- if createhome %}
{%- set required_dir = salt['file.dirname'](home) %}
{%- do create_dirs.append(required_dir) %}
{%- endif %}

{%- if not ( 'sudoonly' in user and user['sudoonly'] ) %}
{% for group in user.get('groups', []) %}
users_{{ name }}_{{ group }}_group:

{#- create prime group #}
{%- if user_group not in created_groups %}
{%- do created_groups.append(user_group) %}
users_group_present_{{ user_group }}:
group.present:
- name: {{ user_group }}
{%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
- gid: {{ user['prime_group']['gid'] }}
{%- elif 'uid' in user %}
- gid: {{ user['uid'] }}
{%- endif %}
{% if 'system' in user and user['system'] %}
- system: True
{% endif %}
{%- endif %}

{#- create required groups #}
{%- for group in user.get('groups', []) if group not in created_groups %}
{%- do created_groups.append(group) %}
users_group_present_{{ group }}:
group.present:
- name: {{ group }}
{% if group == 'sudo' %}
- system: True
{% endif %}
{% endfor %}

{# in case home subfolder doesn't exist, create it before the user exists #}
{% if createhome -%}
users_{{ name }}_user_prereq:
file.directory:
- name: {{ salt['file.dirname'](home) }}
- makedirs: True
- prereq:
- user: users_{{ name }}_user
{%- endif %}

users_{{ name }}_user:
{% if createhome -%}
file.directory:
Expand All @@ -107,16 +124,6 @@ users_{{ name }}_user:
- user: users_{{ name }}_user
- group: {{ user_group }}
{%- endif %}
group.present:
- name: {{ user_group }}
{%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
- gid: {{ user['prime_group']['gid'] }}
{%- elif 'uid' in user %}
- gid: {{ user['uid'] }}
{%- endif %}
{% if 'system' in user and user['system'] %}
- system: True
{% endif %}
user.present:
- name: {{ name }}
- home: {{ home }}
Expand Down Expand Up @@ -207,6 +214,9 @@ users_{{ name }}_user:
{% for group in user.get('groups', []) -%}
- group: {{ group }}
{% endfor %}
{%- if createhome and required_dir != '/' %}
- file: users_prereq_dir_{{ required_dir }}
{%- endif %}


{% if 'ssh_keys' in user or
Expand Down Expand Up @@ -568,6 +578,14 @@ users_{{ name }}_user_gitconfig_absent_{{ key }}:

{% endfor %}

{#- create directories for homes #}
{%- for dir in create_dirs|unique|reject('equalto', '/') %}
users_prereq_dir_{{ dir }}:
file.directory:
- name: {{ dir }}
- makedirs: True
{%- endfor %}


{% for name, user in pillar.get('users', {}).items()
if user.absent is defined and user.absent %}
Expand Down