Initial commit of backports formula
PROD-33570
Change-Id: I0bf5d7458850ce837b3818da1c84c95db330204c
(cherry picked from commit 3895982e0c917c29db62adbdee496ff06ed80a1b)
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..505388c
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2017-2019 Mirantis Inc. et al
+
+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.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..fb69046
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,126 @@
+DESTDIR=/
+SALTENVDIR=/usr/share/salt-formulas/env
+RECLASSDIR=/usr/share/salt-formulas/reclass
+FORMULANAME=$(shell grep name: metadata.yml|head -1|cut -d : -f 2|grep -Eo '[a-z0-9\-\_]*')
+VERSION=$(shell grep version: metadata.yml|head -1|cut -d : -f 2|grep -Eo '[a-z0-9\.\-\_]*')
+VERSION_MAJOR := $(shell echo $(VERSION)|cut -d . -f 1-2)
+VERSION_MINOR := $(shell echo $(VERSION)|cut -d . -f 3)
+
+NEW_MAJOR_VERSION ?= $(shell date +%Y.%m|sed 's,\.0,\.,g')
+NEW_MINOR_VERSION ?= $(shell /bin/bash -c 'echo $$[ $(VERSION_MINOR) + 1 ]')
+
+MAKE_PID := $(shell echo $$PPID)
+JOB_FLAG := $(filter -j%, $(subst -j ,-j,$(shell ps T | grep "^\s*$(MAKE_PID).*$(MAKE)")))
+
+ifneq ($(subst -j,,$(JOB_FLAG)),)
+JOBS := $(subst -j,,$(JOB_FLAG))
+else
+JOBS := 1
+endif
+
+ifeq (,$(wildcard ./.kitchen.openstack.yml))
+KITCHEN_LOCAL_YAML?=.kitchen.openstack.yml
+else
+KITCHEN_LOCAL_YAML?=.kitchen.yml
+endif
+KITCHEN_OPTS?="--concurrency=$(JOBS)"
+KITCHEN_OPTS_CREATE?=""
+KITCHEN_OPTS_CONVERGE?=""
+KITCHEN_OPTS_VERIFY?=""
+KITCHEN_OPTS_TEST?=""
+
+all:
+ @echo "make install - Install into DESTDIR"
+ @echo "make lint - Run lint tests"
+ @echo "make test - Run tests"
+ @echo "make kitchen - Run Kitchen CI tests (create, converge, verify)"
+ @echo "make clean - Cleanup after tests run"
+ @echo "make release-major - Generate new major release"
+ @echo "make release-minor - Generate new minor release"
+ @echo "make changelog - Show changes since last release"
+
+install:
+ # Formula
+ [ -d $(DESTDIR)/$(SALTENVDIR) ] || mkdir -p $(DESTDIR)/$(SALTENVDIR)
+ cp -a $(FORMULANAME) $(DESTDIR)/$(SALTENVDIR)/
+ [ ! -d _modules ] || cp -a _modules $(DESTDIR)/$(SALTENVDIR)/
+ [ ! -d _states ] || cp -a _states $(DESTDIR)/$(SALTENVDIR)/ || true
+ [ ! -d _grains ] || cp -a _grains $(DESTDIR)/$(SALTENVDIR)/ || true
+ # Metadata
+ [ -d $(DESTDIR)/$(RECLASSDIR)/service/$(FORMULANAME) ] || mkdir -p $(DESTDIR)/$(RECLASSDIR)/service/$(FORMULANAME)
+ cp -a metadata/service/* $(DESTDIR)/$(RECLASSDIR)/service/$(FORMULANAME)
+
+lint:
+ [ ! -d tests ] || (cd tests; ./run_tests.sh lint)
+
+test:
+ [ ! -d tests ] || (cd tests; ./run_tests.sh)
+
+release-major: check-changes
+ @echo "Current version is $(VERSION), new version is $(NEW_MAJOR_VERSION)"
+ @[ $(VERSION_MAJOR) != $(NEW_MAJOR_VERSION) ] || (echo "Major version $(NEW_MAJOR_VERSION) already released, nothing to do. Do you want release-minor?" && exit 1)
+ echo "$(NEW_MAJOR_VERSION)" > VERSION
+ sed -i 's,version: .*,version: "$(NEW_MAJOR_VERSION)",g' metadata.yml
+ [ ! -f debian/changelog ] || dch -v $(NEW_MAJOR_VERSION) -m --force-distribution -D `dpkg-parsechangelog -S Distribution` "New version"
+ make genchangelog-$(NEW_MAJOR_VERSION)
+ (git add -u; git commit -m "Version $(NEW_MAJOR_VERSION)")
+ git tag -s -m $(NEW_MAJOR_VERSION) $(NEW_MAJOR_VERSION)
+
+release-minor: check-changes
+ @echo "Current version is $(VERSION), new version is $(VERSION_MAJOR).$(NEW_MINOR_VERSION)"
+ echo "$(VERSION_MAJOR).$(NEW_MINOR_VERSION)" > VERSION
+ sed -i 's,version: .*,version: "$(VERSION_MAJOR).$(NEW_MINOR_VERSION)",g' metadata.yml
+ [ ! -f debian/changelog ] || dch -v $(VERSION_MAJOR).$(NEW_MINOR_VERSION) -m --force-distribution -D `dpkg-parsechangelog -S Distribution` "New version"
+ make genchangelog-$(VERSION_MAJOR).$(NEW_MINOR_VERSION)
+ (git add -u; git commit -m "Version $(VERSION_MAJOR).$(NEW_MINOR_VERSION)")
+ git tag -s -m $(VERSION_MAJOR).$(NEW_MINOR_VERSION) $(VERSION_MAJOR).$(NEW_MINOR_VERSION)
+
+check-changes:
+ @git log --pretty=oneline --decorate $(VERSION)..HEAD | grep -Eqc '.*' || (echo "No new changes since version $(VERSION)"; exit 1)
+
+changelog:
+ git log --pretty=short --invert-grep --grep="Merge pull request" --decorate $(VERSION)..HEAD
+
+genchangelog: genchangelog-$(VERSION_MAJOR).$(NEW_MINOR_VERSION)
+
+genchangelog-%:
+ $(eval NEW_VERSION := $(patsubst genchangelog-%,%,$@))
+ (echo "=========\nChangelog\n=========\n"; \
+ (echo $(NEW_VERSION);git tag) | sort -r | grep -E '^[0-9\.]+' | while read i; do \
+ cur=$$i; \
+ test $$i = $(NEW_VERSION) && i=HEAD; \
+ prev=`(echo $(NEW_VERSION);git tag)|sort|grep -E '^[0-9\.]+'|grep -B1 "$$cur\$$"|head -1`; \
+ echo "Version $$cur\n=============================\n"; \
+ git log --pretty=short --invert-grep --grep="Merge pull request" --decorate $$prev..$$i; \
+ echo; \
+ done) > CHANGELOG.rst
+
+kitchen-check:
+ @[ -e $(KITCHEN_LOCAL_YAML) ] || (echo "Kitchen tests not available, there's no $(KITCHEN_LOCAL_YAML)." && exit 1)
+
+kitchen: kitchen-check kitchen-create kitchen-converge kitchen-verify kitchen-list
+
+kitchen-create: kitchen-check
+ kitchen create ${KITCHEN_OPTS} ${KITCHEN_OPTS_CREATE}
+ [ "$(shell echo $(KITCHEN_LOCAL_YAML)|grep -Eo docker)" = "docker" ] || sleep 120
+
+kitchen-converge: kitchen-check
+ kitchen converge ${KITCHEN_OPTS} ${KITCHEN_OPTS_CONVERGE} &&\
+ kitchen converge ${KITCHEN_OPTS} ${KITCHEN_OPTS_CONVERGE}
+
+kitchen-verify: kitchen-check
+ [ ! -d tests/integration ] || kitchen verify -t tests/integration ${KITCHEN_OPTS} ${KITCHEN_OPTS_VERIFY}
+ [ -d tests/integration ] || kitchen verify ${KITCHEN_OPTS} ${KITCHEN_OPTS_VERIFY}
+
+kitchen-test: kitchen-check
+ [ ! -d tests/integration ] || kitchen test -t tests/integration ${KITCHEN_OPTS} ${KITCHEN_OPTS_TEST}
+ [ -d tests/integration ] || kitchen test ${KITCHEN_OPTS} ${KITCHEN_OPTS_TEST}
+
+kitchen-list: kitchen-check
+ kitchen list
+
+clean:
+ [ ! -x "$(shell which kitchen)" ] || kitchen destroy
+ [ ! -d .kitchen ] || rm -rf .kitchen
+ [ ! -d tests/build ] || rm -rf tests/build
+ [ ! -d build ] || rm -rf build
diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..95b0485
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,219 @@
+=======================
+ Salt formula backports
+=======================
+------------
+ Readme file
+------------
+
+Setting up downstream mirror
+============================
+
+Setup formula syncronisation after you deployed Drivertain. The backport state can be used to apply any patches/modification which hadn't been included to offical packages. The state uses the backports:patches pillar to keep the neccessary information.
+
+Step 1:
+Check jenkins_admin_public_key and add it to service user in gerrit(git repo).
+
+Got to https://gerrit.mirantis.com/login/
+Login as "service" user.
+Navigate to Setting -> SSH Public Keys.
+Add new key.
+
+Step 2:
+
+Add following pillar to cicd leader::
+
+ parameters:
+ _param:
+ jenkins_git_mirror_downstream_jobs:
+ - name: salt-formula-backports
+ downstream: formulas/salt-formula-backports
+ upstream: "ssh://robot@gerrit.example.com:29418/formulas/salt-formula-backports"
+ branches: master
+ gerrit:
+ client:
+ project:
+ formulas/salt-formula-backports:
+ enabled: true
+ description: Backport formula
+ upstream: "ssh://robot@gerrit.example.com:29418/formulas/salt-formula-backports"
+ access: ${gerrit:client:default_access}
+ require_change_id: true
+ require_agreement: false
+ merge_content: true
+
+Step 3:
+Run the highstate on cid01 node::
+
+ salt "cid01*" state.highstate
+
+Setting up downstream mirror
+============================
+
+This this is how you can setup automated formula installation on salt master from local gerrit.
+Step 1:
+Add following pillar to config::
+
+ root@cfg01:/srv/salt/reclass# cat ./classes/cluster/mlab/infra/backports/formula.yml
+ parameters:
+ _param:
+ local_salt_formulas: http://${_param:cicd_control_address}:8080/formulas
+ local_salt_formulas_revision: master
+ salt:
+ master:
+ environment:
+ prd:
+ formula:
+ backports:
+ source: git
+ address: '${_param:local_salt_formulas}/salt-formula-backports'
+ revision: ${_param:local_salt_formulas_revision}
+
+
+Step 2:
+
+Run the highstate on cfg01 node::
+
+ salt 'cfg01*' state.highstate
+
+How to enable specific patch
+============================
+For example to enable patch on salt master's ntp formula
+
+Add following class or define the pillar on the salt master (make sure pillar is visible only for this particular node)::
+
+ root@cfg01:/srv/salt/reclass# cat ./classes/cluster/lab/infra/config/patches.yml
+ applications:
+ - backports
+
+ parameters:
+ backports:
+ patches:
+ compute_patch: # you can refer to jira issue, gerrit CR, salesforce ID or any other id.
+ /etc/init/nova-compute-kvm-upstart.conf: # file to apply the patch
+ md5sum: 34dd520613bda0bf572a3bcee5767d29 # md5sum of resulted file
+ diff: |
+ --- /etc/init/nova-compute-kvm-upstart.conf 2018-03-31 20:48:30.000000000 +0800
+ +++ nova-compute-kvm-upstart.conf.orig 2019-05-07 20:58:26.601836128 +0800
+ @@ -1,7 +1,7 @@
+ description "OpenStack Compute"
+ author "Thomas Goirand <zigo@debian.org>"
+
+ -start on started libvirt-bin
+ +start on started libvirtd
+ stop on runlevel [!2345]
+
+ chdir /var/run
+
+
+How to create a new patch
+=========================
+
+Back original file::
+
+ cp /etc/init/nova-compute-kvm-upstart.conf nova-compute-kvm-upstart.conf.orig
+
+Make the necessary change::
+
+ vim /etc/init/nova-compute-kvm-upstart.conf
+
+Run the diff command to see the difference between files::
+
+ diff -u nova-compute-kvm-upstart.conf.orig /etc/init/nova-compute-kvm-upstart.conf
+
+ --- /etc/init/nova-compute-kvm-upstart.conf 2018-03-31 20:48:30.000000000 +0800
+ +++ nova-compute-kvm-upstart.conf.orig 2019-05-07 20:58:26.601836128 +0800
+ @@ -1,7 +1,7 @@
+ description "OpenStack Compute"
+ author "Thomas Goirand <zigo@debian.org>"
+
+ -start on started libvirt-bin
+ +start on started libvirtd
+ stop on runlevel [!2345]
+
+ chdir /var/run
+
+Save the output into files direcotry in formula::
+
+ files/patch-init-nova-compute.conf
+
+check the md5 sum for the file and add into resource::
+
+ md5sum /etc/init/nova-compute-kvm-upstart.conf
+ 34dd520613bda0bf572a3bcee5767d29 /etc/init/nova-compute-kvm-upstart.conf
+
+This info should be enough to create the pillar data::
+
+ backports:
+ patches:
+ compute_patch:
+ /etc/init/nova-compute-kvm-upstart.conf:
+ md5sum: 34dd520613bda0bf572a3bcee5767d29
+ diff: |
+ --- /etc/init/nova-compute-kvm-upstart.conf 2018-03-31 20:48:30.000000000 +0800
+ +++ nova-compute-kvm-upstart.conf.orig 2019-05-07 20:58:26.601836128 +0800
+ @@ -1,7 +1,7 @@
+ description "OpenStack Compute"
+ author "Thomas Goirand <zigo@debian.org>"
+
+ -start on started libvirt-bin
+ +start on started libvirtd
+ stop on runlevel [!2345]
+
+ chdir /var/run
+
+If the patch data contains any special characters and pillar is failed to build you can use base64 enconding for patch code::
+
+
+ backports:
+ patches:
+ compute_patch:
+ /usr/share/salt-formulas/env/oslo_templates/files/queens/oslo/messaging/_rabbit.conf:
+ md5sum: 73a3eebf769b3038a7c65a5019141938
+ encoding: base64
+ diff: |
+ RnJvbSBiOTIzMGIzMGYwNGRkOTE4YzliOWI0NzkzYjIwNWYwYTZmM2M2ZDZmIE1vbiBTZXAgMTcg
+ ...
+ ID0ge3sgX2RhdGEucnBjX3JldHJ5X2RlbGF5IH19Cit7JS0gZW5kaWYgJX0K
+
+
+Best practice:
+==============
+
+1. Make sure product bug/ticket/review is created to resolve the problem in upstream.
+2. Add a link to the product ticket/review next to the resource in the formula.
+3. Make sure to add service restart if needed.
+4. Pay attention to failed patch resources. Most likely this means that file was changed. review the file and update or disable the patch if necessary.
+
+
+Full pillar list:
+=================
+
+Pillar::
+
+ applications:
+ - backports
+
+ parameters:
+ backports:
+ patch_directory: "/tmp/patches"
+ patches:
+ PROD-26834:
+ /usr/share/salt-formulas/env/jenkins/client/init.sls:
+ md5sum: bdce63b782f9056338cd43114b9b7dfc
+ diff: |
+ diff --git a/jenkins/client/init.sls b/jenkins/client/init.sls
+ index 9c8509c..85cacb3 100644
+ ......
+ - jenkins.client.throttle_category
+ {%- endif %}
+ /usr/share/salt-formulas/env/jenkins/_states/jenkins_location.py:
+ md5sum: e9212236971306230710b41493d7d2fa
+ diff: |
+ diff --git a/_states/jenkins_location.py b/_states/jenkins_location.py
+ new file mode 100644
+ index 0000000..7aac8bf
+ ......
+ + ['CHANGED', 'EXISTS'],
+ + {'url': url, 'email': email},
+ + 'location config')
+
diff --git a/_states/backport.py b/_states/backport.py
new file mode 100644
index 0000000..6e47caf
--- /dev/null
+++ b/_states/backport.py
@@ -0,0 +1,67 @@
+import logging
+from salt.exceptions import CommandExecutionError, SaltInvocationError
+
+log = logging.getLogger(__name__)
+
+def __virtual__():
+ return True
+
+def patch_applied(name,
+ source=None,
+ options='',
+ dry_run_first=True,
+ **kwargs):
+
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'comment': '',
+ 'result': False,
+ }
+
+ hash_ = kwargs.pop('hash', None)
+ encoding_ = kwargs.pop('encoding', None)
+ content_pillar = kwargs.pop('content_pillar', None)
+
+ data_pillar = __salt__['pillar.get'](content_pillar)
+ log.debug('Patch %s data from pillar %s is going to be applied to %s' % (name,content_pillar,source))
+
+ try:
+ if hash_ and __salt__['file.check_hash'](source, hash_):
+ ret['result'] = True
+ ret['comment'] = 'Patch is already applied'
+ return ret
+ except (SaltInvocationError, ValueError) as exc:
+ ret['comment'] = exc.__str__()
+ return ret
+ except IOError:
+ log.info("There is no original file %s available" % source)
+
+ if encoding_ == 'base64':
+ __salt__['hashutil.base64_decodefile']( instr=data_pillar, outfile=name)
+ log.info("Base64 patch identified")
+ else:
+ __salt__['file.manage_file']( name=name,
+ sfn=None,
+ ret=None,
+ source=None,
+ source_sum=None,
+ user=None,
+ group=None,
+ mode=None,
+ saltenv='base',
+ backup=None,
+ makedirs=True,
+ template=None,
+ show_changes=False,
+ contents=data_pillar,
+ dir_mode=None)
+
+ patch_ret = __salt__['file.patch'](source, name)
+ log.debug('Returning patch status %s' % patch_ret)
+ if patch_ret:
+ ret['comment'] = 'Patch file was succesfully applied'
+ ret['result'] = True
+ ret['changes'] = patch_ret.get('stdout',{})
+
+ return ret
diff --git a/backports/init.sls b/backports/init.sls
new file mode 100644
index 0000000..526d7c2
--- /dev/null
+++ b/backports/init.sls
@@ -0,0 +1,25 @@
+{%- from "backports/map.jinja" import backports with context %}
+
+{%- if pillar.backports is defined %}
+
+{% set patch_directory = backports.get('patch_directory','/tmp/patches') %}
+
+{% for fix, fix_data in backports.get('patches').iteritems() %}
+{% for patch_source, patch_data in fix_data.iteritems() %}
+{% set patch_filename = patch_directory + "/" + fix + patch_source.replace('/','_') + ".diff" %}
+{% set patch_encoding = patch_data.get('encoding', 'None') %}
+
+apply_patch_{{ fix }}_{{ patch_filename }}:
+ backport.patch_applied:
+ - name: {{ patch_filename }}
+ - source: {{ patch_source }}
+ - hash: {{ patch_data.md5sum }}
+ - encoding: {{ patch_encoding }}
+ - content_pillar: backports:patches:{{ fix }}:{{ patch_source }}:diff
+
+{% endfor %}
+{% endfor %}
+
+{%- endif %}
+backports.completed:
+ test.nop
diff --git a/backports/map.jinja b/backports/map.jinja
new file mode 100644
index 0000000..29b7e8d
--- /dev/null
+++ b/backports/map.jinja
@@ -0,0 +1,6 @@
+{% set backports = salt['grains.filter_by']({
+ 'defaults':{
+ 'patch_directory': '/tmp/patches',
+ },
+ }
+ ,merge=salt['pillar.get']('backports', {}), base='defaults') %}
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..26a4284
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+salt-formula-backports (0.1) xenial; urgency=medium
+
+ * Initial commit
+
+ -- devops <devops@mirantis.com> Thu, 26 Sep 2019 16:34:41 +0400
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+9
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..076d376
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,15 @@
+Source: salt-formula-backports
+Maintainer: Mirantis Dev <dev@mirantis.com>
+Section: admin
+Priority: optional
+Build-Depends: debhelper (>= 9), salt-master, python, python-yaml
+Standards-Version: 3.9.6
+Homepage: https://www.mirantis.com
+Vcs-Browser: https://gerrit.mcp.mirantis.com/#/admin/projects/salt-formulas/backports
+Vcs-Git: https://gerrit.mcp.mirantis.com/salt-formulas/backports.git
+
+Package: salt-formula-backports
+Architecture: all
+Depends: ${misc:Depends}
+Description: backports salt formula
+ Install backport patches.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..d57a371
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,15 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: salt-formula-backports
+Upstream-Contact: Mirantis Dev <dev@mirantis.com>
+Source: https://gerrit.mcp.mirantis.com/#/admin/projects/salt-formulas/backports
+
+Files: *
+Copyright: 2014-2019 Mirantis Inc. et al
+License: Apache-2.0
+ Copyright (C) 2014-2019 Mirantis Inc. et al
+ .
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ .
+ On a Debian system you can find a copy of this license in
+ /usr/share/common-licenses/Apache-2.0.
diff --git a/debian/docs b/debian/docs
new file mode 100644
index 0000000..a1320b1
--- /dev/null
+++ b/debian/docs
@@ -0,0 +1 @@
+README.rst
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..abde6ef
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,5 @@
+#!/usr/bin/make -f
+
+%:
+ dh $@
+
diff --git a/debian/source/format b/debian/source/format
new file mode 100644
index 0000000..89ae9db
--- /dev/null
+++ b/debian/source/format
@@ -0,0 +1 @@
+3.0 (native)
diff --git a/metadata.yml b/metadata.yml
new file mode 100644
index 0000000..39c1fbc
--- /dev/null
+++ b/metadata.yml
@@ -0,0 +1,3 @@
+name: "backports"
+version: "2017.6"
+source: "https://gerrit.mcp.mirantis.com/salt-formulas/backports"
diff --git a/metadata/service/support.yml b/metadata/service/support.yml
new file mode 100644
index 0000000..4ec24a7
--- /dev/null
+++ b/metadata/service/support.yml
@@ -0,0 +1,13 @@
+parameters:
+ backports:
+ _support:
+ collectd:
+ enabled: false
+ heka:
+ enabled: false
+ sensu:
+ enabled: false
+ sphinx:
+ enabled: true
+ fluentd:
+ enabled: true
diff --git a/tests/pillar/init_single.sls b/tests/pillar/init_single.sls
new file mode 100644
index 0000000..b037640
--- /dev/null
+++ b/tests/pillar/init_single.sls
@@ -0,0 +1,3 @@
+parameters:
+ backports:
+ patch_directory: "/tmp/patches"
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
new file mode 100755
index 0000000..29fb975
--- /dev/null
+++ b/tests/run_tests.sh
@@ -0,0 +1,200 @@
+#!/usr/bin/env bash
+
+set -e
+[ -n "$DEBUG" ] && set -x
+
+CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+METADATA=${CURDIR}/../metadata.yml
+FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
+FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
+
+## Overrideable parameters
+PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
+BUILDDIR=${BUILDDIR:-${CURDIR}/build}
+VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
+MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
+DEPSDIR=${BUILDDIR}/deps
+
+SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
+SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
+SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
+SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
+
+SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
+
+if [ "x${SALT_VERSION}" != "x" ]; then
+ PIP_SALT_VERSION="==${SALT_VERSION}"
+fi
+
+## Functions
+log_info() {
+ echo "[INFO] $*"
+}
+
+log_err() {
+ echo "[ERROR] $*" >&2
+}
+
+setup_virtualenv() {
+ log_info "Setting up Python virtualenv"
+ virtualenv $VENV_DIR
+ source ${VENV_DIR}/bin/activate
+ pip install salt${PIP_SALT_VERSION}
+}
+
+setup_mock_bin() {
+ # If some state requires a binary, a lightweight replacement for
+ # such binary can be put into MOCK_BIN_DIR for test purposes
+ if [ -d "${MOCK_BIN_DIR}" ]; then
+ PATH="${MOCK_BIN_DIR}:$PATH"
+ export PATH
+ fi
+}
+
+setup_pillar() {
+ [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
+ echo "base:" > ${SALT_PILLAR_DIR}/top.sls
+ for pillar in ${PILLARDIR}/*; do
+ grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
+ state_name=$(basename ${pillar%.sls})
+ echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
+ done
+}
+
+setup_salt() {
+ [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
+ [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
+ [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
+
+ echo "base:" > ${SALT_FILE_DIR}/top.sls
+ for pillar in ${PILLARDIR}/*.sls; do
+ grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
+ state_name=$(basename ${pillar%.sls})
+ echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
+ done
+
+ cat << EOF > ${SALT_CONFIG_DIR}/minion
+file_client: local
+cachedir: ${SALT_CACHE_DIR}
+verify_env: False
+minion_id_caching: False
+
+file_roots:
+ base:
+ - ${SALT_FILE_DIR}
+ - ${CURDIR}/..
+ - /usr/share/salt-formulas/env
+
+pillar_roots:
+ base:
+ - ${SALT_PILLAR_DIR}
+ - ${PILLARDIR}
+EOF
+}
+
+fetch_dependency() {
+ dep_name="$(echo $1|cut -d : -f 1)"
+ dep_source="$(echo $1|cut -d : -f 2-)"
+ dep_root="${DEPSDIR}/$(basename $dep_source .git)"
+ dep_metadata="${dep_root}/metadata.yml"
+
+ [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
+ [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
+
+ log_info "Fetching dependency $dep_name"
+ [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
+ git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
+ ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
+
+ METADATA="${dep_metadata}" install_dependencies
+}
+
+install_dependencies() {
+ grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
+ (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
+import sys,yaml
+for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
+ print '%s:%s' % (dep["name"], dep["source"])
+EOF
+}
+
+clean() {
+ log_info "Cleaning up ${BUILDDIR}"
+ [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
+}
+
+salt_run() {
+ [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
+ salt-call ${SALT_OPTS} $*
+}
+
+prepare() {
+ [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
+
+ which salt-call || setup_virtualenv
+ setup_mock_bin
+ setup_pillar
+ setup_salt
+ install_dependencies
+}
+
+run() {
+ for pillar in ${PILLARDIR}/*.sls; do
+ grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
+ state_name=$(basename ${pillar%.sls})
+ salt_run grains.set 'noservices' False force=True
+
+ echo "Checking state ${FORMULA_NAME}.${state_name} ..."
+ salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
+
+ # Check that all files in 'meta' folder can be rendered using any valid pillar
+ for meta in `find ${FORMULA_META_DIR} -type f`; do
+ meta_name=$(basename ${meta})
+ echo "Checking meta ${meta_name} ..."
+ salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
+ || (log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1)
+ cat ${SALT_CACHE_DIR}/${meta_name}
+ done
+ done
+}
+
+real_run() {
+ for pillar in ${PILLARDIR}/*.sls; do
+ state_name=$(basename ${pillar%.sls})
+ salt_run --id=${state_name} state.sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
+ done
+}
+
+_atexit() {
+ RETVAL=$?
+ trap true INT TERM EXIT
+
+ if [ $RETVAL -ne 0 ]; then
+ log_err "Execution failed"
+ else
+ log_info "Execution successful"
+ fi
+ return $RETVAL
+}
+
+## Main
+trap _atexit INT TERM EXIT
+
+case $1 in
+ clean)
+ clean
+ ;;
+ prepare)
+ prepare
+ ;;
+ run)
+ run
+ ;;
+ real-run)
+ real_run
+ ;;
+ *)
+ prepare
+ run
+ ;;
+esac