User Tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
gentoo:locald [2024/04/02 13:05] – [How it works] willygentoo:locald [2024/07/30 09:40] (current) willy
Line 8: Line 8:
  
 Let me show how i am managing this approach. Let me show how i am managing this approach.
 +
 +*NOTE:* this approach _do not applies_ to podman containers. See [[gentoo:containers|Using Containers on Gentoo]] for more info on how i manage to autostart containers on boot.
  
 ===== local.d ===== ===== local.d =====
Line 30: Line 32:
 The other way is to use local.d service leveraging a specific script with symlinks: the idea is to use one script for most of the services, and just create a symlink to the start and stop scripts themselves.  The other way is to use local.d service leveraging a specific script with symlinks: the idea is to use one script for most of the services, and just create a symlink to the start and stop scripts themselves. 
  
-Assuming you have a service called **myservice**, which is a podman container, run as //myuser// user, all you need to do it: +For simple non-containerized service called **mynormalservice** running as user **myotheruser**:
-<code bash> +
-cd /etc/local.d +
-ln -s _servicer.sh 50-myservice-myuser-podman.start  +
-ln -s _servicer.sh 50-myservice-myuser-podman.stop +
-</code> +
- +
-and that's it. This will automatically create the **/var/run/myservice.pid** file for service-monitoring, and allow proper start and stop of the service. This will require the usual //docker-compose.yml// file in the //myuser// folder. +
- +
-Similarly, for a simple non-containerized service:+
 <code bash> <code bash>
 cd /etc/local.d cd /etc/local.d
Line 68: Line 61:
 #  - user is the user the service will run as. Can be omitted, in this case use two "--", and user=service #  - user is the user the service will run as. Can be omitted, in this case use two "--", and user=service
 #  - type is one of: #  - type is one of:
-#        - podman: the service is a podman-compose based containeration 
 #        - script: the service is managed by a script ($user_home/myservice_start.sh or $user_home/myservice.sh, and $user_home/myservice_stop.sh (optional)) #        - script: the service is managed by a script ($user_home/myservice_start.sh or $user_home/myservice.sh, and $user_home/myservice_stop.sh (optional))
 #        - service: the service command is specified inside a service_${service}_start file in the user home folder #        - service: the service command is specified inside a service_${service}_start file in the user home folder
Line 74: Line 66:
 # #
  
 +
 +LOG_PATH=/var/log/servicer
 +test ! -d "${LOG_PATH}" && mkdir "${LOG_PATH}"
 +actions_logs="${LOG_PATH}/servicer.log"
  
 service_user_type_action=${0#*-} service_user_type_action=${0#*-}
Line 85: Line 81:
  
 passwd_entry=$(getent passwd ${USER}) || exit 255 passwd_entry=$(getent passwd ${USER}) || exit 255
-HOME=$(echo ${passwd_entry} | cut -d: -f 6)+USER_HOME=$(echo ${passwd_entry} | cut -d: -f 6) 
 + 
 +echo "$(date): ${ACTION}-ing service '${SERVICE}' for user '${USER}', as '${TYPE}' (${USER_HOME})" >> ${actions_logs}
  
 if [ "${ACTION}" = "start" ] if [ "${ACTION}" = "start" ]
 then then
-        if [ "${TYPE}" "podman] +        test -e "${LOG_PATH}/${SERVICE}" || { 
-        then +                mkdir "${LOG_PATH}/${SERVICE}
-                COMMAND="$(which podman)+        } && chown -R ${USER} "${LOG_PATH}/${SERVICE}
-                ARGUMENTS=(compose up) +        extra_opts=(-1 "${LOG_PATH}/${SERVICE}/${SERVICE}.out.log" -2 "${LOG_PATH}/${SERVICE}/${SERVICE}.err.log") 
-                iptables -L -t nat &> /dev/null +        if [ "${TYPE}" = "script" ]
-                podman network create ${SERVICE}-net &> /dev/null +
-        elif [ "${TYPE}" = "script" ]+
         then         then
-                start_script="${HOME}/${SERVICE}_start.sh" +                echo "          ... checking for start scripts ..." >> ${actions_logs} 
-                test -e ${start_script} || start_script="${HOME}/${SERVICE}.sh" +                start_script="${USER_HOME}/${SERVICE}_start.sh" 
-                COMMAND="${HOME}/${SERVICE}.sh"+                test -e ${start_script} || start_script="${USER_HOME}/${SERVICE}.sh" 
 +                echo          ... detected '${start_script}' ..." >> ${actions_logs} 
 +                COMMAND="${start_script}"
                 ARGUMENTS=""                 ARGUMENTS=""
         elif [ "${TYPE}" = "service" ]         elif [ "${TYPE}" = "service" ]
         then         then
-                source_script="${HOME}/service_${SERVICE}_start"+                echo "          ... checking for config settings ..." >> ${actions_logs} 
 +                source_script="${USER_HOME}/service_${SERVICE}_start"
                 test -e "${source_script}" || {                 test -e "${source_script}" || {
-                        echo "Error, missing '${source_script}'"+                        echo "Error, missing '${source_script}'" >> ${actions_logs}
                         exit 255                         exit 255
                 }                 }
 +                echo "          ... sourcing '${source_script}'..." >> ${actions_logs}
                 source "${source_script}"                 source "${source_script}"
         fi         fi
Line 113: Line 113:
 elif [ "${ACTION}" = "stop" ] elif [ "${ACTION}" = "stop" ]
 then then
-        if [ "${TYPE}" = "podman" ]+        extra_opts=() 
 +        if [ "${TYPE}" = "script" ]
         then         then
-                su - "${USER}-c "$(which podman) compose down" &/dev/null +                echo          ... checking for stop script ..." >${actions_logs
-                action=(--stop "${SERVICE}") +                stop_script="${USER_HOME}/${SERVICE}_stop.sh" 
-        elif [ "${TYPE}" = "script"+                test -e "${stop_script}" && 
-        then +                        echo "          ... running '${stop_script}' ..." >> ${actions_logs} 
-                stop_script="${HOME}/${SERVICE}_stop.sh" +                        su - ${USER} -c "${stop_script}" 
-                test -e "${stop_script}" && su - ${USER} -c "${stop_script}"+                }
         elif [ "${TYPE}" = "service" ]         elif [ "${TYPE}" = "service" ]
         then         then
Line 128: Line 129:
 fi fi
  
-start-stop-daemon -p /var/run/${SERVICE}.pid -${SERVICE} -u ${USER} -d ${HOME} ${action[@]}+echo start-stop-daemon -p /var/run/${SERVICE}.pid ${extra_opts[@]} -u ${USER} -d ${USER_HOME} ${action[@]} >> ${actions_logs} 
 +start-stop-daemon -p /var/run/${SERVICE}.pid ${extra_opts[@]} -u ${USER} -d ${USER_HOME} ${action[@]
 + 
 +echo "$(date): ${ACTION}-ed service '${SERVICE}' for user '${USER}', as '${TYPE}' (${USER_HOME})" >> ${actions_logs}
 </file> </file>
  
Line 147: Line 151:
 **User name:** which user shall run the service. Can be omitted (just leave two "--") and in this case a user with the same name as the service will be used. The user __must__ exist. **User name:** which user shall run the service. Can be omitted (just leave two "--") and in this case a user with the same name as the service will be used. The user __must__ exist.
  
-**Service type:** can be //podman//, //service// or //script//: +**Service type:** can be //service// or //script//:
-  - //podman//: run **podman compose up (or down)** as the user+
   - //service//: source the file $HOME/service_<service>_start and execute COMMAND ARGUMENTS (see example below)   - //service//: source the file $HOME/service_<service>_start and execute COMMAND ARGUMENTS (see example below)
   - //script//: will run $HOME/<service>_start.sh / $HOME/<service>_stop.sh   - //script//: will run $HOME/<service>_start.sh / $HOME/<service>_stop.sh
Line 161: Line 164:
 ===== Adding a container-based service ===== ===== Adding a container-based service =====
  
-It's easy, just create the symlink in /etc/local.d following the syntax:+It's easy, just create the symlink in /etc/local.d
 + 
 + 
 +===== Logrotate ===== 
 + 
 +If you use (and you sohuld) LogRotate to keep your logs sanely rotated and trimmed, add the following **/etc/logrotate.d/servicer** file: 
 +<file - servicer> 
 +/var/log/servicer/*/.log{ 
 +    missingok 
 +
 +/var/log/servicer/servicer.log{ 
 +    missingok 
 +
 +</file>
  
  

This website uses technical cookies only. No information is shared with anybody or used in any way but provide the website in your browser.

More information