Merge "Fix Hypervisor dashboard"
diff --git a/.gitignore b/.gitignore
index 6f76275..aa8e42a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
+.kitchen
 tests/build/
 *.swp
-*.pyc
\ No newline at end of file
+*.pyc
+.ropeproject
diff --git a/.gitreview b/.gitreview
deleted file mode 100644
index 0c96f77..0000000
--- a/.gitreview
+++ /dev/null
@@ -1,4 +0,0 @@
-[gerrit]
-host=review.openstack.org
-port=29418
-project=openstack/salt-formula-nova.git
diff --git a/Makefile b/Makefile
index fc83783..1043fbe 100644
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,38 @@
 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\-]*')
+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
+
+KITCHEN_LOCAL_YAML?=.kitchen.yml
+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 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
@@ -14,6 +40,7 @@
 	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)
@@ -21,6 +48,71 @@
 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 $(NEW_MAJOR_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
index 1f21e51..3fd1918 100644
--- a/README.rst
+++ b/README.rst
@@ -237,6 +237,29 @@
           secret_uuid: 03006edd-d957-40a3-ac4c-26cd254b3731
 
 
+Client role
+-----------
+
+Nova flavors
+
+.. code-block:: yaml
+
+  nova:
+    client:
+      enabled: true
+      server:
+        identity:
+          flavor:
+            jirka-flavor1:
+              flavor_id: 10
+              ram: 4096
+              disk: 10
+              vcpus: 1
+        identity1:
+          flavor:
+            ...
+
+
 Documentation and Bugs
 ============================
 
@@ -262,3 +285,36 @@
 Developers should also join the discussion on the IRC list, at:
 
     https://wiki.openstack.org/wiki/Meetings/openstack-salt
+
+Documentation and Bugs
+======================
+
+To learn how to install and update salt-formulas, consult the documentation
+available online at:
+
+    http://salt-formulas.readthedocs.io/
+
+In the unfortunate event that bugs are discovered, they should be reported to
+the appropriate issue tracker. Use Github issue tracker for specific salt
+formula:
+
+    https://github.com/salt-formulas/salt-formula-nova/issues
+
+For feature requests, bug reports or blueprints affecting entire ecosystem,
+use Launchpad salt-formulas project:
+
+    https://launchpad.net/salt-formulas
+
+You can also join salt-formulas-users team and subscribe to mailing list:
+
+    https://launchpad.net/~salt-formulas-users
+
+Developers wishing to work on the salt-formulas projects should always base
+their work on master branch and submit pull request against specific formula.
+
+    https://github.com/salt-formulas/salt-formula-nova
+
+Any questions or feedback is always welcome so feel free to join our IRC
+channel:
+
+    #salt-formulas @ irc.freenode.net
diff --git a/nova/_states/novang.py b/nova/_states/novang.py
new file mode 100644
index 0000000..13a713a
--- /dev/null
+++ b/nova/_states/novang.py
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+'''
+Nova state that ensures that defined flavor is present
+'''
+
+
+def __virtual__():
+    '''
+    Only load if the nova module is in __salt__
+    '''
+    return 'novang' if 'nova.flavor_list' in __salt__ else False
+
+
+def flavor_present(name, flavor_id=0, ram=0, disk=0, vcpus=1, profile=None):
+    '''
+    Ensures that the nova flavor exists
+    
+    '''
+    print profile
+    print name
+    ret = {'name': name,
+           'changes': {},
+           'result': True,
+           'comment': 'Flavor "{0}" already exists'.format(name)}
+    project = __salt__['nova.flavor_list'](profile)
+    print project
+    if 'Error' in project:
+        pass
+    elif name in project:
+        pass
+    else:
+        __salt__['nova.flavor_create'](name, flavor_id, ram, disk, vcpus, profile)
+        ret['comment'] = 'Flavor {0} has been created'.format(name)
+        ret['changes']['Flavor'] = 'Created'
+    return ret
+
+
diff --git a/nova/client.sls b/nova/client.sls
new file mode 100644
index 0000000..355559f
--- /dev/null
+++ b/nova/client.sls
@@ -0,0 +1,34 @@
+{%- from "nova/map.jinja" import client with context %}
+{%- if client.enabled %}
+
+nova_client_packages:
+  pkg.installed:
+  - names: {{ client.pkgs }}
+
+{%- for identity_name, identity in client.server.iteritems() %}
+
+{%- for flavor_name, flavor in identity.flavor.iteritems() %}
+
+nova_openstack_flavor_{{ flavor_name }}:
+  novang.flavor_present:
+    - name: {{ flavor_name }}
+    - profile: {{ identity_name }}
+
+    {%- if flavor.flavor_id is defined %}
+    - flavor_id: {{ flavor.flavor_id }}
+    {%- endif %}
+    {%- if flavor.ram is defined %}
+    - ram: {{ flavor.ram }}
+    {%- endif %}
+    {%- if flavor.disk is defined %}
+    - disk: {{ flavor.disk }}
+    {%- endif %}
+    {%- if flavor.vcpus is defined %}
+    - vcpus: {{ flavor.vcpus }}
+    {%- endif %}
+
+{%- endfor %}
+
+{%- endfor %}
+
+{%- endif %}
diff --git a/nova/files/liberty/nova-compute.conf.Debian b/nova/files/liberty/nova-compute.conf.Debian
index acb4679..a9dc806 100644
--- a/nova/files/liberty/nova-compute.conf.Debian
+++ b/nova/files/liberty/nova-compute.conf.Debian
@@ -182,6 +182,10 @@
 
 {%- if compute.get('ceph', {}).ephemeral is defined %}
 [libvirt]
+disk_cachemodes="network=writeback,block=none"
+cpu_mode=host-passthrough
+virt_type=kvm
+live_migration_flag=VIR_MIGRATE_UNDEFINE_SOURCE,VIR_MIGRATE_PEER2PEER,VIR_MIGRATE_LIVE,VIR_MIGRATE_PERSIST_DEST
 images_type=rbd
 images_rbd_pool={{ compute.ceph.rbd_pool }}
 images_rbd_ceph_conf=/etc/ceph/ceph.conf
diff --git a/nova/files/mitaka/nova-controller.conf.Debian b/nova/files/mitaka/nova-controller.conf.Debian
index 5d533b6..88589fc 100644
--- a/nova/files/mitaka/nova-controller.conf.Debian
+++ b/nova/files/mitaka/nova-controller.conf.Debian
@@ -124,7 +124,8 @@
 [cache]
 {%- if controller.cache is defined %}
 enabled = true
-memcached_servers={%- for member in controller.cache.members %}{{ member.host }}:11211{% if not loop.last %},{% endif %}{%- endfor %}
+backend = oslo_cache.memcache_pool
+memcache_servers={%- for member in controller.cache.members %}{{ member.host }}:11211{% if not loop.last %},{% endif %}{%- endfor %}
 {%- endif %}
 
 [keystone_authtoken]
diff --git a/nova/init.sls b/nova/init.sls
index 61c4322..3cd900b 100644
--- a/nova/init.sls
+++ b/nova/init.sls
@@ -6,3 +6,6 @@
 {%- if pillar.nova.compute is defined %}
 - nova.compute
 {%- endif %}
+{% if pillar.nova.client is defined %}
+- nova.client
+{% endif %}
diff --git a/nova/map.jinja b/nova/map.jinja
index 2c2d16f..0a8cf24 100644
--- a/nova/map.jinja
+++ b/nova/map.jinja
@@ -26,6 +26,16 @@
     },
 }, merge=pillar.nova.get('controller', {})) %}
 
+
+{% set client = salt['grains.filter_by']({
+    'Debian': {
+        'pkgs': ['python-novaclient']
+    },
+    'RedHat': {
+        'pkgs': ['python-novaclient']
+    },
+}, merge=pillar.nova.get('client', {})) %}
+
 {% set compute = salt['grains.filter_by']({
     'Debian': {
         'pkgs': ['nova-compute-kvm', 'python-novaclient', 'pm-utils', 'sysfsutils', 'sg3-utils', 'libvirt-bin', 'python-memcache', 'qemu-kvm','python-guestfs', 'gettext-base'],
diff --git a/nova/meta/grafana.yml b/nova/meta/grafana.yml
index d291680..cee747e 100644
--- a/nova/meta/grafana.yml
+++ b/nova/meta/grafana.yml
@@ -5,3 +5,31 @@
   hypervisor:
     format: json
     template: nova/files/grafana_dashboards/hypervisor_influxdb.json
+  main:
+    row:
+      ost-control-plane:
+        title: OpenStack Control Plane
+        panel:
+          nova:
+            title: Nova
+            links:
+            - dashboard: Nova
+              title: Nova
+              type: dashboard
+            target:
+              cluster_status:
+                rawQuery: true
+                query: SELECT last(value) FROM cluster_status WHERE cluster_name = 'nova-control' AND environment_label = '$environment' AND $timeFilter GROUP BY time($interval) fill(null)
+      ost-data-plane:
+        title: OpenStack Data Plane
+        panel:
+          nova:
+            title: Nova
+            links:
+            - dashboard: Nova
+              title: Nova
+              type: dashboard
+            target:
+              cluster_status:
+                rawQuery: true
+                query: SELECT last(value) FROM cluster_status WHERE cluster_name = 'nova-data' AND environment_label = '$environment' AND $timeFilter GROUP BY time($interval) fill(null)