-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph-container-manage.sh
executable file
·174 lines (154 loc) · 3.55 KB
/
graph-container-manage.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
# quick script to manage/use the container
# place to store result (default path of graph_requires.py script)
DATA=/tmp/graph
function check_command() {
if ! command -v "$1" &> /dev/null; then
echo "Error: $1 is not installed or not in your PATH."
exit 1
fi
}
show_info() {
FILE=$1
echo "${FILE} is not readable"
echo "As root do:"
echo "setfacl -m u:${USER}:r ${FILE}"
}
check_readable() {
FILE=$1
if [ -r "${FILE}" ]; then
echo "${FILE} is readable"
elif [[ ! -f "${FILE}" ]]; then
echo "${FILE} is not present or readable by user"
show_info ${FILE}
exit 1
else
show_info ${FILE}
exit 1
fi
}
check_sles() {
# graphviz needs to be installed on current system
check_command dot
# Credentials is required to get access to all repositories on SLES
check_readable /etc/zypp/credentials.d/SCCcredentials
}
run_container() {
containerid=$1
PACKAGE=$2
mkdir ${DATA}
podman run \
--name graph \
--rm -ti \
-v /etc/zypp/credentials.d/SCCcredentials:/etc/zypp/credentials.d/SCCcredentials \
--volume ${DATA}:${DATA} \
${containerid} ${PACKAGE}
}
build_container() {
echo "1. openSUSE Leap 15.5"
echo "2. openSUSE Leap 15.6"
echo "3. openSUSE Tumbleweed"
echo "4. SUSE SLES 15SP5"
echo "5. SUSE SLES 15SP6"
echo
echo "Note: for SLES you need to have graphviz-gd installed on your system to render the dot file"
echo
read -p "Enter your OS choice [1-5]: " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter a number."
exit 1
fi
case $choice in
1)
OSREG="opensuse/leap:15.5"
PACKAGETOI="graphviz-gd"
OS="leap-15.5"
;;
2)
OSREG="opensuse/leap:15.6"
PACKAGETOI="graphviz-gd"
OS="leap-15.6"
;;
3)
OSREG="opensuse/tumbleweed"
PACKAGETOI="graphviz-gd"
OS="tumbleweed"
;;
4)
OSREG="bci/bci-base:15.5"
PACKAGETOI="suseconnect-ng"
OS="sles15-sp5"
check_sles
;;
5)
OSREG="bci/bci-base:15.6"
PACKAGETOI="suseconnect-ng"
OS="sles15-sp6"
check_sles
;;
*)
echo "Invalid choice. Please enter a correct number..."
exit 1
;;
esac
podman build \
--build-arg="OSREG=${OSREG}" \
--build-arg="PACKAGETOI=${PACKAGETOI}" \
--tag graph-${OS} .
}
info() {
cat <<EOF
#####################################################
DATA: '${DATA}'
#####################################################
EOF
echo "
First ARG is mandatory:
$0 [build|run|rmcache]
USAGE:
run
podman run container
build
build a local image of this container
rmcache
remove the container image in cache
"
}
###########
# MAIN
###########
#set -uxo pipefail
check_command podman
plop=$1
case ${plop} in
run)
podman images | grep graph
read -p "Enter the container ID: " containerid
read -p "Enter the package name (separated by comma): " PACKAGES
run_container ${containerid} ${PACKAGES}
# if this a SLES, jpg can not be generated, fixing this locally
OS=`podman images | awk -v id="${containerid}" 'NR>1 && $3==id { split($1, parts, "/"); repo_name = parts[length(parts)]; print repo_name; exit }'`
PACKAGESM=${PACKAGES//,/_}
max_length=64
if [ ${#PACKAGEM} -gt $max_length ]; then
PACKAGEM="${PACKAGEM:0:max_length}"
echo "Name truncated to 64 characters"
fi
if [ ! -e "${DATA}/${PACKAGESM}.jpg" ]; then
echo "Generating image ${DATA}/${PACKAGESM}_${OS}.jpg locally"
dot -Tjpg ${DATA}/${PACKAGESM}.dot -o ${DATA}/${PACKAGESM}_${OS}.jpg
else
echo "${DATA}/${PACKAGESM}_${OS}.jpg already exist.... exiting"
fi
;;
rmcache)
podman images | grep graph
echo " podman rmi -f IMAGE"
;;
build)
build_container
;;
*)
info
esac