-
Notifications
You must be signed in to change notification settings - Fork 6
/
netbox_generate_client
executable file
·104 lines (84 loc) · 3.39 KB
/
netbox_generate_client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
set -eu
PROJECT_ROOT=${PROJECT_ROOT:-"$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel/go-netbox"}
get_last_netbox_version() {
version=$1
tags=""
tags_list=""
NEXT=""
max=0
i=0
while [ "$NEXT" != "null" ]
do
i=$((i+1))
JSON=$(curl https://registry.hub.docker.com/v2/repositories/netboxcommunity/netbox/tags/?page=$i\&page_size=1000 2>/dev/null)
tags="$tags $(echo $JSON | jq -r '."results"[]["name"]' | grep $version | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+-[0-9]+\.[0-9]+\.[0-9]+$")"
NEXT=$(echo $JSON | jq '."next"')
done
for t in $tags; do
MAJOR=$(echo $t | cut -d"-" -f1 | cut -d"." -f3)
if [ "$MAJOR" != "" -a $MAJOR -gt $max ]; then
max=$MAJOR
docker="$(echo $t | cut -d"-" -f2)"
fi
done
echo "$version.$max-$docker"
}
NETBOX_MAJOR_VERSION=$(cat netbox_major_version)
echo "NETBOX_MAJOR_VERSION=${NETBOX_MAJOR_VERSION}"
DOCKER_RESULT="$(get_last_netbox_version ${NETBOX_MAJOR_VERSION})"
LAST_NETBOX_VERSION="$(echo ${DOCKER_RESULT} | cut -d"-" -f1)"
echo "LAST_NETBOX_VERSION=${LAST_NETBOX_VERSION}"
DOCKER_VERSION="$(echo ${DOCKER_RESULT} | cut -d"-" -f2)"
echo "DOCKER_VERSION=${DOCKER_VERSION}"
export VERSION=${LAST_NETBOX_VERSION}
echo "Purge old lib generated ..."
rm -rf "${PROJECT_ROOT}/netbox" && mkdir "${PROJECT_ROOT}/netbox" && touch "${PROJECT_ROOT}/netbox/.gitkeep"
if [ ! -f "${PROJECT_ROOT}/swagger/swagger-${VERSION}.json" ]; then
echo "Get github project netbox-docker ..."
rm -rf netbox-docker
git clone https://github.com/netbox-community/netbox-docker.git -q
cd netbox-docker
git config advice.detachedHead false
git checkout ${DOCKER_VERSION}
mv docker-compose.override.yml.example docker-compose.override.yml
sed -i 's/netbox-media-files/gonetbox-media-files/g' docker-compose.yml
sed -i 's/netbox-postgres-data/gonetbox-postgres-data/g' docker-compose.yml
sed -i 's/netbox-redis-data/gonetbox-redis-data/g' docker-compose.yml
sed -i 's/netbox-redis-cache-data/gonetbox-redis-cache-data/g' docker-compose.yml
sed -i 's/start_period: 60s/start_period: 600s/g' docker-compose.yml
docker compose down -v
docker compose up -d --quiet-pull
cd ..
echo "Get swagger from netbox docker ..."
while ! curl -s http://127.0.0.1:8000/api/swagger.json -o swagger.json 2> /dev/null; do sleep 1 && echo "Waiting docker to be up..."; done
jsonlint -S -qf --indent 2 -o "${PROJECT_ROOT}/netbox/swagger-${VERSION}.json" swagger.json
cp "${PROJECT_ROOT}/netbox/swagger-${VERSION}.json" "${PROJECT_ROOT}/swagger/swagger-${VERSION}.json"
else
echo "Get swagger from archive directory ..."
cp "${PROJECT_ROOT}/swagger/swagger-${VERSION}.json" "${PROJECT_ROOT}/netbox/swagger-${VERSION}.json"
fi
echo "Patch swagger definition ..."
cd ../patchs
find . -type f -name "swagger-${VERSION}-*.patch" -exec echo "Using path {}" \; -exec patch -u ../netbox/swagger-${VERSION}.json -i {} \;
cd ../utils
echo "Apply hack for int64 ..."
sed -i 's/922337203685477580/214748364/g' "${PROJECT_ROOT}/netbox/swagger-${VERSION}.json"
echo "Check swagger definition ..."
jsonlint "${PROJECT_ROOT}/netbox/swagger-${VERSION}.json"
./generate_code
if [ -d netbox-docker ]; then
echo "Destroy docker containers ..."
cd netbox-docker
docker compose down -v
cd ..
fi
echo "Execute go mod tidy ... "
cd ..
go mod tidy
cd utils
echo "Cleaning ..."
rm -rf netbox-docker
rm -f swagger
rm -f swagger.json
rm -f ../netbox/swagger-${VERSION}.json.*