def _agent_key_path_in_db(config, node_id, deployment_id):
    postgres = Postgres(config)
    get_node_data = "SELECT properties FROM nodes " \
                    "WHERE id = '{0}' " \
                    "AND deployment_id = '{1}';" \
                    "".format(node_id, deployment_id)
    result = postgres.run_query(get_node_data)
    pickled_buffer = result['all'][0][0]
    properties = pickle.loads(pickled_buffer)
    key_path = properties['cloudify_agent']['key']
    ctx.logger.debug('Agent key path in db: {0}'.format(key_path))
    return key_path
def _restore_snapshot(config, tempdir, metadata, elasticsearch_read_timeout,
                      version_at_least_4):
    # files/dirs copy
    utils.copy_data(tempdir, config, to_archive=False)

    # existing plugins
    existing_plugins_names = _existing_plugins_names()

    # elasticsearch (events)
    es = _create_es_client(config)

    # postgres
    if version_at_least_4:
        _restore_postgres(tempdir, config)
    else:
        ctx.logger.info('Cleaning db before restore..')
        postgres = Postgres(config)
        for query in _clean_db_queries():
            postgres.run_query(query)
        ctx.logger.info('Restoring es data of version previous to 4')
        ElasticSearchDump().restore_prev_4(tempdir)

    _restore_elasticsearch(tempdir, es, metadata, elasticsearch_read_timeout)

    plugins = _plugins_to_install(existing_plugins_names)

    # influxdb
    _restore_influxdb_3_3(tempdir)

    # credentials
    _restore_credentials(tempdir, config)

    es.indices.flush()

    # agents
    _restore_agents_data(tempdir)

    return plugins