Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / autopkg.sh @ 30761818

History | View | Annotate | Download (5.8 kB)

1
#!/bin/bash
2

    
3
usage(){
4

    
5
  echo "
6
Usage: $0: [options]
7
  -h, --help          Prints this help message
8
  --debian [branch]   Local debian branch to use (default debian)
9
  --upstream [branch] Local upstream branch to use (default master)
10
  --remote [repo]     Remote repo to use (default origin)
11
  --packages [dir]    Where to store the created packages (default ~/packages)
12
  --validate          Fetch remote repo branches and
13
                      check if local are up-to-date (default false)
14
  --push              Whether to push upstream (default false)
15
"
16
  exit 1
17
}
18

    
19
parse_git_branch()
20
{
21
    git branch 2> /dev/null | grep '^*' | sed 's/^*\ //g'
22
}
23

    
24
die()
25
{
26
    echo -e $* 1>&2
27
    echo Aborting.
28
    exit 1
29
}
30

    
31
cleanup()
32
{
33
    trap - EXIT
34

    
35
    echo -n Cleaning up...
36
    if [ ${#CLEANUP[*]} -gt 0 ]; then
37
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
38
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
39
        for i in $REVERSE_INDEXES; do
40
            local cmd=${CLEANUP[$i]}
41
            $cmd
42
        done
43
    fi
44
    echo "done"
45
}
46

    
47
add_cleanup() {
48
    local cmd=""
49
    for arg; do cmd+=$(printf "%q " "$arg"); done
50
    CLEANUP+=("$cmd")
51
}
52

    
53

    
54
add_checkpoint()
55
{
56
    commit=$(git reflog | head -n1 | cut -f 1 -d " ")
57
    add_cleanup git reset --hard $commit
58
    LASTCHECKPOINT=$commit
59
}
60

    
61
CLEANUP=( )
62

    
63

    
64
TEMP=$(getopt -o h --long help,validate,push,packages:,upstream:,debian:,remote: -n 'autopkg.sh' -- "$@")
65

    
66
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
67

    
68
eval set -- "$TEMP"
69

    
70
while true ; do
71
  case "$1" in
72
    -h|--help) usage ;;
73
    --upstream) LOCALUPSTREAM=$2 ; shift 2 ;;
74
    --debian) LOCALDEBIAN=$2 ; shift 2 ;;
75
    --remote) REMOTE=$2 ; shift 2 ;;
76
    --packages) PKGAREA=$2 ; shift 2 ;;
77
    --validate) VALIDATE=true ; shift ;;
78
    --push) PUSH=true ; shift ;;
79
    --) shift ; break ;;
80
    *) echo "Internal error!" ; usage ;;
81
  esac
82
done
83

    
84
# The root of the git repository, no matter where we're called from
85
TOPLEVEL="$(git rev-parse --show-toplevel)"
86

    
87
: ${LOCALUPSTREAM:=$(parse_git_branch)}
88
: ${LOCALDEBIAN:=debian}
89
: ${REMOTE:=origin}
90
: ${VALIDATE:=false}
91
: ${PUSH:=false}
92

    
93
: ${PKGAREA:=~/packages}
94
: ${BACKUPAREA:=~/backup}
95

    
96
cd "$TOPLEVEL"
97

    
98
# Prerequisites: Test all important directories exist
99
test -d "$PKGAREA" || die "Package area directory $PKGAREA missing"
100
test -d "$BACKUPAREA" || die "Backup area directory $BACKUPAREA missing"
101

    
102
# Prerequisite: Test the dialog utility is available
103
dialog --help &>/dev/null || die "Could not run the 'dialog' utility"
104

    
105
BUILDAREA=$(mktemp -d --tmpdir=/tmp build-area.XXX)
106
add_cleanup rm -r $BUILDAREA
107

    
108
echo "############################################################################"
109
echo "Will build packages under $BUILDAREA"
110
echo "Local upstream branch: $LOCALUPSTREAM"
111
echo "Local debian branch: $LOCALDEBIAN"
112
$VALIDATE && echo "Will fetch $REMOTE and check if $LOCALUPSTREAM and $LOCALDEBIAN are up-to-date"
113
echo "############################################################################"
114
echo "Press Enter to continue..."
115
read
116

    
117
MODIFIED=$(git status --short | grep -v "??")
118
test -z "$MODIFIED" || die "error: Repository is dirty. Commit your local changes:\n $MODIFIED"
119

    
120
set -e
121
trap cleanup EXIT
122

    
123
add_checkpoint
124

    
125
# Create a temporary debian branch to do everything
126
TMPDEBIAN=$(mktemp -u debian.XXX)
127
git branch --track $TMPDEBIAN  $LOCALDEBIAN
128
add_cleanup git branch -D $TMPDEBIAN
129

    
130
git checkout $TMPDEBIAN
131
add_cleanup git checkout $LOCALUPSTREAM
132

    
133
# Whether we are in snapshot or release mode
134
snap=false
135
mrgextra=-m
136
dchextra=-R
137
mrgmsg="Merge branch '$LOCALUPSTREAM' into $LOCALDEBIAN"
138
dialog --yesno "Create Snapshot?" 5 20 && snap=true  && dchextra=-S && mrgextra= && mrgmsg=
139

    
140
# merge local branch into tmp branch with a nice commit message,
141
# so it can be pushed as is to upstream debian
142
export GIT_MERGE_AUTOEDIT=no
143
git merge $mrgextra ${mrgextra:+"$mrgmsg"} $LOCALUPSTREAM
144

    
145
# auto edit Debian changelog depending on Snapshot or Release mode
146
export EDITOR=/usr/bin/vim
147
git-dch --debian-branch=$TMPDEBIAN --git-author --ignore-regex=".*" --multimaint-merge --since=HEAD $dchextra
148
git add debian/changelog
149

    
150
# get version from the changelog
151
# we add a git tag here, so setup.py sdist works as expected
152
# FIXME: This is a workaround for the way Synnefo packages determine
153
#        the versions for their Python packages
154
version=$(IFS="()" ; read  x v x < debian/changelog  ; echo $v)
155
if ! $snap; then
156
  git commit -s -a -m "Bump new upstream version"
157
  TAGFILE=$(mktemp -t tag.XXX)
158
  add_cleanup rm $TAGFILE
159
  dialog --inputbox "New Debian Tag: " 5 30 "debian/$version" 2>$TAGFILE
160
  git tag $(<$TAGFILE)
161
  add_cleanup git tag -d $(<$TAGFILE)
162
fi
163

    
164
add_cleanup git reset --hard HEAD
165
# Build all packages
166
git-buildpackage --git-export-dir="$BUILDAREA" \
167
                 --git-upstream-branch=$LOCALUPSTREAM \
168
                 --git-debian-branch=$TMPDEBIAN \
169
                 --git-export=INDEX \
170
                 --git-ignore-new -sa
171

    
172
# do some dirty backup
173
# pkgarea might be needed by auto-deploy tool
174
rm -f "$PKGAREA"/* || true
175
cp -v "$BUILDAREA"/* "$PKGAREA"/ || true
176
cp -v "$BUILDAREA"/* "$BACKUPAREA"/ || true
177

    
178

    
179

    
180
function check_remote(){
181

    
182
  git fetch $1 2>/dev/null || die "Could not fetch $1"
183
  git fetch $1 $2 2>/dev/null|| die "Could not fetch $1/$2"
184

    
185
  commits_behind=$(git rev-list $2..$1/$2 | wc -l)
186
  if [ $commits_behind -ne 0 ]; then
187
    die "Your local branch is outdated. Please run:\ngit pull --rebase $1/$2"
188
  fi
189

    
190

    
191
}
192

    
193
if $VALIDATE; then
194
  check_remote $REMOTE $LOCALUPSTREAM
195
  check_remote $REMOTE $LOCALDEBIAN
196
fi
197

    
198

    
199
  # trap - EXIT
200
  # here we can push the commits to the remote debian branch as they are
201
echo
202
echo "#################################################"
203
echo "##                  SUCCESS                    ##"
204
echo "#################################################"
205
if $PUSH; then
206
  git push --tags $REMOTE $TMPDEBIAN:$LOCALDEBIAN
207
  git push $REMOTE $LOCALUPSTREAM:$LOCALUPSTREAM
208
fi
209

    
210
exit 0