util.sh 6.22 KB
Newer Older
Bernhard Landauer's avatar
Bernhard Landauer committed
1
2
#!/bin/bash

Bernhard Landauer's avatar
Bernhard Landauer committed
3
ARCH=$(uname -m)
4
START_DIR=${PWD}
Bernhard Landauer's avatar
Bernhard Landauer committed
5
6
CHROOT_DIR=/var/lib/chrootbuild
BUILD_DIR=${CHROOT_DIR}/build
Bernhard Landauer's avatar
Bernhard Landauer committed
7
PAC_CONF_TPL=/etc/chrootbuild/pacman.conf.${ARCH}
Bernhard Landauer's avatar
revert    
Bernhard Landauer committed
8
PAC_CONF=${CHROOT_DIR}/etc/pacman.conf
9
[[ $EUID = 0 ]] && USER_HOME=/home/${SUDO_USER} || USER_HOME=$HOME
Bernhard Landauer's avatar
Bernhard Landauer committed
10
11
12
BUILDUSER_UID="${SUDO_UID:-$UID}"
BUILDUSER_GID="$(id -g "${BUILDUSER_UID}")"
RM_PKGS=false
Bernhard Landauer's avatar
Bernhard Landauer committed
13
CLEAN=false
Bernhard Landauer's avatar
Bernhard Landauer committed
14
PUSH_GIT=false
Bernhard Landauer's avatar
Bernhard Landauer committed
15
INSTALL=false
16
UPDATE=false
17
SIGN=false
Bernhard Landauer's avatar
Bernhard Landauer committed
18
MULTILIB=false
Bernhard Landauer's avatar
Bernhard Landauer committed
19
MIRROR='https://repo.manjaro.org/repo'
Bernhard Landauer's avatar
Bernhard Landauer committed
20
MIRROR_CONF=etc/pacman-mirrors.conf
21
COLORS=true
Bernhard Landauer's avatar
Bernhard Landauer committed
22
mirror_conf=${CHROOT_DIR}/${MIRROR_CONF}
Bernhard Landauer's avatar
Bernhard Landauer committed
23
24
MP_CONF_GLOB='/etc/makepkg.conf'
MP_CONF_USER="${USER_HOME}/.makepkg.conf"
Bernhard Landauer's avatar
Bernhard Landauer committed
25
26
27
install_pkgs=()
lists=()
pkgs=()
28
custom_repo=
29
check=none
Bernhard Landauer's avatar
Bernhard Landauer committed
30
shopt -s dotglob
Bernhard Landauer's avatar
Bernhard Landauer committed
31

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
enable_colors() {
    if tput setaf 0 &>/dev/null; then
        ALL_OFF="$(tput sgr0)"
        BOLD="$(tput bold)"
        RED="${BOLD}$(tput setaf 1)"
        GREEN="${BOLD}$(tput setaf 2)"
        YELLOW="${BOLD}$(tput setaf 3)"
        BLUE="${BOLD}$(tput setaf 4)"
    else
        ALL_OFF="\e[0m"
        BOLD="\e[1m"
        RED="${BOLD}\e[31m"
        GREEN="${BOLD}\e[32m"
        YELLOW="${BOLD}\e[33m"
        BLUE="${BOLD}\e[34m"
    fi
}
Bernhard Landauer's avatar
Bernhard Landauer committed
49

Bernhard Landauer's avatar
Bernhard Landauer committed
50
51
header() {
    local mesg=$1; shift
Bernhard Landauer's avatar
Bernhard Landauer committed
52
    printf "${YELLOW}${BOLD}  >>  ${mesg}${ALL_OFF}\n" "$@" >&2
Bernhard Landauer's avatar
Bernhard Landauer committed
53
54
}

Bernhard Landauer's avatar
Bernhard Landauer committed
55
56
msg() {
    local mesg=$1; shift
Bernhard Landauer's avatar
Bernhard Landauer committed
57
    printf "\n${GREEN}${BOLD}:: ${mesg}${ALL_OFF}\n" "$@" >&2
Bernhard Landauer's avatar
Bernhard Landauer committed
58
59
60
61
62
63
64
65
66
}

msg2() {
    local mesg=$1; shift
    printf "      ${mesg}${ALL_OFF}\n" "$@" >&2
}

msg3() {
    local mesg=$1; shift
67
    printf "\n${GREEN}      ${mesg}${ALL_OFF}\n\n" "$@" >&2
Bernhard Landauer's avatar
Bernhard Landauer committed
68
69
70
71
72
73
74
75
76
77
78
79
}

msg4() {
    local mesg=$1; shift
    printf "${BOLD}      ${mesg}${ALL_OFF}\n\n" "$@" >&2
}

msg5() {
    local mesg=$1; shift
    printf "\n${BOLD}      ${mesg}${ALL_OFF}\n\n" "$@" >&2
}

80
81
82
83
84
msg6() {
    local mesg=$1; shift
    printf "\r      ${mesg}${ALL_OFF}" "$@" >&2
}

Bernhard Landauer's avatar
Bernhard Landauer committed
85
err() {
Bernhard Landauer's avatar
Bernhard Landauer committed
86
87
    local mesg=$1; shift
    printf "${RED}==> ERROR:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
Bernhard Landauer's avatar
Bernhard Landauer committed
88
89
}

90
91
err_choice() {
    local mesg=$1; shift
Bernhard Landauer's avatar
Bernhard Landauer committed
92
    printf "\n${RED}:: ERROR:${ALL_OFF}${BOLD} ${mesg} ${ALL_OFF}" "$@" >&2
Bernhard Landauer's avatar
Bernhard Landauer committed
93
94
95
96
}

err_build() {
    printf "${RED}${BOLD}      Errors have occurred.${ALL_OFF} Check the log!\n\n"
97
98
}

Bernhard Landauer's avatar
Bernhard Landauer committed
99
100
101
102
103
104
check_root() {
    if [ "$EUID" -ne 0 ]; then
        err "This application needs to be run as root."
        exit 1
    fi
}
Bernhard Landauer's avatar
Bernhard Landauer committed
105

Bernhard Landauer's avatar
Bernhard Landauer committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
query_conf() {
    echo "$(grep "^$1" "$2" | tail -1 | cut -d= -f2)"
}

get_mp_conf() {
    [[ -f ${MP_CONF_USER} ]] && CONF=$(query_conf $1 ${MP_CONF_USER})
    [[ -z ${CONF} ]] && CONF=$(query_conf $1 ${MP_CONF_GLOB})
    echo ${CONF//\"/}
}

get_config() {
    echo $(get_mp_conf $1)
}

Bernhard Landauer's avatar
Bernhard Landauer committed
120
121
122
123
cleanup() {
    mesg=${1:-"Cleaning up."}
    msg4 "$mesg"
    umount -l ${CHROOT_DIR} 2>/dev/null
Bernhard Landauer's avatar
Bernhard Landauer committed
124
    for f in ${CHROOT_DIR}/.{mount,lock} "${START_DIR}/*.list.work" $mon $mon_wait; do
Bernhard Landauer's avatar
Bernhard Landauer committed
125
126
        [[ -e $f ]] && rm $f
    done
Bernhard Landauer's avatar
Bernhard Landauer committed
127
    return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
128
129
    }

Bernhard Landauer's avatar
Bernhard Landauer committed
130
abort() {
Bernhard Landauer's avatar
Bernhard Landauer committed
131
132
133
    err "$1"
    cleanup
    exit 1
Bernhard Landauer's avatar
Bernhard Landauer committed
134
135
}

136
137
138
139
140
141
142
143
144
job() {
    local func=$1
    shift
    arr=("$@")
    for i in ${arr[@]}; do
        $func $i
    done
}

Bernhard Landauer's avatar
Bernhard Landauer committed
145
check_sanity() {
146
    if [ $check = list ]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
147
148
149
150
151
152
153
154
        if [ ! -f $1.list ]; then
            abort "Could not find buildlist [$1.list]. Aborting."
        elif [ ! -d $1 ]; then
            abort "Could not find directory [$1]. Aborting."
        fi
    elif [ ! -f $1/PKGBUILD ]; then
        abort "Could not find PKGBUILD for [$1]. Aborting."
    fi
Bernhard Landauer's avatar
Bernhard Landauer committed
155
}
Bernhard Landauer's avatar
Bernhard Landauer committed
156

157
158
159
160
161
162
163
164
prepare_lists() {
    check=list
    job check_sanity "${lists[@]}"
    . ${LIBDIR}/util-lists.sh
    msg_wait
    prepare_log
    #ssh_add
    msg "List(s) to build:"
Bernhard Landauer's avatar
Bernhard Landauer committed
165
    printf " - %s\n" "${lists[@]//\//}"
Bernhard Landauer's avatar
Bernhard Landauer committed
166
    printf "\n$(date -u +"%y/%m/%d %R:%S %Z"):\nBUILDING LISTS\n" >> $log
Bernhard Landauer's avatar
Bernhard Landauer committed
167
    printf " - %s\n" "${lists[@]//\//}" >> $log
Bernhard Landauer's avatar
Bernhard Landauer committed
168
    echo "" >> $log
169
170
171
172
173
174
175
}

prepare_pkgs() {
    check=package
    job check_sanity "${pkgs[@]}"
    msg "Package(s) to build:"
    printf "   - %s\n" "${pkgs[@]//\//}"
176
}
Bernhard Landauer's avatar
Bernhard Landauer committed
177

178
user_own() {
179
    chown -R ${BUILDUSER_UID}:${BUILDUSER_GID} "$@"
180
181
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
start_agent(){
    echo "Initializing SSH agent..."
    ssh-agent | sed 's/^echo/#echo/' > "$1"
    chmod 600 "$1"
    . "$1" > /dev/null
    ssh-add
}

ssh_add(){
    local ssh_env="$USER_HOME/.ssh/environment"

    if [ -f "${ssh_env}" ]; then
         . "${ssh_env}" > /dev/null
         ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
            start_agent ${ssh_env};
        }
    else
        start_agent ${ssh_env};
    fi
}

203
usage_chrootbuild() {
Bernhard Landauer's avatar
Bernhard Landauer committed
204
    echo ''
Bernhard Landauer's avatar
Bernhard Landauer committed
205
    echo "Usage: ${0##*/} [options]"
Bernhard Landauer's avatar
Bernhard Landauer committed
206
    echo ''
Bernhard Landauer's avatar
Bernhard Landauer committed
207
208
209
    echo '     -b <branch> Branch to use:'
    echo '                 (unstable/testing/stable-staging/stable;'
    echo '                 arm-unstable/arm-testing/arm-stable)'
Bernhard Landauer's avatar
Bernhard Landauer committed
210
    echo '                 default: unstable / arm-unstable'
Bernhard Landauer's avatar
Bernhard Landauer committed
211
    echo '     -c          Start with clean chroot fs'
212
    echo '     -d          Disable colors.'
Bernhard Landauer's avatar
Bernhard Landauer committed
213
    echo '     -g          Push changes to git when building lists'
Bernhard Landauer's avatar
Bernhard Landauer committed
214
    echo '     -h          This help'
215
216
    echo '     -i <pkg>    Install package(s) to chroot fs'
    echo '                 (for multiple packages repeat -i flag)'
217
218
    echo '     -k <repo>   Use custom repo:'
    echo '                 (mobile/kde-unstable)'
219
220
    echo '     -l <list>   List(s) to build'
    echo '                 (for multiple lists repeat -l flag)'
Bernhard Landauer's avatar
Bernhard Landauer committed
221
    echo '     -n          Install built pkg to chroot fs'
222
223
    echo '     -p <pkg>    Package(s) to build'
    echo '                 (for multiple packages repeat -p flag)'
Bernhard Landauer's avatar
Bernhard Landauer committed
224
225
    echo '     -r          custom chrootdir path'
    echo '                 default: /var/lib/chrootbuild'
226
    echo '     -s          Sign package(s)'
227
    echo '     -u          Build pkgs only if update available (lists only)'
Bernhard Landauer's avatar
Bernhard Landauer committed
228
    echo '     -x          Remove previously built packages in $PKGDEST'
Bernhard Landauer's avatar
Bernhard Landauer committed
229
230
    echo ''
    exit $1
231
}
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

usage_prepare_chroot() {
    echo ''
    echo "Usage: ${0##*/} [options]"
    echo ''
    echo '     -b <branch> Branch to use:'
    echo '                 (unstable/testing/stable-staging/stable;'
    echo '                 arm-unstable/arm-testing/arm-stable)'
    echo '                 default: unstable / arm-unstable'
    echo '     -c          Create clean chroot filesystem'
    echo '     -h          This help'
    echo '     -k <repo>   Use custom repo:'
    echo '                 (mobile/kde-unstable)'
    echo '     -m          Keep Chroot filesystem mounted'
    echo '     -u          Unmount Chroot filesystem cleanly'
    echo ''
    exit $1
}