Skip to content

Commit 01592d0

Browse files
author
Aaron Roller
authored
Merge pull request #28 from AutoModality/AM-172/amros-user-check
/var/log/amros works with systems missing amros user
2 parents b415864 + ae017bf commit 01592d0

File tree

1 file changed

+60
-46
lines changed

1 file changed

+60
-46
lines changed

debian/postinst

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
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-
source "/opt/ros/melodic/lib/am_platform_scripts/scripts/amros/amros-constants.sh"
11-
}
12-
13-
# creates a log directory granting access
14-
# outputs a message to the screen trace upon installation
15-
create_log_directory(){
16-
17-
#helpful to the viewer of the message to know the source
18-
OUTPUT_PREFIX="visbox/debian/postinst:"
19-
20-
dir=$1
21-
user_group=$2
22-
23-
# create the directory if it doesn't exist
24-
if [[ -d "$dir" ]];then
25-
echo "$OUTPUT_PREFIX '$dir' exists to receive logs"
26-
else
27-
mkdir -p "$dir"
28-
chown "$user_group:$user_group" "$dir"
29-
echo "$OUTPUT_PREFIX '$dir' created to receive logs"
30-
fi
31-
}
32-
33-
# create the log directory needed for bag files. see bag_logger.cpp
34-
bag_logger_dirs(){
35-
36-
#amros_user comes from common scripts source
37-
#this is also defined in bag_logger.cpp
38-
#the log locations (/var/log/amros) are defined by the platorm installation
39-
user=$(amros_app_user)
40-
create_log_directory $(amros_log_dir) $user
41-
create_log_directory $(amros_log_media_dir) $user
42-
}
43-
44-
# main ...
45-
include_common
46-
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. The directory will be limited to {user} group in future releases.")
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+
#FIXME: enable the code above to grant access to AMROS group only
37+
make_log_dir_available_to_all(log_dir,user)
38+
39+
def get_platform_constant(constant_name):
40+
"""Retrieves constants from scripts available in platform scripts. Must be installed"""
41+
return subprocess.check_output(["/opt/ros/melodic/lib/am_platform_scripts/scripts/amros/amros-constants.sh", constant_name]).decode("utf-8").strip()
42+
43+
def make_dir_if_needed(log_dir):
44+
"""creates the directory and grants access"""
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+
def make_dirs_if_needed():
56+
"""creates all directories needed to suppport am-utils requirements"""
57+
make_dir_if_needed(get_platform_constant( "amros_log_dir"))
58+
make_dir_if_needed(get_platform_constant( "amros_log_media_dir"))
59+
60+
make_dirs_if_needed()

0 commit comments

Comments
 (0)