import etcd
import time

from tendrl.commons.objects.job import Job
from tendrl.commons import sds_sync
from tendrl.commons.utils import log_utils as logger

import uuid


class GlusterIntegrtaionsSyncThread(sds_sync.StateSyncThread):
    def run(self):
        logger.log(
            "debug",
            NS.get("publisher_id", None),
            {"message": "%s running" % self.__class__.__name__}
        )
        _sleep = 0
        while not self._complete.is_set():
            if _sleep > 5:
                _sleep = int(NS.config.data.get("sync_interval", 10))
            else:
                _sleep += 1
            try:
                nodes = NS._int.client.read("/nodes")
            except etcd.EtcdKeyNotFound:
                time.sleep(_sleep)
                continue

            for node in nodes.leaves:
                node_id = node.key.split('/')[-1]
                try:
                    node_context = NS.tendrl.objects.NodeContext(
                        node_id=node_id
                    ).load()
                    tendrl_context = NS.tendrl.objects.TendrlContext(
                        node_id=node_id
                    ).load()

                    if node_context.status != "DOWN" or\
                       tendrl_context.sds_name != "gluster":
                        continue

                    # check if the node belongs to a cluster that is managed

                    cluster = NS.tendrl.objects.Cluster(
                        integration_id=tendrl_context.integration_id
                    ).load()
                    if cluster.is_managed != "yes":
                        continue

                    # check if the bricks of this node are already
                    # marked as down

                    bricks = NS._int.client.read(
                        "clusters/{0}/Bricks/all/{1}".format(
                            tendrl_context.integration_id,
                            node_context.fqdn
                        )
                    )

                    bricks_marked_already = True
                    for brick in bricks.leaves:
                        brick_status = NS._int.client.read(
                            "{0}/status".format(brick.key)
                        ).value
                        if brick_status != "Stopped":
                            bricks_marked_already = False
                            break

                    if bricks_marked_already:
                        continue

                    self.update_brick_status(
                        node_context.fqdn,
                        tendrl_context.integration_id,
                        "Stopped"
                    )
                except etcd.EtcdKeyNotFound:
                    pass
                
            time.sleep(_sleep)

    def update_brick_status(self, fqdn, integration_id, status):
        _job_id = str(uuid.uuid4())
        _params = {
            "TendrlContext.integration_id": integration_id,
            "Node.fqdn": fqdn,
            "Brick.status": status
        }
        _job_payload = {
            "tags": [
                "tendrl/integration/{0}".format(
                    integration_id
                )
            ],
            "run": "gluster.flows.UpdateBrickStatus",
            "status": "new",
            "parameters": _params,
            "type": "sds"
        }
        Job(
            job_id=_job_id,
            status="new",
            payload=_job_payload
        ).save()

@@ -2133,43 +2133,12 @@
     
-brick_status = NS._int.client.read(
+try:
 %0A   
@@ -2162,16 +2162,38 @@
         
+NS._int.wclient.write(
 %22%7B0%7D/sta
@@ -2214,16 +2214,17 @@
 ick.key)
+,
 %0A       
@@ -2244,16 +2244,8 @@
     
-).value%0A
     
@@ -2268,26 +2268,9 @@
     
-if brick_status !=
+ 
  %22St
@@ -2275,17 +2275,17 @@
 Stopped%22
-:
+,
 %0A       
@@ -2309,1160 +2309,273 @@
     
-bricks_marked_already = False%0A                            break%0A%0A                    if bricks_marked_already:%0A                        continue%0A%0A                    self.update_brick_status(%0A                        node_context.fqdn,%0A                        tendrl_context.integration_id,%0A                        %22Stopped%22%0A                    )%0A                except etcd.EtcdKeyNotFound:%0A                    pass%0A                %0A            time.sleep(_sleep)%0A%0A    def update_brick_status(self, fqdn, integration_id, status):%0A        _job_id = str(uuid.uuid4())%0A        _params = %7B%0A            %22TendrlContext.integration_id%22: integration_id,%0A            %22Node.fqdn%22: fqdn,%0A            %22Brick.status%22: status%0A        %7D%0A        _job_payload = %7B%0A            %22tags%22: %5B%0A                %22tendrl/integration/%7B0%7D%22.format(%0A                    integration_id%0A                )%0A            %5D,%0A            %22run%22: %22gluster.flows.UpdateBrickStatus%22,%0A            %22status%22: %22new%22,%0A            %22parameters%22: _params,%0A            %22type%22: %22sds%22%0A        %7D%0A        Job(%0A            job_id=_job_id,%0A            status=%22new%22,%0A            payload=_job_payload%0A        ).save(
+                      prevExist=False)%0A                        except etcd.EtcdAlreadyExist:%0A                            pass%0A                        %0A                except etcd.EtcdKeyNotFound:%0A                    pass%0A                %0A            time.sleep(_sleep
 )%0A

Update __init__.py