Moved common from jenkins folder to devops root folder

Change-Id: Ib56b840f5d91f5962daa5fd06d80eb1888617f9e
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/common/all_funcs b/common/all_funcs
new file mode 100644
index 0000000..d469373
--- /dev/null
+++ b/common/all_funcs
@@ -0,0 +1,28 @@
+# this file is meant to be sourced
+#
+#   Copyright 2016 RIFT.IO Inc
+#
+#   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 all functions
+#
+# 24 June 2016 -- Jeremy Mordkoff -- Genesis
+
+if [ -z "$OSM_DEVOPS" ]; then
+	export OSM_DEVOPS=$(realpath ${BASH_SOURCE[0]} )
+fi
+
+for file in logging config container git_functions; do
+	. ${OSM_DEVOPS}/common/$file
+	INFO "$file sourced"
+done
diff --git a/common/config b/common/config
new file mode 100644
index 0000000..7f49381
--- /dev/null
+++ b/common/config
@@ -0,0 +1,40 @@
+#!/bin/bash
+# This file is meant to be SOURCED
+#
+#   Copyright 2016 RIFT.IO Inc
+#
+#   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.
+#
+#  config functions
+# 24 June 2016 -- Jeremy Mordkoff -- Genesis
+
+
+OSM_load_config() { 
+	OSM_load_config_file ${OSM_JENKINS}/SETTINGS
+	if [ -z "$OSM_MDG" ]; then
+		WARNING "OSM_MDG not set" 
+	else 
+		OSM_load_config_file ${OSM_JENKINS}/${OSM_MDG}/SETTINGS
+	fi
+}
+
+OSM_load_config_file() { 
+	[ $# -eq 1 ] || FATAL "arg is filename"
+	if [ -f "$1" ]; then
+		. "$1"
+		INFO "config file $1 loaded"
+	else
+		WARNING "$1 not found"
+	fi
+}
+		
diff --git a/common/container b/common/container
new file mode 100644
index 0000000..e29d5eb
--- /dev/null
+++ b/common/container
@@ -0,0 +1,89 @@
+# This file is meant to be SOURCED
+#
+#   Copyright 2016 RIFT.IO Inc
+#   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.
+#
+# container_funcs
+# 24 June 2016 -- Jeremy Mordkoff -- Genesis
+#              -- Gerardo García
+
+container_exists() { 
+	if [ $# -ne 1 ]; then
+		FATAL "arg is container name"
+	fi
+	lxc config show $1 >/dev/null 2>&1
+	if [ $? -eq 0 ]; then
+		DEBUG "container $1 exists"
+		return 0
+	else
+		DEBUG "container $1 not found"
+		return 1
+	fi
+}
+
+create_container() { 
+	if [ $# -lt 2 ]; then
+		FATAL "args are image container [options]"
+	fi
+	INFO "creating container $2 using image $1"
+	image=$1
+	container=$2
+	shift 2
+	DEBUG "lxc launch $image $container $*"
+	lxc launch "$image" "$container" $*
+}
+
+container_exec() { 
+	container="$1"
+	shift
+	DEBUG "exec in $container \"$*\""
+	lxc exec "$container" -- $*
+}
+
+container_exec_stderr() { 
+	container="$1"
+	shift
+	DEBUG "exec in $container \"$*\""
+	lxc exec "$container" -- $* 2>&1
+}
+
+wait_container_up() {
+    [ $# -eq 1 ] || FATAL "arg is container name got $# args - $*"
+    RE="200"
+    ct=0
+    while [ $ct -lt 10 ]; do
+        let ct=ct+1
+        output=$(container_exec_stderr "$1" curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null)
+        if [[ $output =~ $RE ]]; then
+            DEBUG "$1 is up"
+            return
+        fi
+        INFO "waiting for container $1 to start"
+        DEBUG "expected '$RE' in $output"
+        sleep 5
+    done
+    FATAL "container $1 did not start"
+}
+container_push_tree() { 
+    # create a tarball locally, pipe it into the container and unpack it there
+	[ $# -eq 3 ] || FATAL "args are container dir_from dir_to (dir_to MUST exist)"
+    tar -C "$2" -c . -f - | container_exec $1 tar -C "$3" -x -f -
+}
+
+container_push_devops() { 
+	[ $# -eq 1 ] || FATAL "arg is container name got $# args - $*"
+    container_exec "$1" mkdir -p /root/devops
+    container_push_tree "$1" "$(dirname $OSM_JENKINS)" "/root/devops"
+}
diff --git a/common/git_functions b/common/git_functions
new file mode 100644
index 0000000..7cfee88
--- /dev/null
+++ b/common/git_functions
@@ -0,0 +1,53 @@
+#!/bin/bash
+#
+#   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.
+#
+
+
+GIT() {
+    CMD git "$@"
+}
+
+
+OSM_git_checkout() {
+
+    # Updates all the branches in the local repo (clones if it does not exist)
+    if [ -d $OSM_MDG ]; then
+        INFO "reusing existing workspace"
+        cd $OSM_MDG
+        GIT fetch --all --tags
+        #git checkout master  #to make sure that we are in the right branch before pulling the code
+        #git pull
+    else
+        INFO "cloning MDG $OSM_MDG from $OSM_GIT_URL/$OSM_MDG"
+        GIT clone $OSM_GIT_URL/$OSM_MDG
+        cd $OSM_MDG
+        for remote in `git branch -r |grep -v /HEAD`; do GIT branch --track ${remote#origin/} $remote; done
+    fi
+    
+    if [ $# -gt 0 ]; then
+        if [ "$1" = "checkout" ]; then
+            INFO "Code to compile: '$2'"
+            GIT checkout $2
+        else
+            INFO "Code to compile: gerrit refspec '$1', commit-id: '$2'"
+            GIT fetch origin $1 || FATAL "git fetch origin '$1' didn't work"
+            GIT checkout -f $2 || FATAL "git checkout -f '$2' didn't work"
+        fi
+    else
+        INFO "Code to compile: master"
+        GIT checkout master
+    fi
+
+}
+
diff --git a/common/install_common b/common/install_common
new file mode 100755
index 0000000..901d287
--- /dev/null
+++ b/common/install_common
@@ -0,0 +1,52 @@
+# this file is meant to be sourced
+#
+#   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.
+#
+#
+
+RELEASE="ReleaseTHREE"
+REPOSITORY_KEY="OSM%20ETSI%20Release%20Key.gpg"
+REPOSITORY="stable"
+REPOSITORY_BASE="http://osm-download.etsi.org/repository/osm/debian"
+
+while getopts ":r:k:u:R:b:-:" o; do
+    case "${o}" in
+        r)
+            REPOSITORY=${OPTARG}
+            ;;
+        R)
+            RELEASE=${OPTARG}
+            ;;
+        k)
+            REPOSITORY_KEY=${OPTARG}
+            ;;
+        u)
+            REPOSITORY_BASE=${OPTARG}
+            ;;
+        b)
+            ## ignore branch option
+            ;;
+        -)
+            ;;
+    esac
+done
+
+key_location=$REPOSITORY_BASE/$RELEASE/$REPOSITORY_KEY
+echo $key_location
+
+curl $key_location | apt-key add -
+
+REPOSITORY_BASE=${REPOSITORY_BASE%/}
+apt-get update && add-apt-repository -y "deb $REPOSITORY_BASE/$RELEASE $REPOSITORY SO UI RO MON IM osmclient openvim"
diff --git a/common/logging b/common/logging
new file mode 100644
index 0000000..a95b563
--- /dev/null
+++ b/common/logging
@@ -0,0 +1,61 @@
+#!/bin/bash
+# This file is meant to be SOURCED
+#
+#   Copyright 2016 RIFT.IO Inc
+#
+#   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.
+#
+# container_funcs
+# 24 June 2016 -- Jeremy Mordkoff -- Genesis
+
+print_stack() {
+   local i
+   local stack_size=${#FUNCNAME[1]}
+   echo  "BACKTRACE:" >&2
+   for (( i=1; i<$stack_size ; i++ )); do
+      local func="${FUNCNAME[$i]}"
+      [ x$func = x ] && func=MAIN
+      local linen="${BASH_LINENO[(( i - 1 ))]}"
+      local src="${BASH_SOURCE[$i]}"
+      [ x"$src" = x ] && src=non_file_source
+      echo "### $func $src $linen" >&2
+   done
+   echo "-------" >&2
+}
+
+
+FATAL() { 
+	echo -e "\n### $(date) ${FUNCNAME[1]}: FATAL error: $*" >&2
+	print_stack
+	exit 1
+}
+	
+WARNING() { 
+	echo -e "\n### $(date) ${FUNCNAME[1]}: WARNING error: $*" >&2
+}
+
+INFO() { 
+	echo "##  $(date) ${FUNCNAME[1]}: $*" >&2
+}
+
+DEBUG() { 
+	echo "#   $(date) ${FUNCNAME[1]}: $*" >&2
+}
+
+CMD() {
+	echo "# executing '$*' ..."
+	"$@"
+	rc=$?
+	echo "# .... '$*' done RC was $rc"
+	return $rc
+}