deploy-packages.sh 7.95 KB
Newer Older
aviau's avatar
aviau committed
1 2
#!/bin/bash
#
3
# Copyright (C) 2016-2017 Savoir-faire Linux Inc.
aviau's avatar
aviau committed
4 5
#
# Author: Alexandre Viau <alexandre.viau@savoirfairelinux.com>
6
# Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
aviau's avatar
aviau committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
# This script sings and deploys pacakges from packages/distro.
# It should be ran from the project root directory.
#

# Exit immediately if a command exits with a non-zero status
set -e

30 31 32 33
###############################
## Debian / Ubuntu packaging ##
###############################

34
function package_deb()
35
{
36 37 38 39 40 41 42 43 44 45 46 47 48
    ##################################################
    ## Create local repository for the given distro ##
    ##################################################
    echo "#########################"
    echo "## Creating repository ##"
    echo "#########################"

    DISTRIBUTION_REPOSITOIRY_FOLDER=$(realpath repositories)/${DISTRIBUTION}
    rm -rf ${DISTRIBUTION_REPOSITOIRY_FOLDER}
    mkdir -p ${DISTRIBUTION_REPOSITOIRY_FOLDER}/conf

    # Distributions file
    cat << EOF > ${DISTRIBUTION_REPOSITOIRY_FOLDER}/conf/distributions
49 50 51 52 53 54 55 56 57
Origin: ring
Label: Ring ${DISTRIBUTION} Repository
Codename: ring
Architectures: i386 amd64
Components: main
Description: This repository contains Ring ${DISTRIBUTION} packages
SignWith: ${KEYID}
EOF

58 59
    # Options file
    cat << EOF > ${DISTRIBUTION_REPOSITOIRY_FOLDER}/conf/options
60 61 62
basedir ${DISTRIBUTION_REPOSITOIRY_FOLDER}
EOF

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
    ####################################
    ## Add packages to the repository ##
    ####################################

    for package in packages/${DISTRIBUTION}*/*.deb; do

        # Sign the deb
        echo "## signing: ${package} ##"
        dpkg-sig -k ${KEYID} --sign builder ${package}

        # Include the deb
        echo "## including ${package} ##"
        package_name=$(dpkg -I ${package} | grep -m 1 Package: | awk '{print $2}')
        package_arch=$(dpkg -I ${package} | grep -m 1 Architecture: | awk '{print $2}')
        if [ ${package_arch} = "all" ]; then
            # Removing to avoid the error of adding the same deb twice.
            # This happens with arch all packages, which are generated in amd64 and i386.
            reprepro --verbose --basedir ${DISTRIBUTION_REPOSITOIRY_FOLDER} remove ring ${package_name}
        fi
        reprepro --verbose --basedir ${DISTRIBUTION_REPOSITOIRY_FOLDER} includedeb ring ${package}
    done

    # Rebuild the index
    reprepro --verbose --basedir ${DISTRIBUTION_REPOSITOIRY_FOLDER} export ring

    # Show the contents
    reprepro --verbose --basedir ${DISTRIBUTION_REPOSITOIRY_FOLDER} list ring

    #######################################
    ## create the manual download folder ##
    #######################################
    DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER=$(realpath manual-download)/${DISTRIBUTION}
    mkdir -p ${DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER}
96 97
    ls packages/${DISTRIBUTION}
    # packages with dfsg1-0 contains the postinstall script that adds the repository
98
    cp packages/${DISTRIBUTION}*/jami-all_????????.?.*\~dfsg1-0_*.deb ${DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER}
99
    for package in ${DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER}/*; do
100 101
        package_name=$(dpkg -I ${package} | grep -m 1 Package: | awk '{print $2}')
        package_arch=$(dpkg -I ${package} | grep -m 1 Architecture: | awk '{print $2}')
102 103 104 105
	package_linkname=${package_name}_${package_arch}.deb
	cd ${DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER}
	cp ${package} ${package_linkname}
	cd -
106
    done
107 108 109 110 111 112 113
}


######################
## Fedora packaging ##
######################

114
function package_rpm()
115
{
116 117 118 119 120 121 122 123 124 125 126 127
    ##################################################
    ## Create local repository for the given distro ##
    ##################################################
    echo "#########################"
    echo "## Creating repository ##"
    echo "#########################"

    DISTRIBUTION_REPOSITOIRY_FOLDER=$(realpath repositories)/${DISTRIBUTION}
    rm -rf ${DISTRIBUTION_REPOSITOIRY_FOLDER}
    mkdir -p ${DISTRIBUTION_REPOSITOIRY_FOLDER}

    # .repo file
128
    cat << EOF > ${DISTRIBUTION_REPOSITOIRY_FOLDER}/ring-nightly.repo
129 130
[ring]
name=Ring \$releasever - \$basearch - ring
131
baseurl=https://dl.jami.net/ring-nightly/fedora_\$releasever
132
gpgcheck=1
Adrien Béraud's avatar
Adrien Béraud committed
133
gpgkey=https://dl.jami.net/ring.pub.key
134 135 136
enabled=1
EOF

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    ####################################
    ## Add packages to the repository ##
    ####################################

    # Sign the rpms
    echo "##################"
    echo "## signing rpms ##"
    echo "##################"

    # RPM macros
    if [ ! -f ~/.rpmmacros ];
    then
        echo "%_signature gpg" > ~/.rpmmacros
        echo "%_gpg_name ${KEYID}" >> ~/.rpmmacros
    fi

    for package in packages/${DISTRIBUTION}*/*.rpm; do
        rpmsign --resign --key-id=${KEYID} ${package}
        cp ${package} ${DISTRIBUTION_REPOSITOIRY_FOLDER}
    done

    # Create the repo
    createrepo --update ${DISTRIBUTION_REPOSITOIRY_FOLDER}

    #######################################
    ## create the manual download folder ##
    #######################################
    DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER=$(realpath manual-download)/${DISTRIBUTION}
    mkdir -p ${DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER}
    for package in packages/${DISTRIBUTION}*/*.rpm; do
        cp ${package} ${DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER}
        package_name=$(rpm -qp --queryformat '%{NAME}' ${package})
        package_arch=$(rpm -qp --queryformat '%{ARCH}' ${package})
        cp ${package} ${DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER}/${package_name}_${package_arch}.rpm
    done
172 173 174 175 176 177 178 179 180
}


################################################
## Deploy packages on given remote repository ##
################################################

function deploy()
{
181
    if [ -f "${SSH_IDENTIY_FILE}" ];
182 183 184 185 186 187 188 189
    then
        RSYNC_RSH="ssh -i ${SSH_IDENTIY_FILE}"
    fi

    # Deploy the repository
    echo "##########################"
    echo "## deploying repository ##"
    echo "##########################"
190
    echo "Using RSYNC_RSH='${RSYNC_RSH}'"
191 192 193 194 195 196 197 198 199 200 201
    rsync --archive --recursive --verbose --delete ${DISTRIBUTION_REPOSITOIRY_FOLDER} ${REMOTE_REPOSITORY_LOCATION}

    # deploy the manual download files
    echo "#####################################"
    echo "## deploying manual download files ##"
    echo "#####################################"
    rsync --archive --recursive --verbose --delete ${DISTRIBUTION_MANUAL_DOWNLOAD_FOLDER} ${REMOTE_MANUAL_DOWNLOAD_LOCATION}

    # remove deployed files
    rm -rf manual-download
    rm -rf repositories
202 203 204 205 206 207 208
}


##########################################################
## Detect suitable packaging based on distribution name ##
##########################################################

209
function package()
210
{
Guillaume Roguez's avatar
Guillaume Roguez committed
211
    if [[ "${DISTRIBUTION:0:6}" == "debian" || "${DISTRIBUTION:0:6}" == "ubuntu" ]];
212 213
    then
        package_deb
214
    elif [[ "${DISTRIBUTION:0:6}" == "fedora" ]];
215 216 217
    then
        package_rpm
    else
Guillaume Roguez's avatar
Guillaume Roguez committed
218
        echo "ERROR: Distribution '${DISTRIBUTION}' is unsupported"
219
    fi
220 221 222
}


aviau's avatar
aviau committed
223 224 225 226 227 228 229 230 231 232 233
for i in "$@"
do
case $i in
    --distribution=*)
    DISTRIBUTION="${i#*=}"
    shift
    ;;
    --keyid=*)
    KEYID="${i#*=}"
    shift
    ;;
234 235 236 237 238 239
    --remote-repository-location=*)
    REMOTE_REPOSITORY_LOCATION="${i#*=}"
    shift
    ;;
    --remote-manual-download-location=*)
    REMOTE_MANUAL_DOWNLOAD_LOCATION="${i#*=}"
aviau's avatar
aviau committed
240
    shift
241
    ;;
242
    --remote-ssh-identity-file=*)
243 244
    SSH_IDENTIY_FILE="${i#*=}"
    shift
aviau's avatar
aviau committed
245 246 247 248 249 250 251 252
    ;;
    *)
    echo "Unrecognized option ${i}"
    exit 1
    ;;
esac
done

253 254
package
deploy