Skip to content

Commit 63ab64e

Browse files
authored
Merge pull request #14 from husarnet/filter-env
You can use any env in `filter.yaml` (not only `ROS_NAMESPACE`)
2 parents d42828c + 980dba0 commit 63ab64e

File tree

8 files changed

+397
-350
lines changed

8 files changed

+397
-350
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ RUN curl -L https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_l
5252
COPY --from=ddsrouter_builder /dds_router /dds_router
5353

5454
COPY entrypoint.sh /
55+
COPY run_auto_config.sh /
5556
COPY config.lan.template.yaml /
5657
COPY config.discovery-server.template.yaml /
5758
COPY config.wan.template.yaml /

config_daemon.sh

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55

66
CFG_PATH=/var/tmp
77

8+
envsubst_custom() {
9+
local content=$(<"$1")
10+
echo "$content" | while IFS= read -r line; do
11+
if [[ $line =~ \{\{env\ [\"]*([^\"]+)[\"]*\}\} ]]; then
12+
var="${BASH_REMATCH[1]}"
13+
value=$(eval echo "\$$var")
14+
line=$(echo "$line" | sed "s/{{env [\"]*$var[\"]*}}/$value/g")
15+
fi
16+
echo "$line"
17+
done
18+
}
19+
820
while true; do
921
if [ -f $CFG_PATH/config.yaml ]; then
1022
# config.yaml exists
@@ -53,17 +65,26 @@ while true; do
5365
fi
5466
fi
5567
fi
56-
68+
69+
# if [[ -n "${FILTER}" ]]; then
70+
# yq -i '. * env(FILTER)' $CFG_PATH/config.yaml
71+
# else
72+
# yq -i '. * load("/filter.yaml")' $CFG_PATH/config.yaml
73+
# fi
74+
75+
# yq -i '(.allowlist[] | select(.name)).name |= sub("{{env [\"]*ROS_NAMESPACE[\"]*}}";"'$ROS_NAMESPACE'")' $CFG_PATH/config.yaml
76+
# yq -i '(.blocklist[] | select(.name)).name |= sub("{{env [\"]*ROS_NAMESPACE[\"]*}}";"'$ROS_NAMESPACE'")' $CFG_PATH/config.yaml
77+
# yq -i '(.builtin-topics[] | select(.name)).name |= sub("{{env [\"]*ROS_NAMESPACE[\"]*}}";"'$ROS_NAMESPACE'")' $CFG_PATH/config.yaml
78+
5779
if [[ -n "${FILTER}" ]]; then
58-
yq -i '. * env(FILTER)' $CFG_PATH/config.yaml
80+
echo "$FILTER" >$CFG_PATH/filter.tmp.yaml
5981
else
60-
yq -i '. * load("/filter.yaml")' $CFG_PATH/config.yaml
82+
cp /filter.yaml $CFG_PATH/filter.tmp.yaml
6183
fi
6284

63-
yq -i '(.allowlist[] | select(.name)).name |= sub("{{env [\"]*ROS_NAMESPACE[\"]*}}";"'$ROS_NAMESPACE'")' $CFG_PATH/config.yaml
64-
yq -i '(.blocklist[] | select(.name)).name |= sub("{{env [\"]*ROS_NAMESPACE[\"]*}}";"'$ROS_NAMESPACE'")' $CFG_PATH/config.yaml
65-
yq -i '(.builtin-topics[] | select(.name)).name |= sub("{{env [\"]*ROS_NAMESPACE[\"]*}}";"'$ROS_NAMESPACE'")' $CFG_PATH/config.yaml
66-
85+
envsubst_custom $CFG_PATH/filter.tmp.yaml >$CFG_PATH/filter.yaml
86+
yq -i '. * load("'$CFG_PATH'/filter.yaml")' $CFG_PATH/config.yaml
87+
6788
# remove comments
6889
yq -i '... comments=""' $CFG_PATH/config.yaml
6990

devel_setup/compose.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ services:
1414
volumes:
1515
- ../config_daemon.sh:/config_daemon.sh
1616
- ../entrypoint.sh:/entrypoint.sh
17+
- ../run_auto_config.sh:/run_auto_config.sh
1718
- ./filter.yaml:/filter.yaml
1819
- ./test2.sh:/test2.sh
1920
- ../config.discovery-server.template.yaml:/config.discovery-server.template.yaml
2021
- ../config.lan.template.yaml:/config.lan.template.yaml
2122
- ../config.wan.template.yaml:/config.wan.template.yaml
2223
- ../local-participants.yaml:/local-participants.yaml
2324
environment:
25+
USER: $USER
2426
ROS_DOMAIN_ID: 123
27+
RANDOM_ENV: abcde
2528
ROS_DISCOVERY_SERVER: ";;;;rosbot2r:123;;rosbot2r:874"
2629
DISCOVERY_SERVER_LISTENING_PORT: "12345"
2730
DISCOVERY_SERVER_ID: 1

devel_setup/filter.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
allowlist:
44
- name: rt/robocik/chatter
55
type: "std_msgs::msg::dds_::String_"
6-
- name: "rt/{{env ROS_NAMESPACE}}/chatter2"
6+
- name: "rt/{{env RANDOM_ENV}}/chatter2"
77
type: "std_msgs::msg::dds_::String_"
8-
- name: rt/chatter3
8+
- name: rt/{{env "RANDOM_ENV1"}}/chatter3
99
type: "std_msgs::msg::dds_::String_"
1010
blocklist:
11-
- name: "rt/rosout"
11+
- name: "rt/{{env RANDOM_ENV1}}/rosout"
1212
type: "rcl_interfaces::msg::dds_::Log_"
1313
builtin-topics: []

devel_setup/test2.sh

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
#!/bin/bash
22

3-
# Path to your YAML file
4-
yaml_file="/filter.yaml"
3+
# # Path to your YAML file
4+
# yaml_file="/filter.yaml"
55

6-
# Function to expand environment variables in the YAML content
7-
expand_envs_in_yaml() {
8-
local file=$1
9-
cp /filter.yaml /var/tmp/filter.yaml
10-
yq -i '(.allowlist[] | select(.name)).name |= sub("{{env \"ROS_NAMESPACE\"}}"; env(ROS_NAMESPACE))' /var/tmp/filter.yaml
11-
12-
# Use sed to replace '//' with '/'
13-
sed -i 's#//#/#g' /var/tmp/filter.yaml
6+
# # Function to expand environment variables in the YAML content
7+
# expand_envs_in_yaml() {
8+
# local file=$1
9+
# cp /filter.yaml /var/tmp/filter.yaml
10+
# yq -i '(.allowlist[] | select(.name)).name |= sub("{{env \"ROS_NAMESPACE\"}}"; env(ROS_NAMESPACE))' /var/tmp/filter.yaml
11+
12+
# # Use sed to replace '//' with '/'
13+
# sed -i 's#//#/#g' /var/tmp/filter.yaml
14+
# }
15+
# # Call the function and output to terminal
16+
# expand_envs_in_yaml "$yaml_file"
17+
18+
CFG_PATH="filter.tmp.yaml"
19+
RANDOM_ENV="abcd" # Define your environment variable
20+
21+
# Function to replace {{env "VARIABLE"}} with its value
22+
envsubst_custom() {
23+
local content=$(<"$1")
24+
echo "$content" | while IFS= read -r line; do
25+
if [[ $line =~ \{\{env\ [\"]*([^\"]+)[\"]*\}\} ]]; then
26+
var="${BASH_REMATCH[1]}"
27+
value=$(eval echo "\$$var")
28+
line=$(echo "$line" | sed "s/{{env [\"]*$var[\"]*}}/$value/g")
29+
fi
30+
echo "$line"
31+
done
1432
}
15-
# Call the function and output to terminal
16-
expand_envs_in_yaml "$yaml_file"
33+
34+
# envsubst_custom > "${CFG_PATH}.tmp" && mv "${CFG_PATH}.tmp" "$CFG_PATH"
35+
envsubst_custom

0 commit comments

Comments
 (0)