Run dbsync on primary node only

Move dbsync to separate file.
Run only on primary node.
Do not run redundant versioned dbsyncs.
Run online dbsync when services are online.

Change-Id: I4fd4ffd1d0ef6b3d8585358a2d3e92b69c53e366
Related-Prod: PROD-21267
diff --git a/.travis.yml b/.travis.yml
index 03c1a7e..60015c8 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -20,12 +20,6 @@
   - bundle install
 
 env:
-  - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2016.3 OS_VERSION=mitaka SUITE=compute-cluster
-  - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2016.3 OS_VERSION=mitaka SUITE=control-cluster
-  - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2016.3 OS_VERSION=newton SUITE=compute-cluster
-  - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2016.3 OS_VERSION=newton SUITE=control-cluster
-  - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2016.3 OS_VERSION=ocata SUITE=compute-cluster
-  - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2016.3 OS_VERSION=ocata SUITE=control-cluster
   - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2017.7 OS_VERSION=mitaka SUITE=compute-cluster
   - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2017.7 OS_VERSION=mitaka SUITE=control-cluster
   - PLATFORM=epcim/salt:saltstack-ubuntu-xenial-salt-2017.7 OS_VERSION=newton SUITE=compute-cluster
diff --git a/nova/controller.sls b/nova/controller.sls
index 2ea0510..1fc2886 100644
--- a/nova/controller.sls
+++ b/nova/controller.sls
@@ -2,6 +2,16 @@
 
 {%- if controller.get('enabled') %}
 
+include:
+{#- Always include apache when horizon/apache formulas doesn't intersect #}
+{%- if pillar.get('apache', {}).get('server', {}).get('site', {}).nova_placement is defined %}
+ - apache
+{%- endif %}
+ - nova.db.offline_sync
+ # TODO(vsaienko) we need to run online dbsync only once after upgrade
+ # Move to appropriate upgrade phase
+ - nova.db.online_sync
+
 {%- if grains.os_family == 'Debian' %}
 debconf-set-prerequisite:
     pkg.installed:
@@ -159,6 +169,9 @@
   - template: jinja
   - require:
     - pkg: nova_controller_packages
+  - require_in:
+    - sls: nova.db.offline_sync
+    - sls: nova.db.online_sync
 
 /etc/nova/api-paste.ini:
   file.managed:
@@ -293,41 +306,6 @@
 
 {%- if controller.version not in ["juno", "kilo", "liberty", "mitaka", "newton"] %}
 
-{#- the following api_db sync --version 20 happens only if the current api_db version is < 20 #}
-
-nova_controller_api_db_sync_version_20:
-  novang.api_db_version_present:
-  - version: "20"
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - require:
-    - file: /etc/nova/nova.conf
-
-{#- the following db sync --version 334 happens only if the current db version is < 334 #}
-
-nova_controller_db_sync_version_334:
-  novang.db_version_present:
-  - version: "334"
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - require:
-    - file: /etc/nova/nova.conf
-
-{#- the following db online_data_migrations executes only if the current db version == 334 && api_db version == 20 #}
-
-online_data_migrations_for_apidb20_and_db334:
-  novang.online_data_migrations_present:
-  - api_db_version: "20"
-  - db_version: "334"
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - require:
-    - novang: nova_controller_api_db_sync_version_20
-    - novang: nova_controller_db_sync_version_334
-
 nova_controller_map_cell0:
   cmd.run:
   - name: nova-manage cell_v2 map_cell0
@@ -335,7 +313,7 @@
   - onlyif: /bin/false
   {%- endif %}
   - require:
-    - cmd: nova_controller_syncdb
+    - sls: nova.db.offline_sync
 
 nova_cell1_create:
   cmd.run:
@@ -345,7 +323,7 @@
   {%- endif %}
   - unless: 'nova-manage cell_v2 list_cells | grep cell1'
   - require:
-    - cmd: nova_controller_syncdb
+    - sls: nova.db.offline_sync
 
 {%- if controller.get('update_cells') %}
 
@@ -417,9 +395,6 @@
 
 {%- else %}
 
-include:
- - apache
-
 nova_cleanup_configs:
   file.absent:
     - names:
@@ -450,7 +425,6 @@
   - require:
     - cmd: nova_controller_map_cell0
     - cmd: nova_cell1_create
-    - cmd: nova_controller_syncdb
 
 nova_controller_map_instances:
   novang.map_instances:
@@ -464,41 +438,6 @@
 
 {%- endif %}
 
-{%- if controller.version not in ["juno", "kilo", "liberty"] %}
-nova_controller_sync_apidb:
-  cmd.run:
-  - name: nova-manage api_db sync
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - require:
-    - file: /etc/nova/nova.conf
-
-{%- endif %}
-
-nova_controller_syncdb:
-  cmd.run:
-  - names:
-    - nova-manage db sync
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - require:
-    - file: /etc/nova/nova.conf
-
-{%- if controller.version not in ["juno", "kilo", "liberty"] %}
-
-nova_controller_online_data_migrations:
-  cmd.run:
-  - name: nova-manage db online_data_migrations
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - require:
-    - cmd: nova_controller_syncdb
-
-{%- endif %}
-
 {%- if controller.version not in ["juno", "kilo", "liberty", "mitaka", "newton"] %}
 
 nova_apache_restart:
@@ -509,7 +448,7 @@
   - onlyif: /bin/false
   {%- endif %}
   - require:
-    - cmd: nova_controller_syncdb
+    - sls: nova.db.offline_sync
   - watch:
     - file: /etc/nova/nova.conf
     - file: /etc/nova/api-paste.ini
@@ -528,7 +467,9 @@
   - onlyif: /bin/false
   {%- endif %}
   - require:
-    - cmd: nova_controller_syncdb
+    - sls: nova.db.offline_sync
+  - require_in:
+    - sls: nova.db.online_sync
   - watch:
     - file: /etc/nova/nova.conf
     - file: /etc/nova/api-paste.ini
diff --git a/nova/db/offline_sync.sls b/nova/db/offline_sync.sls
new file mode 100644
index 0000000..71c858c
--- /dev/null
+++ b/nova/db/offline_sync.sls
@@ -0,0 +1,18 @@
+{% from "nova/map.jinja" import controller with context %}
+
+nova_controller_syncdb:
+  cmd.run:
+  - name: nova-manage db sync
+  {%- if grains.get('noservices') or controller.get('role', 'primary') == 'secondary' %}
+  - onlyif: /bin/false
+  {%- endif %}
+
+{%- if controller.version not in ["juno", "kilo", "liberty"] %}
+nova_controller_sync_apidb:
+  cmd.run:
+  - name: nova-manage api_db sync
+  {%- if grains.get('noservices') or controller.get('role', 'primary') == 'secondary' %}
+  - onlyif: /bin/false
+  {%- endif %}
+
+{%- endif %}
diff --git a/nova/db/online_sync.sls b/nova/db/online_sync.sls
new file mode 100644
index 0000000..7c13c52
--- /dev/null
+++ b/nova/db/online_sync.sls
@@ -0,0 +1,11 @@
+{% from "nova/map.jinja" import controller with context %}
+
+{%- set should_run = '/bin/false' %}
+{%- if not grains.get('noservices') and controller.version not in ["juno", "kilo", "liberty"] and controller.get('role', 'primary') == 'primary' %}
+{%- set should_run = '/bin/true' %}
+{%- endif %}
+
+nova_controller_online_data_migrations:
+  cmd.run:
+  - name: nova-manage db online_data_migrations
+  - onlyif: {{ should_run }}