Skip to content

Instantly share code, notes, and snippets.

Created January 2, 2016 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a778ad2adbd8534aac6e to your computer and use it in GitHub Desktop.
Save anonymous/a778ad2adbd8534aac6e to your computer and use it in GitHub Desktop.
sls files. vm sls first, then template further down
sys-net.sls
# -*- coding: utf-8 -*-
# vim: set syntax=yaml ts=2 sw=2 sts=2 et :
##
# qvm.sys-net
# ===========
#
# Installs 'sys-net' NetVM.
#
# Pillar data will also be merged if available within the ``qvm`` pillar key:
# ``qvm:sys-net``
#
# located in ``/srv/pillar/dom0/qvm/init.sls``
#
# Execute:
# qubesctl state.sls qvm.sys-net dom0
##
{%- from "qvm/template.jinja" import load -%}
{% load_yaml as defaults -%}
name: sys-net
present:
- label: red
- mem: 300
- flags:
- net
prefs:
- netvm: 'none'
- autostart: true
- pcidevs: {{ salt['grains.get']('pci_net_devs', []) }}
{%- endload %}
{{ load(defaults) }}
template.jinja
# -*- coding: utf-8 -*-
# vim: set syntax=yaml ts=2 sw=2 sts=2 et :
##
# Virtual Machine State Template
# ==============================
##
#
# get_state_options
#
{% macro get_state_options(vm) -%}
{%- set actions = [] %}
{%- set options = [] %}
{%- set valid_actions = [
'kill',
'halted',
'absent',
'missing',
'present',
'exists',
'prefs',
'service',
'start',
'running',
'pause',
'unpause',
'shutdown',
'run',
'clone',
] %}
{%- set state_options = [
'name',
'require',
] %}
{%- for option in state_options %}
{%- if option in vm %}
{%- do options.append({option: vm[option]}) %}
{%- endif %}
{%- endfor -%}
{%- for action in valid_actions %}
{%- if action in vm %}
{%- do actions.append(action) %}
{%- do options.append({action: vm[action]}) %}
{%- endif %}
{%- endfor -%}
{%- do options.append({'actions': actions }) %}
{%- set state = {'qvm.vm': options} %}
{%- if caller is defined %}
{{ caller(state) }}
{%- else %}
{{- state|yaml(False)|indent(2) }}
{%- endif %}
{%- endmacro %}
#
# state_debug
#
{% macro state_debug(defaults, vm) %}
{% if defaults.get('debug', False) -%}
{{ vm.name }}-debug:
debug.mode:
- enable-all: True
- require-in:
- qvm: {{ vm.name }}
{%- endif %}
{% endmacro %}
#
# state_vm
#
{% macro state_vm(vm) %}
{{ vm.name }}:
{{ get_state_options(vm) }}
{% endmacro %}
#
# skip
#
{% macro skip(vm) -%}
{{ vm.name }}:
qvm.exists: []
{%- endmacro %}
#
# load
#
{% macro load(defaults) -%}
{%- set vmname = defaults.name %}
{%- set pillar_data = salt['pillar.get']('qvm', {}) %}
{%- set vm = defaults %}
{%- do vm.update(pillar_data.get(vmname, {})) %}
{#- Update 'defaults' after vm/template to prevent clobbering data #}
{%- do defaults.update(pillar_data) %}
{%- set force = defaults.get('force', vm.get('force', False)) %}
{#- Only attempt to install the VM if it is missing (not installed) to prevent
# changing an existing configuration, unless ``force`` is True.
#}
{%- if force or salt['qvm.check'](vm.name, 'missing').passed() -%}
{{- state_debug(defaults, vm) }}
{{- state_vm(vm) }}
{%- else -%}
{{- skip(vm) }}
{%- endif -%}
{%- endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment