epm-addrepo 10.3 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2017, 2019  Etersoft
# Copyright (C) 2012, 2017, 2019  Vitaly Lipatov <lav@etersoft.ru>
5
#
6 7 8
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
9 10 11 12 13
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16 17
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18 19
#

20 21
load_helper epm-sh-altlinux

22 23 24
ETERSOFTPUBURL=http://download.etersoft.ru/pub
ALTLINUXPUBURL=http://ftp.altlinux.org/pub/distributions

25 26
__epm_addrepo_rhel()
{
27
	local repo="$*"
28 29 30 31
	if [ -z "$repo" ] ; then
		echo "Add repo."
		echo "1. Use with repository URL, f.i. http://www.example.com/example.repo"
		echo "2. Use with epel to add EPEL repository"
32
		echo "3. Use with powertools to add PowerTools repository"
33
		echo "4. Use with crb to add Rocky Linux CRB repository"
34 35 36 37
		return 1
	fi
	case "$1" in
		epel)
38
			# dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
39 40 41
			epm install epel-release
			return 1
			;;
42 43 44 45 46 47
		powertools)
			# https://serverfault.com/questions/997896/how-to-enable-powertools-repository-in-centos-8
			epm install --skip-installed dnf-plugins-core
			sudocmd dnf config-manager --set-enabled powertools
			return 1
			;;
48 49 50 51 52 53
		crb)
			# https://wiki.rockylinux.org/rocky/repo/
			epm install --skip-installed dnf-plugins-core
			sudocmd dnf config-manager --set-enabled crb
			return 1
			;;
54 55 56 57
	esac
	return 0
}

58 59 60 61 62 63 64 65 66 67 68
__epm_addrepo_etersoft_addon()
{
	epm install --skip-installed apt-conf-etersoft-common apt-conf-etersoft-hold || fatal
	# TODO: ignore only error code 22 (skipped) || fatal

	local pb="$DISTRVERSION/branch"
	[ "$DISTRVERSION" = "Sisyphus" ] && pb="$DISTRVERSION"

	# FIXME
	[ -n "$DISTRVERSION" ] || fatal "Empty DISTRVERSION"

69 70
	docmd epm repo add "rpm [etersoft] $ETERSOFTPUBURL/Etersoft LINUX@Etersoft/$pb/noarch addon"
	docmd epm repo add "rpm [etersoft] $ETERSOFTPUBURL/Etersoft LINUX@Etersoft/$pb/$DISTRARCH addon"
71
	if [ "$DISTRARCH" = "x86_64" ] ; then
72
		docmd epm repo add "rpm [etersoft] $ETERSOFTPUBURL/Etersoft LINUX@Etersoft/$pb/x86_64-i586 addon"
73 74 75
	fi
}

76 77 78 79 80 81 82 83 84 85 86
get_archlist()
{
    echo "noarch"
    echo "$DISTRARCH"
    case $DISTRARCH in
        x86_64)
            echo "i586"
            ;;
    esac
}

87 88 89 90 91 92 93 94 95 96 97 98 99
# 'rpm protocol:/path/to/repo component'
__epm_addrepo_altlinux_short()
{
	[ -n "$1" ] || fatal "only for rpm repo"
	local url="$2"
	local REPO_NAME="$3"
	local arch

	arch="$(basename "$url")"
	url="$(dirname "$url")"
	docmd epm repo add "rpm $url $arch $REPO_NAME"
}

100

101 102 103 104 105 106 107 108 109 110
__epm_addrepo_altlinux_url()
{
	local url="$1"
	local arch
	local base

	# URL to path/RPMS.addon
	base="$(basename "$url")"
	if echo "$base" | grep -q "^RPMS\." ; then
		REPO_NAME="$(echo $base | sed -e 's|.*\.||')"
111 112
		url="$(dirname $url)"
		docmd epm repo add "rpm $url $REPO_NAME"
113 114 115
		return
	fi

116
	# TODO: add to eget file:/ support and use here
117 118 119 120 121
	# URL to path (where RPMS.addon is exists)
	local baseurl="$(eget --list "$url/RPMS.*")"
	base="$(basename "$baseurl")"
	if echo "$base" | grep -q "^RPMS\." ; then
		REPO_NAME="$(echo $base | sed -e 's|.*\.||')"
122
		docmd epm repo add "rpm $url $REPO_NAME"
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		return
	fi

	# URL to {i586,x86_64,noarch}/RPMS.addon
	local res=''
	for arch in $(get_archlist) ; do
		local rd="$(eget --list $url/$arch/RPMS.*)"
		[ -d "$rd" ] || continue
		local REPO_NAME="$(echo "$rd" | sed -e 's|.*\.||')"
		docmd epm repo add "rpm $url $arch $REPO_NAME"
		res='1'
	done
	[ -n "$res" ] || warning "There is no arch repos in $url"
}

138

139
__epm_addrepo_altlinux()
140
{
141
	local repo="$*"
142 143
	local branch="$(echo "$DISTRVERSION" | tr "[:upper:]" "[:lower:]")"
	[ -n "$branch" ] || fatal "Empty DISTRVERSION"
144 145 146 147 148 149 150 151 152 153

	if [ -z "$repo" ] || [ "$repo" == "--help" ] ; then
		info "Add branch repo. Use follow params:"
		sudocmd apt-repo $dryrun add branch
		echo "etersoft           - for LINUX@Etersoft repo"
		echo "basealt            - for BaseALT repo"
		echo "yandex             - for BaseALT repo mirror on yandex (recommended)"
		echo "<task number>      - add task repo"
		echo "archive 2018/02/09 - for archive from that date"
		echo "autoimports        - for BaseALT autoimports repo"
154 155 156 157
		echo "/dir/to/repo       - add repo dir generated with epm repo index --init"
		return
	fi

158 159 160 161 162 163 164
	# 'rpm protocol:/path/to/repo component'
	if [ "$1" = "rpm" ] && [ -n "$2" ] && [ -n "$3" ] && [ -z "$4" ] ; then
		__epm_addrepo_altlinux_short "$@"
		return
	fi

	# /path/to/repo
165
	if [ -d "$1" ] ; then
166
		__epm_addrepo_altlinux_url "file:$1"
167 168 169
		return
	fi

170
	# file:/path/to/repo or http://path/to/repo
171 172 173 174 175
	if is_url "$1" ; then
		__epm_addrepo_altlinux_url "$1"
		return
	fi

176 177 178
	case "$1" in
		etersoft)
			info "add Etersoft's addon repo"
179 180
			assure_exists apt-repo
			__epm_addrepo_etersoft_addon
181
			sudocmd apt-repo add $branch
182 183 184
			epm repofix etersoft
			return 0
			;;
185
		basealt|ALTLinux|ALTServer)
186 187
			# TODO: setrepo?
			assure_exists apt-repo
188
			sudocmd apt-repo add $branch
189 190 191 192
			return 0
			;;
		yandex)
			assure_exists apt-repo
193
			sudocmd apt-repo add $branch
194
			epm repofix yandex
195 196 197
			return 0
			;;
		autoimports)
198
			repo="autoimports.$branch"
199 200
			;;
		archive)
201
			datestr="$2"
202
			echo "$datestr" | grep -Eq "^20[0-2][0-9]/[01][0-9]/[0-3][0-9]$" || fatal "use follow date format: 2017/12/31"
203

204
			echo "" | sudocmd tee -a /etc/apt/sources.list
205 206 207
			local distrversion="$(echo "$DISTRVERSION" | tr "[:upper:]" "[:lower:]")"
			local rpmsign='[alt]'
			[ "$distrversion" != "sisyphus" ] && rpmsign="[$distrversion]"
208 209 210
			echo "rpm $rpmsign $ALTLINUXPUBURL archive/$distrversion/date/$datestr/$DISTRARCH classic" | sudocmd tee -a /etc/apt/sources.list
			if [ "$DISTRARCH" = "x86_64" ] ; then
				echo "rpm $rpmsign $ALTLINUXPUBURL archive/$distrversion/date/$datestr/x86_64-i586 classic" | sudocmd tee -a /etc/apt/sources.list
211
			fi
212
			echo "rpm $rpmsign $ALTLINUXPUBURL archive/$distrversion/date/$datestr/noarch classic" | sudocmd tee -a /etc/apt/sources.list
213
			return 0
214 215
			;;
	esac
216

217
	assure_exists apt-repo
218

219
	if tasknumber "$repo" >/dev/null ; then
220
		sudocmd_foreach "apt-repo $dryrun add" $(tasknumber "$repo")
221 222 223
		return
	fi

224 225 226 227
	sudocmd apt-repo $dryrun add "$repo"

}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
__epm_addkey_altlinux()
{
    local url="$1"
    local fingerprint="$2"
    local comment="$3"

    local name="$(basename "$url" .gpg)"

    [ -s /etc/apt/vendors.list.d/$name.list ] && return

# TODO: get this info from the gpg key
    cat << EOF | sudorun tee /etc/apt/vendors.list.d/$name.list
simple-key "$name" {
        FingerPrint "$fingerprint";
        Name "$comment";
}
EOF
        eget -q -O /tmp/$name.gpg $url || fatal
        sudorun gpg --no-default-keyring --keyring /usr/lib/alt-gpgkeys/pubring.gpg --import /tmp/$name.gpg
        rm -f /tmp/$name.gpg
}

250 251 252 253
__epm_addkey_deb()
{
    local url="$1"
    local fingerprint="$2"
254 255 256 257 258 259
    local comment="$3"

    local name="$(basename $url .gpg)"

    [ -s /etc/apt/trusted.gpg.d/$name.gpg ] && return

260
    if [ -z "$fingerprint" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
261
        set_sudo
262 263 264 265 266 267 268
        eget -q -O- "$url" | sudorun apt-key add -
#
#        if [ ! -f /etc/apt/trusted.gpg.d/$name.gpg ]; then
#                epm tool eget -q -O /etc/apt/trusted.gpg.d/$name.gpg https://example.com/$name.gpg > /dev/null
#                chmod 0644 /etc/apt/trusted.gpg.d/$name.gpg
#        fi

269 270 271 272 273
        return
    fi
    sudocmd apt-key adv --keyserver "$url" --recv "$fingerprint"
}

274 275 276
epm_addkey()
{

277 278
case $BASEDISTRNAME in
	"alt")
279 280 281 282 283 284 285 286 287 288 289 290 291
		__epm_addkey_altlinux "$@"
		return
		;;
esac

case $PMTYPE in
	apt-dpkg)
		__epm_addkey_deb "$@"
		;;
esac

}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
__epm_addrepo_deb()
{
	assure_exists apt-add-repository software-properties-common
	local ad="$($DISTRVENDOR --distro-arch)"
	# TODO: move to distro_info
	local nd="$(lsb_release -cs)"
	local repo="$*"

	if [ -z "$repo" ] || [ "$repo" = "--help" ]; then
		info "Add repo. You can use follow params:"
		echo "  docker - add official docker repo"
		echo "  ppa:<user>/<ppa-name> - add PPA repo"
		echo "  distribution component name"
		echo "  full sources list line"
		echo "  URL version component"
307 308
		return
	fi
309

310
	if [ "$DISTRNAME" = "AstraLinuxCE" ] || [ "$DISTRNAME" = "AstraLinuxSE" ] ; then
311 312 313 314
		echo "Use workaround for AstraLinux"
		# aptsources.distro.NoDistroTemplateException: Error: could not find a distribution template for AstraLinuxCE/orel
		echo "" | sudocmd tee -a /etc/apt/sources.list
		echo "$repo" | sudocmd tee -a /etc/apt/sources.list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
315
		return
316 317 318
	fi


319 320 321
	# keywords
	case "$1" in
		docker)
322
			__epm_addkey_deb https://download.docker.com/linux/$PKGVENDOR/gpg "9DC858229FC7DD38854AE2D88D81803C0EBFCD88"
323 324 325 326 327
			repo="https://download.docker.com/linux/$PKGVENDOR $nd stable"
			;;
	esac

	# if started from url, use heroistic
Vitaly Lipatov's avatar
Vitaly Lipatov committed
328
	if echo "$repo" | grep -E -q "^https?://" ; then
329 330 331 332 333 334 335
		repo="deb [arch=$ad] $repo"
	fi

	if echo "$repo" | grep -q "https://" ; then
		assure_exists /usr/share/doc/apt-transport-https apt-transport-https
		assure_exists /usr/sbin/update-ca-certificates ca-certificates 
	fi
336

337 338 339 340 341
	if [ -d "$repo" ] ; then
		sudocmd epm repo add "deb file:$repo ./"
		return
	fi

342 343
	# FIXME: quotes in showcmd/sudocmd
	showcmd apt-add-repository "$repo"
344
	sudorun apt-add-repository "$repo"
345
	info "Check file /etc/apt/sources.list if needed"
346
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
347

348 349
epm_addrepo()
{
350
local repo="$*"
351

352 353
case $BASEDISTRNAME in
	"alt")
354
		# Note! Don't use quotes here
355
		__epm_addrepo_altlinux $repo
356
		return
357
		;;
358 359 360
esac

case $PMTYPE in
361
	apt-dpkg)
362 363
		# Note! Don't use quotes here
		__epm_addrepo_deb $repo
364 365
		;;
	aptitude-dpkg)
366
		info "You need manually add repo to /etc/apt/sources.list (TODO)"
367 368
		;;
	yum-rpm)
369
		assure_exists yum-utils
370
		__epm_addrepo_rhel "$repo" || return
371
		sudocmd yum-config-manager --add-repo "$repo"
372
		;;
373 374 375 376
	dnf-rpm)
		__epm_addrepo_rhel "$repo" || return
		sudocmd dnf config-manager --add-repo "$repo"
		;;
377
	urpm-rpm)
378
		sudocmd urpmi.addmedia "$repo"
379 380
		;;
	zypper-rpm)
381
		sudocmd zypper ar "$repo"
382 383
		;;
	emerge)
384
		sudocmd layman -a "$repo"
385 386
		;;
	pacman)
387
		info "You need manually add repo to /etc/pacman.conf"
388 389
		# Only for alone packages:
		#sudocmd repo-add $pkg_filenames
390
		;;
391
	npackd)
392
		sudocmd npackdcl add-repo --url="$repo"
393
		;;
394 395 396
	winget)
		sudocmd winget source add "$repo"
		;;
397
	slackpkg)
398
		info "You need manually add repo to /etc/slackpkg/mirrors"
399 400
		;;
	*)
401
		fatal "Have no suitable command for $PMTYPE"
402 403 404 405
		;;
esac

}