Adjust Aodh formula granularity

Change-Id: I141500643a2def53747b9cd7dd4502904c0ffd33
Related-Prod: https://mirantis.jira.com/browse/PROD-21938
diff --git a/aodh/client.sls b/aodh/client.sls
new file mode 100644
index 0000000..4fdd1dd
--- /dev/null
+++ b/aodh/client.sls
@@ -0,0 +1,8 @@
+{%- from "aodh/map.jinja" import client with context %}
+{%- if client.enabled %}
+
+aodh_client_packages:
+  pkg.installed:
+  - names: {{ client.pkgs }}
+
+{%- endif %}
diff --git a/aodh/db/offline_sync.sls b/aodh/db/offline_sync.sls
new file mode 100644
index 0000000..8b2ee71
--- /dev/null
+++ b/aodh/db/offline_sync.sls
@@ -0,0 +1,8 @@
+{%- from "aodh/map.jinja" import server with context %}
+
+aodh_syncdb:
+  cmd.run:
+  - name: aodh-dbsync
+  {%- if grains.get('noservices') or server.get('role', 'primary') == 'secondary' %}
+  - onlyif: /bin/false
+  {%- endif %}
diff --git a/aodh/init.sls b/aodh/init.sls
index 355ddbf..9877617 100644
--- a/aodh/init.sls
+++ b/aodh/init.sls
@@ -3,4 +3,7 @@
 {%- if pillar.aodh.server is defined %}
 - aodh.server
 {%- endif %}
+{%- if pillar.aodh.client is defined %}
+- aodh.client
+{%- endif %}
 {%- endif %}
diff --git a/aodh/map.jinja b/aodh/map.jinja
index 121956a..b28b9eb 100644
--- a/aodh/map.jinja
+++ b/aodh/map.jinja
@@ -54,3 +54,15 @@
 {%- if salt['pillar.get']('aodh:server:version', 'mitaka') in ['mitaka'] %}
     {%- do server.update({'services': server.services + server.webserved}) %}
 {%- endif %}
+
+{%- load_yaml as client_defaults %}
+
+Debian:
+  enabled: false
+  pkgs:
+    - python-aodhclient
+{%- endload %}
+
+{%- set client = salt['grains.filter_by'](client_defaults, merge=salt['pillar.get']('aodh:client')) %}
+{%- set upgrade = pillar.get('aodh', {}).get('upgrade', {}) %}
+
diff --git a/aodh/meta/salt.yml b/aodh/meta/salt.yml
new file mode 100644
index 0000000..e917909
--- /dev/null
+++ b/aodh/meta/salt.yml
@@ -0,0 +1,5 @@
+orchestration:
+  upgrade:
+    applications:
+      aodh:
+        priority: 2000
diff --git a/aodh/server.sls b/aodh/server.sls
index 83a43d4..04b0395 100644
--- a/aodh/server.sls
+++ b/aodh/server.sls
@@ -6,6 +6,7 @@
 {%- if server.version not in ['liberty', 'juno', 'kilo'] %}
 
 include:
+  - aodh.db.offline_sync
   - aodh._ssl.mysql
   - aodh._ssl.rabbitmq
   - apache
@@ -16,6 +17,7 @@
   - require_in:
     - sls: aodh._ssl.mysql
     - sls: aodh._ssl.rabbitmq
+    - sls: aodh.db.offline_sync
 
 /etc/aodh/aodh.conf:
   file.managed:
@@ -27,6 +29,8 @@
     - pkg: aodh_server_packages
     - sls: aodh._ssl.mysql
     - sls: aodh._ssl.rabbitmq
+  - require_in:
+    - sls: aodh.db.offline_sync
 
 {% for service_name in server.services %}
 {{ service_name }}_default:
@@ -64,6 +68,8 @@
 {%- if server.logging.log_handlers.get('fluentd', {}).get('enabled', False) %}
       - pkg: aodh_fluentd_logger_package
 {%- endif %}
+    - require_in:
+      - sls: aodh.db.offline_sync
     - defaults:
         service_name: aodh
         _data: {{ server.logging }}
@@ -107,16 +113,6 @@
 
 {% endif %}
 
-aodh_syncdb:
-  cmd.run:
-  - name: aodh-dbsync
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - require:
-    - file: /etc/aodh/aodh.conf
-    - pkg: aodh_server_packages
-
 {%- if server.get('role', 'secondary') == 'primary' %}
 {%- set cron = server.expirer.cron %}
 aodh_expirer_cron:
@@ -213,6 +209,8 @@
   {%- endif %}
   - watch:
     - file: /etc/aodh/aodh.conf
+  - require:
+    - sls: aodh.db.offline_sync
 
 {%- endif %}
 {%- endif %}
diff --git a/aodh/upgrade/pkgs_latest.sls b/aodh/upgrade/pkgs_latest.sls
new file mode 100644
index 0000000..08b6590
--- /dev/null
+++ b/aodh/upgrade/pkgs_latest.sls
@@ -0,0 +1,33 @@
+{%- from "aodh/map.jinja" import server,client,upgrade with context %}
+
+aodh_task_pkg_latest:
+  test.show_notification:
+    - text: "Running aodh.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) %}
+{%- endif %}
+{%- if client.enabled %}
+  {%- do pkgs.extend(client.pkgs) %}
+{%- endif %}
+
+aodh_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/aodh/upgrade/post/init.sls b/aodh/upgrade/post/init.sls
new file mode 100644
index 0000000..92f74b8
--- /dev/null
+++ b/aodh/upgrade/post/init.sls
@@ -0,0 +1,7 @@
+aodh_post:
+  test.show_notification:
+    - text: "Running aodh.upgrade.post"
+
+keystone_os_client_config_absent:
+  file.absent:
+    - name: /etc/openstack/clouds.yml
diff --git a/aodh/upgrade/pre/init.sls b/aodh/upgrade/pre/init.sls
new file mode 100644
index 0000000..a71cd46
--- /dev/null
+++ b/aodh/upgrade/pre/init.sls
@@ -0,0 +1,14 @@
+aodh_pre:
+  test.show_notification:
+    - text: "Running aodh.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/aodh/upgrade/render_config.sls b/aodh/upgrade/render_config.sls
new file mode 100644
index 0000000..4296334
--- /dev/null
+++ b/aodh/upgrade/render_config.sls
@@ -0,0 +1,12 @@
+{%- from "aodh/map.jinja" import server with context %}
+
+aodh_render_config:
+  test.show_notification:
+    - text: "Running aodh.upgrade.render_config"
+
+{%- if server.enabled %}
+/etc/aodh/aodh.conf:
+  file.managed:
+  - source: salt://aodh/files/{{ server.version }}/aodh.conf.{{ grains.os_family }}
+  - template: jinja
+{%- endif %}
diff --git a/aodh/upgrade/service_running.sls b/aodh/upgrade/service_running.sls
new file mode 100644
index 0000000..89346e0
--- /dev/null
+++ b/aodh/upgrade/service_running.sls
@@ -0,0 +1,18 @@
+{%- from "aodh/map.jinja" import server,upgrade with context %}
+
+aodh_task_service_running:
+  test.show_notification:
+    - text: "Running aodh.upgrade.service_running"
+
+{%- if server.enabled %}
+  {%- set aservices = server.services %}
+  {%- if server.version not in ['mitaka'] %}
+    {%- do aservices.append('apache2') %}
+  {%- endif %}
+  {%- for aservice in aservices %}
+aodh_service_running_{{ aservice }}:
+  service.running:
+  - name: {{ aservice }}
+  - enable: True
+  {%- endfor %}
+{%- endif %}
diff --git a/aodh/upgrade/service_stopped.sls b/aodh/upgrade/service_stopped.sls
new file mode 100644
index 0000000..f7401cb
--- /dev/null
+++ b/aodh/upgrade/service_stopped.sls
@@ -0,0 +1,18 @@
+{%- from "aodh/map.jinja" import server,upgrade with context %}
+
+aodh_task_service_stopped:
+  test.show_notification:
+    - text: "Running aodh.upgrade.service_stopped"
+
+{%- if server.enabled %}
+  {%- set aservices = server.services %}
+  {%- if server.version not in ['mitaka'] %}
+    {%- do aservices.append('apache2') %}
+  {%- endif %}
+  {%- for aservice in aservices %}
+aodh_service_stopped_{{ aservice }}:
+  service.dead:
+  - name: {{ aservice }}
+  - enable: False
+  {%- endfor %}
+{%- endif %}
diff --git a/aodh/upgrade/upgrade/init.sls b/aodh/upgrade/upgrade/init.sls
new file mode 100644
index 0000000..ed4548d
--- /dev/null
+++ b/aodh/upgrade/upgrade/init.sls
@@ -0,0 +1,8 @@
+{%- from "aodh/map.jinja" import server with context %}
+
+include:
+ - aodh.upgrade.service_stopped
+ - aodh.upgrade.pkgs_latest
+ - aodh.upgrade.render_config
+ - aodh.db.offline_sync
+ - aodh.upgrade.service_running
diff --git a/aodh/upgrade/upgrade/post.sls b/aodh/upgrade/upgrade/post.sls
new file mode 100644
index 0000000..50b1066
--- /dev/null
+++ b/aodh/upgrade/upgrade/post.sls
@@ -0,0 +1,3 @@
+aodh_upgrade_uprade_post:
+  test.show_notification:
+    - text: "Running aodh.upgrade.upgrade.post"
diff --git a/aodh/upgrade/upgrade/pre.sls b/aodh/upgrade/upgrade/pre.sls
new file mode 100644
index 0000000..c237545
--- /dev/null
+++ b/aodh/upgrade/upgrade/pre.sls
@@ -0,0 +1,3 @@
+aodh_upgrade_upgrade_pre:
+  test.show_notification:
+    - text: "Running aodh.upgrade.upgrade.pre"
diff --git a/aodh/upgrade/verify/_api.sls b/aodh/upgrade/verify/_api.sls
new file mode 100644
index 0000000..e34edf5
--- /dev/null
+++ b/aodh/upgrade/verify/_api.sls
@@ -0,0 +1,7 @@
+
+aodh_upgrade_verify_api:
+  test.show_notification:
+    - text: "Running aodh.upgrade.verify.api"
+
+# TODO: Implement api checks when aodh salt
+# modules are implemented
diff --git a/aodh/upgrade/verify/init.sls b/aodh/upgrade/verify/init.sls
new file mode 100644
index 0000000..128546b
--- /dev/null
+++ b/aodh/upgrade/verify/init.sls
@@ -0,0 +1,2 @@
+include:
+ - aodh.upgrade.verify._api