Skip to content

Commit 7a467d3

Browse files
committed
(bootstrap) convert to function
1 parent 0458d50 commit 7a467d3

File tree

1 file changed

+178
-173
lines changed

1 file changed

+178
-173
lines changed

installer/bootstrap.bash

Lines changed: 178 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function conditional_apt_update
55
TUE_APT_GET_UPDATED_FILE=/tmp/tue_get_apt_get_updated
66
if [[ ! -f ${TUE_APT_GET_UPDATED_FILE} ]]
77
then
8-
echo -e "[tue-env](bootstrap) sudo apt-get update -qq"
8+
echo "[tue-env](bootstrap) sudo apt-get update -qq"
99
sudo apt-get update -qq || return 1
1010
touch ${TUE_APT_GET_UPDATED_FILE}
1111
fi
@@ -18,77 +18,115 @@ function installed_or_install
1818
# Provide package name if it differs from executable name
1919
if [[ -z "$1" ]]
2020
then
21-
echo "[bootstrap] Error! No package name provided to check for installation."
21+
echo "[tue-env](bootstrap) Error! No package name provided to check for installation."
2222
return 1
2323
fi
2424
local executable package
2525
executable=$1
2626
package=$1
2727
[[ -n "$2" ]] && package=$2
2828
hash "${executable}" 2> /dev/null && return 0
29-
conditional_apt_update || { echo "[bootstrap] Error! Could not update apt-get."; return 1; }
30-
sudo apt-get install --assume-yes -qq "${package}" || { echo "[bootstrap] Error! Could not install ${package}."; return 1; }
29+
conditional_apt_update || { echo "[tue-env](bootstrap)Error! Could not update apt-get."; return 1; }
30+
sudo apt-get install --assume-yes -qq "${package}" || { echo "[tue-env](bootstrap) Error! Could not install ${package}."; return 1; }
3131
return 0
3232
}
3333

34-
# Make sure curl is installed
35-
installed_or_install curl
36-
# Make sure git is installed
37-
installed_or_install git
38-
# Make sure lsb-release is installed
39-
installed_or_install lsb_release lsb-release
40-
# Make sure python3 is installed
41-
installed_or_install python3
42-
# Make sure python3-virtualenv is installed
43-
installed_or_install virtualenv python3-virtualenv
44-
45-
# Check if OS is Ubuntu
46-
DISTRIB_ID="$(lsb_release -si)"
47-
DISTRIB_RELEASE="$(lsb_release -sr)"
48-
49-
if [ "${DISTRIB_ID}" != "Ubuntu" ]
50-
then
51-
echo "[bootstrap] Unsupported OS ${DISTRIB_ID}. Use Ubuntu."
52-
return 1
53-
fi
54-
55-
# Set ROS version
56-
TUE_ROS_DISTRO=
57-
TUE_ROS_VERSION=
58-
59-
for i in "$@"
60-
do
61-
case $i in
62-
--ros-version=* )
63-
ros_version="${i#*=}"
64-
;;
65-
--ros-distro=* )
66-
ros_distro="${i#*=}"
67-
;;
68-
--targets-repo=* )
69-
targets_repo="${i#*=}"
70-
;;
71-
--create-virtualenv=* )
72-
create_virtualenv="${i#*=}"
73-
;;
74-
* )
75-
echo "[tue-env](bootstrap) Error! Unknown argument '${i}' provided to bootstrap script."
76-
return 1
77-
;;
78-
esac
79-
done
34+
function main
35+
{
36+
# Make sure curl is installed
37+
installed_or_install curl
38+
# Make sure git is installed
39+
installed_or_install git
40+
# Make sure lsb-release is installed
41+
installed_or_install lsb_release lsb-release
42+
# Make sure python3 is installed
43+
installed_or_install python3
44+
# Make sure python3-virtualenv is installed
45+
installed_or_install virtualenv python3-virtualenv
46+
47+
# Check if OS is Ubuntu
48+
DISTRIB_ID="$(lsb_release -si)"
49+
DISTRIB_RELEASE="$(lsb_release -sr)"
50+
51+
if [[ "${DISTRIB_ID}" != "Ubuntu" ]]
52+
then
53+
echo "[tue-env](bootstrap) Unsupported OS ${DISTRIB_ID}. Use Ubuntu."
54+
return 1
55+
fi
8056

81-
case ${DISTRIB_RELEASE} in
82-
"20.04")
83-
if [[ "${ros_version}" -eq 2 ]]
84-
then
85-
TUE_ROS_VERSION=2
86-
if [[ "${ros_distro}" == "foxy" ]]
57+
# Set ROS version
58+
TUE_ROS_DISTRO=
59+
TUE_ROS_VERSION=
60+
61+
for i in "$@"
62+
do
63+
case $i in
64+
--ros-version=* )
65+
ros_version="${i#*=}"
66+
;;
67+
--ros-distro=* )
68+
ros_distro="${i#*=}"
69+
;;
70+
--targets-repo=* )
71+
targets_repo="${i#*=}"
72+
;;
73+
--create-virtualenv=* )
74+
create_virtualenv="${i#*=}"
75+
;;
76+
* )
77+
echo "[tue-env](bootstrap) Error! Unknown argument '${i}' provided to bootstrap script."
78+
return 1
79+
;;
80+
esac
81+
done
82+
83+
case ${DISTRIB_RELEASE} in
84+
"20.04")
85+
if [[ "${ros_version}" -eq 2 ]]
86+
then
87+
TUE_ROS_VERSION=2
88+
if [[ "${ros_distro}" == "foxy" ]]
89+
then
90+
TUE_ROS_DISTRO="foxy"
91+
elif [[ "${ros_distro}" == "galactic" ]]
92+
then
93+
TUE_ROS_DISTRO="galactic"
94+
elif [[ "${ros_distro}" == "rolling" ]]
95+
then
96+
TUE_ROS_DISTRO="rolling"
97+
elif [[ -n "${ros_distro}" ]]
98+
then
99+
echo "[tue-env](bootstrap) Error! ROS ${ros_distro} is unsupported with tue-env."
100+
return 1
101+
else
102+
TUE_ROS_DISTRO="galactic"
103+
echo "[tue-env](bootstrap) Using default ROS_DISTRO '${TUE_ROS_DISTRO}' with ROS_VERSION '${TUE_ROS_VERSION}'"
104+
fi
105+
elif [[ "${ros_version}" -eq 1 ]]
106+
then
107+
TUE_ROS_DISTRO="noetic"
108+
TUE_ROS_VERSION=1
109+
elif [[ -n "${ros_version}" ]]
110+
then
111+
echo "[tue-env](bootstrap) Error! ROS ${ros_version} is unsupported with tue-env."
112+
return 1
113+
else
114+
TUE_ROS_DISTRO="noetic"
115+
TUE_ROS_VERSION=1
116+
echo "[tue-env](bootstrap) Using default ROS_DISTRO '${TUE_ROS_DISTRO}' with ROS_VERSION '${TUE_ROS_VERSION}'"
117+
fi
118+
;;
119+
"22.04")
120+
if [[ -n "${ros_version}" ]] && [[ "${ros_version}" -ne 2 ]]
87121
then
88-
TUE_ROS_DISTRO="foxy"
89-
elif [[ "${ros_distro}" == "galactic" ]]
122+
echo "[tue-env](bootstrap) Error! Only ROS version 2 is supported with ubuntu 22.04 and newer"
123+
return 1
124+
fi
125+
TUE_ROS_VERSION=2
126+
127+
if [[ "${ros_distro}" == "humble" ]]
90128
then
91-
TUE_ROS_DISTRO="galactic"
129+
TUE_ROS_DISTRO="humble"
92130
elif [[ "${ros_distro}" == "rolling" ]]
93131
then
94132
TUE_ROS_DISTRO="rolling"
@@ -97,134 +135,101 @@ case ${DISTRIB_RELEASE} in
97135
echo "[tue-env](bootstrap) Error! ROS ${ros_distro} is unsupported with tue-env."
98136
return 1
99137
else
100-
TUE_ROS_DISTRO="galactic"
138+
TUE_ROS_DISTRO="humble"
101139
echo "[tue-env](bootstrap) Using default ROS_DISTRO '${TUE_ROS_DISTRO}' with ROS_VERSION '${TUE_ROS_VERSION}'"
102140
fi
103-
elif [[ "${ros_version}" -eq 1 ]]
104-
then
105-
TUE_ROS_DISTRO="noetic"
106-
TUE_ROS_VERSION=1
107-
elif [[ -n "${ros_version}" ]]
108-
then
109-
echo "[tue-env](bootstrap) Error! ROS ${ros_version} is unsupported with tue-env."
141+
;;
142+
*)
143+
echo "[tue-env](bootstrap) Ubuntu ${DISTRIB_RELEASE} is unsupported. Please use one of Ubuntu 20.04 or 22.04."
110144
return 1
111-
else
112-
TUE_ROS_DISTRO="noetic"
113-
TUE_ROS_VERSION=1
114-
echo "[tue-env](bootstrap) Using default ROS_DISTRO '${TUE_ROS_DISTRO}' with ROS_VERSION '${TUE_ROS_VERSION}'"
115-
fi
116-
;;
117-
"22.04")
118-
if [[ -n "${ros_version}" ]] && [[ "${ros_version}" -ne 2 ]]
119-
then
120-
echo "[tue-env](bootstrap) Error! Only ROS version 2 is supported with ubuntu 22.04 and newer"
121-
return 1
122-
fi
123-
TUE_ROS_VERSION=2
145+
;;
146+
esac
124147

125-
if [[ "${ros_distro}" == "humble" ]]
126-
then
127-
TUE_ROS_DISTRO="humble"
128-
elif [[ "${ros_distro}" == "rolling" ]]
129-
then
130-
TUE_ROS_DISTRO="rolling"
131-
elif [[ -n "${ros_distro}" ]]
132-
then
133-
echo "[tue-env](bootstrap) Error! ROS ${ros_distro} is unsupported with tue-env."
134-
return 1
135-
else
136-
TUE_ROS_DISTRO="humble"
137-
echo "[tue-env](bootstrap) Using default ROS_DISTRO '${TUE_ROS_DISTRO}' with ROS_VERSION '${TUE_ROS_VERSION}'"
138-
fi
139-
;;
140-
*)
141-
echo "[tue-env](bootstrap) Ubuntu ${DISTRIB_RELEASE} is unsupported. Please use one of Ubuntu 20.04 or 22.04."
142-
return 1
143-
;;
144-
esac
145-
146-
# Script variables
147-
env_url="https://github.com/tue-robotics/tue-env.git"
148-
{ [[ -n "${targets_repo}" ]] && env_targets_url="${targets_repo}"; } || env_targets_url="https://github.com/tue-robotics/tue-env-targets.git"
149-
[[ -n "${create_virtualenv}" ]] || create_virtualenv="true"
150-
env_dir="${HOME}/.tue"
151-
workspace="ros-${TUE_ROS_DISTRO}"
152-
workspace_dir="${HOME}/ros/${TUE_ROS_DISTRO}"
153-
154-
# Move old environments and installer
155-
if [ -d "${env_dir}" ] && [ -z "${CI}" ]
156-
then
157-
FILES=$(find "${env_dir}"/user/envs -maxdepth 1 -type f)
158-
date_now=$(date +%F_%R)
159-
for env in ${FILES}
160-
do
161-
mv -f "$(cat "${env}")" "$(cat "${env}")"."${date_now}"
162-
done
163-
mv -f "${env_dir}" "${env_dir}"."${date_now}"
164-
fi
165-
166-
# If in CI with Docker, then clone tue-env with BRANCH when not testing a PR
167-
if [ "${CI}" == "true" ] && [ "${DOCKER}" == "true" ]
168-
then
169-
# Docker has a default value as false for PULL_REQUEST
170-
if [ "${PULL_REQUEST}" == "false" ]
148+
# Script variables
149+
env_url="https://github.com/tue-robotics/tue-env.git"
150+
{ [[ -n "${targets_repo}" ]] && env_targets_url="${targets_repo}"; } || env_targets_url="https://github.com/tue-robotics/tue-env-targets.git"
151+
[[ -n "${create_virtualenv}" ]] || create_virtualenv="true"
152+
env_dir="${HOME}/.tue"
153+
workspace="ros-${TUE_ROS_DISTRO}"
154+
workspace_dir="${HOME}/ros/${TUE_ROS_DISTRO}"
155+
156+
# Move old environments and installer
157+
if [[ -d "${env_dir}" ]] && [[ -z "${CI}" ]]
171158
then
172-
if [ -n "${COMMIT}" ]
159+
FILES=$(find "${env_dir}"/user/envs -maxdepth 1 -type f)
160+
date_now=$(date +%F_%R)
161+
for env in ${FILES}
162+
do
163+
mv -f "$(cat "${env}")" "$(cat "${env}")"."${date_now}"
164+
done
165+
mv -f "${env_dir}" "${env_dir}"."${date_now}"
166+
fi
167+
168+
# If in CI with Docker, then clone tue-env with BRANCH when not testing a PR
169+
if [[ "${CI}" == "true" ]] && [[ "${DOCKER}" == "true" ]]
170+
then
171+
# Docker has a default value as false for PULL_REQUEST
172+
if [[ "${PULL_REQUEST}" == "false" ]]
173173
then
174-
if [ -n "${BRANCH}" ]
174+
if [[ -n "${COMMIT}" ]]
175175
then
176-
echo -e "[tue-env](bootstrap) Cloning tue-env repository with branch: ${BRANCH} at commit: ${COMMIT}"
177-
git clone -q --single-branch --branch "${BRANCH}" "${env_url}" "${env_dir}"
176+
if [[ -n "${BRANCH}" ]]
177+
then
178+
echo -e "[tue-env](bootstrap) Cloning tue-env repository with branch: ${BRANCH} at commit: ${COMMIT}"
179+
git clone -q --single-branch --branch "${BRANCH}" "${env_url}" "${env_dir}"
180+
else
181+
echo -e "[tue-env](bootstrap) Cloning tue-env repository with default branch at commit: ${COMMIT}"
182+
git clone -q --single-branch "${env_url}" "${env_dir}"
183+
fi
184+
git -C "${env_dir}" reset --hard "${COMMIT}"
178185
else
179-
echo -e "[tue-env](bootstrap) Cloning tue-env repository with default branch at commit: ${COMMIT}"
180-
git clone -q --single-branch "${env_url}" "${env_dir}"
186+
echo -e "[tue-env](bootstrap) Error! CI branch or commit is unset"
187+
return 1
181188
fi
182-
git -C "${env_dir}" reset --hard "${COMMIT}"
183189
else
184-
echo -e "[tue-env](bootstrap) Error! CI branch or commit is unset"
185-
return 1
190+
echo -e "[tue-env](bootstrap) Testing Pull Request"
191+
[[ -z "${REF_NAME}" ]] && { echo "[tue-env](bootstrap) Error! Environment variable REF_NAME is not set."; return 1; }
192+
193+
git clone -q --depth=10 "${env_url}" "${env_dir}"
194+
git -C "${env_dir}" fetch origin "${REF_NAME}"/"${PULL_REQUEST}"/merge:PULLREQUEST || { echo "[tue-env](bootstrap) Error! Could not fetch refs"; return 1; }
195+
git -C "${env_dir}" checkout PULLREQUEST
186196
fi
187197
else
188-
echo -e "[tue-env](bootstrap) Testing Pull Request"
189-
[ -z "${REF_NAME}" ] && { echo "Error! Environment variable REF_NAME is not set."; return 1; }
190-
191-
git clone -q --depth=10 "${env_url}" "${env_dir}"
192-
git -C "${env_dir}" fetch origin "${REF_NAME}"/"${PULL_REQUEST}"/merge:PULLREQUEST || { echo "Error! Could not fetch refs"; return 1; }
193-
git -C "${env_dir}" checkout PULLREQUEST
198+
# Update installer
199+
echo -e "[tue-env](bootstrap) Cloning tue-env repository"
200+
git clone "${env_url}" "${env_dir}"
194201
fi
195-
else
196-
# Update installer
197-
echo -e "[tue-env](bootstrap) Cloning tue-env repository"
198-
git clone "${env_url}" "${env_dir}"
199-
fi
200-
201-
# Source the installer commands
202-
# No need to follow to a file which is already checked by CI
203-
# shellcheck disable=SC1090
204-
source "${env_dir}"/setup.bash
205-
206-
# Create ros environment directory
207-
mkdir -p "${workspace_dir}"
208-
209-
# Initialize ros environment directory incl. targets
210-
tue-env init "${workspace}" "${workspace_dir}" "--create-virtualenv=${create_virtualenv}" "--targets-url=${env_targets_url}"
211-
212-
# Configure environment
213-
tue-env config "${workspace}" set "TUE_ROS_DISTRO" "${TUE_ROS_DISTRO}"
214-
tue-env config "${workspace}" set "TUE_ROS_VERSION" "${TUE_ROS_VERSION}"
215-
216-
# Add loading of TU/e tools (tue-env, tue-get, etc) to bashrc
217-
# shellcheck disable=SC2088
218-
if ! grep -q "${env_dir}/setup.bash" ~/.bashrc;
219-
then
220-
echo "
202+
203+
# Source the installer commands
204+
# No need to follow to a file which is already checked by CI
205+
# shellcheck disable=SC1090
206+
source "${env_dir}"/setup.bash
207+
208+
# Create ros environment directory
209+
mkdir -p "${workspace_dir}"
210+
211+
# Initialize ros environment directory incl. targets
212+
tue-env init "${workspace}" "${workspace_dir}" "--create-virtualenv=${create_virtualenv}" "--targets-url=${env_targets_url}"
213+
214+
# Configure environment
215+
tue-env config "${workspace}" set "TUE_ROS_DISTRO" "${TUE_ROS_DISTRO}"
216+
tue-env config "${workspace}" set "TUE_ROS_VERSION" "${TUE_ROS_VERSION}"
217+
218+
# Add loading of TU/e tools (tue-env, tue-get, etc) to bashrc
219+
# shellcheck disable=SC2088
220+
if ! grep -q "${env_dir}/setup.bash" ~/.bashrc;
221+
then
222+
echo "
221223
# Load TU/e tools
222224
source ${env_dir}/setup.bash" >> ~/.bashrc
223-
fi
225+
fi
224226

225-
# Set this environment as default
226-
tue-env set-default "${workspace}"
227+
# Set this environment as default
228+
tue-env set-default "${workspace}"
229+
230+
# Activate the default environment
231+
# shellcheck disable=SC1090
232+
source "${env_dir}"/setup.bash
233+
}
227234

228-
# Activate the default environment
229-
# shellcheck disable=SC1090
230-
source "${env_dir}"/setup.bash
235+
main "$@" || echo "[tue-env](bootstrap) Error! Could not install tue-env."

0 commit comments

Comments
 (0)