예제 #1
0
def main(etcdctl_peers, host, service_name, backend):
    etcd_url = urlparse(etcdctl_peers)
    etcd = Client(host=etcd_url.hostname, port=etcd_url.port)
    key = "/vulcand/frontends/%s/frontend" % service_name

    click.secho("Connecting to ECTD: %s" % etcdctl_peers, fg='green')
    try:
        etcd.read(key='/')
    except EtcdConnectionFailed:
        click.secho('ETCD Connection Failed', fg='red')
        sys.exit(99)

    click.secho("Writing key %s" % key, fg='green')
    try:
        value = etcd.read(key=key).value
    except EtcdKeyNotFound:
        value = '{}'

    j = json.loads(value)
    j['Route'] = "Host(`%s`) && PathRegexp(`/.*`)" % host
    j['Type'] = 'http'

    if backend:
        j['BackendId'] = backend

    # Write / Update Key
    try:
        etcd.write(key=key, value=json.dumps(j))
    except Exception as e:
        raise e  # TODO: Handle specific exceptions

    click.secho("All done.", fg='green')
예제 #2
0
 def __init__(self,
              dev_params=None,
              prefix='config',
              protocol='http',
              host='localhost',
              port=2379,
              username=None,
              password=None,
              long_polling_timeout=50,
              long_polling_safety_delay=5):
     self._client = Client(host=host,
                           port=port,
                           protocol=protocol,
                           allow_redirect=True,
                           username=username,
                           password=password)
     # Overriding retries for urllib3.PoolManager.connection_pool_kw
     self._client.http.connection_pool_kw['retries'] = 0
     self._base_config_path = prefix
     self._dev_params = dev_params
     self._base_config_set_path = "{}/extensions" \
         .format(self._base_config_path)
     r = ('^(?P<path>{}/(?:extensions/)?'
          '(?P<envorset>[\w\-\.]+))/(?P<key>.+)$')
     self._key_regex = re.compile(r.format(self._base_config_path))
     self.long_polling_timeout = long_polling_timeout
     self.long_polling_safety_delay = long_polling_safety_delay
     self._init_logger()
예제 #3
0
def EtcdFactory(*args, **kwargs):
    """Factory method, returning a connection to a real etcd if we need one for
    FV, or to an in-memory implementation for UT."""
    if os.environ.get('ETCD_IP'):
        return Client(os.environ.get('ETCD_IP'),
                      int(os.environ.get('ETCD_PORT', 4001)))
    else:
        return MockEtcdClient(None, None)
예제 #4
0
def main(etcdctl_peers, services_base, name, output, maps, links):
    click.secho("Connecting to ECTD: %s" % etcdctl_peers, fg='green')
    click.secho("Links: %s" % " ".join(str(i) for i in links), fg='green')

    if len(links) == 0:
        click.echo('Nothing to do')
        sys.exit()

    etcd_url = urlparse(etcdctl_peers)
    etcd = Client(host=etcd_url.hostname, port=etcd_url.port)

    environment = {}

    # Get values from etcd for the services specified
    # --link <name or id>:alias
    for link in links:
        lname, _, alias = link.partition(":")
        if not alias:
            alias = lname.replace('-', '_')
        try:
            # Try to get the service
            service = etcd.read(os.path.join(services_base, lname))
        except EtcdKeyNotFound:
            click.secho("Service: %s not found" % lname, fg='red')
        else:
            # Expects the service to have some children
            for child in service._children:
                value = child['value']
                ip, _, port = value.partition(":")
                click.secho("Key: %s found" % lname, fg='green')
                environment["%s_NAME" %
                            alias.upper()] = "/%s/%s" % (name, lname)
                environment["%s_PORT" % alias.upper()] = "tcp://%s" % value
                environment["%s_PORT_%s_TCP" %
                            (alias.upper(), port)] = "tcp://%s" % value
                environment["%s_PORT_%s_TCP_PROTO" %
                            (alias.upper(), port)] = "tcp"
                environment["%s_PORT_%s_TCP_PORT" %
                            (alias.upper(), port)] = "%s" % port
                environment["%s_PORT_%s_TCP_ADDR" %
                            (alias.upper(), port)] = "%s" % ip

    # Do mapping
    for item in maps:
        mname, _, alias = item.partition(":")
        if mname and alias:
            try:
                environment[alias] = environment[mname]
            except KeyError:
                click.secho("Missing Key: %s" % mname, fg='red')

    with open(output, 'w') as f:
        for key, value in environment.iteritems():
            f.write("%s=%s\n" % (key, value))
            click.secho("%s=%s" % (key, value), fg='yellow')

    click.secho("All done.", fg='green')
 def __create_client(cls, etcd_host, etcd_port) -> Client:
     """ Create etcd client and test connection. """
     Logger(cls.__name__).info('Connecting to etcd server...')
     client = Client(host=etcd_host, port=etcd_port)
     # Test connection by trying to read a random value
     try:
         client.read('nodes')
     except EtcdConnectionFailed:
         raise EtcdConnectionError(port=etcd_port)
     except EtcdKeyNotFound:
         # This is to handle the case where etcd did not have the key (we don't care) but it is running
         pass
     return client
예제 #6
0
 def __init__(self, config, section):
     super(EtcdStore, self).__init__(config, section)
     # Initialize the DB by trying to create the default table
     try:
         self.etcd = Client(self.etcd_server, self.etcd_port)
         self.etcd.write(self.namespace, None, dir=True)
     except EtcdNotFile:
         # Already exists
         pass
     except EtcdException:
         self.logger.exception("Error creating namespace %s",
                               self.namespace)
         raise CSStoreError('Error occurred while trying to init db')
예제 #7
0
 def __init__(self, keyspace=None, **kwargs):
     """
         Initialize the handler data store.
         :param keyspace: etcd keyspace for configuration map starting with /
         :type key: string
         :param kwargs: generic params forwarded from the Configmanager
         :type key: dict
     """
     super().__init__()
     self.client = Client(**kwargs)
     self.keyspace = keyspace if keyspace else '/config'
     if not self.keyspace.startswith('/'):
         self.keyspace = '/' + self.keyspace
     self.config = self.load()
예제 #8
0
 def _connect(self):
     return Client(host=self.host,
                   port=self.port,
                   srv_domain=self.srv_domain,
                   version_prefix=self.version_prefix,
                   read_timeout=self.read_timeout,
                   allow_redirect=self.allow_redirect,
                   protocol=self.protocol,
                   cert=self.cert,
                   ca_cert=self.ca_cert,
                   username=self.username,
                   password=self.password,
                   allow_reconnect=self.allow_reconnect,
                   use_proxies=self.use_proxies,
                   expected_cluster_id=self.expected_cluster_id,
                   per_host_pool_size=self.per_host_pool_size)
예제 #9
0
    def __init__(self, config):
        super(EtcdStore, self).__init__(config)
        self.server = config.get('etcd_server', '127.0.0.1')
        self.port = int(config.get('etcd_port', 4001))
        self.namespace = config.get('namespace', "/custodia")

        # Initialize the DB by trying to create the default table
        try:
            self.etcd = Client(self.server, self.port)
            self.etcd.write(self.namespace, None, dir=True)
        except EtcdNotFile:
            # Already exists
            pass
        except EtcdException:
            self.logger.exception("Error creating namespace %s",
                                  self.namespace)
            raise CSStoreError('Error occurred while trying to init db')
예제 #10
0
 def __init__(self, port):
     asyncore.dispatcher.__init__(self)
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
     self.set_reuse_addr()
     self.bind(("localhost", port))
     self.listen(1)
     self.child_pids = None
     self.zk = KazooClient()
     self.etcd = Client(port=2379)
     self.port = port
     if self.prefork(10):
         # self.register_zk(port)
         self.register_etcd()
         self.register_parent_signal()
     else:
         # in child process, pid = 0
         self.register_child_signal()
예제 #11
0
    def test_order_of_entries_on_dump(self):
        backup = tempfile()

        with EtcdWrap() as etcd:
            try:
                client = Client(etcd.host, etcd.port)
                for i in range(10):
                    client.write("/{}".format(i), i**i)
                Dumper(etcd.client_urls[0]).dump(filename=backup)
            finally:
                etcd.terminate()

        self.assertGreater(stat(backup).st_size, 0)
        with open(backup, encoding="utf-8") as fp:
            lastindex = 0
            for i, entry in enumerate(load(fp)):
                self.assertEqual(entry["key"], "/{}".format(i))
                self.assertEqual(entry["value"], str(i**i))
                self.assertTrue(i <= lastindex < entry["index"])
                lastindex = entry["index"]
예제 #12
0
def get_servers_with_etcd():
    client = Client(port=2379)

    # 获取新的服务地址,并监听服务变动

    def get_all_addr():
        new_addrs = set()
        rpc_dir = client.get(zk_rpc)
        for child in rpc_dir.leaves:
            addr = json.loads(child.value)
            new_addrs.add("%s:%d" % (addr["host"], addr["port"]))
        return new_addrs

    # 当前活跃地址
    current_addrs = get_all_addr()
    G["servers"] = [RemoteServer(s) for s in current_addrs]
    for _ in client.eternal_watch(zk_rpc, recursive=True):
        print("listening etcd")
        new_addrs = get_all_addr()
        # 新增
        add_addrs = new_addrs - current_addrs
        # 需要删除
        del_addrs = current_addrs - new_addrs
        del_servers = []
        for addr in del_addrs:
            for s in G["servers"]:
                if s.addr == addr:
                    del_servers.append(s)
                    break
        for server in del_servers:
            G["servers"].remove(server)
            current_addrs.remove(server.addr)
        # 新增
        for addr in add_addrs:
            G["servers"].append(RemoteServer(addr))
            current_addrs.add(addr)
        print("last g[servers]", G["servers"])
    return G["servers"]
예제 #13
0
 def __init__(self, conf):
     self.conf = conf
     self.host, self.port = self.conf['host'], self.conf['port']
     self.client = Client(host=self.host, port=self.port)
     self.test_connect()
예제 #14
0
파일: app.py 프로젝트: taoprogramer/etcd-ui
import traceback
from flask import Flask, jsonify, request, render_template
from etcd import Client, EtcdKeyNotFound

app = Flask(__name__)

cli = Client(host='localhost', port=4001)

to_json = lambda node: dict([(attr, getattr(node, attr))
                             for attr in node._node_props])


@app.route('/')
def base():
    return render_template('base.html')


@app.route('/keys', methods=['GET', 'PUT', 'DELETE'])
def proxy():
    if request.method == 'GET':
        key = request.args.get('key', '')
        try:
            context = {}
            node = cli.read(key)
            context.update(to_json(node))
            context.update(
                children=[to_json(child) for child in node.children])
            return jsonify(**context)
        except EtcdKeyNotFound:
            return jsonify({})
예제 #15
0
    vs.delete_service(service_name)
def list_services(params):
    global vs
    services = vs.get_services()
    print services

def list_tasks(params):
    global vs
    service_name=params[0]
    tasks = vs.get_tasks(service_name)
    print tasks

def help(params):
    global command_router
    print command_router.keys()

command_router = {
    "list-services":list_services
    ,"list-tasks":list_tasks
    ,"delete-service":delete_service
    ,"service-info":service_info
    ,"get-file":get_file
    ,"help":help
}
client = Client(ip,port)
vs = HorizonStatus(client)
action = command_router.get(cmd)
action(params)


예제 #16
0
from flask import Flask
from redis import Redis
from etcd import Client
import socket
import json
import os

app = Flask(__name__)
redis = Redis(host="redis", port=6379)
client = Client(host="5cq_etcd_1",port=2379)
//***************************
@app.route("/")
def clicknums():
    count = redis.incr("hits")
    return "Requested {} times.\n".format(count)

@app.route("/clicks")
def click():
    return redis.get("hits")

@app.route("/click",methods=['POST'])
def storeclick():
    count = redis.incr("hits")
    client.write("/clicks/nums", count )
    return str( count )
//***************************

if __name__ == "__main__":
    client.write("/services/clicks",json.dumps({"host":socket.gethostbyname(socket.gethostname()),"port":"5000","pid":os.getpid()}))
    app.run(host="0.0.0.0", debug=True)
    
예제 #17
0
파일: ext.py 프로젝트: CMGS/citadel
def get_etcd(zone):
    cluster = ZONE_CONFIG[zone]['ETCD_CLUSTER']
    return Client(cluster, allow_reconnect=True)
예제 #18
0
 def __init__(self, config):
     self.etcd = Client(host=config.etcd_hosts, allow_reconnect=True)
     self.stream_changes = ReplaySubject()
     self.initialize_store()
예제 #19
0
#!/usr/bin/env python
# coding=utf-8

import os
import ldap
from etcd import Client
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType

ETCD_HOST = os.environ['EV_ETCD_HOST']
ETCD_PORT = int(os.environ['EV_ETCD_PORT'])
ETCD_USERNAME = os.environ.get('EV_ETCD_USERNAME')
ETCD_PASSWORD = os.environ.get('EV_ETCD_PASSWORD')
client = Client(host=ETCD_HOST,
                port=ETCD_PORT,
                username=ETCD_USERNAME,
                password=ETCD_PASSWORD,
                allow_redirect=False)
LDAP_CONFIG_DICT = eval(client.read('/common/ldap').value)
client.__del__()

__all__ = [
    'AUTH_LDAP_SERVER_URI', 'AUTH_LDAP_CONNECTION_OPTIONS',
    'AUTH_LDAP_BIND_DN', 'AUTH_LDAP_BIND_PASSWORD', 'AUTH_LDAP_USER_SEARCH',
    'AUTH_LDAP_USER_ATTR_MAP', 'AUTH_LDAP_ALWAYS_UPDATE_USER',
    'AUTHENTICATION_BACKENDS'
]

AUTH_LDAP_SERVER_URI = LDAP_CONFIG_DICT['host']
AUTH_LDAP_CONNECTION_OPTIONS = {ldap.OPT_REFERRALS: 0}
AUTH_LDAP_BIND_DN = LDAP_CONFIG_DICT['user']
AUTH_LDAP_BIND_PASSWORD = LDAP_CONFIG_DICT['passwd']
예제 #20
0
 def _get_etcd_client(self):
     return Client(host=cfg.etcd_host, port=cfg.etcd_port)
예제 #21
0
 def __init__(self, ip, port):
     self._client = Client(ip, port)
예제 #22
0
 def connect(self):
     self.__client = Client(**self.__default_args)
     self.__connected = True
예제 #23
0
from etcd import Client
import os

etcd_host = os.environ.get("ETCD_HOST", os.getenv("ETCD_HOST"))
etcd_port = os.environ.get("ETCD_PORT", os.getenv("ETCD_PORT"))
client = Client(host=etcd_host, port=int(etcd_port))

# Execute in app start
# client.write('/user', None, dir=True)
# client.write('/ttl', None, dir=True)
# client.write('/type', None, dir=True)
# client.write('/zone', None, dir=True)
# client.write('/record', None, dir=True)
# client.write('/serial', None, dir=True)
# client.write('/conten', None, dir=True)

# client.write("/user/1", {"key": "1","email": "*****@*****.**", "project_id": "001","state": "inserted", "created_at":"2019-07-20 23:04:22.420505"})
client.write("/ttl/1", {"key": "1", "value": "300"})
client.write("/ttl/2", {"key": "2", "value": "900"})
client.write("/ttl/3", {"key": "3", "value": "1800"})
client.write("/ttl/4", {"key": "4", "value": "3600"})
client.write("/ttl/5", {"key": "5", "value": "7200"})
client.write("/ttl/6", {"key": "6", "value": "14400"})
client.write("/ttl/7", {"key": "7", "value": "28800"})
client.write("/ttl/8", {"key": "8", "value": "43200"})
client.write("/ttl/9", {"key": "9", "value": "86400"})
예제 #24
0
# check if ETCD_PROTO is https but there is no ETCD_CERT and ETCD_CA_CERT.
if ETCD_PROTO == 'https':
    for f in ETCD_CERT:
        if not os.path.isfile(f):
            raise Exception('Fail to set ETCD: File not found {}'.format(f))
    if not os.path.isfile(ETCD_CA_CERT):
        raise Exception('Fail to set ETCD: File not found {}'.format(f))

# Read ETCD_CA_CERT if it exists.
if ETCD_PROTO == 'https':
    s_cacert = ''
    with open(ETCD_CA_CERT, 'r') as f:
        s_cacert = f.read()
    etcdc = Client(host=ETCD_HOST,
                   port=ETCD_PORT,
                   allow_reconnect=True,
                   protocol=ETCD_PROTO,
                   cert=ETCD_CERT,
                   ca_cert=ETCD_CA_CERT)
else:
    etcdc = Client(host=ETCD_HOST,
                   port=ETCD_PORT,
                   allow_reconnect=True,
                   protocol=ETCD_PROTO)

etcdc.prefix = ETCD_PREFIX

# check if ETCD_PREFIX exists on etcd server. If not, create it.
try:
    r = etcdc.read(etcdc.prefix)
except etcd.EtcdKeyNotFound as e:
    etcdc.write(etcdc.prefix, None, dir=True)