Import from GitHub
Change-Id: Iae8a9ecd755e62ec97c1a0a0da943fd90131f0af
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
new file mode 100644
index 0000000..e70aad6
--- /dev/null
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -0,0 +1,9 @@
+FROM xosproject/xos-synchronizer-base
+
+ADD . /opt/xos/synchronizers/exampleservice
+
+ENTRYPOINT []
+
+WORKDIR "/opt/xos/synchronizers/exampleservice"
+
+CMD bash -c "cd /opt/xos/synchronizers/exampleservice; ./run-from-api.sh"
diff --git a/xos/synchronizer/docker-compose.yml b/xos/synchronizer/docker-compose.yml
new file mode 100644
index 0000000..551094a
--- /dev/null
+++ b/xos/synchronizer/docker-compose.yml
@@ -0,0 +1,24 @@
+version: '2'
+
+networks:
+ xos:
+ external: true
+
+services:
+ synchronizer:
+ image: xosproject/exampleservice-synchronizer
+ networks:
+ - xos
+ #command: bash -c "cd /opt/xos/synchronizers/exampleservice; ./run-from-api.sh"
+ #command: bash -c "sleep 86400"
+ volumes:
+ - /opt/cord_profile/key_import/exampleservice_rsa:/opt/xos/services/exampleservice/keys/exampleservice_rsa:ro
+ - /opt/cord_profile/node_key:/opt/cord_profile/node_key:ro
+ - /opt/cord/build/platform-install/credentials/xosadmin@opencord.org:/opt/xos/services/exampleservice/credentials/xosadmin@opencord.org
+ external_links:
+ - rcordbs_xos_redis_1:redis
+ logging:
+ driver: "json-file"
+ options:
+ max-size: "1000k"
+ max-file: "5"
diff --git a/xos/synchronizer/exampleservice-synchronizer.py b/xos/synchronizer/exampleservice-synchronizer.py
new file mode 100644
index 0000000..aafdb44
--- /dev/null
+++ b/xos/synchronizer/exampleservice-synchronizer.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+# Runs the standard XOS synchronizer
+
+import importlib
+import os
+import sys
+
+synchronizer_path = os.path.join(os.path.dirname(
+ os.path.realpath(__file__)), "../../synchronizers/new_base")
+sys.path.append(synchronizer_path)
+mod = importlib.import_module("xos-synchronizer")
+mod.main()
+
diff --git a/xos/synchronizer/exampleservice_from_api_config b/xos/synchronizer/exampleservice_from_api_config
new file mode 100644
index 0000000..bb76435
--- /dev/null
+++ b/xos/synchronizer/exampleservice_from_api_config
@@ -0,0 +1,19 @@
+# Sets options for the synchronizer
+[observer]
+name=exampleservice
+dependency_graph=/opt/xos/synchronizers/exampleservice/model-deps
+steps_dir=/opt/xos/synchronizers/exampleservice/steps
+sys_dir=/opt/xos/synchronizers/exampleservice/sys
+#logfile=/var/log/xos_backend.log
+log_file=console
+log_level=debug
+pretend=False
+backoff_disabled=True
+save_ansible_output=True
+proxy_ssh=True
+proxy_ssh_key=/opt/cord_profile/node_key
+proxy_ssh_user=root
+enable_watchers=True
+accessor_kind=api
+accessor_password=@/opt/xos/services/exampleservice/credentials/xosadmin@opencord.org
+required_models=ExampleService, ExampleTenant, ServiceDependency, ServiceMonitoringAgentInfo
diff --git a/xos/synchronizer/model-deps b/xos/synchronizer/model-deps
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/xos/synchronizer/model-deps
@@ -0,0 +1 @@
+{}
diff --git a/xos/synchronizer/run-from-api.sh b/xos/synchronizer/run-from-api.sh
new file mode 100755
index 0000000..1245084
--- /dev/null
+++ b/xos/synchronizer/run-from-api.sh
@@ -0,0 +1,2 @@
+export XOS_DIR=/opt/xos
+python exampleservice-synchronizer.py -C $XOS_DIR/synchronizers/exampleservice/exampleservice_from_api_config
diff --git a/xos/synchronizer/steps/exampletenant_playbook.yaml b/xos/synchronizer/steps/exampletenant_playbook.yaml
new file mode 100644
index 0000000..89e4617
--- /dev/null
+++ b/xos/synchronizer/steps/exampletenant_playbook.yaml
@@ -0,0 +1,16 @@
+---
+# exampletenant_playbook
+
+- hosts: "{{ instance_name }}"
+ connection: ssh
+ user: ubuntu
+ sudo: yes
+ gather_facts: no
+ vars:
+ - tenant_message: "{{ tenant_message }}"
+ - service_message: "{{ service_message }}"
+
+ roles:
+ - install_apache
+ - create_index
+
diff --git a/xos/synchronizer/steps/monitoring_agent.yaml b/xos/synchronizer/steps/monitoring_agent.yaml
new file mode 100644
index 0000000..87cffde
--- /dev/null
+++ b/xos/synchronizer/steps/monitoring_agent.yaml
@@ -0,0 +1,61 @@
+---
+- hosts: {{ instance_name }}
+ gather_facts: False
+ connection: ssh
+ user: ubuntu
+ vars:
+ keystone_tenant_id: {{ keystone_tenant_id }}
+ keystone_user_id: {{ keystone_user_id }}
+ target_uri: {{ target_uri }}
+
+ tasks:
+ - name: Verify if monitoring_agent ([] is to avoid capturing the shell process) cron job is already running
+ shell: pgrep -f [m]onitoring_agent | wc -l
+ register: cron_job_pids_count
+
+ - name: DEBUG
+ debug: var=cron_job_pids_count.stdout
+
+ - name: make sure /usr/local/share/monitoring_agent exists
+ file: path=/usr/local/share/monitoring_agent state=directory owner=root group=root
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: Copy cron job to destination
+ copy: src=/opt/xos/synchronizers/exampleservice/monitoring_agent/
+ dest=/usr/local/share/monitoring_agent/
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: Installing python-pip
+ apt: name=python-pip state=present update_cache=yes
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: Installing Flask
+ pip: name=Flask
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: install python-kombu
+ apt: name=python-kombu state=present
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: Initiate monitoring agent cron job
+ command: python monitoring_agent.py &
+ async: 9999999999999999
+ poll: 0
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+ args:
+ chdir: /usr/local/share/monitoring_agent/
+
+ - name : starting monitoring agent
+ uri:
+ url: http://localhost:5004/monitoring/agent/exampleservice/start
+ method: POST
+ body: '{ "target":"{{target_uri}}", "keystone_user_id": "{{keystone_user_id}}", "keystone_tenant_id": "{{keystone_tenant_id}}" }'
+ force_basic_auth: yes
+ status_code: 200
+ body_format: json
diff --git a/xos/synchronizer/steps/roles/create_index/tasks/main.yml b/xos/synchronizer/steps/roles/create_index/tasks/main.yml
new file mode 100644
index 0000000..91c6029
--- /dev/null
+++ b/xos/synchronizer/steps/roles/create_index/tasks/main.yml
@@ -0,0 +1,7 @@
+---
+
+- name: Write index.html file to apache document root
+ template:
+ src=index.html.j2
+ dest=/var/www/html/index.html
+
diff --git a/xos/synchronizer/steps/roles/create_index/templates/index.html.j2 b/xos/synchronizer/steps/roles/create_index/templates/index.html.j2
new file mode 100644
index 0000000..9cec084
--- /dev/null
+++ b/xos/synchronizer/steps/roles/create_index/templates/index.html.j2
@@ -0,0 +1,4 @@
+ExampleService
+ Service Message: "{{ service_message }}"
+ Tenant Message: "{{ tenant_message }}"
+
diff --git a/xos/synchronizer/steps/roles/install_apache/tasks/main.yml b/xos/synchronizer/steps/roles/install_apache/tasks/main.yml
new file mode 100644
index 0000000..d9a155c
--- /dev/null
+++ b/xos/synchronizer/steps/roles/install_apache/tasks/main.yml
@@ -0,0 +1,7 @@
+---
+
+- name: Install apache using apt
+ apt:
+ name=apache2
+ update_cache=yes
+
diff --git a/xos/synchronizer/steps/sync_exampletenant.py b/xos/synchronizer/steps/sync_exampletenant.py
new file mode 100644
index 0000000..47c045f
--- /dev/null
+++ b/xos/synchronizer/steps/sync_exampletenant.py
@@ -0,0 +1,83 @@
+import os
+import sys
+from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+from synchronizers.new_base.modelaccessor import *
+from xos.logger import Logger, logging
+
+parentdir = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, parentdir)
+
+logger = Logger(level=logging.INFO)
+
+class SyncExampleTenant(SyncInstanceUsingAnsible):
+
+ provides = [ExampleTenant]
+
+ observes = ExampleTenant
+
+ requested_interval = 0
+
+ template_name = "exampletenant_playbook.yaml"
+
+ service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key"
+
+ watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
+
+ def __init__(self, *args, **kwargs):
+ super(SyncExampleTenant, self).__init__(*args, **kwargs)
+
+ def get_exampleservice(self, o):
+ if not o.provider_service:
+ return None
+
+ exampleservice = ExampleService.objects.filter(id=o.provider_service.id)
+
+ if not exampleservice:
+ return None
+
+ return exampleservice[0]
+
+ # Gets the attributes that are used by the Ansible template but are not
+ # part of the set of default attributes.
+ def get_extra_attributes(self, o):
+ fields = {}
+ fields['tenant_message'] = o.tenant_message
+ exampleservice = self.get_exampleservice(o)
+ fields['service_message'] = exampleservice.service_message
+ return fields
+
+ def delete_record(self, port):
+ # Nothing needs to be done to delete an exampleservice; it goes away
+ # when the instance holding the exampleservice is deleted.
+ pass
+
+ def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info):
+ if not monitoring_agent_info.service:
+ logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
+ return
+
+ if not monitoring_agent_info.target_uri:
+ logger.info("handle watch notifications for service monitoring agent info...ignoring because target_uri attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
+ return
+
+ objs = ExampleTenant.objects.all()
+ for obj in objs:
+ if obj.provider_service.id != monitoring_agent_info.service.id:
+ logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is not matching" % (monitoring_agent_info))
+ return
+
+ instance = self.get_instance(obj)
+ if not instance:
+ logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj)))
+ return
+
+ logger.info("handling watch notification for monitoring agent info:%s for ExampleTenant object:%s" % (monitoring_agent_info, obj))
+
+ #Run ansible playbook to update the routing table entries in the instance
+ fields = self.get_ansible_fields(instance)
+ fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_monitoring"
+ fields["target_uri"] = monitoring_agent_info.target_uri
+
+ template_name = "monitoring_agent.yaml"
+ super(SyncExampleTenant, self).run_playbook(obj, fields, template_name)
+ pass