Exemplo n.º 1
0
def load_cfg(cfg):
    global cfgpath
    if os.path.exists(cfg):
        pass
    elif os.path.exists(os.path.join("tests", cfg)):
        cfg = os.path.join("tests", cfg)
    elif os.path.exists(os.path.join(os.pardir, cfg)):
        cfg = os.path.join(os.pardir, cfg)
    else:
        raise RuntimeError("Config file '%s' not found" % (cfg,))

    cfgpath = cfg
    with open(cfg) as f:
        cfg = safe_load(f)

    kw = cfg["config"]["etcd"].copy()
    r = kw.pop("root")

    from etcd.client import Client

    c = Client(**kw)
    try:
        c.delete(r, recursive=True)
    except etcd.EtcdKeyNotFound:
        pass
    return cfg
Exemplo n.º 2
0
def load_cfg(cfg, do_client=True):
    global cfgpath
    if os.path.exists(cfg):
        pass
    elif os.path.exists(os.path.join("tests", cfg)):
        cfg = os.path.join("tests", cfg)
    elif os.path.exists(os.path.join(os.pardir, cfg)):
        cfg = os.path.join(os.pardir, cfg)
    else:
        raise RuntimeError("Config file '%s' not found" % (cfg, ))

    cfgpath = cfg
    with open(cfg) as f:
        cfg = safe_load(f)

    kw = cfg['config']['etcd'].copy()
    r = kw.pop('root')

    if do_client:
        from etcd.client import Client
        c = Client(**kw)
        try:
            c.delete(r, recursive=True)
        except etcd.EtcdKeyNotFound:
            pass
    return cfg
Exemplo n.º 3
0
class EtcdStore(object):
    """Adapter class for etcd api

    We will store the services in folders at etcd separated by service_name
    version and locaion
    /services/{service_name}/{version}/{location} = {location}
    So that way we can list all different locations for the same service and
    utilize the etcd ttl to expire keys.

    Versions, if not provided, will be 1.0 by default when setting values
    """

    def __init__(self, host="127.0.0.1", base_path="/services",
                 **kwargs):
        self._connect(host, **kwargs)
        self.base_path = base_path

    def _connect(self, etcd_location, **kwargs):
        self.connection = Client(host=etcd_location, **kwargs)

    def get_key(self, service_name, version, location):
        key = self._build_path(service_name, version, location)
        items = self.connection.read(key, recursive=True)
        return [item.value for item in items.children]

    def set_key(self, service_name, version, location, ttl=None):
        assert location
        if not version:
            version = '1.0'
        key = self._build_path(service_name, version, location)
        return bool(self.connection.set(key, location, ttl=ttl))

    def delete_key(self, service_name, version, location):
        "version and location cannot be None"
        assert version
        assert location
        key = self._build_path(service_name, version, location)
        return bool(self.connection.delete(key))

    def _build_path(self, service_name, version, location):
        "Builds the query path for the value in the etcd"
        assert service_name  # service_name is the minimum requirement
        key = "{0}/{1}".format(self.base_path, service_name)
        if version:
            key = "{0}/{1}".format(key, version)
            if location:
                key = "{0}/{1}".format(key, location)

        return key

    def _extract_key(self, path):
        "Removes Base Path of the key"
        return path.split("{0}/".format(self.base_path)).pop()
Exemplo n.º 4
0
    def _conn_etcd(self):
        """ 连接服务器 """
        # 解析ip
        if ',' and ':' in self.host:
            host = []
            for k, v in (u.split(':')
                         for u in (i for i in self.host.split(','))):
                host.append((k, int(v)))
            host = tuple(host)
        elif ':' in self.host:
            host = tuple(self.host.split(':'))
        else:
            host = self.host

        client = Client(host=host, port=self.port, allow_reconnect=True)
        return client
Exemplo n.º 5
0
from sys import exit
from os.path import join

from etcd.client import Client
from etcd.exceptions import EtcdAlreadyExistsException

#           ssl_ca_bundle_filepath='/home/dustin/development/python/etcd/tests/ssl/rootCA.pem')

#c = Client()

ssl_root = '/home/dustin/development/python/etcd/tests/ssl'

c = Client(host='127.0.0.1',#etcd.local', 
#           is_ssl=True, 
#           ssl_ca_bundle_filepath=join(ssl_root, 'demoCA', 'cacert.pem'),
#           ssl_client_cert_filepath=join(ssl_root, 'client.crt'), 
#           ssl_client_key_filepath=join(ssl_root, 'client.key'),
#           ssl_client_cert_filepath='/home/dustin/development/python/etcd/tests/ssl/cert_2_newca/alien_client.crt', 
#           ssl_client_key_filepath='/home/dustin/development/python/etcd/tests/ssl/cert_2_newca/alien_client.key')
)

#machines = c.server.get_machines()
#for machine in machines:
#    print(machine)
#
#print
#
#exit(0)

print("LEADER")

s = c.stat.get_leader_stats()
Exemplo n.º 6
0
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'diff.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DB_CONFIG_DICT = {}
client = Client(host='10.51.58.229', port=2379, allow_redirect=False)
db_config = client.read('/db', recursive=True)
for c in db_config.children:
    DB_CONFIG_DICT[c.key.split('/')[2]] = eval(c.value)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DB_CONFIG_DICT['question_offline_db']['db'],
        'USER': DB_CONFIG_DICT['question_offline_db']['user'],
        'PASSWORD': DB_CONFIG_DICT['question_offline_db']['passwd'],
        'HOST': DB_CONFIG_DICT['question_offline_db']['host'],
        'PORT': DB_CONFIG_DICT['question_offline_db']['port'],
    },
    'html_db': {
        'ENGINE': 'django.db.backends.mysql',
Exemplo n.º 7
0
 def _connect(self, etcd_location, **kwargs):
     self.connection = Client(host=etcd_location, **kwargs)
Exemplo n.º 8
0
from etcd.client import Client
from etcd.exceptions import EtcdAlreadyExistsException

#os.environ['PEC_SCHEME'] = 'https'
#os.environ['PEC_HOSTNAME'] = 'server.etcd'
#os.environ['PEC_SSL_CA_BUNDLE_FILEPATH'] = 'ssl/ca.crt.pem'

#           ssl_ca_bundle_filepath='/home/dustin/development/python/etcd/tests/ssl/rootCA.pem')

#c = Client()

ssl_root = '/Users/dustin/development/python/etcd/dev/ssl'

c = Client(host='server.etcd',
           is_ssl=True,
           ssl_ca_bundle_filepath=join(ssl_root, 'ca.crt.pem'),
           ssl_client_cert_filepath=join(ssl_root, 'client.crt.pem'),
           ssl_client_key_filepath=join(ssl_root, 'client.key.pem'))

#machines = c.server.get_machines()
#for machine in machines:
#    print(machine)
#
#print
#
#exit(0)

#print("LEADER")

#s = c.stat.get_leader_stats()
#print(s)
Exemplo n.º 9
0
from sys import exit

from etcd.client import Client

#           ssl_ca_bundle_filepath='/home/dustin/development/python/etcd/tests/ssl/rootCA.pem')

c = Client()

#c = Client(host='etcd.local', 
#           is_ssl=True, 
#           ssl_client_cert_filepath='/home/dustin/development/python/etcd/tests/ssl/client.crt', 
#           ssl_client_key_filepath='/home/dustin/development/python/etcd/tests/ssl/client.key')
#           ssl_client_cert_filepath='/home/dustin/development/python/etcd/tests/ssl/cert_2_newca/alien_client.crt', 
#           ssl_client_key_filepath='/home/dustin/development/python/etcd/tests/ssl/cert_2_newca/alien_client.key')

#machines = c.server.get_machines()
#for machine in machines:
#    print(machine)
#
#print
#
#exit(0)

r = c.node.get('/node_test/subkey1')
print(r)

r = c.node.wait('/node_test/subkey1')
print(r)

print(r.node.value)
Exemplo n.º 10
0
"""

import os
from etcd.client import Client
import pymysql
pymysql.install_as_MySQLdb()


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')
ETCD_CLIENT = Client(
    host=ETCD_HOST,
    port=ETCD_PORT,
    username=ETCD_USERNAME,
    password=ETCD_PASSWORD,
    allow_redirect=False
)

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'd512q-h72nx@g33l24pekc+swopr%*xqo8w$vtvtv0&s0hat9s'

# SECURITY WARNING: don't run with debug turned on in production!
def main():
    """
    Main function that handles running the command.
    """

    parser = argparse.ArgumentParser()
    parser.add_argument('key', nargs=1, help='etcd key')
    parser.add_argument('template', nargs=1, help='template to render')
    parser.add_argument('destination', nargs=1, help='template testination')
    parser.add_argument('command',
                        nargs='*',
                        help='command to run after generating template')
    parser.add_argument('-e',
                        '--etcd',
                        action='store',
                        default=None,
                        help='etcd host to connect to')
    parser.add_argument('-r',
                        '--recursive',
                        action='store_true',
                        help='prefix when saving to etcd')
    parser.add_argument('-q',
                        '--quiet',
                        action='store_true',
                        help='suppress output')
    args = parser.parse_args()

    if not args.quiet:
        logging.getLogger().setLevel(logging.INFO)

    if args.etcd:
        host = gethostbyname(args.etcd)
    else:
        host = requests.get(
            "http://169.254.169.254/latest/meta-data/local-ipv4").content

    if not os.path.isfile(args.template[0]):
        raise Exception('Template does not exist: {0}'.format(
            args.template[0]))

    client = Client(host=host, port=4001)
    key = '/tasks/{0}'.format(args.key[0])

    logging.info('Checking key: {0}'.format(key))
    if args.recursive:
        response = client.directory.list(key)
        services = [{
            'service': n.key.split('/')[-1],
            'tasks': json.loads(n.value)
        } for n in response.node.children]
        generate_template(args.template[0],
                          args.destination[0],
                          args.command,
                          services=services)
    else:
        response = client.node.get(key)
        tasks = json.loads(response.node.value)
        generate_template(args.template[0],
                          args.destination[0],
                          args.command,
                          tasks=tasks)

    while True:
        logging.info('Waiting for key change: {0}'.format(key))

        if args.recursive:
            try:
                client.directory.wait(key, recursive=True)
                response = client.directory.list(key)
            except (EtcdWaitFaultException, IncompleteRead):
                pass
            else:
                services = [{
                    'service': n.key.split('/')[-1],
                    'tasks': json.loads(n.value)
                } for n in response.node.children]
                generate_template(args.template[0],
                                  args.destination[0],
                                  args.command,
                                  services=services)
        else:
            try:
                response = client.node.wait(key)
            except (EtcdWaitFaultException, IncompleteRead):
                pass
            else:
                tasks = json.loads(response.node.value)
                generate_template(args.template[0],
                                  args.destination[0],
                                  args.command,
                                  tasks=tasks)