Skip to content

Commit 4be12b2

Browse files
authored
Add prerequisites script and update README (#24567)
Why I did it Add prerequisites script for automated prerequisites installation and repository cloning. Update README with single script solution.
1 parent a1f1027 commit 4be12b2

File tree

2 files changed

+131
-2
lines changed

2 files changed

+131
-2
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,22 @@ Any server can be a build image server as long as it has:
8282
8383
A good choice of OS for building SONiC is currently Ubuntu 22.04.
8484

85-
## Prerequisites
85+
## Automated prerequisites installation and repository cloning
86+
87+
For convenience, you can use the automated prerequisites script to handle both prerequisites installation and repository cloning:
88+
89+
```shell
90+
curl -sSL https://raw.githubusercontent.com/sonic-net/sonic-buildimage/master/scripts/prerequisites.sh | bash
91+
```
92+
93+
This script will automatically:
94+
* Install required packages (pip, jinja, Docker)
95+
* Configure Docker for non-root usage
96+
* Clone the repository with all submodules
97+
98+
After completing this step, proceed to the [Usage](#usage) section below.
99+
100+
## Manual prerequisites installation
86101

87102
* Install pip and jinja in host build machine, execute below commands
88103
if j2/jinjanator is not available:
@@ -92,6 +107,9 @@ sudo apt install -y python3-pip
92107
pip3 install --user jinjanator
93108
```
94109

110+
> **Note:** If you cannot run the `j2` command after installation, this is likely because the `~/.local/bin` directory was just created and is not yet included in your `$PATH`. Please log out and log back in to refresh your environment, then test the command again.
111+
112+
95113
* Install [Docker](https://docs.docker.com/engine/install/) and configure your
96114
system to allow running the 'docker' command without 'sudo':
97115
* Add current user to the docker group: `sudo gpasswd -a ${USER} docker`
@@ -103,7 +121,7 @@ pip3 install --user jinjanator
103121
> This will avoid [known bugs that falsely report read-only filesystems issues](https://stackoverflow.com/questions/52526219/docker-mkdir-read-only-file-system)
104122
> during the build process.
105123
106-
## Clone the repository with all the git submodules
124+
## Manual clone the repository with all the git submodules
107125

108126
To clone the code repository recursively:
109127

scripts/prerequisites.sh

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env bash
2+
# Strict mode: exit on error, undefined variable, or pipeline failure
3+
set -euo pipefail
4+
5+
###############################################
6+
# Error Handling
7+
###############################################
8+
trap 'echo "[ERROR] Script failed at line $LINENO. Exiting."; exit 1' ERR
9+
10+
# Wrapper to provide better log messages for each step
11+
run_step() {
12+
local msg="$1"
13+
shift
14+
echo "==> $msg"
15+
"$@"
16+
echo "==> Completed: $msg"
17+
}
18+
19+
###############################################
20+
# Configurable Variables
21+
# Can be overridden via environment variables:
22+
# SONIC_REPO=<url> SONIC_DIR=<path> BRANCH=<name> bash prerequisites.sh
23+
###############################################
24+
SONIC_REPO="${SONIC_REPO:-https://github.com/sonic-net/sonic-buildimage.git}"
25+
SONIC_DIR="${SONIC_DIR:-$HOME/sonic-buildimage}"
26+
BRANCH="${BRANCH:-master}"
27+
28+
###############################################
29+
# Steps Begin
30+
###############################################
31+
32+
run_step "Updating apt package index" \
33+
sudo apt update
34+
35+
run_step "Installing prerequisites (python3-pip, git)" \
36+
sudo apt install -y python3-pip git
37+
38+
run_step "Installing jinjanator (j2)" \
39+
bash -c 'pip3 install --user jinjanator || sudo apt install j2cli'
40+
41+
echo "==> Testing j2 availability..."
42+
if ! command -v j2 >/dev/null 2>&1; then
43+
echo "[ERROR] j2 is not runnable."
44+
echo "Please logout and login, then run the script again."
45+
exit 1
46+
else
47+
echo "==> j2 is available: $(command -v j2)"
48+
fi
49+
50+
###############################################
51+
# Docker Install Check
52+
###############################################
53+
if ! command -v docker >/dev/null 2>&1; then
54+
run_step "Installing Docker dependencies" \
55+
sudo apt install -y ca-certificates curl gnupg lsb-release
56+
57+
run_step "Adding Docker GPG key" \
58+
bash -c "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg"
59+
60+
run_step "Adding Docker apt repository" \
61+
bash -c "echo \
62+
\"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
63+
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\" \
64+
| sudo tee /etc/apt/sources.list.d/docker.list >/dev/null"
65+
66+
run_step "Updating apt after adding Docker repo" \
67+
sudo apt update
68+
69+
run_step "Installing Docker Engine" \
70+
sudo apt install -y docker-ce docker-ce-cli containerd.io
71+
else
72+
echo "==> Docker already installed, skipping."
73+
fi
74+
75+
run_step "Adding user '${USER}' to docker group" \
76+
sudo gpasswd -a "${USER}" docker
77+
78+
echo "==> Testing Docker availability without sudo..."
79+
if ! docker ps >/dev/null 2>&1; then
80+
echo "[WARNING] Docker cannot be run without sudo yet."
81+
echo "Please log out and log back in for the docker group membership to take effect."
82+
echo "After logging back in, you can continue with the build process."
83+
else
84+
echo "==> Docker is available without sudo."
85+
fi
86+
87+
###############################################
88+
# Clone SONiC Repo
89+
###############################################
90+
if [ -d "$SONIC_DIR" ]; then
91+
echo "==> Directory $SONIC_DIR already exists, skipping clone."
92+
else
93+
run_step "Cloning SONiC repository" \
94+
git clone --recurse-submodules "$SONIC_REPO" "$SONIC_DIR"
95+
fi
96+
97+
cd "$SONIC_DIR"
98+
99+
run_step "Checking out branch ${BRANCH}" \
100+
git checkout "${BRANCH}"
101+
102+
run_step "Fetching latest code" \
103+
git fetch
104+
105+
###############################################
106+
# Done
107+
###############################################
108+
echo "===================================================="
109+
echo "Done!"
110+
echo "===================================================="
111+

0 commit comments

Comments
 (0)