check if config file exists with a post script (during package installation process)

This commit is contained in:
Lukas Heiligenbrunner 2020-05-01 16:47:22 +02:00
parent 2505e2cbf4
commit ae9aa1d423

View File

@ -10,16 +10,16 @@
cmake_minimum_required(VERSION 3.13) cmake_minimum_required(VERSION 3.13)
project(iprefresher DESCRIPTION "Dynu ip refresher") project(iprefresher DESCRIPTION "Dynu ip refresher")
set(CMAKE_CXX_STANDARD 17) SET(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug) SET(CMAKE_BUILD_TYPE Release) # manually SET build type (Release / Debug)
set(LIB_METHOD STATIC) #SHARED / STATIC SET(LIB_METHOD STATIC) #SHARED / STATIC
set(PROJECT_VERSION 1.3.1-dev) SET(PROJECT_VERSION 1.3.1-dev)
option(BUILD_DOC "Build documentation" ON) option(BUILD_DOC "Build documentation" ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# config libs # config libs
message(STATUS "Config of Libraries") message(STATUS "Config of Libraries")
@ -59,6 +59,17 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h
"\#pragma once\n#include <string>\nnamespace Version {\n const std::string VERSION = \"${PROJECT_VERSION}\";\n}" "\#pragma once\n#include <string>\nnamespace Version {\n const std::string VERSION = \"${PROJECT_VERSION}\";\n}"
) )
#read sample config
FILE(READ ${CMAKE_SOURCE_DIR}/config/iprefresher.cfg SAMPLECONFIG)
# generate post script for checking if configuration already exists
FILE(WRITE ${CMAKE_SOURCE_DIR}/postinstall.sh
"#!/bin/bash
if [ ! -f /etc/iprefresher.cfg ]; then
cat > /etc/iprefresher.cfg <<- EOM
${SAMPLECONFIG}EOM
fi"
)
add_library(api ${LIB_METHOD} add_library(api ${LIB_METHOD}
src/api/API.cpp src/api/API.cpp
@ -75,7 +86,7 @@ add_library(logger ${LIB_METHOD}
) )
set(SOURCE SET(SOURCE
src/main.cpp src/main.cpp
src/IPRefresher.cpp src/IPRefresher.cpp
src/Config.cpp) src/Config.cpp)
@ -86,16 +97,9 @@ add_executable(iprefresher ${SOURCE})
target_link_libraries(iprefresher api logger ${CURL_LIBRARIES} config++) target_link_libraries(iprefresher api logger ${CURL_LIBRARIES} config++)
# INSTALL to SYSTEM # # INSTALL to SYSTEM #
set(CMAKE_INSTALL_PREFIX "/") SET(CMAKE_INSTALL_PREFIX "/")
# install binaries # install binaries
install(TARGETS iprefresher DESTINATION usr/bin) install(TARGETS iprefresher DESTINATION usr/bin)
# install config file if it doesn't exist
install(CODE "MESSAGE(STATUS \"Copy iprefresher.cfg to ${CMAKE_INSTALL_PREFIX}etc/iprefresher.cfg\")
IF (NOT EXISTS ${CMAKE_INSTALL_PREFIX}etc/iprefresher.cfg)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/config/iprefresher.cfg
${CMAKE_INSTALL_PREFIX}etc/iprefresher.cfg)
ENDIF (NOT EXISTS ${CMAKE_INSTALL_PREFIX}etc/iprefresher.cfg)")
# install systemd service and enable it # install systemd service and enable it
install(FILES service/iprefresher.service DESTINATION lib/systemd/system) install(FILES service/iprefresher.service DESTINATION lib/systemd/system)
@ -104,7 +108,7 @@ IF (UNIX)
message(STATUS "config of Package build") message(STATUS "config of Package build")
SET(CPACK_DEB_COMPONENT_INSTALL 1) SET(CPACK_DEB_COMPONENT_INSTALL 1)
SET(CPACK_OUTPUT_FILE_PREFIX packages) SET(CPACK_OUTPUT_FILE_PREFIX packages)
set(CPACK_PACKAGING_INSTALL_PREFIX "/") # no prefix for package structure SET(CPACK_PACKAGING_INSTALL_PREFIX "/") # no prefix for package structure
FIND_PROGRAM(RPMBUILD_EXECUTABLE rpmbuild) FIND_PROGRAM(RPMBUILD_EXECUTABLE rpmbuild)
FIND_PROGRAM(DEB_EXECUTABLE dpkg) FIND_PROGRAM(DEB_EXECUTABLE dpkg)
@ -114,8 +118,9 @@ IF (UNIX)
#check if rpm build is possible #check if rpm build is possible
if (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND") if (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
message(STATUS "found rpm build executeable --> able to build rpm") message(STATUS "found rpm build executeable --> able to build rpm")
set(CPACK_GENERATOR "${CPACK_GENERATOR};RPM") SET(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/lib/systemd/system" "/lib/systemd" "/lib" "/usr/local/bin" "/usr/local") # --> needed to not override existing folders SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/postinstall.sh")
SET(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/lib/systemd/system" "/lib/systemd" "/lib" "/usr/local/bin" "/usr/local") # --> needed to not override existing folders
else (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND") else (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
message(STATUS "not found rpm build tools --> not building rpm") message(STATUS "not found rpm build tools --> not building rpm")
endif (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND") endif (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
@ -123,7 +128,8 @@ IF (UNIX)
#check if deb build is possible #check if deb build is possible
if (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND") if (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
message(STATUS "found deb build tools --> able to build deb") message(STATUS "found deb build tools --> able to build deb")
set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB") SET(CPACK_GENERATOR "${CPACK_GENERATOR};DEB")
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/postinstall.sh")
else (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND") else (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
message(STATUS "not found deb build tools --> not building deb") message(STATUS "not found deb build tools --> not building deb")
endif (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND") endif (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
@ -139,7 +145,8 @@ IF (UNIX)
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${PROJECT_VERSION}") SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${PROJECT_VERSION}")
SET(CPACK_PACKAGE_CONTACT "Lukas Heiligenbrunner <lukas.heiligenbrunner@gmail.com>") SET(CPACK_PACKAGE_CONTACT "Lukas Heiligenbrunner <lukas.heiligenbrunner@gmail.com>")
SET(CPACK_PACKAGE_SECTION "games") SET(CPACK_PACKAGE_SECTION "games")
set(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1), libconfig (>= 1.5.0)") SET(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1), libconfig (>= 1.5.0)")
SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/postinstall.sh")
INCLUDE(CPack) INCLUDE(CPack)
@ -155,9 +162,9 @@ if (BUILD_DOC)
message(STATUS "config of DOxygen build") message(STATUS "config of DOxygen build")
find_package(Doxygen) find_package(Doxygen)
if (DOXYGEN_FOUND) if (DOXYGEN_FOUND)
# set input and output files # SET input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in) SET(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile) SET(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# request to configure the file # request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY) configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)