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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/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