Import from GitHub
Change-Id: Iae8a9ecd755e62ec97c1a0a0da943fd90131f0af
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