Skip to content

Commit

Permalink
ds redis NEW datastore plugin for Redis database
Browse files Browse the repository at this point in the history
  • Loading branch information
Humblesaw committed Apr 11, 2024
1 parent 184cc1b commit 782b447
Show file tree
Hide file tree
Showing 7 changed files with 3,104 additions and 0 deletions.
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ set(DEFAULT_NOTIFICATION_DS_PLG "JSON notif" CACHE STRING "Default datastore plu
set(MONGO_USERNAME "" CACHE STRING "Username for MongoDB administrator client, all operations are done via this client in case MongoDB plugin is supported.")
set(MONGO_PASSWORD "" CACHE STRING "Password for all MongoDB clients in case MongoDB plugin is supported.")

# Redis username and password for the client
set(REDIS_USERNAME "" CACHE STRING "Username for Redis user, all operations are done via this client in case Redis plugin is supported.")
set(REDIS_PASSWORD "" CACHE STRING "Password for Redis user in case Redis plugin is supported.")

# ietf-yang-library revision
set(YANGLIB_REVISION "2019-01-04" CACHE STRING
"YANG module ietf-yang-library revision to implement. Only 2019-01-04 and 2016-06-21 are supported.")
Expand Down Expand Up @@ -409,6 +413,29 @@ else()
message(STATUS "Datastore plugin ds_mongo not supported.")
endif()

# libhiredis - optional
find_package(LibHiredis)
if(LIBHIREDIS_FOUND)
# datastore plugin added if libraries exist
list(APPEND LIB_SRC src/plugins/ds_redis.c)
message(STATUS "Datastore plugin ds_redis supported.")

# try running a command in Redis database and check authentication
execute_process(COMMAND "echo" "ACL WHOAMI"
COMMAND "redis-cli"
OUTPUT_VARIABLE REDIS_STDOUT
ERROR_VARIABLE REDIS_STDERR)
if("${REDIS_STDOUT}" MATCHES "^NOAUTH.*")
message(STATUS "Redis authentication is supported!")
elseif("${REDIS_STDERR}" MATCHES "^Could not connect to Redis.*")
message(WARNING "Redis authentication cannot be checked - a request to Redis failed with: ${REDIS_STDERR}")
else()
message(WARNING "Redis authentication is NOT supported! Data stored by sysrepo in Redis will be accessible by anyone.")
endif()
else()
message(STATUS "Datastore plugin ds_redis not supported.")
endif()

# sysrepo
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
add_library(srobj OBJECT ${LIB_SRC} ${compatsrc})
Expand All @@ -422,6 +449,11 @@ if(TARGET mongo::mongoc_shared)
set_property(TARGET srobj APPEND PROPERTY INCLUDE_DIRECTORIES ${BSON_INTERFACE_INCLUDE_DIR} ${MONGOC_INTERFACE_INCLUDE_DIR})
target_link_libraries(sysrepo mongo::mongoc_shared)
endif()
if (LIBHIREDIS_FOUND)
# to add include directories
target_link_libraries(sysrepo ${LIBHIREDIS_LIBRARIES})
include_directories(${LIBHIREDIS_INCLUDE_DIRS})
endif()

if(ENABLE_SYSREPOCTL)
# sysrepoctl tool
Expand Down Expand Up @@ -505,6 +537,7 @@ endif()
# generate files
configure_file("${PROJECT_SOURCE_DIR}/src/config.h.in" "${PROJECT_BINARY_DIR}/config.h" ESCAPE_QUOTES @ONLY)
configure_file("${PROJECT_SOURCE_DIR}/src/plugins/ds_mongo_config.h.in" "${PROJECT_BINARY_DIR}/ds_mongo_config.h" ESCAPE_QUOTES @ONLY)
configure_file("${PROJECT_SOURCE_DIR}/src/plugins/ds_redis_config.h.in" "${PROJECT_BINARY_DIR}/ds_redis_config.h" ESCAPE_QUOTES @ONLY)
configure_file("${PROJECT_SOURCE_DIR}/sysrepo.pc.in" "${PROJECT_BINARY_DIR}/sysrepo.pc" @ONLY)
configure_file("${PROJECT_SOURCE_DIR}/src/executables/bin_common.h.in" "${PROJECT_BINARY_DIR}/bin_common.h" ESCAPE_QUOTES @ONLY)
configure_file("${PROJECT_SOURCE_DIR}/src/executables/sysrepo-plugind.service.in" "${PROJECT_BINARY_DIR}/sysrepo-plugind.service" @ONLY)
Expand Down Expand Up @@ -596,6 +629,11 @@ add_custom_target(sr_clean
"--eval" "use sr_operational" "--eval" "\"db.dropDatabase()\""
"--eval" "use sr_factory-default" "--eval" "\"db.dropDatabase()\""
"1>/dev/null"

# flush all sysrepo related data from redis
COMMAND "redis-cli" "--scan" "--pattern" "\"sr:*\"" "|" "xargs" "-d" "\"\\n\"" "-I," "redis-cli" "unlink" "\",\"" "1>/dev/null"
COMMAND "redis-cli" "--raw" "ft._list" "|" "grep" "-x" "-G" "\"^sr:.*\"" "|" "xargs" "-n" "1" "redis-cli" "ft.dropindex" "1>/dev/null"

COMMAND rm -rf ${REPO_PATH}
DEPENDS shm_clean
COMMENT "Removing the whole persistent repository \"${REPO_PATH}\""
Expand Down
76 changes: 76 additions & 0 deletions CMakeModules/FindLibHiredis.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# - Try to find LibHiredis
# Once done this will define
#
# LIBHIREDIS_FOUND - system has LibHiredis
# LIBHIREDIS_INCLUDE_DIRS - the LibHiredis include directory
# LIBHIREDIS_LIBRARIES - Link these to use LibHiredis
# LIBHIREDIS_VERSION - SO version of the found hiredis library
#
# Author Ondrej Kusnirik <Ondrej.Kusnirik@cesnet.cz>
# Copyright (c) 2021 CESNET, z.s.p.o.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
include(FindPackageHandleStandardArgs)

if(LIBHIREDIS_LIBRARIES AND LIBHIREDIS_INCLUDE_DIRS)
# in cache already
set(LIBHIREDIS_FOUND TRUE)
else()
find_path(LIBHIREDIS_INCLUDE_DIR
NAMES
hiredis/hiredis.h
PATHS
/usr/include
/usr/local/include
/opt/local/include
/sw/include
${CMAKE_INCLUDE_PATH}
${CMAKE_INSTALL_PREFIX}/include
)

find_library(LIBHIREDIS_LIBRARY
NAMES
hiredis
libhiredis
PATHS
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/local/lib
/sw/lib
${CMAKE_LIBRARY_PATH}
${CMAKE_INSTALL_PREFIX}/lib
)

set(LIBHIREDIS_INCLUDE_DIRS ${LIBHIREDIS_INCLUDE_DIR})
set(LIBHIREDIS_LIBRARIES ${LIBHIREDIS_LIBRARY})
mark_as_advanced(LIBHIREDIS_INCLUDE_DIRS LIBHIREDIS_LIBRARIES)

# handle the QUIETLY and REQUIRED arguments and set LIBSYSTEMD_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(LibHiredis FOUND_VAR LIBHIREDIS_FOUND
REQUIRED_VARS LIBHIREDIS_LIBRARY LIBHIREDIS_INCLUDE_DIR)
endif()
3 changes: 3 additions & 0 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ const struct srplg_ds_s *sr_internal_ds_plugins[] = {
#ifdef SR_HAVE_LIBMONGOC
&srpds_mongo, /**< MONGO DS */
#endif
#ifdef SR_HAVE_LIBHIREDIS
&srpds_redis, /**< REDIS DS */
#endif
};

/**
Expand Down
7 changes: 7 additions & 0 deletions src/common_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ extern const struct srplg_ds_s srpds_json;
extern const struct srplg_ds_s srpds_mongo;
#endif

/**
* @brief Internal DS plugin "REDIS DS".
*/
#ifdef SR_HAVE_LIBHIREDIS
extern const struct srplg_ds_s srpds_redis;
#endif

/**
* @brief Internal notif plugin "JSON notif".
*/
Expand Down
3 changes: 3 additions & 0 deletions src/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,7 @@
/** compile MongoDB datastore plugin if libmongoc is available */
#define SR_HAVE_LIBMONGOC @LIBMONGOC_FOUND@

/** compile Redis datastore plugin if libhiredis is available */
#define SR_HAVE_LIBHIREDIS @LIBHIREDIS_FOUND@

#endif /* _CONFIG_H */
Loading

0 comments on commit 782b447

Please sign in to comment.