Adjust Gnocchi formula granularity

Change-Id: Id5e480b44a0d164cbba9ed9b7769e3a301fd0196
Related-Prod: https://mirantis.jira.com/browse/PROD-21937
diff --git a/.gitreview b/.gitreview
index bb83f19..929046e 100644
--- a/.gitreview
+++ b/.gitreview
@@ -1,4 +1,4 @@
 [gerrit]
-host=172.16.48.254
+host=gerrit.mcp.mirantis.com
 port=29418
 project=salt-formulas/gnocchi.git
diff --git a/gnocchi/backend/upgrade.sls b/gnocchi/backend/upgrade.sls
new file mode 100644
index 0000000..7f0bb98
--- /dev/null
+++ b/gnocchi/backend/upgrade.sls
@@ -0,0 +1,9 @@
+{%- from "gnocchi/map.jinja" import server with context %}
+
+gnocchi_upgrade:
+  cmd.run:
+  - name: gnocchi-upgrade
+  {%- if grains.get('noservices') or server.get('role', 'primary') == 'secondary' %}
+  - onlyif: /bin/false
+  {%- endif %}
+  - runas: gnocchi
\ No newline at end of file
diff --git a/gnocchi/map.jinja b/gnocchi/map.jinja
index 69b5d50..c0f5468 100644
--- a/gnocchi/map.jinja
+++ b/gnocchi/map.jinja
@@ -9,6 +9,7 @@
 {%- load_yaml as server_defaults %}
 BaseDefaults: {{ default_params }}
 Debian:
+  enabled: false
   pkgs:
   - gnocchi-api
   - gnocchi-metricd
@@ -21,7 +22,7 @@
 
 {% set server = salt["grains.filter_by"](server_defaults, merge=pillar.gnocchi.get('server', {}), base='BaseDefaults') %}
 
-{%- if server.get('enabled', False) %}
+{%- if server.enabled %}
 
 {% set cfg = salt['grains.filter_by']({
     'default': cfg,
@@ -32,6 +33,7 @@
 {%- load_yaml as statsd_defaults %}
 BaseDefaults: {{ default_params }}
 Debian:
+  enabled: false
   pkgs:
   - gnocchi-statsd
   services:
@@ -41,7 +43,7 @@
 
 {% set statsd = salt["grains.filter_by"](statsd_defaults, merge=pillar.gnocchi.get('statsd', {}), base='BaseDefaults') %}
 
-{%- if statsd.get('enabled', False) %}
+{%- if statsd.enabled %}
 
 {% set cfg = salt['grains.filter_by']({
     'default': cfg,
@@ -52,8 +54,11 @@
 {%- load_yaml as client_defaults %}
 BaseDefaults: {{ default_params }}
 Debian:
+  enabled: false
   pkgs:
   - python-gnocchiclient
 {%- endload %}
 
 {% set client = salt['grains.filter_by'](client_defaults, merge=pillar.gnocchi.get('client', {}), base='BaseDefaults') %}
+
+{%- set upgrade = pillar.get('gnocchi', {}).get('upgrade', {}) %}
diff --git a/gnocchi/meta/salt.yml b/gnocchi/meta/salt.yml
new file mode 100644
index 0000000..71bb39e
--- /dev/null
+++ b/gnocchi/meta/salt.yml
@@ -0,0 +1,5 @@
+orchestration:
+  upgrade:
+    applications:
+      gnocchi:
+        priority: 1850
diff --git a/gnocchi/server.sls b/gnocchi/server.sls
index a8cb5f9..c91d668 100644
--- a/gnocchi/server.sls
+++ b/gnocchi/server.sls
@@ -4,20 +4,15 @@
 include:
   - apache
   - gnocchi._common
+  - gnocchi.backend.upgrade
 
 gnocchi_server_packages:
   pkg.installed:
   - names: {{ server.pkgs }}
-
-gnocchi_upgrade:
-  cmd.run:
-  - name: gnocchi-upgrade
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - runas: gnocchi
   - require:
-    - file: gnocchi_common_conf
+    - sls: gnocchi._common
+  - require_in:
+    - sls: gnocchi.backend.upgrade
 
 apache_enable_gnocchi_wsgi:
   apache_site.enabled:
@@ -26,7 +21,7 @@
   - onlyif: /bin/false
   {%- endif %}
   - require:
-    - cmd: gnocchi_upgrade
+    - sls: gnocchi.backend.upgrade
 
 gnocchi_apache_restart:
   service.running:
@@ -46,7 +41,7 @@
   - onlyif: /bin/false
   {%- endif %}
   - require:
-    - cmd: gnocchi_upgrade
+    - sls: gnocchi.backend.upgrade
     - sls: gnocchi._common
   - watch:
     - gnocchi_common_conf
diff --git a/gnocchi/upgrade/pkgs_latest.sls b/gnocchi/upgrade/pkgs_latest.sls
new file mode 100644
index 0000000..5841805
--- /dev/null
+++ b/gnocchi/upgrade/pkgs_latest.sls
@@ -0,0 +1,38 @@
+{%- from "gnocchi/map.jinja" import server,statsd,client,upgrade with context %}
+
+gnocchi_task_pkg_latest:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.pkg_latest"
+
+policy-rc.d_present:
+  file.managed:
+    - name: /usr/sbin/policy-rc.d
+    - mode: 755
+    - contents: |
+        #!/bin/sh
+        exit 101
+
+{%- set pkgs = [] %}
+{%- if server.enabled %}
+  {%- do pkgs.extend(server.pkgs) %}
+  {# Gnocchi requires python-tenacity package which has no strict dependency on python-tornado #}
+  {%- do pkgs.append('python-tornado') %}
+{%- endif %}
+{%- if statsd.enabled %}
+  {%- do pkgs.extend(statsd.pkgs) %}
+{%- endif %}
+{%- if client.enabled %}
+  {%- do pkgs.extend(client.pkgs) %}
+{%- endif %}
+
+gnocchi_pkg_latest:
+  pkg.latest:
+  - names: {{ pkgs|unique }}
+  - require:
+    - file: policy-rc.d_present
+  - require_in:
+    - file: policy-rc.d_absent
+
+policy-rc.d_absent:
+  file.absent:
+    - name: /usr/sbin/policy-rc.d
diff --git a/gnocchi/upgrade/post/init.sls b/gnocchi/upgrade/post/init.sls
new file mode 100644
index 0000000..a11f799
--- /dev/null
+++ b/gnocchi/upgrade/post/init.sls
@@ -0,0 +1,7 @@
+gnocchi_post:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.post"
+
+keystone_os_client_config_absent:
+  file.absent:
+    - name: /etc/openstack/clouds.yml
diff --git a/gnocchi/upgrade/pre/init.sls b/gnocchi/upgrade/pre/init.sls
new file mode 100644
index 0000000..62fa99d
--- /dev/null
+++ b/gnocchi/upgrade/pre/init.sls
@@ -0,0 +1,14 @@
+gnocchi_pre:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.pre"
+
+{%- set os_content = salt['mine.get']('I@keystone:client:os_client_config:enabled:true', 'keystone_os_client_config', 'compound').values()[0] %}
+keystone_os_client_config:
+  file.managed:
+    - name: /etc/openstack/clouds.yml
+    - contents: |
+        {{ os_content |yaml(False)|indent(8) }}
+    - user: 'root'
+    - group: 'root'
+    - makedirs: True
+    - unless: test -f /etc/openstack/clouds.yml
diff --git a/gnocchi/upgrade/render_config.sls b/gnocchi/upgrade/render_config.sls
new file mode 100644
index 0000000..ad7b4eb
--- /dev/null
+++ b/gnocchi/upgrade/render_config.sls
@@ -0,0 +1,12 @@
+{%- from "gnocchi/map.jinja" import cfg,upgrade with context %}
+
+gnocchi_render_config:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.render_config"
+
+{%- if cfg.enabled %}
+/etc/gnocchi/gnocchi.conf:
+  file.managed:
+  - source: salt://gnocchi/files/{{ upgrade.new_release }}/gnocchi.conf
+  - template: jinja
+{%- endif %}
diff --git a/gnocchi/upgrade/service_running.sls b/gnocchi/upgrade/service_running.sls
new file mode 100644
index 0000000..a5cda14
--- /dev/null
+++ b/gnocchi/upgrade/service_running.sls
@@ -0,0 +1,22 @@
+{%- from "gnocchi/map.jinja" import server,statsd,upgrade with context %}
+
+gnocchi_task_service_running:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.service_running"
+
+{%- set gservices = [] %}
+{%- if server.enabled %}
+  {%- set gservices = ['apache2'] %}
+  {%- do gservices.extend(server.services) %}
+{%- endif %}
+{%- if statsd.enabled %}
+  {%- do gservices.extend(statsd.services) %}
+{%- endif %}
+
+
+{%- for gservice in gservices %}
+gnocchi_service_running_{{ gservice }}:
+  service.running:
+  - name: {{ gservice }}
+  - enable: True
+{%- endfor %}
diff --git a/gnocchi/upgrade/service_stopped.sls b/gnocchi/upgrade/service_stopped.sls
new file mode 100644
index 0000000..53b9587
--- /dev/null
+++ b/gnocchi/upgrade/service_stopped.sls
@@ -0,0 +1,22 @@
+{%- from "gnocchi/map.jinja" import server,statsd,upgrade with context %}
+
+gnocchi_task_service_stopped:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.service_stopped"
+
+{%- set gservices = [] %}
+{%- if server.enabled %}
+  {%- set gservices = ['apache2'] %}
+  {%- do gservices.extend(server.services) %}
+{%- endif %}
+{%- if statsd.enabled %}
+  {%- do gservices.extend(statsd.services) %}
+{%- endif %}
+
+
+{%- for gservice in gservices %}
+gnocchi_service_stopped_{{ gservice }}:
+  service.dead:
+  - name: {{ gservice }}
+  - enable: False
+{%- endfor %}
diff --git a/gnocchi/upgrade/upgrade/init.sls b/gnocchi/upgrade/upgrade/init.sls
new file mode 100644
index 0000000..0427c2d
--- /dev/null
+++ b/gnocchi/upgrade/upgrade/init.sls
@@ -0,0 +1,8 @@
+{%- from "gnocchi/map.jinja" import server with context %}
+
+include:
+ - gnocchi.upgrade.service_stopped
+ - gnocchi.upgrade.pkgs_latest
+ - gnocchi.upgrade.render_config
+ - gnocchi.backend.upgrade
+ - gnocchi.upgrade.service_running
diff --git a/gnocchi/upgrade/upgrade/post.sls b/gnocchi/upgrade/upgrade/post.sls
new file mode 100644
index 0000000..8cb7da3
--- /dev/null
+++ b/gnocchi/upgrade/upgrade/post.sls
@@ -0,0 +1,3 @@
+gnocchi_upgrade_uprade_post:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.upgrade.post"
diff --git a/gnocchi/upgrade/upgrade/pre.sls b/gnocchi/upgrade/upgrade/pre.sls
new file mode 100644
index 0000000..662e0fe
--- /dev/null
+++ b/gnocchi/upgrade/upgrade/pre.sls
@@ -0,0 +1,3 @@
+gnocchi_upgrade_upgrade_pre:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.upgrade.pre"
diff --git a/gnocchi/upgrade/verify/_api.sls b/gnocchi/upgrade/verify/_api.sls
new file mode 100644
index 0000000..be58139
--- /dev/null
+++ b/gnocchi/upgrade/verify/_api.sls
@@ -0,0 +1,113 @@
+{%- from "gnocchi/map.jinja" import server with context %}
+
+
+gnocchi_upgrade_verify_api:
+  test.show_notification:
+    - text: "Running gnocchi.upgrade.verify.api"
+
+{%- if server.get('role', 'primary') == 'primary' %}
+  {%- set archive_policy_name = 'test_upgrade_policy' %}
+  {%- set archive_policy_rule_name = archive_policy_name + '_rule' %}
+gnocchi_archive_policy_create:
+  module.run:
+    - name: gnocchiv1.archive_policy_create
+    - kwargs:
+        name: {{ archive_policy_name }}
+        definition:
+          - granularity: '1h'
+            points: 100
+            timespan: '100h'
+          - granularity: '2h'
+            points: 100
+            timespan: '200h'
+        aggregation_methods:
+          - mean
+          - count
+          - max
+        back_window: 2
+        cloud_name: admin_identity
+
+gnocchi_archive_policy_list:
+  module.run:
+    - name: gnocchiv1.archive_policy_list
+    - kwargs:
+        cloud_name: admin_identity
+    - require:
+      - gnocchi_archive_policy_create
+
+gnocchi_archive_policy_read:
+  module.run:
+    - name: gnocchiv1.archive_policy_read
+    - args:
+      - {{ archive_policy_name }}
+    - kwargs:
+        cloud_name: admin_identity
+    - require:
+      - gnocchi_archive_policy_list
+
+gnocchi_archive_policy_update:
+  module.run:
+    - name: gnocchiv1.archive_policy_update
+    - args:
+      - {{ archive_policy_name }}
+    - kwargs:
+        cloud_name: admin_identity
+        definition:
+          - granularity: '1h'
+            points: 300
+            timespan: '300h'
+          - granularity: '2h'
+            points: 400
+            timespan: '800h'
+    - require:
+      - gnocchi_archive_policy_read
+
+gnocchi_archive_policy_rule_create:
+  module.run:
+    - name: gnocchiv1.archive_policy_rule_create
+    - kwargs:
+        name: {{ archive_policy_rule_name }}
+        archive_policy_name: {{ archive_policy_name }}
+        metric_pattern: 'disk.*'
+        cloud_name: admin_identity
+    - require:
+      - gnocchi_archive_policy_update
+
+gnocchi_archive_policy_rule_list:
+  module.run:
+    - name: gnocchiv1.archive_policy_rule_list
+    - kwargs:
+        cloud_name: admin_identity
+    - require:
+      - gnocchi_archive_policy_rule_create
+
+gnocchi_archive_policy_rule_read:
+  module.run:
+    - name: gnocchiv1.archive_policy_rule_read
+    - args:
+      - {{ archive_policy_rule_name }}
+    - kwargs:
+        cloud_name: admin_identity
+    - require:
+      - gnocchi_archive_policy_rule_list
+
+gnocchi_archive_policy_rule_delete:
+  module.run:
+    - name: gnocchiv1.archive_policy_rule_delete
+    - args:
+      - {{ archive_policy_rule_name }}
+    - kwargs:
+        cloud_name: admin_identity
+    - require:
+      - gnocchi_archive_policy_rule_list
+
+gnocchi_archive_policy_delete:
+  module.run:
+    - name: gnocchiv1.archive_policy_delete
+    - args:
+      - {{ archive_policy_name }}
+    - kwargs:
+        cloud_name: admin_identity
+    - require:
+      - gnocchi_archive_policy_rule_delete
+{%- endif %}
diff --git a/gnocchi/upgrade/verify/init.sls b/gnocchi/upgrade/verify/init.sls
new file mode 100644
index 0000000..c3130f8
--- /dev/null
+++ b/gnocchi/upgrade/verify/init.sls
@@ -0,0 +1,2 @@
+include:
+ - gnocchi.upgrade.verify._api