-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.sh
executable file
·194 lines (160 loc) · 4.67 KB
/
setup.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
# The script helps to set up the app
set -e
set -o pipefail
COMP_DIR="${BASH_SOURCE%/*}"
cd "$COMP_DIR"
if [ -n "$(git status --porcelain)" ]; then
GIT_STATUS="(dirty)"
fi
about() {
echo "Criticality Score Setup tool"
echo "============================"
echo "Homepage: https://github.com/HUSTSeclab/criticality_score"
echo "Version :" "$(git rev-parse HEAD)" "$GIT_STATUS"
echo
}
help() {
echo "Usage: $0 [options...]"
echo "Options:"
echo " -h Show this help message and exit"
echo " -a <api_port> The port for the api server, default is 8081"
echo " -s <storage_dir> The directory to store the git repositories, default is ./data/git"
echo " -d <data_dir> The directory to store the data, default is ./data"
echo " -p <db_passwd> The password for the database, default is randomly generated"
echo " -w <web_port> The port for the web server, default is 8080"
echo " -b <db_port> The port for the database, default is 5432"
}
echo_red() {
echo -e "\033[31m$*\033[0m"
}
########## Init ##########
about
DB_HOST_PORT="5432"
WEB_HOST_PORT="8080"
APISERVER_HOST_PORT="8081"
STORAGE_DIR="./data/git"
while getopts "s:a:d:p:w:b:h" opt; do
case $opt in
a)
APISERVER_HOST_PORT="$OPTARG"
;;
d)
DATA_DIR="$OPTARG"
;;
p)
DB_PASSWD="$OPTARG"
;;
w)
WEB_HOST_PORT="$OPTARG"
;;
b)
DB_HOST_PORT="$OPTARG"
;;
s)
STORAGE_DIR="$OPTARG"
;;
h)
help
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
help
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ -z "$DATA_DIR" ]; then
DATA_DIR="./data"
fi
if [ -z "$DB_PASSWD" ]; then
DB_PASSWD=$(openssl rand -base64 12)
fi
if [ -f "$DATA_DIR"/DB_PASSWD ]; then
echo_red "Password file already exists, -p will be ignored"
DB_PASSWD=$(cat "$DATA_DIR"/DB_PASSWD)
else
mkdir -p "$DATA_DIR"
echo "$DB_PASSWD" >"$DATA_DIR"/DB_PASSWD
fi
########## Process ##########
if [ -f ".env" ]; then
echo_red "It seems that the app is already set up."
echo_red "If you want to upgrade, please run "
echo_red " docker compose build & docker compose up -d"
echo
echo -n "Do you want to continue setup again? [y/N] "
read -r answer
if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
exit 0
fi
fi
if [ -z "$GITHUB_TOKEN" ]; then
echo_red "It seems that you haven't set the GITHUB_TOKEN."
echo_red "enumerate_github will not work without it."
echo
echo -n "Do you want to ignore it and continue setup? [y/N] "
read -r answer
if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
exit 0
fi
fi
if [ -z "$GENTOO_PREFIX_DIR" ]; then
echo_red "It seems that you haven't set the GENTOO_PREFIX_DIR."
echo_red "enumerate_gentoo will not work without it."
echo
echo 'About how to set GENTOO_PREFIX_DIR, please refer to the `docs/setup/gentoo.md`.'
echo
echo -n "Do you want to ignore it and continue setup? [y/N] "
read -r answer
if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
exit 0
fi
GENTOO_PREFIX_DIR="$DATA_DIR/gentoo"
fi
# 1. Create dirs and files
echo "Setting up files..."
mkdir -p "$DATA_DIR/db" "$DATA_DIR/rec" "$DATA_DIR/config" "$DATA_DIR/git" "$DATA_DIR/log" "$DATA_DIR/gentoo"
cat <<EOF >"$DATA_DIR/config/config.json"
{
"database": "criticality_score",
"host": "db",
"user": "postgres",
"password": "$DB_PASSWD",
"port": "5432",
"GitHubToken": "$GITHUB_TOKEN"
}
EOF
cat <<EOF >".env"
DATA_DIR=$DATA_DIR
DB_HOST_PORT=$DB_HOST_PORT
DB_PASSWD=$DB_PASSWD
WEB_HOST_PORT=$WEB_HOST_PORT
APISERVER_HOST_PORT=$APISERVER_HOST_PORT
STORAGE_DIR=$STORAGE_DIR
GITHUB_TOKEN=$GITHUB_TOKEN
GENTOO_PREFIX_DIR=$GENTOO_PREFIX_DIR
EOF
# 2. Start docker compose
echo "Setting up app..."
docker compose build
docker compose up -d
# 3. Create database and tables
echo "Waiting for database to start..."
sleep 5
docker compose cp ./schema.sql db:/tmp/schema.sql
docker compose exec db psql -h localhost -U postgres -f /tmp/schema.sql
docker compose exec db rm /tmp/schema.sql
# 3. Run first time collector
echo "Running workflow for the first time..."
docker compose exec app /workflow/update.sh -C /data/rec package
echo_red "========== NOTICE =========="
echo_red "git link could only be updated manually."
echo_red "Try following steps to update git link:"
echo_red " 1. use home2git tool to find the git link"
echo_red " 2. update the git link in database, database password is $DB_PASSWD"
echo_red " 3. run 'docker compose exec app /workflow/update.sh gitlink'"
echo
echo "Done!"