[SEBA-497]

Change to use 'xosproject/alpine-grpc-base' as Dockerfile parent image
Makefile, tox.ini and various improvements to dev tooling

Change-Id: Iddbfa7981cd3acac6f03a50949ea72c0bf5f27ad
diff --git a/.gitignore b/.gitignore
index 0f03cd1..0d57996 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,7 @@
 .idea
-*.pyc
-
+.tox
 .coverage
-htmlcov
 coverage.xml
-nose2-junit.xml
-
+nose2-results.xml
+venv-service
+*.pyc
diff --git a/Dockerfile.synchronizer b/Dockerfile.synchronizer
index 62f9cc4..949221d 100644
--- a/Dockerfile.synchronizer
+++ b/Dockerfile.synchronizer
@@ -12,25 +12,24 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# docker build -t xosproject/kubernetes-synchronizer:candidate -f Dockerfile.synchronizer .
-
 # xosproject/kubernetes-synchronizer
-FROM xosproject/xos-synchronizer-base:2.2.18
+FROM xosproject/alpine-grpc-base:0.9.0
 
+# Install curl (to download kubectl later)
+RUN apk add --no-cache curl
+
+# Install pip packages
+COPY requirements.txt /tmp/requirements.txt
+RUN pip install -r /tmp/requirements.txt \
+ && pip freeze > /var/xos/pip_freeze_kubernetes_service_`date -u +%Y%m%dT%H%M%S`
+
+# Copy code
 COPY xos/synchronizer /opt/xos/synchronizers/kubernetes
 COPY VERSION /opt/xos/synchronizers/kubernetes/
 
-COPY pip_requirements.txt /tmp/pip_requirements_kubernetes_service.txt
-RUN pip install --no-deps -r /tmp/pip_requirements_kubernetes_service.txt \
- && pip freeze > /var/xos/pip_freeze_pip_up_kubernetes_services_`date -u +%Y%m%dT%H%M%S`
-
-# Workaround for kubernetes library issue, see CORD-3054
-RUN echo "7ef7f85c6da121fd96c0ba4a22517169  /usr/local/lib/python2.7/dist-packages/kubernetes/client/api_client.py" | md5sum -c -
-COPY api_client.patch /tmp/api_client.patch
-RUN patch -d /usr/local/lib/python2.7/dist-packages/kubernetes/client < /tmp/api_client.patch
-
-ENV KUBECTL_VERSION "1.10.5"
-ENV KUBECTL_SHA256SUM "a9e7f82e516aa8a652bc485620483ea964eb940787cabf890d7eab96eaac294d"
+# Install kubectl
+ENV KUBECTL_VERSION "1.12.6"
+ENV KUBECTL_SHA256SUM "ff94e3024dd245bf8761fac1b244776b169ade056daec2e1ccaf9b65f1d646ca"
 RUN curl -L -o /tmp/kubectl "https://storage.googleapis.com/kubernetes-release/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl" \
   && echo "$KUBECTL_SHA256SUM  /tmp/kubectl" | sha256sum -c - \
   && mv /tmp/kubectl /usr/local/bin/kubectl \
@@ -40,32 +39,18 @@
 WORKDIR "/opt/xos/synchronizers/kubernetes"
 
 # Label image
-ARG org_label_schema_schema_version=1.0
-ARG org_label_schema_name=kubernetes-synchronizer
 ARG org_label_schema_version=unknown
 ARG org_label_schema_vcs_url=unknown
 ARG org_label_schema_vcs_ref=unknown
 ARG org_label_schema_build_date=unknown
 ARG org_opencord_vcs_commit_date=unknown
-ARG org_opencord_component_chameleon_version=unknown
-ARG org_opencord_component_chameleon_vcs_url=unknown
-ARG org_opencord_component_chameleon_vcs_ref=unknown
-ARG org_opencord_component_xos_version=unknown
-ARG org_opencord_component_xos_vcs_url=unknown
-ARG org_opencord_component_xos_vcs_ref=unknown
 
-LABEL org.label-schema.schema-version=$org_label_schema_schema_version \
-      org.label-schema.name=$org_label_schema_name \
+LABEL org.label-schema.schema-version=1.0 \
+      org.label-schema.name=kubernetes-synchronizer \
       org.label-schema.version=$org_label_schema_version \
       org.label-schema.vcs-url=$org_label_schema_vcs_url \
       org.label-schema.vcs-ref=$org_label_schema_vcs_ref \
       org.label-schema.build-date=$org_label_schema_build_date \
-      org.opencord.vcs-commit-date=$org_opencord_vcs_commit_date \
-      org.opencord.component.chameleon.version=$org_opencord_component_chameleon_version \
-      org.opencord.component.chameleon.vcs-url=$org_opencord_component_chameleon_vcs_url \
-      org.opencord.component.chameleon.vcs-ref=$org_opencord_component_chameleon_vcs_ref \
-      org.opencord.component.xos.version=$org_opencord_component_xos_version \
-      org.opencord.component.xos.vcs-url=$org_opencord_component_xos_vcs_url \
-      org.opencord.component.xos.vcs-ref=$org_opencord_component_xos_vcs_ref
+      org.opencord.vcs-commit-date=$org_opencord_vcs_commit_date
 
 CMD ["/usr/bin/python", "/opt/xos/synchronizers/kubernetes/kubernetes-synchronizer.py"]
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..22c1531
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,79 @@
+# 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.
+
+# Configure shell
+SHELL = bash -e -o pipefail
+
+# Variables
+VERSION                  ?= $(shell cat ./VERSION)
+SERVICE_NAME             ?= $(notdir $(abspath .))
+
+## Docker related
+DOCKER_REGISTRY          ?=
+DOCKER_REPOSITORY        ?=
+DOCKER_BUILD_ARGS        ?=
+DOCKER_TAG               ?= ${VERSION}
+DOCKER_IMAGENAME         := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}${SERVICE_NAME}:${DOCKER_TAG}
+
+## Docker labels. Only set ref and commit date if committed
+DOCKER_LABEL_VCS_URL     ?= $(shell git remote get-url $(shell git remote))
+DOCKER_LABEL_VCS_REF     ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown")
+DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" )
+DOCKER_LABEL_BUILD_DATE  ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
+
+## Migration related - paths are relative to the xos subdirectory within this repo
+XOS_DIR                  ?= "../../../xos"
+SERVICES_DIR             ?= "../.."
+
+all: test
+
+docker-build:
+	docker build $(DOCKER_BUILD_ARGS) \
+    -t ${DOCKER_IMAGENAME} \
+    --build-arg org_label_schema_version="${VERSION}" \
+    --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
+    --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
+    --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
+    --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
+    -f Dockerfile.synchronizer .
+
+docker-push:
+	docker push ${DOCKER_IMAGENAME}
+
+test: test-unit test-migration
+
+test-unit:
+	tox
+
+venv-service:
+	virtualenv $@;\
+    source ./$@/bin/activate ; set -u ;\
+    pip install -r requirements.txt xosmigrate~=3.0.1
+
+create-migration: venv-service
+	source ./venv-service/bin/activate; set -u;\
+    cd xos; xos-migrate --xos-dir ${XOS_DIR} --services-dir ${SERVICES_DIR} -s ${SERVICE_NAME}
+
+test-migration: venv-service
+	source ./venv-service/bin/activate; set -u;\
+    cd xos; xos-migrate --xos-dir ${XOS_DIR} --services-dir ${SERVICES_DIR} -s ${SERVICE_NAME} --check
+
+clean:
+	find . -name '*.pyc' | xargs rm -f
+	rm -rf \
+    .tox \
+    venv-service \
+    xos/.coverage \
+    xos/coverage.xml \
+    xos/nose2-results.xml
diff --git a/VERSION b/VERSION
index 18efdb9..26aaba0 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.1.8
+1.2.0
diff --git a/api_client.patch b/api_client.patch
deleted file mode 100644
index 864bbb4..0000000
--- a/api_client.patch
+++ /dev/null
@@ -1,33 +0,0 @@
---- api_client.py       2018-05-29 17:59:49.000000000 +0000
-+++ api_client.py.new   2018-05-29 17:56:27.000000000 +0000
-@@ -64,7 +64,7 @@
-             configuration = Configuration()
-         self.configuration = configuration
- 
--        self.pool = ThreadPool()
-+        self.pool = None # NOTE(smbaker): disabled threadpool 
-         self.rest_client = RESTClientObject(configuration)
-         self.default_headers = {}
-         if header_name is not None:
-@@ -73,9 +73,10 @@
-         # Set default User-Agent.
-         self.user_agent = 'Swagger-Codegen/6.0.0/python'
-     
--    def __del__(self):
--        self.pool.close()
--        self.pool.join()
-+# NOTE(smbaker): disabled threadpool
-+#    def __del__(self):
-+#        self.pool.close()
-+#        self.pool.join()
- 
-     @property
-     def user_agent(self):
-@@ -320,6 +321,7 @@
-                                    response_type, auth_settings,
-                                    _return_http_data_only, collection_formats, _preload_content, _request_timeout)
-         else:
-+            raise Exception("Async is not supported")  # NOTE(smbaker): disabled thread pool
-             thread = self.pool.apply_async(self.__call_api, (resource_path, method,
-                                            path_params, query_params,
-                                            header_params, body,
diff --git a/pip_requested.txt b/pip_requested.txt
deleted file mode 100644
index 7a51a8d..0000000
--- a/pip_requested.txt
+++ /dev/null
@@ -1 +0,0 @@
-kubernetes==8.0.1
diff --git a/pip_requirements.txt b/pip_requirements.txt
deleted file mode 100644
index c71d8b4..0000000
--- a/pip_requirements.txt
+++ /dev/null
@@ -1,20 +0,0 @@
-kubernetes==8.0.1
-PyJWT==1.7.1
-adal==1.2.1
-cachetools==3.1.0
-certifi==2018.11.29
-cffi==1.12.2
-cryptography==2.6
-google-auth==1.6.3
-idna==2.8
-oauthlib==3.0.1
-pyasn1==0.4.5
-pyasn1-modules==0.2.4
-python-dateutil==2.8.0
-pyyaml==3.13
-requests==2.21.0
-requests-oauthlib==1.2.0
-setuptools==40.8.0
-six==1.12.0
-urllib3==1.24.1
-websocket-client==0.55.0
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..bcc4ae3
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,5 @@
+kubernetes~=8.0.1
+xossynchronizer~=3.0.1
+xosapi~=3.0.1
+xoskafka~=3.0.1
+kafkaloghandler~=0.9.0
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000..d5e43ef
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,45 @@
+; 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.
+
+[tox]
+envlist = py27
+; future; envlist = py27,py35,py36,py37
+skip_missing_interpreters = true
+skipsdist = True
+
+[testenv]
+deps =
+  -r requirements.txt
+  nose2
+; flake8
+
+changedir = xos
+commands =
+  nose2 -c ../tox.ini --verbose --junit-xml
+; future: flake8
+
+[flake8]
+max-line-length = 119
+
+[unittest]
+plugins = nose2.plugins.junitxml
+
+[junit-xml]
+path = nose2-results.xml
+
+[coverage]
+always-on = True
+coverage-report =
+  term
+  xml
diff --git a/xos/synchronizer/__init__.py b/xos/synchronizer/__init__.py
new file mode 100644
index 0000000..19d1424
--- /dev/null
+++ b/xos/synchronizer/__init__.py
@@ -0,0 +1,13 @@
+# 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.