Skip to content

Commit c5ec222

Browse files
authored
Add script for D2D with SCIO using docker container (#1912)
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent eb8d4bb commit c5ec222

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

etc/scripts/d2d/README.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Run ScanCode.io Mapping Script
2+
================================
3+
4+
This script executes the ``map_deploy_to_develop`` mapping workflow from
5+
ScanCode.io inside a Docker container. It optionally spins up a temporary
6+
PostgreSQL instance when needed. The script copies the specified input files to
7+
a working directory, runs the mapping, writes the output to a file, and cleans
8+
up afterward.
9+
10+
Usage
11+
-----
12+
13+
.. code-block:: bash
14+
15+
./map-deploy-to-develop.sh <from-path> <to-path> <output-file> [options] <spin-db> [db-port]
16+
17+
Arguments
18+
---------
19+
20+
+-----------------+-------------------------------------------------------------+
21+
| Argument | Description |
22+
+=================+=============================================================+
23+
| ``from-path`` | Path to the base deployment/scan file |
24+
+-----------------+-------------------------------------------------------------+
25+
| ``to-path`` | Path to the target deployment/scan file |
26+
+-----------------+-------------------------------------------------------------+
27+
| ``options`` | D2D pipeline parameters (can be empty ``""``) |
28+
+-----------------+-------------------------------------------------------------+
29+
| ``output-file`` | File where ScanCode.io output will be written |
30+
+-----------------+-------------------------------------------------------------+
31+
| ``spin-db`` | ``true`` = spin temp DB container, ``false`` = skip |
32+
+-----------------+-------------------------------------------------------------+
33+
| ``db-port`` | Port to bind Postgres (default: ``5432``) |
34+
+-----------------+-------------------------------------------------------------+
35+
36+
37+
Example
38+
-------
39+
40+
Run mapping without database:
41+
42+
.. code-block:: bash
43+
44+
./map-deploy-to-develop.sh ./from.tar.gz ./to.whl results.json
45+
46+
Run mapping with database on a custom port:
47+
48+
.. code-block:: bash
49+
50+
./map-deploy-to-develop.sh ./from.tar.gz ./to.whl output.json --options "Python,Java" --spin-db --port 5433
51+
52+
Script Actions
53+
--------------
54+
55+
1. Validates required arguments
56+
2. Starts PostgreSQL in Docker (if ``spin-db=true``)
57+
3. Creates a temporary working directory: ``./d2d``
58+
4. Copies input files into working directory
59+
5. Runs ScanCode.io mapping step:
60+
61+
.. code-block:: text
62+
63+
run map_deploy_to_develop:<D2D_OPTIONS> \
64+
"/code/<from-file>:from,/code/<to-file>:to"
65+
66+
6. Writes mapping output into ``output-file``
67+
7. Cleans up temp directory
68+
8. Stops DB container if it was started
69+
70+
Dependencies
71+
------------
72+
73+
* Bash
74+
* Docker
75+
* Local filesystem permissions for creating ``./d2d`` and writing output
76+
77+
78+
Before running the script:
79+
----------------------------------
80+
81+
Ensure the script has execute permissions:
82+
83+
.. code-block:: bash
84+
85+
sudo su -
86+
chmod +x map-deploy-to-develop.sh
87+
88+
Then execute:
89+
90+
.. code-block:: bash
91+
92+
./map-deploy-to-develop.sh ...
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
set -e
3+
4+
FROM_PATH="$1"
5+
TO_PATH="$2"
6+
OUTPUT_FILE="$3"
7+
8+
D2D_OPTIONS=""
9+
SPIN_DB=false
10+
DB_PORT=5432
11+
12+
13+
shift 3
14+
while [[ "$#" -gt 0 ]]; do
15+
case "$1" in
16+
--options)
17+
D2D_OPTIONS="$2"
18+
shift 2
19+
;;
20+
--spin-db)
21+
SPIN_DB=true
22+
shift 1
23+
;;
24+
--port)
25+
DB_PORT="$2"
26+
shift 2
27+
;;
28+
*)
29+
echo "Unknown parameter: $1"
30+
exit 1
31+
;;
32+
esac
33+
done
34+
35+
if [ -z "$FROM_PATH" ] || [ -z "$TO_PATH" ] || [ -z "$OUTPUT_FILE" ]; then
36+
echo "Missing required arguments!"
37+
echo "Usage: $0 <from-path> <to-path> [options] <output-file> <spin-db(true|false)> [db-port]"
38+
exit 1
39+
fi
40+
41+
if [ -z "$DB_PORT" ]; then
42+
DB_PORT=5432
43+
fi
44+
45+
echo "Arguments:"
46+
echo "FROM_PATH: $FROM_PATH"
47+
echo "TO_PATH: $TO_PATH"
48+
echo "D2D_OPTIONS: $D2D_OPTIONS"
49+
echo "OUTPUT_FILE: $OUTPUT_FILE"
50+
echo "SPIN_DB: $SPIN_DB"
51+
echo "DB_PORT: $DB_PORT"
52+
53+
DB_STARTED=false
54+
55+
if [ "$SPIN_DB" = true ]; then
56+
echo "Starting Postgres container on port $DB_PORT..."
57+
58+
docker run -d \
59+
--name scancodeio-run-db \
60+
-e POSTGRES_DB=scancodeio \
61+
-e POSTGRES_USER=scancodeio \
62+
-e POSTGRES_PASSWORD=scancodeio \
63+
-e POSTGRES_INITDB_ARGS="--encoding=UTF-8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8" \
64+
-v scancodeio_pgdata:/var/lib/postgresql/data \
65+
-p "${DB_PORT}:5432" \
66+
postgres:17 || {
67+
echo "Failed to start DB container. Cleaning up…"
68+
docker rm -f scancodeio-run-db >/dev/null 2>&1 || true
69+
exit 1
70+
}
71+
72+
DB_STARTED=true
73+
echo "DB container started"
74+
fi
75+
76+
WORKDIR="d2d"
77+
mkdir -p "$WORKDIR"
78+
79+
cp "$FROM_PATH" "$WORKDIR/"
80+
cp "$TO_PATH" "$WORKDIR/"
81+
82+
FROM_FILENAME=$(basename "$FROM_PATH")
83+
TO_FILENAME=$(basename "$TO_PATH")
84+
85+
echo "Running ScanCode.io mapping..."
86+
87+
docker run --rm \
88+
-v "$(pwd)/$WORKDIR":/code \
89+
--network host \
90+
-e SCANCODEIO_NO_AUTO_DB=1 \
91+
ghcr.io/aboutcode-org/scancode.io:latest \
92+
run map_deploy_to_develop:"$D2D_OPTIONS" \
93+
"/code/${FROM_FILENAME}:from,/code/${TO_FILENAME}:to" \
94+
> "$OUTPUT_FILE"
95+
96+
echo "Output saved to $OUTPUT_FILE"
97+
98+
99+
rm -rf "$WORKDIR"
100+
echo "Temporary directory cleaned up"
101+
102+
if [ "$DB_STARTED" = true ]; then
103+
echo "Stopping DB container..."
104+
docker rm -f scancodeio-run-db >/dev/null 2>&1 || true
105+
echo "DB container removed"
106+
fi
107+
108+
echo "Done!"

0 commit comments

Comments
 (0)