Adjust Ceilometer formula granularity

Change-Id: If091a071d8861eb8ec86522296e549cd6ad4808d
Related-Prod: https://mirantis.jira.com/browse/PROD-23755
diff --git a/ceilometer/backend/upgrade.sls b/ceilometer/backend/upgrade.sls
new file mode 100644
index 0000000..31efe2c
--- /dev/null
+++ b/ceilometer/backend/upgrade.sls
@@ -0,0 +1,8 @@
+{%- from "ceilometer/map.jinja" import server with context %}
+
+ceilometer_upgrade:
+  cmd.run:
+    - name: ceilometer-upgrade
+    {%- if grains.get('noservices') or server.get('role', 'primary') == 'secondary' %}
+    - onlyif: /bin/false
+    {%- endif %}
diff --git a/ceilometer/map.jinja b/ceilometer/map.jinja
index 6b863c9..492afa7 100644
--- a/ceilometer/map.jinja
+++ b/ceilometer/map.jinja
@@ -8,6 +8,7 @@
 {% set agent = salt['grains.filter_by']({
     'BaseDefaults': default_params,
     'Debian': {
+        'enabled': false,
         'pkgs': ['ceilometer-agent-compute'],
         'services': ['ceilometer-agent-compute'],
         'notification': {
@@ -24,6 +25,7 @@
         },
     },
     'RedHat': {
+        'enabled': false,
         'pkgs': ['openstack-ceilometer-compute'],
         'services': ['openstack-ceilometer-compute'],
         'notification': {
@@ -80,6 +82,7 @@
 {%- set server = salt['grains.filter_by']({
     'BaseDefaults': default_params,
     'default': {
+        'enabled': false,
         'notification': {
           'topics': 'notifications'
         },
@@ -117,3 +120,5 @@
 {%- else %}
 {%-   do server.update({'services': services.basic}) %}
 {%- endif %}
+
+{%- set upgrade = pillar.get('ceilometer', {}).get('upgrade', {}) %}
diff --git a/ceilometer/meta/salt.yml b/ceilometer/meta/salt.yml
index 73236db..f3f5495 100644
--- a/ceilometer/meta/salt.yml
+++ b/ceilometer/meta/salt.yml
@@ -8,4 +8,8 @@
     priority: 690
     require:
     - salt: ceilometer.server
-
+orchestration:
+  upgrade:
+    applications:
+      ceilometer:
+        priority: 1950
diff --git a/ceilometer/server.sls b/ceilometer/server.sls
index 46204ca..3b2056d 100644
--- a/ceilometer/server.sls
+++ b/ceilometer/server.sls
@@ -1,8 +1,13 @@
 {%- from "ceilometer/map.jinja" import server with context %}
 {%- if server.enabled %}
 
+{%- set gnocchi_publisher = server.get('publisher', {}).get('gnocchi', {}) %}
+
 include:
   - ceilometer._ssl.rabbitmq
+{%- if gnocchi_publisher.get('create_resources', False) and gnocchi_publisher.get('enabled', False) %}
+  - ceilometer.backend.upgrade
+{%- endif %}
 
 ceilometer_server_packages:
   pkg.installed:
@@ -19,6 +24,10 @@
   - require:
     - pkg: ceilometer_server_packages
     - sls: ceilometer._ssl.rabbitmq
+{%- if gnocchi_publisher.get('create_resources', False) and gnocchi_publisher.get('enabled', False) %}
+  - require_in:
+    - sls: ceilometer.backend.upgrade
+{%- endif %}
 
 {%- for service_name in server.services %}
 {{ service_name }}_default:
@@ -64,6 +73,10 @@
 {%- if server.version in  ['newton', 'ocata'] %}
       - service: ceilometer_apache_restart
 {%- endif %}
+{%- if gnocchi_publisher.get('create_resources', False) and gnocchi_publisher.get('enabled', False) %}
+    - require_in:
+      - sls: ceilometer.backend.upgrade
+{%- endif %}
 
 /var/log/ceilometer/ceilometer.log:
   file.managed:
@@ -159,15 +172,8 @@
   - require:
     - pkg: ceilometer_server_packages
     - pkg: ceilometer_gnocchiclient_pkg
-
-ceilometer_upgrade:
-  cmd.run:
-    - name: ceilometer-upgrade
-    {%- if grains.get('noservices') %}
-    - onlyif: /bin/false
-    {%- endif %}
-    - onchanges:
-      - ceilometer_server_gnocchi_resources
+  - onchanges_in:
+    - sls: ceilometer.backend.upgrade
 
 {%- endif %}
 {%- endif %}
@@ -263,10 +269,9 @@
   # ceilometer-upgrade requires operational Gnocchi service API,
   # which can be run on the same node as Ceilometer under apache,
   # the command is executed only when Gnocchi is enabled.
-  {%- set gnocchi_publisher = server.get('publisher', {}).get('gnocchi', {}) %}
   {%- if gnocchi_publisher.get('create_resources', False) and gnocchi_publisher.get('enabled', False) %}
   - require:
-    - ceilometer_upgrade
+    - sls: ceilometer.backend.upgrade
   {%- endif %}
 
 {%- endif %}
diff --git a/ceilometer/upgrade/pkgs_latest.sls b/ceilometer/upgrade/pkgs_latest.sls
new file mode 100644
index 0000000..cf0792b
--- /dev/null
+++ b/ceilometer/upgrade/pkgs_latest.sls
@@ -0,0 +1,37 @@
+{%- from "ceilometer/map.jinja" import server,agent,upgrade with context %}
+{%- set gnocchi_publisher = server.get('publisher', {}).get('gnocchi', {}) %}
+
+ceilometer_task_pkg_latest:
+  test.show_notification:
+    - text: "Running ceilometer.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) %}
+  {%- if gnocchi_publisher.get('create_resources', False) and gnocchi_publisher.get('enabled', False) %}
+    {%- do pkgs.append('python-gnocchiclient') %}
+  {%- endif %}
+{%- endif %}
+{%- if agent.enabled %}
+  {%- do pkgs.extend(agent.pkgs) %}
+{%- endif %}
+
+ceilometer_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/ceilometer/upgrade/post/init.sls b/ceilometer/upgrade/post/init.sls
new file mode 100644
index 0000000..8c0a4ca
--- /dev/null
+++ b/ceilometer/upgrade/post/init.sls
@@ -0,0 +1,7 @@
+ceilometer_post:
+  test.show_notification:
+    - text: "Running ceilometer.upgrade.post"
+
+keystone_os_client_config_absent:
+  file.absent:
+    - name: /etc/openstack/clouds.yml
diff --git a/ceilometer/upgrade/pre/init.sls b/ceilometer/upgrade/pre/init.sls
new file mode 100644
index 0000000..c1f3204
--- /dev/null
+++ b/ceilometer/upgrade/pre/init.sls
@@ -0,0 +1,14 @@
+ceilometer_pre:
+  test.show_notification:
+    - text: "Running ceilometer.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
\ No newline at end of file
diff --git a/ceilometer/upgrade/render_config.sls b/ceilometer/upgrade/render_config.sls
new file mode 100644
index 0000000..88fe5a8
--- /dev/null
+++ b/ceilometer/upgrade/render_config.sls
@@ -0,0 +1,31 @@
+{%- from "ceilometer/map.jinja" import server,agent with context %}
+
+ceilometer_render_config:
+  test.show_notification:
+    - text: "Running ceilometer.upgrade.render_config"
+
+{%- if server.enabled %}
+{%- set gnocchi_publisher = server.get('publisher', {}).get('gnocchi', {}) %}
+
+ceilometer_server_conf:
+  file.managed:
+  - name: /etc/ceilometer/ceilometer.conf
+  - source: salt://ceilometer/files/{{ server.version }}/ceilometer-server.conf.{{ grains.os_family }}
+  - template: jinja
+
+{%- if gnocchi_publisher.get('create_resources', False) and gnocchi_publisher.get('enabled', False) %}
+ceilometer_server_gnocchi_resources:
+  file.managed:
+  - name: /etc/ceilometer/gnocchi_resources.yaml
+  - source: salt://ceilometer/files/{{ server.version }}/gnocchi_resources.yaml
+{%- endif %}
+{%- endif %}
+
+{%- if agent.enabled %}
+ceilometer_agent_conf:
+  file.managed:
+  - name: /etc/ceilometer/ceilometer.conf
+  - source: salt://ceilometer/files/{{ agent.version }}/ceilometer-agent.conf.{{ grains.os_family }}
+  - template: jinja
+{%- endif %}
+
diff --git a/ceilometer/upgrade/service_running.sls b/ceilometer/upgrade/service_running.sls
new file mode 100644
index 0000000..952c2ee
--- /dev/null
+++ b/ceilometer/upgrade/service_running.sls
@@ -0,0 +1,20 @@
+{%- from "ceilometer/map.jinja" import server,agent,upgrade with context %}
+
+ceilometer_task_service_running:
+  test.show_notification:
+    - text: "Running ceilometer.upgrade.service_running"
+
+{%- set cservices = [] %}
+{%- if server.enabled %}
+  {%- do cservices.extend(server.services) %}
+{%- endif %}
+{%- if agent.enabled %}
+  {%- do cservices.extend(agent.services) %}
+{%- endif %}
+
+{%- for cservice in cservices %}
+ceilometer_service_running_{{ cservice }}:
+  service.running:
+  - name: {{ cservice }}
+  - enable: True
+{%- endfor %}
diff --git a/ceilometer/upgrade/service_stopped.sls b/ceilometer/upgrade/service_stopped.sls
new file mode 100644
index 0000000..1d090d6
--- /dev/null
+++ b/ceilometer/upgrade/service_stopped.sls
@@ -0,0 +1,20 @@
+{%- from "ceilometer/map.jinja" import server,agent,upgrade with context %}
+
+ceilometer_task_service_stopped:
+  test.show_notification:
+    - text: "Running ceilometer.upgrade.service_stopped"
+
+{%- set cservices = [] %}
+{%- if server.enabled %}
+  {%- do cservices.extend(server.services) %}
+{%- endif %}
+{%- if agent.enabled %}
+  {%- do cservices.extend(agent.services) %}
+{%- endif %}
+
+{%- for cservice in cservices %}
+ceilometer_service_stopped_{{ cservice }}:
+  service.dead:
+  - name: {{ cservice }}
+  - enable: False
+{%- endfor %}
diff --git a/ceilometer/upgrade/upgrade/init.sls b/ceilometer/upgrade/upgrade/init.sls
new file mode 100644
index 0000000..3e31641
--- /dev/null
+++ b/ceilometer/upgrade/upgrade/init.sls
@@ -0,0 +1,11 @@
+{%- from "ceilometer/map.jinja" import server with context %}
+{%- set gnocchi_publisher = server.get('publisher', {}).get('gnocchi', {}) %}
+
+include:
+ - ceilometer.upgrade.service_stopped
+ - ceilometer.upgrade.pkgs_latest
+ - ceilometer.upgrade.render_config
+{%- if gnocchi_publisher.get('create_resources', False) and gnocchi_publisher.get('enabled', False) %}
+ - ceilometer.backend.upgrade
+{%- endif %}
+ - ceilometer.upgrade.service_running
diff --git a/ceilometer/upgrade/upgrade/post.sls b/ceilometer/upgrade/upgrade/post.sls
new file mode 100644
index 0000000..885509e
--- /dev/null
+++ b/ceilometer/upgrade/upgrade/post.sls
@@ -0,0 +1,3 @@
+ceilometer_upgrade_uprade_post:
+  test.show_notification:
+    - text: "Running ceilometer.upgrade.upgrade.post"
diff --git a/ceilometer/upgrade/upgrade/pre.sls b/ceilometer/upgrade/upgrade/pre.sls
new file mode 100644
index 0000000..fd481b5
--- /dev/null
+++ b/ceilometer/upgrade/upgrade/pre.sls
@@ -0,0 +1,3 @@
+ceilometer_upgrade_upgrade_pre:
+  test.show_notification:
+    - text: "Running ceilometer.upgrade.upgrade.pre"
diff --git a/ceilometer/upgrade/verify/_service.sls b/ceilometer/upgrade/verify/_service.sls
new file mode 100644
index 0000000..0fbe83a
--- /dev/null
+++ b/ceilometer/upgrade/verify/_service.sls
@@ -0,0 +1,7 @@
+
+ceilometer_upgrade_verify_service:
+  test.show_notification:
+    - text: "Running ceilometer.upgrade.verify.service"
+
+# TODO: Implement service checks when ceilometer salt
+# modules are implemented
diff --git a/ceilometer/upgrade/verify/init.sls b/ceilometer/upgrade/verify/init.sls
new file mode 100644
index 0000000..845fa79
--- /dev/null
+++ b/ceilometer/upgrade/verify/init.sls
@@ -0,0 +1,2 @@
+include:
+ - ceilometer.upgrade.verify._service