COMAC-8 initial version of comac-in-a-box makefile

This makefile installs OMEC and OAI eNB and UE in a single node.
It does not require any special hardwares.
Run "make" -> "make run-test" for test.
CORD platform support will be added later.

Change-Id: I9f27219522c85dcd95c50a23a9bab5f32f7484b1
diff --git a/comac-in-a-box/Makefile b/comac-in-a-box/Makefile
new file mode 100644
index 0000000..7eca5ee
--- /dev/null
+++ b/comac-in-a-box/Makefile
@@ -0,0 +1,189 @@
+SHELL		:= /bin/bash
+BUILD		?= /tmp/build
+M		?= $(BUILD)/milestones
+MAKEDIR		:= $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
+SCRIPTDIR	:= $(MAKEDIR)../scripts
+RESOURCEDIR	:= $(MAKEDIR)/resources
+WORKSPACE	?= $(HOME)
+VENV		?= $(BUILD)/venv/ciab
+CIABVALUES	?= $(MAKEDIR)/comac-in-a-box-values.yaml
+
+KUBESPRAY_VERSION ?= release-2.10
+DOCKER_VERSION	?= 18.06
+K8S_VERSION	?= v1.14.3
+HELM_VERSION	?= v2.13.1
+
+# used to start logging/monitoring and other infrastructure charts
+INFRA_CHARTS	?=
+INFRA_PREREQS   = $(foreach chart,$(INFRA_CHARTS),$(M)/$(chart))
+
+KAFKA_CHART_VERSION ?= 0.13.3
+KAFKA_POD	:= "pod/cord-kafka-0"
+
+ETCD_OPERATOR_VERSION ?= 0.8.3
+
+HELM_GLOBAL_ARGS ?=
+HELM_NEM_ARGS	?= $(HELM_GLOBAL_ARGS)
+HELM_ONOS_ARGS	?= $(HELM_GLOBAL_ARGS)
+
+cpu_family	:= $(shell lscpu | grep 'CPU family:' | awk '{print $$3}')
+cpu_model	:= $(shell lscpu | grep 'Model:' | awk '{print $$2}')
+os_vendor	:= $(shell lsb_release -i -s)
+os_distro	:= $(shell lsb_release -c -s)
+
+# TODO: include CORD platform
+ciab: $(M)/system_check $(M)/omec $(M)/oaisim
+
+.PHONY: ciab run-test reset-test clean
+
+$(M):
+	mkdir -p $(M)
+
+$(M)/system_check: | $(M)
+	@if [[ $(cpu_family) -eq 6 ]]; then \
+		if [[ $(cpu_model) -lt 60 ]]; then \
+			echo "FATAL: haswell CPU or newer is required."; \
+			exit 1; \
+		fi \
+	else \
+		echo "FATAL: unsupported CPU family."; \
+		exit 1; \
+	fi
+	@if [[ $(os_vendor) =~ (Ubuntu) ]]; then \
+		if [[ ! $(os_distro) =~ (xenial) ]]; then \
+			echo "WARN: $(os_vendor) $(os_distro) has not been tested."; \
+		fi \
+	else \
+		echo "FAIL: unsupported OS."; \
+		exit 1; \
+	fi
+	touch $@
+
+$(M)/setup: | $(M)
+	sudo $(SCRIPTDIR)/cloudlab-disksetup.sh
+	sudo apt update; sudo apt install -y software-properties-common python-pip jq httpie ipvsadm
+	touch $@
+
+$(BUILD)/kubespray: | $(M)/setup
+	mkdir -p $(BUILD)
+	cd $(BUILD); git clone https://github.com/kubernetes-incubator/kubespray.git -b $(KUBESPRAY_VERSION)
+
+$(VENV)/bin/activate: | $(M)/setup
+	sudo pip install virtualenv
+	virtualenv $(VENV) --no-site-packages
+
+$(M)/kubespray-requirements: $(BUILD)/kubespray | $(VENV)/bin/activate
+	source "$(VENV)/bin/activate" && \
+	pip install -r $(BUILD)/kubespray/requirements.txt
+	touch $@
+
+$(M)/k8s-ready: | $(M)/setup $(BUILD)/kubespray $(VENV)/bin/activate $(M)/kubespray-requirements
+	source "$(VENV)/bin/activate" && cd $(BUILD)/kubespray; \
+	ansible-playbook -b -i inventory/local/hosts.ini \
+		-e "{'override_system_hostname' : False, 'disable_swap' : True}" \
+		-e "{'docker_version' : $(DOCKER_VERSION)}" \
+		-e "{'docker_iptables_enabled' : True}" \
+		-e "{'kube_version' : $(K8S_VERSION)}" \
+		-e "{'kube_network_plugin_multus' : True}" \
+		-e "{'kube_proxy_mode': iptables}" \
+		-e "{'kube_pods_subnet' : 192.168.0.0/17, 'kube_service_addresses' : 192.168.128.0/17}" \
+		-e "{'kube_apiserver_node_port_range' : 2000-36767}" \
+		-e "{'kubeadm_enabled': True}" \
+		-e "{'kube_feature_gates' : [SCTPSupport=True]}" \
+		-e "{'kubelet_custom_flags' : [--allowed-unsafe-sysctls=net.*]}" \
+		-e "{'dns_min_replicas' : 1}" \
+		-e "{'helm_enabled' : True, 'helm_version' : $(HELM_VERSION)}" \
+		cluster.yml
+	mkdir -p $(HOME)/.kube
+	sudo cp -f /etc/kubernetes/admin.conf $(HOME)/.kube/config
+	sudo chown $(shell id -u):$(shell id -g) $(HOME)/.kube/config
+	kubectl wait pod -n kube-system --for=condition=Ready --all
+	touch $@
+
+$(M)/helm-ready: | $(M)/k8s-ready
+	helm init --wait --client-only
+	helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com/
+	helm repo add cord https://charts.opencord.org
+	touch $@
+
+$(WORKSPACE)/cord/helm-charts: | $(M)/setup
+	mkdir -p $(WORKSPACE)/cord
+	cd $(WORKSPACE)/cord; git clone https://gerrit.opencord.org/helm-charts
+
+# TODO: need to connect ONOS
+$(M)/ovs-setup: | $(M)/setup
+	sudo apt install -y openvswitch-switch
+	sudo ovs-vsctl --may-exist add-br br-s1u-net
+	sudo ovs-vsctl --may-exist add-port br-s1u-net s1u-enb -- set Interface s1u-enb type=internal
+	sudo ip link set s1u-enb address 0a:00:00:00:00:01
+	sudo ip addr add 119.0.0.253/24 dev s1u-enb || true
+	sudo ip link set s1u-enb up
+	touch $@
+
+/opt/cni/bin/simpleovs: | $(M)/k8s-ready
+	sudo cp $(RESOURCEDIR)/simpleovs /opt/cni/bin/
+
+$(M)/omec: | $(M)/ovs-setup $(M)/helm-ready $(WORKSPACE)/cord/helm-charts /opt/cni/bin/simpleovs
+	cd $(WORKSPACE)/cord/helm-charts/mcord-release; \
+	helm upgrade --install $(HELM_GLOBAL_ARGS) mcord-services mcord-services -f $(CIABVALUES)
+	$(WORKSPACE)/cord/helm-charts/scripts/wait_for_pods.sh default
+	touch $@
+
+# UE images includes kernel module, ue_ip.ko
+# which should be built in the exactly same kernel version of the host machine
+$(BUILD)/openairinterface: | $(M)/setup
+	mkdir -p $(BUILD)
+	cd $(BUILD); git clone https://github.com/opencord/openairinterface.git
+
+$(M)/ue-image: | $(M)/k8s-ready $(BUILD)/openairinterface
+	cd $(BUILD)/openairinterface; \
+	sudo docker build . --target lte-uesoftmodem \
+		--build-arg build_base=omecproject/oai-base:1.0.0 \
+		--file Dockerfile.ue \
+		--tag omecproject/lte-uesoftmodem:1.0.0
+	touch $@
+
+$(M)/oaisim: | $(M)/omec $(M)/ue-image
+	sudo ip addr add 127.0.0.2/8 dev lo || true
+	$(eval mme_iface=$(shell ip -4 route list default | awk -F 'dev' '{ print $$2; exit }' | awk '{ print $$1 }'))
+	cd $(WORKSPACE)/cord/helm-charts; \
+	helm upgrade --install $(HELM_GLOBAL_ARGS) oaisim oaisim -f $(CIABVALUES) \
+		--set conf.enb.networks.s1_mme.interface=$(mme_iface)
+	$(WORKSPACE)/cord/helm-charts/scripts/wait_for_pods.sh default
+	@timeout 60s bash -c \
+	"until ip addr show oip1 | grep -q inet; \
+	do \
+		echo 'Waiting for UE 1 gets IP address'; \
+		sleep 3; \
+	done"
+	touch $@
+
+$(M)/router: | /opt/cni/bin/simpleovs $(M)/ovs-setup
+	kubectl apply -f $(RESOURCEDIR)/quagga.yaml
+	kubectl wait pod --for=condition=Ready -l app=quagga
+	$(eval spgwu_ip=$(shell kubectl get pod -ojson spgwu-0 2>/dev/null | \
+		jq -r '.metadata.annotations["k8s.v1.cni.cncf.io/networks-status"]' | \
+		jq -r '.[] | select(.name=="sgi-net") | .ips[0]'))
+	kubectl exec router ip route add 16.0.0.0/8 via $(spgwu_ip)
+	touch $@
+
+run-test: | $(M)/router
+	ping -I oip1 13.1.1.254 -c 5
+
+reset-test:
+	-kubectl delete po router
+	-helm delete --purge oaisim mcord-services
+	cd $(M); rm -f router oaisim omec
+
+clean: reset-test
+	helm delete --purge $(shell helm ls -q) || true
+	sudo ovs-vsctl del-br br-s1u-net || true
+	sudo ovs-vsctl del-br br-sgi-net || true
+	sudo apt remove --purge openvswitch-switch -y
+	source "$(VENV)/bin/activate" && cd $(BUILD)/kubespray; \
+	ansible-playbook -b -i inventory/local/hosts.ini reset.yml
+	@if [ -d /usr/local/etc/emulab ]; then \
+		mount | grep /mnt/extra/kubelet/pods | cut -d" " -f3 | sudo xargs umount; \
+		sudo rm -rf /mnt/extra/kubelet; \
+	fi
+	rm -rf $(M)
diff --git a/comac-in-a-box/comac-in-a-box-values.yaml b/comac-in-a-box/comac-in-a-box-values.yaml
new file mode 100644
index 0000000..df30271
--- /dev/null
+++ b/comac-in-a-box/comac-in-a-box-values.yaml
@@ -0,0 +1,77 @@
+# Copyright 2019-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.
+
+---
+# etcd-cluster
+etcdNodePort: 32379
+clusterSize: 1
+
+# mcord-services values
+hssdb:
+  cpu: 1
+hss:
+  cpu: 0.5
+spgwc:
+  image: omecproject/ngic-cp:0.1.0-debug
+  cpu: 0.5
+spgwu:
+  images:
+    tags:
+      spgwu: omecproject/ngic-dp:0.1.0-debug
+  resources:
+    cpu: 1
+  conf:
+    dp:
+      devices: "--no-pci --vdev eth_af_packet0,iface=s1u-net --vdev eth_af_packet1,iface=sgi-net"
+mme:
+  type: openmme
+  resources:
+    cpu: 0.5
+network:
+  sriov:
+    enabled: false
+  s1u:
+    cni_plugin: simpleovs
+    ipam: host-local
+    static_arp:
+    - ip: 119.0.0.253
+      mac: 0a:00:00:00:00:01
+  sgi:
+    cni_plugin: simpleovs
+    ipam: host-local
+    static_arp:
+    - ip: 13.1.1.254
+      mac: 0e:00:00:00:00:01
+
+# oaisim values
+conf:
+  plmn:
+    name: "COMAC Test Network"
+    short_name: "COMAC"
+    mcc: "208"
+    mnc: "01"
+  enb:
+    mme:
+      address: 127.0.0.1
+    networks:
+      s1u:
+        interface: s1u-enb
+  ue:
+    # 1 UE is supported for now
+    name: ue
+    sim:
+      msin: "4567891201"
+      api_key: "465b5ce8b199b49faa5f0a2ee238a6bc"
+      opc: "d4416644f6154936193433dd20a0ace0"
+      msisdn: "1122334456"
diff --git a/comac-in-a-box/resources/quagga.yaml b/comac-in-a-box/resources/quagga.yaml
new file mode 100644
index 0000000..63f87f5
--- /dev/null
+++ b/comac-in-a-box/resources/quagga.yaml
@@ -0,0 +1,43 @@
+#!/bin/bash -x
+#
+# Copyright 2019-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.
+---
+apiVersion: v1
+kind: Pod
+metadata:
+  name: router
+  labels:
+    app: quagga
+  annotations:
+    k8s.v1.cni.cncf.io/networks: '[
+            { "name": "sgi-net", "interface": "sgi-rtr", "interfaceRequest": "sgi-rtr" }
+    ]'
+spec:
+  containers:
+  - name: quagga
+    command: ["/bin/bash", "-c"]
+    args:
+      - >
+        ip addr flush sgi-rtr;
+        ip link set sgi-rtr address 0e:00:00:00:00:01;
+        ip addr add 13.1.1.254/24 dev sgi-rtr;
+        trap : TERM INT; sleep infinity & wait
+    image: opencord/quagga
+    securityContext:
+      capabilities:
+        add:
+          - NET_ADMIN
+    ports:
+    - containerPort: 2601
diff --git a/comac-in-a-box/resources/simpleovs b/comac-in-a-box/resources/simpleovs
new file mode 100755
index 0000000..f43b8a1
--- /dev/null
+++ b/comac-in-a-box/resources/simpleovs
@@ -0,0 +1,77 @@
+#!/bin/bash -x
+#
+# Copyright 2019-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.
+
+set -o errexit
+set -o pipefail
+set -o nounset
+
+exec 3>&1
+exec &>>/var/log/simple-ovs-cni.log
+
+PATH="$CNI_PATH:$(dirname "${BASH_SOURCE[0]}"):$PATH"
+CNI_CONF=$(cat /dev/stdin)
+
+get_bridge() {
+	echo "br-$(echo $CNI_CONF | jq -r '.name')"
+}
+
+ipam() {
+	local plugin=$(echo $CNI_CONF | jq -r '.ipam.type')
+	local res=$(echo $"$CNI_CONF" | "$plugin" | jq -c '.')
+	echo $res
+}
+
+add_br_and_port() {
+	mkdir -p /var/run/netns/
+        ln -sfT $CNI_NETNS /var/run/netns/$CNI_CONTAINERID
+
+	local bridge=$(get_bridge)
+	local ip=$1
+
+	ovs-vsctl --may-exist add-br $bridge
+	ovs-vsctl add-port $bridge $CNI_IFNAME -- set Interface $CNI_IFNAME type=internal
+
+	ip link set $CNI_IFNAME netns $CNI_CONTAINERID
+        ip netns exec $CNI_CONTAINERID ip addr add $ip dev $CNI_IFNAME
+        ip netns exec $CNI_CONTAINERID ip link set $CNI_IFNAME up
+}
+
+delete_br_and_port() {
+	local bridge="br-$(echo $CNI_CONF | jq -r '.name')"
+	ovs-vsctl del-port $CNI_IFNAME
+	if [ -z "$(ovs-vsctl list-ports $bridge)" ]; then
+		ovs-vsctl del-br $bridge
+	fi
+}
+
+case $CNI_COMMAND in
+ADD)
+	res=$(ipam)
+	ip=$(echo $res | jq -r '.ip4.ip')
+	add_br_and_port $ip
+        echo '{"cniVersion":"0.2.0"}' | jq -c --arg ip $ip '.ip4.ip = $ip' >&3
+	;;
+DEL)
+	set +o errexit
+	ipam
+	delete_br_and_port
+	set -o errexit
+	;;
+*)
+	echo "CNI_COMMAND=[ADD|DEL] only supported"
+	exit 1
+	;;
+esac