Skip to content

Commit

Permalink
Merge pull request saltstack-formulas#71 from netmanagers/master
Browse files Browse the repository at this point in the history
fix(states): deploy packages only if required
  • Loading branch information
myii authored Oct 23, 2020
2 parents 59c3288 + bb60deb commit a68dc3d
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 249 deletions.
47 changes: 25 additions & 22 deletions packages/archives.sls
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{% from "packages/map.jinja" import packages with context %}
{%- from "packages/map.jinja" import packages with context %}
{% set req_packages = packages.pkgs.required.pkgs + packages.archives.pkgs.required %}
{%- set wanted_archives = packages.archives.required.archives %}
{%- do wanted_archives.update( packages.archives.wanted ) %}
{%- set unwanted_archives = packages.archives.unwanted %}
{%- if wanted_archives or unwanted_archives %}
{%- set req_packages = packages.pkgs.required.pkgs + packages.archives.pkgs.required %}
include:
- packages.pkgs
Expand All @@ -11,20 +17,16 @@ extend:
pkg.installed:
- pkgs: {{ req_packages | json }}
{% set wanted_archives = packages.archives.required.archives %}
{% do wanted_archives.update( packages.archives.wanted ) %}
{% set unwanted_archives = packages.archives.unwanted %}
# unwanted 'archive' software
{% for file_or_directory in unwanted_archives %}
{%- for file_or_directory in unwanted_archives %}
packages-archive-unwanted-{{ file_or_directory }}:
file.absent:
- name: {{ file_or_directory }}
{% endfor %}
{%- endfor %}
# wanted 'archive' software
{% for package, archive in wanted_archives.items() %}
{%- set archivename = archive.dl.source.split('/')[-1] %}
{%- for package, archive in wanted_archives.items() %}
{%- set archivename = archive.dl.source.split('/')[-1] %}
packages-archive-wanted-target-{{ package }}-directory:
file.directory:
Expand All @@ -39,7 +41,7 @@ packages-archive-wanted-target-{{ package }}-directory:
- require_in:
- packages-archive-wanted-download-{{ package }}
{%- if 'format' in archive.dl and archive.dl.format in packages.archives.types %}
{%- if 'format' in archive.dl and archive.dl.format in packages.archives.types %}
packages-archive-wanted-download-{{ package }}:
cmd.run:
Expand All @@ -48,7 +50,7 @@ packages-archive-wanted-download-{{ package }}:
- retry: {{ packages.retry_options|json }}
{%- if 'hashsum' in archive.dl and archive.dl.hashsum %}
{# see https://github.com/saltstack/salt/pull/41914 #}
{# see https://github.com/saltstack/salt/pull/41914 #}
packages-archive-wanted-{{ package }}-check-hashsum:
module.run:
Expand All @@ -67,18 +69,18 @@ packages-archive-wanted-install-{{ package }}:
- source: file://{{ packages.tmpdir }}/{{ archivename }}
- name: {{ archive.dest }}/
- archive_format: {{ archive.dl.format }}
{%- if 'hashurl' in archive.dl and archive.dl.hashurl %}
{%- if 'hashurl' in archive.dl and archive.dl.hashurl %}
- source_hash: {{ archive.dl.hashurl }}
{%- endif %}
{%- if 'options' in archive and archive.options %}
{%- endif %}
{%- if 'options' in archive and archive.options %}
- options: {{ archive.options }}
- enforce_toplevel: {{ 'False' if 'strip-components' in archive.options else 'True' }}
{%- endif %}
{%- endif %}
- onlyif: test -d {{ archive.dest }}
- require:
- packages-archive-wanted-download-{{ package }}
{%- else %}
{%- else %}
packages-archive-wanted-download-{{ package }}:
file.managed:
Expand All @@ -87,12 +89,13 @@ packages-archive-wanted-download-{{ package }}:
- mode: {{ '0755' if archive.dl.format in ('bin',) else '0644' if 'mode' not in archive else archive.mode }}
- user: {{ packages.rootuser if 'user' not in archive else archive.user }}
- makedirs: True
{%- if 'hashsum' in archive.dl and archive.dl.hashsum %}
{%- if 'hashsum' in archive.dl and archive.dl.hashsum %}
- source_hash: {{ archive.dl.hashsum }}
{%- else %}
{%- else %}
- skip_verify: True
{%- endif %}
{%- endif %}
- retry: {{ packages.retry_options|json }}
{% endif %}
{%- endfor %}
{%- endif %}
{%- endfor %}
{%- endif %}
49 changes: 27 additions & 22 deletions packages/chocolatey.sls
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{% from "packages/map.jinja" import packages with context %}
{%- from "packages/map.jinja" import packages with context %}
{% if grains['os'] == 'Windows' %}
{%- if grains['os'] == 'Windows' %}
{% if packages.chocolatey %}
{% set req_states = packages.chocolatey.required.states %}
{% set req_pkgs = packages.chocolatey.required.pkgs %}
{% set wanted_chocolatey = packages.chocolatey.wanted %}
{% set unwanted_chocolatey = packages.chocolatey.unwanted %}
{%- if packages.chocolatey %}
{%- set req_states = packages.chocolatey.required.states %}
{%- set req_pkgs = packages.chocolatey.required.pkgs %}
{%- set wanted_chocolatey = packages.chocolatey.wanted %}
{%- set unwanted_chocolatey = packages.chocolatey.unwanted %}
{% if req_states %}
{%- if wanted_chocolatey or unwanted_chocolatey %}
{%- if req_states %}
include:
{% for dep in req_states %}
{%- for dep in req_states %}
- {{ dep }}
{% endfor %}
{% endif %}
{%- endfor %}
{%- endif %}
chocolatey_req_pkgs:
pkg.installed:
- pkgs: {{ req_pkgs | json }}
- retry: {{ packages.retry_options|json }}
### CHOCOLATEY PACKAGES to install
{% if wanted_chocolatey %}
{% for choco, settings in wanted_chocolatey.items() %}
{%- if wanted_chocolatey %}
{%- for choco, settings in wanted_chocolatey.items() %}
{{ choco }}:
chocolatey.installed:
- name: {{ choco }}
Expand All @@ -37,17 +38,21 @@ chocolatey_req_pkgs:
- force_x86: {{ False if 'force_x86' not in settings else settings.force_x86 }}
- package_args: {{ '' if 'package_args' not in settings else settings.package_args }}
- allow_multiple: {{ False if 'allow_multiple' not in settings else settings.allow_multiple }}
{% endfor %}
{% endif %}
- require:
- pkg: chocolatey_req_pkgs
{%- endfor %}
{%- endif %}
### CHOCOLATEY PACKAGES to uninstall
{% if unwanted_chocolatey %}
{% for uchoco in unwanted_chocolatey %}
{%- if unwanted_chocolatey %}
{%- for uchoco in unwanted_chocolatey %}
{{ uchoco }}:
chocolatey.uninstalled:
- name: {{ uchoco }}
{% endfor %}
{% endif %}
{% endif %}
{% endif %}
- require:
- pkg: chocolatey_req_pkgs
{%- endfor %}
{%- endif %}
{%- endif %}
{%- endif %}
{%- endif %}
43 changes: 25 additions & 18 deletions packages/gems.sls
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{% from "packages/map.jinja" import packages with context %}

{% set req_states = packages.gems.required.states %}
{% set req_pkgs = packages.gems.required.pkgs %}
{% set wanted_gems = packages.gems.wanted %}
{% set unwanted_gems = packages.gems.unwanted %}
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import packages with context %}
{% if req_states %}
{%- set req_states = packages.gems.required.states %}
{%- set req_pkgs = packages.gems.required.pkgs %}
{%- set wanted_gems = packages.gems.wanted %}
{%- set unwanted_gems = packages.gems.unwanted %}
{%- if wanted_gems or unwanted_gems %}
{%- if req_states %}
include:
{% for dep in req_states %}
{%- for dep in req_states %}
- {{ dep }}
{% endfor %}
{% endif %}
{%- endfor %}
{%- endif %}
### REQ PKGS (without these, some of the WANTED GEMS will fail to install)
gem_req_pkgs:
Expand All @@ -23,25 +27,28 @@ gem_req_pkgs:
### GEMS to install
# (requires the ruby/rubygem deb/rpm installed, either by the system or listed in
# the required packages
{% for gm in wanted_gems %}
{%- for gm in wanted_gems %}
{{ gm }}:
gem.installed:
- require:
- pkg: gem_req_pkgs
{% if req_states %}
{% for dep in req_states %}
{%- if req_states %}
{%- for dep in req_states %}
- sls: {{ dep }}
{% endfor %}
{% endif %}
{%- endfor %}
{%- endif %}
- retry: {{ packages.retry_options|json }}
{#- Not specific to Arch but needed for the newest versions of Ruby #}
{%- if grains.os_family == 'Arch' %}
- rdoc: True
- ri: True
{%- endif %}
{% endfor %}
{%- endfor %}
{% for ugm in unwanted_gems %}
{%- for ugm in unwanted_gems %}
{{ ugm }}:
gem.removed
{% endfor %}
gem.removed:
- require:
- pkg: gem_req_pkgs
{%- endfor %}
{%- endif %}
8 changes: 4 additions & 4 deletions packages/map.jinja
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
# vim: ft=jinja

{% import_yaml 'packages/defaults.yaml' as defaults %}
{% import_yaml 'packages/osfamilymap.yaml' as osfamilymap %}
{% import_yaml 'packages/osmap.yaml' as osmap %}
{%- import_yaml 'packages/defaults.yaml' as defaults %}
{%- import_yaml 'packages/osfamilymap.yaml' as osfamilymap %}
{%- import_yaml 'packages/osmap.yaml' as osmap %}

{% set packages = salt['grains.filter_by'](
{%- set packages = salt['grains.filter_by'](
defaults,
merge = salt['grains.filter_by'](
osfamilymap,
Expand Down
73 changes: 37 additions & 36 deletions packages/npms.sls
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{% from "packages/map.jinja" import packages with context %}
{%- from "packages/map.jinja" import packages with context %}
{% set req_states = packages.npms.required.states %}
{% set req_pkgs = packages.npms.required.pkgs %}
{% set wanted_npms = packages.npms.wanted %}
{% set unwanted_npms = packages.npms.unwanted %}
{% if req_states %}
include:
{% for dep in req_states %}
- {{ dep }}
{% endfor %}
{% endif %}
{%- set req_states = packages.npms.required.states %}
{%- set req_pkgs = packages.npms.required.pkgs %}
{%- set wanted_npms = packages.npms.wanted %}
{%- set unwanted_npms = packages.npms.unwanted %}
# As we depend on npm installed, if this state file is invoked every time
# if will fail with 'npm not found'. This condition makes sure it's run
# only when explicitly asking for adding/removing npms
{% if wanted_npms or unwanted_npms %}
{%- if wanted_npms or unwanted_npms %}
{%- if req_states %}
include:
{%- for dep in req_states %}
- {{ dep }}
{%- endfor %}
{%- endif %}
### REQ PKGS (without these, some of the WANTED NPMS will fail to install)
npm_req_pkgs:
Expand All @@ -28,49 +28,50 @@ npm_req_pkgs:
# (requires the npm binary installed, either by the system or listed in
# the required packages
{% if packages.npms.dir is defined %}
{%- if packages.npms.dir is defined %}
npms_dir:
file.directory:
- name: {{ packages.npms.dir }}
- user: {{ packages.rootuser if 'user' not in packages.npms else packages.npms.user }}
- group: {{ packages.rootuser if 'group' not in packages.npms else packages.npms.group }}
- mode: {{ '0755' if 'mode' not in packages.npms else packages.npms.mode }}
- makedirs: True
{% endif %}
{%- endif %}
wanted_npms:
npm.installed:
- pkgs: {{ wanted_npms | json }}
{% if packages.npms.dir is defined %}
{%- if packages.npms.dir is defined %}
- dir: {{ packages.npms.dir }}
{% endif %}
{% if packages.npms.user is defined %}
{%- endif %}
{%- if packages.npms.user is defined %}
- user: {{ packages.npms.user }}
{% endif %}
{% if packages.npms.registry is defined %}
{%- endif %}
{%- if packages.npms.registry is defined %}
- registry: {{ packages.npms.registry }}
{% endif %}
{% if packages.npms.env is defined %}
{%- endif %}
{%- if packages.npms.env is defined %}
- env: {{ packages.npms.env | json }}
{% endif %}
{% if packages.npms.force_reinstall is defined %}
{%- endif %}
{%- if packages.npms.force_reinstall is defined %}
- force_reinstall: {{ packages.npms.force_reinstall }}
{% endif %}
{%- endif %}
- require:
{% if packages.npms.dir is defined %}
{%- if packages.npms.dir is defined %}
- file: npms_dir
{% endif %}
{%- endif %}
- pkg: npm_req_pkgs
{% if req_states %}
{% for dep in req_states %}
{%- if req_states %}
{%- for dep in req_states %}
- sls: {{ dep }}
{% endfor %}
{% endif %}
{%- endfor %}
{%- endif %}
- retry: {{ packages.retry_options|json }}
{% for upn in unwanted_npms %}
{%- for upn in unwanted_npms %}
{{ upn }}:
npm.removed
{% endfor %}
{% endif %}
npm.removed:
- require:
- pkg: npm_req_pkgs
{%- endfor %}
{%- endif %}
Loading

0 comments on commit a68dc3d

Please sign in to comment.