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

Change-Id: Ib1d347f47f5423e9fc16d5a350c09705aef949b2
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: