[CORD-1556] Seeding db

Change-Id: I9c1fad53f5e6ab04145dad4fac2392aad17e60d1
diff --git a/.gitignore b/.gitignore
index 38883e4..8c5ce73 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,7 +22,6 @@
 !xos/core/static/xos.css
 .DS_Store
 xos/configurations/setup/
-migrations/
 onboarding-docker-compose/
 key_import/
 *.retry
diff --git a/xos/core/migrations/.gitignore b/xos/core/migrations/.gitignore
new file mode 100644
index 0000000..68e8e0f
--- /dev/null
+++ b/xos/core/migrations/.gitignore
@@ -0,0 +1,2 @@
+*
+!0002_initial_data.py
\ No newline at end of file
diff --git a/xos/core/migrations/0002_initial_data.py b/xos/core/migrations/0002_initial_data.py
new file mode 100644
index 0000000..6198806
--- /dev/null
+++ b/xos/core/migrations/0002_initial_data.py
@@ -0,0 +1,73 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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.
+
+import os
+import yaml
+from django.db import models, migrations
+from django.contrib.auth.hashers import make_password
+
+FIXTURES = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/initial_data.yaml')
+
+def load_data_from_yaml():
+    file = open(FIXTURES, 'r').read()
+    try:
+        data = yaml.load(file)
+        return data
+    except Exception, e:
+        raise Exception("Cannot load inital data file: %s" % e.message)
+
+
+def persist_data(apps, schema_editor):
+
+    data = load_data_from_yaml()
+
+    # iterate over the data
+    for entry in data:
+
+        # retrieve the class for that model
+        [app, model_name] = entry['model'].split('.')
+        model_class = apps.get_model(app, model_name)
+
+        # create a new instance for that model
+        i = model_class(**entry['fields'])
+
+        # if model is user hash the password
+        if model_name == "User":
+            i.password = make_password(entry['fields']['password'])
+
+        # check relations
+        if 'relations' in entry:
+            for (r_name, r) in entry['relations'].items():
+                # retrieve the related model
+                [r_app, r_model_name] = r['model'].split('.')
+                related_model_class = apps.get_model(r_app, r_model_name)
+                r_model = related_model_class.objects.get(**r['fields'])
+
+                # assign relation
+                setattr(i, r_name, r_model)
+
+        # save the instance
+        i.save()
+        print "Created %s: %s" % (model_name, entry['fields'])
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('core', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.RunPython(persist_data)
+    ]
\ No newline at end of file
diff --git a/xos/coreapi/start_coreapi.sh b/xos/coreapi/start_coreapi.sh
index 580a295..5b8a78f 100755
--- a/xos/coreapi/start_coreapi.sh
+++ b/xos/coreapi/start_coreapi.sh
@@ -15,9 +15,16 @@
 # limitations under the License.
 
 
+# seed initial data in the db
+bash /opt/xos/tools/xos-manage makemigrations
+python /opt/xos/manage.py migrate;
+
+# build protobuf
 cd protos
 make rebuild-protos
 make
+
+# start the grpc server
 cd ..
 source env.sh
 python ./core_main.py
diff --git a/xos/tools/xos-manage b/xos/tools/xos-manage
index 363b00c..50b152b 100755
--- a/xos/tools/xos-manage
+++ b/xos/tools/xos-manage
@@ -138,7 +138,9 @@
 }
 
 function makemigrations {
+    cp /opt/xos/core/migrations/0002_initial_data.py /opt/xos/0002_initial_data.py # HACK to save 0002_initial_data.py
     rm -rf /opt/xos/*/migrations /opt/xos/services/*/migrations
+    cp /opt/xos/0002_initial_data.py /opt/xos/core/migrations/0002_initial_data.py # HACK to restore 0002_initial_data.py
     python ./manage.py makemigrations core
     python ./manage.py makemigrations syndicate_storage