epm-downgrade 4.09 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012,2014,2016  Etersoft
# Copyright (C) 2012,2014,2016  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# 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/>.
#

load_helper epm-check_updated_repo

22
__epm_add_alt_apt_downgrade_preferences()
23
{
24 25
    [ -r /etc/apt/preferences ] && fatal "/etc/apt/preferences already exists"
    cat <<EOF | sudocmd tee /etc/apt/preferences
26
# classic repo
27
Package: *
28
Pin: release c=classic
29
Pin-Priority: 1001
30 31 32 33 34

# overload with addon repo
Package: *
Pin: release c=addon
Pin-Priority: 1101
35

36 37 38 39 40
# main repo
Package: *
Pin: release c=main
Pin-Priority: 1201

41 42 43
# overload with test (task) repo
Package: *
Pin: release c=task
44
Pin-Priority: 1301
45 46 47 48 49 50
EOF
}

# See https://wiki.debian.org/ru/AptPreferences
__epm_add_deb_apt_downgrade_preferences()
{
51 52 53
    [ -r /etc/apt/preferences ] && fatal "/etc/apt/preferences already exists"
    info "Running with /etc/apt/preferences:"
    cat <<EOF | sudorun tee /etc/apt/preferences
54 55 56 57 58 59 60 61 62 63 64
Package: *
Pin: release a=stable
Pin-Priority: 1001

Package: *
Pin: release a=testing
Pin-Priority: 900

Package: *
Pin: release a=unstable
Pin-Priority: 800
65 66 67 68 69
EOF
}

__epm_remove_apt_downgrade_preferences()
{
70
    sudocmd rm -f /etc/apt/preferences
71 72 73 74
}

epm_downgrade()
{
75
    local CMD
76

77 78
    # it is useful for first time running
    update_repo_if_needed
79

80 81 82 83 84 85 86
    # if possible, it will put pkg_urls into pkg_files and reconstruct pkg_filenames
    if [ -n "$pkg_urls" ] ; then
        info "Downloading packages assigned to downgrade ..."
        load_helper epm-download
        __handle_pkg_urls_to_install
    fi

87
    info "Running command for downgrade packages"
88

89 90
    case $BASEDISTRNAME in
    alt)
91 92 93 94 95 96 97 98
        # pass pkg_filenames too
        if [ -n "$pkg_names" ] ; then
            __epm_add_alt_apt_downgrade_preferences || return
            load_helper epm-install
            (pkg_names=$(get_only_installed_packages $pkg_names) epm_install)
            __epm_remove_apt_downgrade_preferences
        elif [ -n "$pkg_files" ] ; then
            load_helper epm-install
99 100 101 102 103 104 105 106
            local pkgs=''
            local i
            for i in $pkg_files ; do
                local pkgname="$(epm print name for package $i)"
                is_installed $pkgname || continue
                pkgs="$pkgs $i"
            done
            (force="$force --oldpackage" epm_install_files $pkgs)
107 108 109 110 111
        else
            __epm_add_alt_apt_downgrade_preferences || return
            load_helper epm-upgrade
            epm_upgrade "$@"
            __epm_remove_apt_downgrade_preferences
112
        fi
113
        return
114
        ;;
115 116 117 118 119
    esac

    case $PMTYPE in
    #apt-rpm)
    #    ;;
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
    apt-dpkg)
        local APTOPTIONS="$(subst_option non_interactive -y) $force_yes"
        __epm_add_deb_apt_downgrade_preferences || return
        if [ -n "$pkg_filenames" ] ; then
            sudocmd apt-get $APTOPTIONS install $pkg_filenames
        else
            sudocmd apt-get $APTOPTIONS dist-upgrade
        fi
        __epm_remove_apt_downgrade_preferences
        ;;
    yum-rpm)
        # can do update repobase automagically
        if [ -n "$pkg_filenames" ] ; then
            sudocmd yum downgrade $pkg_filenames
        else
            sudocmd yum distro-sync
        fi
        ;;
    dnf-rpm)
        if [ -n "$pkg_filenames" ] ; then
            sudocmd dnf downgrade $pkg_filenames
        else
            sudocmd dnf distro-sync
        fi
        ;;
    urpm-rpm)
        assure_exists urpm-reposync urpm-tools
        sudocmd urpm-reposync -v
        ;;
    *)
150
        fatal 'Have no suitable command for $PMTYPE'
151 152
        ;;
    esac
153
}