Beispiel #1
0
def main(args=None):
    parser = argparse.ArgumentParser(description='KVDN PYCLIENT')
    parser.add_argument('--debug', action='store_true', help='debug')
    parser.add_argument('--set', action='store_true',  help='set a value', )
    parser.add_argument('--submit', action='store_true',  help='submit a value', )
    parser.add_argument('--delete', action='store_true',  help='delete a value', )
    parser.add_argument('straddr', type=str, help='the kvdn map address to work with')
    parser.add_argument('--key', type=str, default='', help='the kvdn key to work with')
    s=''
    args = parser.parse_args()
    if(args.set or args.submit):
        s = sys.stdin.read()
    _baseurl=''
    _token=''
    try:
        _baseurl=os.environ["KVDN_BASE_URL"]
    except KeyError:
        _baseurl='http://localhost:6500'
        sys.stderr.write( "using default url : " + _baseurl + '\n')

    try:
        _token=os.environ["JWT_TOKEN"]
        k = kvdn_client(baseurl=_baseurl, token=_token)
    except KeyError:
        sys.stderr.write( 'JWT_TOKEN not set \n')
        k = kvdn_client(baseurl=_baseurl)



    if(args.debug):
      sys.stderr.write("KVDN VERSION: " + k.version() + "\n")


    if(args.set):
      print k.set(args.straddr,args.key,s)
    elif(args.submit):
      print k.submit_cas(args.straddr,s)
    else:
      if(args.delete):
          print k.delete(args.straddr, args.key)
      elif(args.key):
          print k.get(args.straddr, args.key)
      else:
          print k.getKeys(args.straddr)
Beispiel #2
0
def ext_pillar(minion_id, pillar, *args, **kwargs):
    """ Main handler. Compile pillar data for the specified minion ID
    """
    kvdn_pillar = {}

    # Load configuration values
    for key in CONF:
        if kwargs.get(key, None):
            CONF[key] = kwargs.get(key)

    # Resolve salt:// fileserver path, if necessary
    if CONF["config"].startswith("salt://"):
        local_opts = __opts__.copy()
        local_opts["file_client"] = "local"
        minion = salt.minion.MasterMinion(local_opts)
        CONF["config"] = minion.functions["cp.cache_file"](CONF["config"])

    # Read the kvdn_value map
    renderers = salt.loader.render(__opts__, __salt__)
    raw_yml = salt.template.compile_template(CONF["config"], renderers, 'jinja')
    if raw_yml:
        config_map = yaml.safe_load(raw_yml.getvalue()) or {}
    else:
        LOG.error("Unable to read configuration file '%s'", CONF["config"])
        return kvdn_pillar

    if not CONF["url"]:
        LOG.error("'url' must be specified for KVDN configuration");
        return kvdn_pillar

    #  KVDN
    kvdn = kvdn_client.kvdn_client()
    
    _authenticate(kvdn)
    
    # Apply the compound filters to determine which mappings to expose for this minion
    ckminions = salt.utils.minions.CkMinions(__opts__)

    for filter, mappings in config_map.items():
        if minion_id in ckminions.check_minions(filter, "compound"):
            for variable, location in mappings.items():

                # Determine if a specific key was requested
                try:
                    (path, key) = location.split('?' , 1)
                except ValueError:
                    (path, key) = (location, None)

                # Return only the key value, if requested, otherwise return
                # the entire kvdn_value json structure
                kvdn_value = kvdn.read(path)
                if key:
                    kvdn_value = kvdn_value["data"].get(key, None)
                    # Decode base64 data, if detected
                    prefix = "base64:"
                    if kvdn_value.startswith(prefix):
                        kvdn_value = base64.b64decode(kvdn_value[len(prefix):]).rstrip()

                if kvdn_value or not CONF["unset_if_missing"]:
                    kvdn_pillar[variable] = kvdn_value

    return kvdn_pillar