about summary refs log tree commit diff stats
path: root/tools/conf/srv/gitolite/deploy-web.sh
blob: 01e92ac33a16fe552f52ccdf59edac6c8ed7ee1c (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash

pkg_path=$1

www_root="/srv/www"
www_user="nginx"
www_group="www"

pkg_file="${pkg_path}/project"
pkg_rm="${pkg_path}/deleted"
pkg_files="${pkg_path}/files"

if [ ! -f ${pkg_file} ]; then
    echo "Deploy web: invalid pkg_file ${pkg_file}"
    exit 1
fi

pkg_name=$(head -1 ${pkg_file})
pkg_new=$(head -3 ${pkg_file} | tail -1)
pkg_new7=$(echo ${pkg_new} | cut -c1-7)

pkg_www="${www_root}/${pkg_name}"
pkg_back="${pkg_www}/backup_deploy"
pkg_last="${pkg_www}/.last_deploy"

if [ ! -d ${pkg_www} ]; then
    echo "Deploy web: invalid pkg_www ${pkg_www}"
    exit 1
fi

# first backup all data
if [[ ! $(ls ${pkg_www} | grep -v "backup_deploy") = "" ]]; then
    if [ ! -d ${pkg_back} ]; then
        sudo -u ${www_user} mkdir -p ${pkg_back}
    fi
    backup_file="${pkg_back}/${pkg_name}-$(date '+%Y-%j-%H-%M-%S').tar.gz"
    echo "Deploy web: making backup ${backup_file}"
    sudo -u ${www_user} tar --exclude ${pkg_back} --xattrs -zcpf ${backup_file} ${pkg_www}
fi

# remove files and directories that have been deleted
if [ -f ${pkg_rm} ]; then

    echo "Deploy web: files to delete:"
    # first we delete files
    while read deleted_file; do
        deleted_file="${pkg_www}/${deleted_file}"
        if [ -f ${deleted_file} ]; then
            echo "file      rm ${deleted_file}"
            rm ${deleted_file}
        fi
    done <${pkg_rm}

    # delete directories
    while read deleted_file; do
        deleted_file="${pkg_www}/${deleted_file}"
        if [ -d ${deleted_file} ]; then
            echo "file      rm ${deleted_file}"
            rm ${deleted_file}
        fi
    done <${pkg_rm}

fi

# copy new files to destination
if [ -d ${pkg_files} ]; then
    echo "Deploy web: cp from ${pkg_files} to ${pkg_www}"
    sudo -u ${www_user} cp -r ${pkg_files}/* ${pkg_www}
fi

echo ${pkg_new} > ${pkg_last}
echo "Deploy: scripts/deployweb.sh ${pkg_name} ${pkg_new7} deployed."

#remove temporary package
rm -r ${pkg_path}