systest jenkins pipeline

Change-Id: I496980ad1dcad1be691587caec41d7d86259a55f
Signed-off-by: Mike Marchetti <mmarchetti@sandvine.com>
diff --git a/jenkins/system/Jenkinsfile b/jenkins/system/Jenkinsfile
new file mode 100644
index 0000000..d3688c4
--- /dev/null
+++ b/jenkins/system/Jenkinsfile
@@ -0,0 +1,30 @@
+// input parameters:
+//   boolean: BUILD_FROM_SOURCE
+//   boolean: COMMIT_ID
+//   string:  NODE
+
+node("${params.NODE}") {
+
+    stage("Setup") {
+        tag_or_branch = params.COMMIT_ID.replaceAll(/\./,"")
+        container_name_prefix = "osm-${tag_or_branch}"
+        container_name = "${container_name_prefix}-${BUILD_NUMBER}"
+    }
+
+    stage("Cleanup") {
+        // check for previous containers and clean them up
+        sh "jenkins/system/delete_old_containers.sh ${container_name_prefix}"
+    }
+
+    stage("Build") {
+        from_source = ''
+        if ( params.BUILD_FROM_SOURCE )
+        {
+            from_source = '--source'
+        }
+
+        sh "jenkins/host/start_build system --build-container ${container_name} -b ${params.COMMIT_ID} ${from_source}"
+        sh "echo ${container_name} > build_version.txt"
+        archiveArtifacts artifacts: "build_version.txt"
+    }
+}
diff --git a/jenkins/system/SETTINGS b/jenkins/system/SETTINGS
new file mode 100644
index 0000000..912799c
--- /dev/null
+++ b/jenkins/system/SETTINGS
@@ -0,0 +1,39 @@
+#   Copyright 2016 Telefónica Investigación y Desarrollo, S.A.U.
+#
+#   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.
+#
+# sample SETTINGS file
+#
+# Authors:
+#     - Gerardo Garcia
+#
+# this variable holds the name of the container image needed to build or run this product
+export OSM_BASE_IMAGE=ubuntu:16.04
+#
+# this variable must be set to allow creating the build container in privileged mode
+# this variable should be removed in the future when no privileged mode is required
+export OSM_BUILD_CONTAINER_PRIVILEGED=yes
+#
+# this variable must be set to allow ensted containers in the build container
+export OSM_BUILD_CONTAINER_ALLOW_NESTED=yes
+#
+# this variable must be set to allow creating the runtime container in privileged mode
+# this variable should be removed in the future when no privileged mode is required
+export OSM_RUNTIME_CONTAINER_PRIVILEGED=yes
+#
+# this variable must be set to allow ensted containers in the build container
+export OSM_RUNTIME_CONTAINER_ALLOW_NESTED=yes
+#
+#
+export OSM_BUILD_CONTAINER=osm
+export OSM_RUNTIME_CONTAINER=osm
diff --git a/jenkins/system/delete_old_containers.sh b/jenkins/system/delete_old_containers.sh
new file mode 100755
index 0000000..13c6df4
--- /dev/null
+++ b/jenkins/system/delete_old_containers.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+#   Copyright 2017 Sandvine
+#
+#   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.
+#
+# A helper routine that cleans up old container images based
+# on an input prefix to check. Jenkins builds will add an incrementing
+# build suffix  (build number) to the prefix
+#
+#$1 container prefix name
+
+keep_number=1
+prefix=$1
+
+# keep the first build
+keep=$(lxc list | grep $prefix | awk '{print $2}' | sort -rn | head -n$keep_number)
+for container in $(lxc list | grep $prefix | awk '{print $2}' | sort -rn); do
+    if [ "$container" != "$keep" ]; then
+        echo "deleting old container $container"
+        lxc delete $container --force
+    fi
+done
diff --git a/jenkins/system/start_build b/jenkins/system/start_build
new file mode 100755
index 0000000..a50a5a8
--- /dev/null
+++ b/jenkins/system/start_build
@@ -0,0 +1,77 @@
+#!/bin/bash
+#   Copyright 2017
+#
+#   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.
+#
+# 01 May 2017 -- Michael Marchetti -- adapted from template
+#
+
+HERE=$(realpath $(dirname $0))
+OSM_JENKINS=$(dirname $HERE)
+. $OSM_JENKINS/common/all_funcs
+
+INFO "Installing packages"
+apt-get update
+
+INFO "Configuring LXD"
+# ZFS doesn't work inside a nested container. ZFS should be configured in the host LXD.
+lxd init --auto
+lxd waitready
+systemctl stop lxd-bridge
+systemctl --system daemon-reload
+cat <<EOF > /etc/default/lxd-bridge
+USE_LXD_BRIDGE="true"
+LXD_BRIDGE="lxdbr0"
+UPDATE_PROFILE="true"
+LXD_CONFILE=""
+LXD_DOMAIN="lxd"
+LXD_IPV4_ADDR="10.44.126.1"
+LXD_IPV4_NETMASK="255.255.255.0"
+LXD_IPV4_NETWORK="10.44.126.1/24"
+LXD_IPV4_DHCP_RANGE="10.44.126.2,10.44.126.254"
+LXD_IPV4_DHCP_MAX="252"
+LXD_IPV4_NAT="true"
+LXD_IPV6_ADDR=""
+LXD_IPV6_MASK=""
+LXD_IPV6_NETWORK=""
+LXD_IPV6_NAT="false"
+LXD_IPV6_PROXY="false"
+EOF
+
+systemctl enable lxd-bridge
+systemctl start lxd-bridge
+
+apt-get install -y python-pip python python-pycurl charm-tools python-pytest
+
+# TODO: use package when available on osm repo
+git clone https://osm.etsi.org/gerrit/osm/osmclient
+pip install osmclient/.
+
+devops/installers/install_osm.sh $*
+RC=$?
+
+# workaround.  for upload packages, lxdbr0 needs to be promiscuous
+# as the upload calls back to the UI server so the lxdbr0 needs
+# to operate as a bridge
+ifconfig lxdbr0 promisc
+
+if [ $RC == 0 ]; then
+   # success. find all the resulting containers
+   . devops/installers/export_ips
+
+   TO_ADD="export OSM_HOSTNAME=$SO_CONTAINER_IP"
+   grep -q OSM_HOSTNAME ~/.bashrc && sed -i "s/.*OSM_HOSTNAME.*/$TO_ADD/" ~/.bashrc || echo -e "$TO_ADD\n$(cat ~/.bashrc)" > ~/.bashrc
+fi
+
+INFO "done, RC=$RC"
+exit $RC