Repository navigation
Expand file tree
/
Copy pathdata.functions
More file actions
132 lines (124 loc) · 4.8 KB
/
Copy pathdata.functions
File metadata and controls
132 lines (124 loc) · 4.8 KB
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
#!/bin/bash
check_core_functions
function create_key_sscertificate () {
### create_key_sscertificate: a key and a self-signed certificate in the data of the project,
### named PKI_KEY_NAME with the subject PKI_SUBJECT (bbxb.conf), kept once created
### openssl is the one of lfs/openssl:native (setup_full_toolchain)
local PKI_KEY_NAME=${PKI_KEY_NAME:-$(whoami)}
local PKI_SUBJECT=${PKI_SUBJECT:-/CN=$(whoami)/emailAddress=$(whoami)@$(uname -n)}
if [ ! -d "${USERDATA_PATH}/pki" ]
then
run_cmd "mkdir -p \"${USERDATA_PATH}/pki\""
fi
true > "${LOG_PATH}/pki.log"
if [ ! -f "${USERDATA_PATH}/pki/${PKI_KEY_NAME}.key.pem" ] && [ ! -f "${USERDATA_PATH}/pki/${PKI_KEY_NAME}.x509.pem" ]
then
run_cmd "${GLOBAL_TOOLCHAIN_PATH}/bin/openssl req -newkey rsa:2048 -nodes -keyout \"${USERDATA_PATH}/pki/${PKI_KEY_NAME}.key.pem\" -x509 -days 1461 -subj \"${PKI_SUBJECT}\" -out \"${USERDATA_PATH}/pki/${PKI_KEY_NAME}.x509.pem\"" >> "${LOG_PATH}/pki.log" 2>&1
fi
}
### generate_ssh_keys is the ssh identity of a system the framework builds: it lives in
### osconfig.functions, next to the other directives that configure the target
function data_purge () {
### data_purge [--yes] [<project> [<platform>]]: empty the data directory (DATA_PATH) for a build
### from scratch. Without a project everything but the downloads, the cache of sccache and the
### data of every project (<project>/data: the key of create_key_sscertificate, what the user put
### there): the global toolchain goes too, which every project and platform share; with a project
### its platforms and its sources, not its data; with a platform that platform only, the way the
### README restarts a platform. It asks first (--yes does not) and refuses while an image is
### mounted under what it removes or a build runs in a container of bbxb. The files the root steps
### of a build left (the sysroot, the images) go through sudo.
local ASSUME_YES PROJECT PLATFORM TARGETS ENTRY SUB MOUNTED ANSWER
OPTS="--yes" OPTS_WITH_VALUE="" param2value "${@}"
ASSUME_YES=${_PARAM[yes]:-0}
PROJECT=${_PARAM[1]}
PLATFORM=${_PARAM[2]}
if [ -n "${_PARAM[3]}" ]
then
trow_error "${ERROR_NOT_VALID_OPTION}" "Too many arguments: bbxb purge [--yes] [<project> [<platform>]]." || return
fi
if [[ ${PROJECT} == @(*/*|.*) ]] || [[ ${PLATFORM} == @(*/*|.*) ]]
then
trow_error "${ERROR_NOT_VALID_OPTION}" "Not a project or platform name: ${PROJECT}${PLATFORM:+ ${PLATFORM}}." || return
fi
if [ -z "${DATA_PATH}" ] || [ "$(realpath -m "${DATA_PATH}")" == / ]
then
trow_error "${ERROR_NOT_VALID_OPTION}" "DATA_PATH is empty or the root directory." || return
fi
TARGETS=()
if [ -z "${PROJECT}" ]
then
while IFS= read -r -d '' ENTRY
do
case ${ENTRY##*/} in
"${DOWNLOAD_DIR:-downloads}"|"${CACHE_DIR:-cache}") continue ;;
esac
if [ -d "${ENTRY}/${USERDATA_DIR:-data}" ]
then
while IFS= read -r -d '' SUB
do
TARGETS+=( "${SUB}" )
done < <(find "${ENTRY}" -mindepth 1 -maxdepth 1 ! -name "${USERDATA_DIR:-data}" -print0 | sort -z)
else
TARGETS+=( "${ENTRY}" )
fi
done < <(find "${DATA_PATH}" -mindepth 1 -maxdepth 1 -print0 2> /dev/null | sort -z)
elif [ -z "${PLATFORM}" ]
then
while IFS= read -r -d '' SUB
do
TARGETS+=( "${SUB}" )
done < <(find "${DATA_PATH}/${PROJECT}" -mindepth 1 -maxdepth 1 ! -name "${USERDATA_DIR:-data}" -print0 2> /dev/null | sort -z)
elif [ -e "${DATA_PATH}/${PROJECT}/${PLATFORM}" ]
then
TARGETS=( "${DATA_PATH}/${PROJECT}/${PLATFORM}" )
fi
if [ ${#TARGETS[@]} -eq 0 ]
then
echo "Nothing to remove."
return 0
fi
### A mounted image or a loop device of a file under what goes: a build or bbxb mount holds it
MOUNTED=$( { findmnt -rn -o TARGET 2> /dev/null; losetup -n -O BACK-FILE 2> /dev/null; } | while read -r ENTRY
do
for SUB in "${TARGETS[@]}"
do
if [ "${ENTRY}" == "${SUB}" ] || [[ ${ENTRY} == "${SUB}"/* ]]
then
echo " ${ENTRY}"
fi
done
done | sort -u )
if [ -n "${MOUNTED}" ]
then
echo "${MOUNTED}"
trow_error "${ERROR_GENERIC}" "Images are mounted there: bbxb mount list, bbxb umount." || return
fi
if docker version > /dev/null 2>&1 && [ -n "$(container_running)" ]
then
container_running | cut -f1,3,4 | tr '\t' ' '
trow_error "${ERROR_GENERIC}" "Builds are running: stop them first (bbxb container stop)." || return
fi
echo "To remove:"
for ENTRY in "${TARGETS[@]}"
do
echo " ${ENTRY} ($(du -sh "${ENTRY}" 2> /dev/null | cut -f1))"
done
if [ "${ASSUME_YES}" -ne 1 ]
then
if [ ! -t 0 ]
then
trow_error "${ERROR_NOT_VALID_OPTION}" "No terminal to confirm on: bbxb purge --yes." || return
fi
read -r -p "Remove them? [y/N] " ANSWER
if [[ ${ANSWER} != [yY]* ]]
then
echo "Nothing removed."
return 0
fi
fi
for ENTRY in "${TARGETS[@]}"
do
run_cmd "rm -rf \"${ENTRY}\" 2> /dev/null" || run_cmd -s "rm -rf \"${ENTRY}\"" || return
done
echo "Purged."
}