CORD-1081 corebuilder playbooks

Change-Id: I1d9d03d57d096ae1ee439ca5c37d854abfa4f4cf
diff --git a/build-platform-install-playbook.yml b/build-platform-install-playbook.yml
new file mode 100644
index 0000000..8466693
--- /dev/null
+++ b/build-platform-install-playbook.yml
@@ -0,0 +1,18 @@
+---
+# build-platform-install-playbook.yml
+
+- name: Include vars
+  hosts: localhost
+  tasks:
+    - name: Include variables
+      include_vars: "{{ item }}"
+      with_items:
+        - "profile_manifests/{{ cord_profile }}.yml"
+        - profile_manifests/local_vars.yml
+
+- name: Build the core image
+  hosts: localhost
+  roles:
+    - xos-core-build
+
+
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..bf3d0dc
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,57 @@
+import org.yaml.snakeyaml.Yaml
+
+ext {
+
+    // Deployment target config file (yaml format); this can be overwritten from the command line
+    // using the -PdeployConfig=<file-path> syntax.
+    deployConfig = project.hasProperty('deployConfig') ? project.getProperty('deployConfig') : './config/default.yml'
+
+    println "Using deployment config: $deployConfig"
+    File configFile = new File(deployConfig)
+    def yaml = new Yaml()
+    config = yaml.load(configFile.newReader())
+
+    // Upstream registry to simplify filling out the comps table below
+    upstreamReg = project.hasProperty('upstreamReg') ? project.getProperty('upstreamReg') : 'docker.io'
+
+    // Target registry to be used to publish docker images needed for deployment
+    targetReg = project.hasProperty('targetReg')
+        ? project.getProperty('targetReg')
+        : config.docker && config.docker.registry
+            ? config.docker.registry
+            : config.headnode.ip
+                ? config.headnode.ip + ":5000"
+                : 'localhost:5000'
+
+    // The tag used to tag the docker images push to the target registry
+    targetTag = project.hasProperty('targetTag')
+        ? project.getProperty('targetTag')
+        : config.docker && config.docker.imageVersion
+            ? config.docker.imageVersion
+            : 'candidate'
+
+    profileName = config.common && config.common.cord_profile
+        ? config.common.cord_profile
+        : 'rcord'
+
+    inventoryFileName = 'inventory/' + profileName
+}
+
+task buildImages(type: Exec) {
+  executable = "ansible-playbook"
+  args = [ "-i", inventoryFileName,
+    "--extra-vars", "@../genconfig/config.yml",
+    "--extra-vars", "build_docker_tag="+targetTag,
+    "--extra-vars", "cord_dir=../..",
+    "build-platform-install-playbook.yml" ]
+}
+
+task publish(type: Exec) {
+  executable = "ansible-playbook"
+  args = [ "-i", "../genconfig/cord-inv",
+    "--extra-vars", "@../genconfig/config.yml",
+    "--extra-vars", "build_docker_tag="+targetTag,
+    "--extra-vars", "deploy_docker_tag="+targetTag,
+    "--extra-vars", "deploy_docker_registry="+targetReg,
+    "publish-platform-install-playbook.yml" ]
+}
diff --git a/publish-platform-install-playbook.yml b/publish-platform-install-playbook.yml
new file mode 100644
index 0000000..576e21d
--- /dev/null
+++ b/publish-platform-install-playbook.yml
@@ -0,0 +1,17 @@
+---
+# publish-platform-install-playbook.yml
+
+- name: Include vars
+  hosts: localhost
+  tasks:
+    - name: Include variables
+      include_vars: "{{ item }}"
+      with_items:
+        - "profile_manifests/{{ cord_profile }}.yml"
+        - profile_manifests/local_vars.yml
+
+- name: Publish the core image
+  hosts: localhost
+  roles:
+    - xos-core-publish
+
diff --git a/roles/cord-profile/templates/cord-services.yaml.j2 b/roles/cord-profile/templates/cord-services.yaml.j2
index 7343ad1..f6295ae 100644
--- a/roles/cord-profile/templates/cord-services.yaml.j2
+++ b/roles/cord-profile/templates/cord-services.yaml.j2
@@ -70,7 +70,7 @@
         public_key: { get_artifact: [ SELF, pubkey, LOCAL_FILE] }
         private_key_fn: /opt/xos/services/volt/keys/volt_rsa
       artifacts:
-        pubkey: /opt/xos/services/volt/keys/volt_rsa.pub
+        pubkey: /opt/cord_profile/key_import/volt_rsa.pub
 
     addresses_vsg:
       type: tosca.nodes.AddressPool
@@ -103,7 +103,7 @@
 #       node_label: label_vsg
         replaces: service_vsg
       artifacts:
-        pubkey: /opt/xos/services/vsg/keys/vsg_rsa.pub
+        pubkey: /opt/cord_profile/key_import/vsg_rsa.pub
 
     service#vrouter:
       type: tosca.nodes.VRouterService
diff --git a/roles/cord-profile/templates/xos.yaml.j2 b/roles/cord-profile/templates/xos.yaml.j2
index b886d87..db10d44 100644
--- a/roles/cord-profile/templates/xos.yaml.j2
+++ b/roles/cord-profile/templates/xos.yaml.j2
@@ -16,6 +16,8 @@
         docker_project_name: {{ cord_profile | regex_replace('\W','') }}
         db_container_name: {{ cord_profile | regex_replace('\W','') }}bs_xos_db_1
         source_ui_image: {{ xos_docker_image }}
+#        dest_ui_image: docker-registry:5000/xosproject/xos-ui:candidate
+#        no_build: True
 {% if use_redis %}
         redis_container_name: {{ cord_profile | regex_replace('\W','') }}bs_xos_redis_1
 {% endif %}
diff --git a/roles/exampleservice-config/templates/test-exampleservice.yaml.j2 b/roles/exampleservice-config/templates/test-exampleservice.yaml.j2
index 9e5f4e5..08b98ee 100644
--- a/roles/exampleservice-config/templates/test-exampleservice.yaml.j2
+++ b/roles/exampleservice-config/templates/test-exampleservice.yaml.j2
@@ -104,7 +104,7 @@
         private_key_fn: /opt/xos/services/exampleservice/keys/exampleservice_rsa
         service_message: hello
       artifacts:
-        pubkey: /opt/xos/services/exampleservice/keys/exampleservice_rsa.pub
+        pubkey: /opt/cord_profile/key_import/exampleservice_rsa.pub
 
     tenant#exampletenant1:
       type: tosca.nodes.ExampleTenant
diff --git a/roles/xos-core-build/defaults/main.yml b/roles/xos-core-build/defaults/main.yml
new file mode 100644
index 0000000..6805c14
--- /dev/null
+++ b/roles/xos-core-build/defaults/main.yml
@@ -0,0 +1,10 @@
+---
+
+cord_dir: "{{ playbook_dir + '/../..' }}"
+build_docker_tag: candidate
+
+deploy_docker_registry: "localhost:5000"
+deploy_docker_tag: "candidate"
+
+xos_libraries:
+  - "ng-xos-lib"
diff --git a/roles/xos-core-build/tasks/main.yml b/roles/xos-core-build/tasks/main.yml
new file mode 100644
index 0000000..09ea5b9
--- /dev/null
+++ b/roles/xos-core-build/tasks/main.yml
@@ -0,0 +1,54 @@
+---
+# xos-core-build/tasks/main.yml
+
+- name: Intialize recipe list
+  set_fact:
+    recipes: []
+
+- name: Generate list of recipes
+  set_fact:
+    recipes: "{{ recipes }} + [ '/opt/cord/{{ item.path }}/xos/{{ item.name }}-{{ item.operation | default('onboard') }}.yaml']"
+  with_items: "{{ xos_services }}"
+
+- name: Generate list of libraries
+  set_fact:
+    recipes: "{{ recipes }} + [ '/opt/cord/orchestration/xos_libraries/{{ item }}/{{ item }}-onboard.yaml']"
+  with_items: "{{ xos_libraries }}"
+
+- name: Join the list of recipes
+  set_fact:
+    recipes_string: "{{ recipes | join(' ') }}"
+
+- debug:
+    msg: "{{ recipes }}"
+
+- name: Create the BUILD directory
+  file:
+    path: "{{ cord_dir | realpath }}/orchestration/xos/containers/xos/BUILD"
+    state: directory
+    mode: 0755
+
+# Run the corebuilder tool -- we can't do this using a docker ansible command,
+# as we have to wait for the tool to complete before continuing.
+
+- name: Run the corebuilder tool
+  shell: >
+    docker run
+    --rm
+    -v {{ cord_dir | realpath }}:/opt/cord
+    -v {{ cord_dir | realpath }}/orchestration/xos/containers/xos/BUILD:/opt/xos_corebuilder/BUILD
+    xosproject/xos-corebuilder:candidate
+    {{ recipes_string }}
+  tags:
+   - skip_ansible_lint # running a build task
+
+- name: Build UI image
+  docker_image:
+    name: "xosproject/xos-ui"
+    path: "{{ cord_dir | realpath }}/orchestration/xos"
+    tag: "{{ build_docker_tag }}"
+    dockerfile: "containers/xos/Dockerfile.UI"
+    pull: False
+
+
+
diff --git a/roles/xos-core-publish/defaults/main.yml b/roles/xos-core-publish/defaults/main.yml
new file mode 100644
index 0000000..cfc4100
--- /dev/null
+++ b/roles/xos-core-publish/defaults/main.yml
@@ -0,0 +1,6 @@
+---
+
+build_docker_tag: candidate
+
+deploy_docker_registry: "localhost:5000"
+deploy_docker_tag: "candidate"
diff --git a/roles/xos-core-publish/tasks/main.yml b/roles/xos-core-publish/tasks/main.yml
new file mode 100644
index 0000000..803f5a4
--- /dev/null
+++ b/roles/xos-core-publish/tasks/main.yml
@@ -0,0 +1,10 @@
+---
+# xos-core-deploy/tasks/main.yml
+
+- name: Tag and push the UI image
+  docker_image:
+    name: "xosproject/xos-ui:{{ build_docker_tag }}"
+    repository: "{{ deploy_docker_registry }}/xosproject/xos-ui:{{ deploy_docker_tag }}"
+    push: True
+
+