about summary refs log tree commit diff stats
path: root/tools/conf/srv/gitolite/deploy.sh
blob: 1091ea9380a9ff8c69e0bd50328e9c8293c85dab (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
76
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; f
#!/bin/bash

# origin package directory
packages_dir="/srv/gitolite/deploy/packages"
# temporary work directory
deploy_dir="/srv/deploy_dir"
# scripts to deploy packages
deploy_scripts="/srv/gitolite/deploy/scripts"

function get_script(){
    # receives package path return script to call
    local pkg_path=$1
    echo $(head -2 ${pkg_path}/project | tail -1)
}

function get_new(){
    # receives package path return commit hash (new)
    local pkg_path=$1
    echo $(head -3 ${pkg_path}/project | tail -1)
}

function get_dep(){
    # receives package path return previews commit hash (old)
    local pkg_path=$1

    new=$(head -3 ${pkg_path}/project | tail -1)
    old=$(head -4 ${pkg_path}/project | tail -1)

    if [[ ! ${new} = ${old} ]]; then
        echo ${old} | cut -c1-7
    fi
}

function project_extract(){

    # project directory containing extracted packages
    local prj_dir=$1

    # final extracted package
    local prj_pkg="${prj_dir}/package"

    # temporary vars for swapping/iterating pkg_news
    local pkg_new=""
    local pkg_old=""
    local pkg_dir=""
    local pkg_temp=""
    local pkg_next=1
    local pkg_del=""
    local x=0
    local y=0

    # array with all the news hashes
    local pkg_news=($(ls ${prj_dir}))

    # total new packages
    local total=${#pkg_news[@]}

    echo "Deploy: $(basename ${prj_dir}) extracting packages ${pkg_news[*]}"

    # find first package
    for pkg_new in ${pkg_news[@]}
    do
        # get package dependency
        pkg_dir="${prj_dir}/${pkg_new}"
        pkg_old=$(get_dep ${pkg_dir})
        if [[ ! " ${pkg_news[@]} " =~ " ${pkg_old} " ]]; then
            # pkg_news don't contain package
            # we found initial package
            pkg_temp=${pkg_news[0]}
            pkg_news[0]=${pkg_new}
            pkg_news[${x}]=${pkg_temp}
            break
        fi
        x=$((${x}+1))
    done

    # Order packages by dependency start with first package
    for (( y=0; y<${total}; y++ ))
    do
        pkg_next=$(($y+1))
        if [[ ${pkg_next} = ${total} ]]; then
            ## we are in the last one
            break
        fi

        pkg_new=${pkg_news[$y]}
        for (( x=pkg_next; x<${total}; x++ ))
        do
            pkg_dir="${prj_dir}/${pkg_news[${x}]}"
            pkg_old=$(get_dep ${pkg_dir})
            # is dependent on current
            if [[ ${pkg_old} = ${pkg_new} ]]; then
                pkg_temp=${pkg_news[${pkg_next}]}
                pkg_news[${pkg_next}]=${pkg_news[${x}]}
                pkg_news[${x}]=${pkg_temp}
                # we can break and pass next one
                break
            fi
        done
    done

    # create project final package directory
    mkdir -p ${prj_pkg}/files

    # copy project information of last commit
    cp ${prj_dir}/${pkg_news[$((${total}-1))]}/project ${prj_pkg}

    # now that packages are ordered we can start creating files
    for pkg_new in ${pkg_news[@]}
    do
        pkg_dir=${prj_dir}/${pkg_new}
        tar xf ${pkg_dir}/files.tar.xz \
            --directory ${prj_pkg}/files

        # if deleted files exists
        if [ -f "${pkg_dir}/deleted" ]; then
            # first collect all files/directories don't exist
            while read pkg_del; do
                # if file don't exist add entry to project deleted file
                pkg_temp="${prj_pkg}/files/${pkg_del}"
                if [ ! -f ${pkg_temp} ]; then
                    if [ ! -d ${pkg_temp} ]; then
                        # is not a file or directory from previous packages
                        echo ${pkg_del} >> ${prj_pkg}/deleted
                    fi
                fi
            done <${prj_dir}/${pkg_new}/deleted

            # delete directories and files
            while read pkg_del; do
                pkg_temp="${prj_pkg}/files/${pkg_del}"
                if [ -d ${pkg_temp} ]; then
                    rm -r ${pkg_temp}
                elif [ -f ${pkg_temp} ]; then
                    rm ${pkg_temp}
                fi
            done <${prj_dir}/${pkg_new}/deleted
        fi

        #remove temporary directory
        rm -r ${prj_dir}/${pkg_new}
    done

    # call project deploy script
    call_script=${deploy_scripts}/$(get_script $prj_pkg)
    echo "Deploy: calling deploy script: ${call_script}"
    /bin/bash ${call_script} ${prj_pkg}

}

if [[ ! $(ls ${deploy_dir}) = "" ]]; then
    rm -r ${deploy_dir}/*
fi

# first extract all packages from origin directory
for pkg_path in `find ${packages_dir} -type f -name "*.tar.gz"`
do
    if [ -f ${pkg_path} ]; then
        pkg_name=$(basename ${pkg_path})
        pkg_proj=$(echo ${pkg_name} | cut -d "_" -f 1)
        pkg_new7=$(echo ${pkg_name} | tail -c -15 | cut -c -7)
        pkg_temp=${deploy_dir}/${pkg_proj}/${pkg_new7}
        mkdir -p ${pkg_temp}
        tar xf ${pkg_path} --directory ${pkg_temp}
        rm ${pkg_path}
    fi
done

# loop for all projects and deploy them
for prj_dir in `find ${deploy_dir} -maxdepth 1 -mindepth 1 -type d`
do
    # order index of hashes based on old commit
    echo "prj_dir $prj_dir"
    project_extract ${prj_dir}
done