epm-sh-altlinux-contents-index 5.67 KB
Newer Older
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
#!/bin/sh
#
# Copyright (C) 2014, 2017, 2021  Etersoft
# Copyright (C) 2014, 2017, 2021  Vitaly Lipatov <lav@etersoft.ru>
#
# 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
# (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
# GNU Affero General Public License for more details.
#
# 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/>.
#

# apt-file like. See also
# https://bugzilla.altlinux.org/show_bug.cgi?id=14449

# convert "http://download.etersoft.ru/pub/ALTLinux/p8/branch x86_64" to ALTLinux/p8/branch/x86_64
get_alt_repo_path()
{
    local DN1=$(dirname "$1")
    local DN2=$(dirname $DN1)
    local DN3=$(dirname $DN2)

    local BN0=$(basename "$1") # arch
    local BN1=$(basename $DN1) # branch/Sisyphus
    local BN2=$(basename $DN2) # p8/ALTLinux
    local BN3=$(basename $DN3) # ALTLinux/

    [ "$BN1" = "branch" ] && echo "$BN3/$BN2/$BN1/$BN0" || echo "$BN2/$BN1/$BN0"
}

# convert "http://download.etersoft.ru/pub/ALTLinux/p8/branch x86_64" to /tmp/epm/ALTLinux/p8/branch/x86_64
get_local_alt_mirror_path()
{
41
    echo "$epm_cachedir/contents_index/$(get_alt_repo_path "$1")"
42 43
}

44
ALT_CONTENTS_INDEX_LIST=$epm_cachedir/contents_index/contents_index_list
45

46 47 48 49 50
__rsync_check()
{
    a= rsync -n "$1" >/dev/null 2>/dev/null
}

51
# URL TARGETDIR OPTIONS
52 53 54 55
rsync_alt_contents_index()
{
    local URL="$1"
    local TD="$2"
56
    local res
57
    try_assure_exists rsync || return
58

59
    if ! __rsync_check "$URL" ; then
60
        warning '$URL is not accessible via rsync, skipping contents index update...'
61 62
        return
    fi
63

64
    mkdir -p "$(dirname "$TD")"
65 66 67

    [ -n "$USER" ] && sudorun chown -R $USER "$TD"

68
    if [ -z "$quiet" ] ; then
69
        docmd rsync --partial --inplace $3 -a "$URL" "$TD"
70
    else
71
        a= rsync --partial --inplace $3 -a "$URL" "$TD"
72
    fi
73
    res=$?
74
    [ -f "$TD" ] && sudorun chmod a+rw "$TD"
75
    return $res
76 77 78 79 80 81 82 83 84 85 86 87
}

# URL
get_url_to_etersoft_mirror()
{
    local REPOPATH
    local ETERSOFT_MIRROR="rsync://download.etersoft.ru/pub"
    local ALTREPO=$(get_alt_repo_path "$1")
    echo "$ALTREPO" | grep -q "^ALTLinux" || return
    echo "$ETERSOFT_MIRROR/$(get_alt_repo_path "$1" | sed -e "s|^ALTLinux/|ALTLinux/contents_index/|")"
}

88
# "comment" "file"
89 90
__add_to_contents_index_list()
{
91
    [ -n "$verbose" ] && echo " $1 -> $2"
92
    [ -s "$2" ] || return
93
    echo "$2" >>$ALT_CONTENTS_INDEX_LIST
94 95
}

96
# "comment" file file2
97 98
__add_better_to_contents_index_list()
{
99 100 101
    if [ -s "$2" ] && [ -s "$3" ] ; then
        [ "$2" -ot "$3" ] && __add_to_contents_index_list "$1" "$3" && return
        __add_to_contents_index_list "$1" "$2" && return
102
    fi
103 104
    [ -s "$2" ] && __add_to_contents_index_list "$1" "$2" && return
    [ -s "$3" ] && __add_to_contents_index_list "$1" "$3" && return
105 106 107
}


108 109 110 111 112
check_alt_contents_index()
{
    [ -f "$ALT_CONTENTS_INDEX_LIST" ]
}

113
# TODO: use special user for this files?
114 115 116 117 118 119 120 121 122
init_alt_contents_index()
{
    sudocmd mkdir -p "$(dirname $ALT_CONTENTS_INDEX_LIST)"
    sudocmd chmod a+rw "$(dirname $ALT_CONTENTS_INDEX_LIST)"
    sudocmd truncate -s0 $ALT_CONTENTS_INDEX_LIST
    sudocmd chmod a+rw $ALT_CONTENTS_INDEX_LIST
    update_alt_contents_index
}

123 124 125 126
# fills ALT_CONTENTS_INDEX_LIST
update_alt_contents_index()
{
    load_helper epm-repolist
127
    check_alt_contents_index || return
128 129

    truncate -s0 "$ALT_CONTENTS_INDEX_LIST"
130 131 132
    # TODO: fix for Etersoft/LINUX@Etersoft
    # TODO: fix for rsync
    info "Retrieving contents_index ..."
133
    (quiet=1 epm_repolist) | grep -v " task$" | grep -E "rpm.*(ftp://|http://|https://|rsync://|file:/)" | sed -e "s@^rpm.*\(ftp://\|http://\|https://\)@rsync://@g" | sed -e "s@^rpm.*\(file:\)@@g" | while read -r URL1 URL2 component ; do
134 135
        [ "$component" = "debuginfo" ] && continue
        URL="$URL1/$URL2"
136
        if is_abs_path "$URL" ; then
137 138 139
            # first check for local mirror
            local LOCALPATH="$(echo "$URL/base")"
            local LOCALPATHGZIP="$(echo "$LOCALPATH" | sed -e "s|/ALTLinux/|/ALTLinux/contents_index/|")"
140
            __add_better_to_contents_index_list "$URL" "$LOCALPATHGZIP/contents_index.gz" "$LOCALPATH/contents_index"
141 142 143 144
        else
            local LOCALPATH="$(get_local_alt_mirror_path "$URL")"
            local REMOTEURL="$(get_url_to_etersoft_mirror "$URL")"
            if [ -n "$REMOTEURL" ] ; then
145
                rsync_alt_contents_index $REMOTEURL/base/contents_index.gz $LOCALPATH/contents_index.gz && __add_to_contents_index_list "$REMOTEURL" "$LOCALPATH/contents_index.gz" && continue
146
                [ -n "$verbose" ] && info 'Note: Can'\''t retrieve $REMOTEURL/base/contents_index.gz, fallback to $URL/base/contents_index'
147
            fi
148
            # we don't know if remote server has rsync
149
            # fix rsync URL firstly
150 151
            #local RSYNCURL="$(echo "$URL" | sed -e "s|rsync://\(ftp.basealt.ru\|basealt.org\|altlinux.ru\)/pub/distributions/ALTLinux|rsync://\1/ALTLinux|")" #"
            #rsync_alt_contents_index $RSYNCURL/base/contents_index $LOCALPATH/contents_index -z && __add_to_contents_index_list "$RSYNCURL" "$LOCALPATH/contents_index" && continue
152 153
            #mkdir -p "$LOCALPATH"
            #eget -O $LOCALPATH/contents_index $URL/base/contents_index && __add_to_contents_index_list "$RSYNCURL" "$LOCALPATH/contents_index" && continue
154

155
            #__add_better_to_contents_index_list "(cached)" "$LOCALPATH/contents_index.gz" "$LOCALPATH/contents_index"
156 157 158 159
        fi
    done
}