1- #! /bin/bash
2-
3- # post installation script to set up necessary resources
4-
5- set -e
6-
7-
8- # provides the prod_user we use
9- include_common (){
10- # import amros_log_dir, amros_log_media_dir
11- source " /opt/ros/melodic/lib/am_platform_scripts/scripts/amros/amros-constants.sh "
12- # import user_exists
13- source " /opt/ros/melodic/lib/am_platform_scripts/scripts/common/common.sh "
14- }
15-
16-
17- # creates a log directory granting access
18- # outputs a message to the screen trace upon installation
19- create_log_directory (){
20-
21- # helpful to the viewer of the message to know the source
22- OUTPUT_PREFIX= " visbox/debian/postinst: "
23-
24- dir= $1
25- user_group= $2
26-
27- # create the directory if it doesn't exist
28- if [[ -d " $ dir" ]] ; then
29- echo " $OUTPUT_PREFIX ' $dir ' exists to receive logs "
30- else
31- mkdir -p " $dir "
32- if [[ $( user_exists $user ) ]] ; then
33- chown " $user_group : $user_group " " $dir "
34- else
35- echo " WARNING: $user user does not exist. Making $dir global writeable. "
36- chmod +777 " $dir "
37- fi
38- echo " $OUTPUT_PREFIX ' $dir ' created to receive logs "
39- fi
40- }
41-
42- # create the log directory needed for bag files. see bag_logger.cpp
43- bag_logger_dirs (){
44-
45- # amros_user comes from common scripts source
46- # this is also defined in bag_logger.cpp
47- # the log locations (/var/log/amros) are defined by the platorm installation
48- user=$( amros_app_user)
49- create_log_directory $( amros_log_dir ) $ user
50- create_log_directory $( amros_log_media_dir ) $user
51- }
52-
53- # main ...
54- include_common
55- bag_logger_dirs
1+ #!/usr/ bin/env python3
2+ """
3+ Set up /var/log/amros directory declared in bag_logger
4+ The script:
5+ 1. creates the directory if not already exists
6+ 2. grants access to the amros user if it exists
7+ 3. grants access to everyone if amros user doesn't exist
8+
9+ The script depends upon platform scripts, as identified in the debian/control file.
10+ """
11+
12+ import os
13+ import subprocess
14+
15+ def make_log_dir_available_to_all ( log_dir , user ):
16+ """changes access of directory allowing anyone to read and prints a warning"""
17+ try :
18+ subprocess . check_output ([ "chmod" , "777" , log_dir ])
19+ print ( f"WARNING: making log dir { log_dir } avilable to all users since { user } does not exist." )
20+ except subprocess . CalledProcessError as e :
21+ print ( f"ERROR: unable to modify access to log dir { log_dir } : { e } " )
22+
23+ def grant_log_dir_ownership_to_user ( log_dir , user ):
24+ """changes ownership if possible. prints error if not"""
25+ try :
26+ subprocess . check_output ([ "chown" , f" { user } : { user } " , log_dir ])
27+ except :
28+ print ( f"ERROR: unable to grant access of log dir { log_dir } to user { user } : { e } " )
29+
30+ def modify_access_to_log_dir ( log_dir , user ):
31+ """grants access to the amros user or everyone if the user is not available"" "
32+ try :
33+ subprocess . check_output ([ "id" , "-u" , user ])
34+ grant_log_dir_ownership_to_user ( log_dir , user )
35+ except subprocess . CalledProcessError :
36+ make_log_dir_available_to_all ( log_dir , user )
37+
38+ def get_platform_constant ( constant_name ):
39+ """Retrieves constants from scripts available in platform scripts. Must be installed"""
40+ return subprocess . check_output ([ "/opt/ros/melodic/lib/am_platform_scripts/scripts/amros/amros-constants.sh" , constant_name ]). decode ( "utf-8" ). strip ()
41+
42+ def make_dir_if_needed ():
43+ """creates the directory and grants access"""
44+ log_dir = get_platform_constant ( "amros_log_dir" )
45+ try :
46+ os . mkdir ( log_dir )
47+ print ( f"INFO: Created log directory { log_dir } " )
48+ user = get_platform_constant ( " amros_app_user" )
49+ modify_access_to_log_dir ( log_dir , user )
50+ except FileExistsError :
51+ print ( f"INFO: Log directory { log_dir } already exists" )
52+ except OSError as e :
53+ print ( f"ERROR: Unable to create log directory { log_dir } : { e } " )
54+
55+ make_dir_if_needed ()
0 commit comments