[SEBA-492] Adding --check flag to xos-migrate to check migration status

Change-Id: Ib1d347f47f5423e9fc16d5a350c09705aef949b2
diff --git a/VERSION b/VERSION
index fa2d855..c594452 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.1.50
+2.1.51
diff --git a/containers/chameleon/Dockerfile.chameleon b/containers/chameleon/Dockerfile.chameleon
index 5c3e90c..1e242a2 100644
--- a/containers/chameleon/Dockerfile.chameleon
+++ b/containers/chameleon/Dockerfile.chameleon
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 # xosproject/chameleon
-FROM xosproject/xos-base:2.1.50
+FROM xosproject/xos-base:2.1.51
 
 # xos-base already has protoc and dependencies installed
 
diff --git a/containers/xos/Dockerfile.client b/containers/xos/Dockerfile.client
index 3b00725..781b3e8 100644
--- a/containers/xos/Dockerfile.client
+++ b/containers/xos/Dockerfile.client
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 # xosproject/xos-client
-FROM xosproject/xos-libraries:2.1.50
+FROM xosproject/xos-libraries:2.1.51
 
 
 # Install XOS client
diff --git a/containers/xos/Dockerfile.libraries b/containers/xos/Dockerfile.libraries
index 6ab827f..6f2041b 100644
--- a/containers/xos/Dockerfile.libraries
+++ b/containers/xos/Dockerfile.libraries
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 # xosproject/xos-libraries
-FROM xosproject/xos-base:2.1.50
+FROM xosproject/xos-base:2.1.51
 
 # Add libraries
 COPY lib /opt/xos/lib
diff --git a/containers/xos/Dockerfile.synchronizer-base b/containers/xos/Dockerfile.synchronizer-base
index 5a64c55..4043b9c 100644
--- a/containers/xos/Dockerfile.synchronizer-base
+++ b/containers/xos/Dockerfile.synchronizer-base
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 # xosproject/xos-synchronizer-base
-FROM xosproject/xos-client:2.1.50
+FROM xosproject/xos-client:2.1.51
 
 COPY xos/synchronizers/new_base /opt/xos/synchronizers/new_base
 COPY xos/xos/logger.py /opt/xos/xos/logger.py
diff --git a/containers/xos/Dockerfile.xos-core b/containers/xos/Dockerfile.xos-core
index c9045ef..cc92556 100644
--- a/containers/xos/Dockerfile.xos-core
+++ b/containers/xos/Dockerfile.xos-core
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 # xosproject/xos-core
-FROM xosproject/xos-libraries:2.1.50
+FROM xosproject/xos-libraries:2.1.51
 
 # Install XOS
 ADD xos /opt/xos
diff --git a/lib/xos-migrate/xosmigrate/main.py b/lib/xos-migrate/xosmigrate/main.py
index f4620de..6e0ca77 100644
--- a/lib/xos-migrate/xosmigrate/main.py
+++ b/lib/xos-migrate/xosmigrate/main.py
@@ -31,13 +31,14 @@
 import shutil
 from xosgenx.generator import XOSProcessor, XOSProcessorArgs
 from xosconfig import Config
+import subprocess
 from multistructlog import create_logger
 
 
 def get_abs_path(dir_):
     if os.path.isabs(dir_):
         return os.path.realpath(dir_)
-    if dir_[0] == '~' and not os.path.exists(dir_):
+    if dir_[0] == "~" and not os.path.exists(dir_):
         dir_ = os.path.expanduser(dir_)
         return os.path.abspath(dir_)
     return os.path.dirname(os.path.realpath(__file__)) + "/" + dir_
@@ -141,7 +142,7 @@
 
     cfg_file = open(config)
     cfg = yaml.load(cfg_file)
-    return cfg['name']
+    return cfg["name"]
 
 
 def generate_service_models(service_dir, service_dest_dir, service_name):
@@ -234,7 +235,7 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-    
+
 # -*- coding: utf-8 -*-
 # Generated by Django %(version)s on %(timestamp)s
 from __future__ import unicode_literals
@@ -252,33 +253,54 @@
     ]
 """
 
+
+def configure_logging(verbose):
+    global log
+    # INITIALIZING LOGGER
+    Config.init()
+
+    cfg = Config().get("logging")
+    if verbose:
+        cfg["handlers"]["console"]["level"] = "DEBUG"
+
+    log = create_logger(cfg)
+
+
 # SETTING ENV
-os.environ['LOG_FILE'] = get_abs_path("django.log")
-os.environ['XOS_CONFIG_SCHEMA'] = get_abs_path("migration_cfg_schema.yaml")
-os.environ['XOS_CONFIG_FILE'] = get_abs_path("migration_cfg.yaml")
+os.environ["LOG_FILE"] = get_abs_path("django.log")
+os.environ["XOS_CONFIG_SCHEMA"] = get_abs_path("migration_cfg_schema.yaml")
+os.environ["XOS_CONFIG_FILE"] = get_abs_path("migration_cfg.yaml")
 os.environ["MIGRATIONS"] = "true"
 # this is populated in case we generate migrations for services and it's used in settings.py
 os.environ["INSTALLED_APPS"] = ""
 
 # PARAMS
 parser = argparse.ArgumentParser(description="XOS Migrations")
-required = parser.add_argument_group('required arguments')
+required = parser.add_argument_group("required arguments")
 
 required.add_argument(
-    '-s',
-    '--service',
-    action='append',
+    "-s",
+    "--service",
+    action="append",
     required=True,
     dest="service_names",
-    help='The name of the folder containing the service in cord/orchestration/xos_services'
+    help="The name of the folder containing the service in cord/orchestration/xos_services"
 )
 
 parser.add_argument(
-    '-r',
-    '--repo',
+    "-r",
+    "--repo",
     default=get_abs_path("~/cord"),
     dest="repo_root",
-    help='The location of the folder containing the CORD repo root (default to ~/cord)'
+    help="The location of the folder containing the CORD repo root (default to ~/cord)"
+)
+
+parser.add_argument(
+    "--check",
+    default=False,
+    action="store_true",
+    dest="check",
+    help="Check if the migrations are generated for a given service. Does not apply any change."
 )
 
 # FIXME this is not working with multistructlog
@@ -286,22 +308,22 @@
     "-v",
     "--verbose",
     help="increase log verbosity",
+    dest="verbose",
     action="store_true"
 )
 
-# INITIALIZING LOGGER
-Config.init()
-log = create_logger(Config().get('logging'))
-
 
 def run():
 
     args = parser.parse_args()
 
+    configure_logging(args.verbose)
+
     print_banner(args.repo_root)
 
     # find absolute path to the code
     xos_path = get_abs_path(os.path.join(args.repo_root, "orchestration/xos/xos/"))
+    django_path = get_abs_path(os.path.join(xos_path, "manage.py"))
     core_dir = get_abs_path(os.path.join(xos_path, "core/models/"))
     service_base_dir = get_abs_path(os.path.join(xos_path, "../../xos_services/"))
     service_dest_dir = get_abs_path(os.path.join(xos_path, "services/"))
@@ -312,7 +334,7 @@
 
     log.info("Services: %s" % ", ".join(args.service_names))
 
-    django_cli_args = ['xos-migrate.py', 'makemigrations']
+    django_cli_args = ["makemigrations"]
 
     # generate the code for each service and create a list of parameters to pass to django
     app_list = []
@@ -335,9 +357,21 @@
 
     monkey_patch_migration_template()
 
-    from django.core.management import execute_from_command_line
+    if args.check:
+        django_cli_args.append("--check")
+        django_cli_args.append("--dry-run")
 
-    execute_from_command_line(django_cli_args)
+    cmd = "python %s %s" % (django_path, " ".join(django_cli_args))
+    result = subprocess.Popen(cmd, shell=True)
+    result.wait()
+    returncode = result.returncode
+
+    if returncode != 0:
+        if args.check:
+            log.error("Migrations are not up to date with the service changes!")
+        else:
+            log.error(result.communicate()[0])
+        sys.exit(returncode)
 
     # copying migrations back to the service
     for service in args.service_names:
diff --git a/lib/xos-migrate/xosmigrate/migration_cfg.yaml b/lib/xos-migrate/xosmigrate/migration_cfg.yaml
index b3d6e58..5b74be7 100644
--- a/lib/xos-migrate/xosmigrate/migration_cfg.yaml
+++ b/lib/xos-migrate/xosmigrate/migration_cfg.yaml
@@ -20,7 +20,9 @@
   handlers:
     console:
       class: logging.StreamHandler
+      formatter: structured-color
+      level: INFO
   loggers:
     'multistructlog':
       handlers:
-          - console
+        - console