예제 #1
0
def set_tcp_keepalive(sock, opts=None):
    # Warn if ZMQ < 3.2
    try:
        zmq_version_info = zmq.zmq_version_info()
    except AttributeError:
        # PyZMQ <= 2.1.9 does not have zmq_version_info, fall back to
        # using zmq.zmq_version() and build a version info tuple.
        zmq_version_info = tuple(
            [int(x) for x in zmq.zmq_version().split('.')]
        )
    if zmq_version_info < (3, 2):
        log.warning(
            'You have a version of ZMQ less than ZMQ 3.2! There are '
            'known connection keep-alive issues with ZMQ < 3.2 which '
            'may result in loss of contact with minions. Please '
            'upgrade your ZMQ!'
        )
    ####
    # try:
    #     ip=socket.gethostbyname(socket.gethostname())
    #     if ip == "127.0.0.1":
    #         raise Exception("127.0.0.1")
    # except Exception,e:
    #     (status,ip) = commands.getstatusoutput("/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d 'addr:'")
    #     ip = ip
    if opts['ha'] == True:
        while True:
            time.sleep(5)
            try:
                sys.path.append("/usr/lib/python2.7/site-packages/salt")
                from zkclient import ZKClient, zookeeper, watchmethod
                zk = ZKClient('172.16.163.14:2181',1000)
                if zk.exists('/broker'):
                    value = zk.get('/broker')
                    logging.error(str(value[0]))
                    if value[0] == opts['id']:
                        break
                else:
                    zk.create('/broker')
                    zk.set('/broker',data='%s'%opts['id'])
                    logging.error("create the zk path is successful !")
                    continue
            except Exception,e:
                logging.error(str(e))
                continue
예제 #2
0
    rootpath = "/zk-smoketest"

    if zk.exists(rootpath):
        if not options.quiet:
            print("Node %s already exists, attempting reuse" % (rootpath))
    else:
        zk.create(
            rootpath, "smoketest root, delete after test done, created %s" %
            (datetime.datetime.now().ctime()))

    # make sure previous cleaned up properly
    children = zk.get_children(rootpath)
    if len(children) > 0:
        raise SmokeError("there should not be children in %s"
                         "root data is: %s, count is %d" %
                         (rootpath, zk.get(rootpath), len(children)))

    zk.close()

    def child_path(i):
        return "%s/session_%d" % (rootpath, i)

    # create znodes, one znode per session (client), one session per server
    for i, server in enumerate(servers):
        # ensure this server is up to date with leader
        sessions[i]. async ()

        ## create child znode
        sessions[i].create(child_path(i), "", zookeeper.EPHEMERAL)

    watchers = []
예제 #3
0
    rootpath = "/zk-smoketest"

    if zk.exists(rootpath):
        if not options.quiet:
            print("Node %s already exists, attempting reuse" % (rootpath))
    else:
        zk.create(rootpath,
                  "smoketest root, delete after test done, created %s" %
                  (datetime.datetime.now().ctime()))

    # make sure previous cleaned up properly
    children = zk.get_children(rootpath)
    if len(children) > 0:
        raise SmokeError("there should not be children in %s"
                         "root data is: %s, count is %d"
                         % (rootpath, zk.get(rootpath), len(children)))

    zk.close()

    def child_path(i):
        return "%s/session_%d" % (rootpath, i)

    # create znodes, one znode per session (client), one session per server
    for i, server in enumerate(servers):
        # ensure this server is up to date with leader
        sessions[i].async()

        ## create child znode
        sessions[i].create(child_path(i), "", zookeeper.EPHEMERAL)

    watchers = []
예제 #4
0
파일: zkstore.py 프로젝트: iYefeng/scrapyd
class ZookeeperStore(object):

    TIMEOUT = 1000

    def __init__(self,
                 verbose=True,
                 database='test',
                 table='targets',
                 zkhost='127.0.0.1:32181'):
        self.VERBOSE = verbose
        self.path = None
        self.ZK_HOST = zkhost
        self.database = "/" + database
        self.table = os.path.join(self.database, table)

        self.zk = ZKClient(self.ZK_HOST, timeout=self.TIMEOUT)
        self.say("login ok")
        self._init_zk()

    def __del__(self):
        self.zk.close()

    def _init_zk(self):
        nodes = (self.database, self.table)
        for node in nodes:
            if not self.zk.exists(node):
                try:
                    self.zk.create(node, "")
                except:
                    pass

    def create(self, path, msg, flags=0):
        msg = self.encode(msg)
        self.path = os.path.join(self.table, path)
        nodes = self.path.split("/")
        # FIXME
        for i in range(4, len(nodes)):
            node = "/".join(nodes[:i])
            if not self.zk.exists(node):
                try:
                    self.zk.create(node, "")
                except:
                    pass
        if not self.zk.exists(self.path):
            self.path = self.zk.create(self.path, msg, flags=flags)
        else:
            self.zk.set(self.path, msg)
        #self.path = os.path.basename(self.path)
        self.say("register ok! I'm %s" % self.path)

    def get_children(self, path=""):
        if not path:
            path = self.table
        children = self.zk.get_children(path)
        return children

    def get(self, node):
        msg = self.zk.get(node)
        return msg

    def decode(self, obj):
        return obj

    def encode(self, msg):
        return msg

    def say(self, msg):
        if self.VERBOSE:
            if self.path:
                log.info(msg)
            else:
                log.info(msg)
예제 #5
0
class ZookeeperStore(object):
  
  ZK_HOST = "127.0.0.1:32181"
  TIMEOUT = 1000

  def __init__(self, verbose = True, database='/test', table='targets'):
    self.VERBOSE = verbose
    self.path = None
    self.database = database
    self.table = os.path.join(self.database, table)

    self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT)
    self.say("login ok")
    self._init_zk()

  def __del__(self):
    self.zk.close()

  def _init_zk(self):
    nodes = (self.database, self.table)
    for node in nodes:
      if not self.zk.exists(node):
        try:
          self.zk.create(node, "")
        except:
          pass

  def create(self, path, msg, flags=0):
    msg = self.encode(msg)
    self.path = os.path.join(self.table, path)
    nodes = self.path.split("/")
    # FIXME
    for i in range(4,len(nodes)):
      node = "/".join(nodes[:i])
      if not self.zk.exists(node):
        try:
          self.zk.create(node, "")
        except:
          pass
    if not self.zk.exists(self.path):
      self.path = self.zk.create(self.path, msg, flags=flags)
    else:
      self.zk.set(self.path, msg)
    #self.path = os.path.basename(self.path)
    self.say("register ok! I'm %s" % self.path)
  
  def get_children(self, path=""):
    if not path:
      path = self.table
    children = self.zk.get_children(path)
    return children

  def get(self, node):
    msg = self.zk.get(node)
    return msg

  def decode(self, obj):
    return obj

  def encode(self, msg):
    return msg

  def say(self, msg):
    if self.VERBOSE:
      if self.path:
        log.info(msg)
      else:
        log.info(msg)
예제 #6
0
class GJZookeeper(object):

  ZK_HOST = "localhost:2181"
  ROOT = "/Roles"
  WORKERS_PATH = join(ROOT, "workers")
  TIMEOUT = 10000
  RULE_PATH = "/opt/interlive/nginx/conf/self_rules.xml"

  def __init__(self, verbose = True):
    self.VERBOSE = verbose
    self.path = None

    self.APP_ROOT = "/Applications"
    self.APP_CONF = join(self.APP_ROOT,"NginxConf")

    self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT)
    self.say("login zookeeper successfully!")
    # init
    self.create_roles_znode()
    # register
    self.register()
    self.watch_app_znode()

  def create_roles_znode(self):
    """
    create the zookeeper node if not exist
    |-Roles
       |-workers
    """
    nodes = (self.ROOT, self.WORKERS_PATH)
    for node in nodes: 
      if not self.zk.exists(node):
        try:
          self.zk.create(node, "")
        except:
          pass

  def register(self):
    """
    register a node for this worker,znode type : EPHEMERAL | SEQUENCE
    |-Roles
       |-workers
           |-worker000000000x      ==>>master
           |-worker000000000x+1    ==>>worker
           ....
    """
    self.path = self.zk.create(self.WORKERS_PATH + "/worker", "1", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
    self.path = basename(self.path)
    self.say("I'm %s" % self.path)

  def watch_app_znode(self):
    @watchmethod
    def watcher(event):
      self.say("first watcher")
      child_nodes = self.zk.get_children(self.APP_CONF)
      child_nodes.sort(reverse=True)
      data = self.zk.get(self.APP_CONF + "/" + child_nodes[0])
      self.say(data[0])
      file_object = open(self.RULE_PATH, 'w')
      try:
        file_object.write(data[0])
      finally:
        file_object.close()
      self.zk.get_children(self.APP_CONF, watcher)

    self.zk.get_children(self.APP_CONF, watcher)

  def say(self, msg):
    """
    print messages to screen
    """
    if self.VERBOSE:
      if self.path:
        log.info("[ %s ] %s" % (self.path, msg))
      else:
        log.info(msg)
예제 #7
0
if 0:
    mutex = threading.Lock()

    def test_fun(event):
        global mutex
        print "type_name:", event.type_name
        print "state_name:", event.state_name
        zk.exists("/xyz", watchmethod(test_fun))
        mutex.release()

    zk = ZKClient(config.server_list)

    flag = 1

    zk.get("/xyz", watchmethod(test_fun))

    mutex.acquire()
    
    mutex.acquire()

    print "done."

if 0:
    #threading lock test
    mutex = threading.Lock()
    
    mutex.acquire()
    
    print "acquire done. 1"