Opennet Firmware
 Alle Dateien Funktionen Variablen Gruppen Seiten
export-build.sh
gehe zur Dokumentation dieser Datei
1 #!/bin/bash
2 
3 #
4 # Opennet trac-bitten-slave Scripts
5 # Mathias Mahnke, created 2014/05/05
6 # Opennet Admin Group <admin@opennet-initiative.de>
7 #
8 
9 # stop on error and unset variables
10 set -eu
11 
12 # get config file name
13 FILE="$(basename "$0")"
14 CFG="${FILE%.*}.cfg"
15 
16 # get current script dir
17 HOME="$(dirname $(readlink -f "$0"))"
18 OPENWRT_CONFIG="${HOME}/../../../openwrt/.config"
19 
20 # read variables
21 . "$HOME/$CFG"
22 
23 # retrieve commands
24 action="${1:-help}"
25 
26 
27 get_snapshot_name() {
28  grep "^CONFIG_VERSION_NUMBER=" "$OPENWRT_CONFIG" | cut -f 2 -d '"'
29 }
30 
31 
32 get_commit_info() {
33  # kurze Uebersicht aller commits des aktuellen Builds inkl. eines Commit-Zaehlers
34  git log --oneline | sed -n '1!G;h;$p' | nl | tac
35 }
36 
37 
38 build_platform() {
39  local platform="$1"
40  local snapshot_name=$(get_snapshot_name)
41  [ -z "$snapshot_name" ] && return 1
42 
43  # prepare export directory
44  local dest_dir="$HOME/$EXPORT_DIR/$snapshot_name"
45  local dest_platform_dir="$dest_dir/$platform"
46  mkdir -p "$dest_platform_dir"
47 
48  # copy build to export directory
49  local src_dir="$HOME/$BIN_DIR/$platform"
50  rsync $RSYNC_OPTIONS "$src_dir/" "$dest_platform_dir/"
51 
52  # set version number in export directories
53  get_commit_info > "$dest_dir/__${snapshot_name}__"
54  get_commit_info > "$dest_platform_dir/__${snapshot_name}__"
55 
56  # generate latest link
57  rm -f "$HOME/$EXPORT_DIR/$LATEST_LINK"
58  (cd "$HOME/$EXPORT_DIR" && ln -s "$snapshot_name" "$LATEST_LINK")
59 }
60 
61 
62 export_doc() {
63  local snapshot_name=$(get_snapshot_name)
64  [ -z "$snapshot_name" ] && return 1
65  local dest_dir="$HOME/$EXPORT_DIR/$snapshot_name/doc"
66  mkdir -p "$dest_dir"
67  local src_dir="$HOME/$DOC_DIR"
68  echo "Copying documentation: $src_dir -> $dest_dir"
69  rsync $RSYNC_OPTIONS "$src_dir/" "$dest_dir/"
70 }
71 
72 
73 purge_old_exports() {
74  local keep_builds="$1"
75  cd "$HOME/$EXPORT_DIR"
76  # "uniq -u" entfernt doppelte Zeilen - also verbleiben nur die alten Dateien
77  (
78  ls -t | head -n "$keep_builds"
79  ls
80  ) | sort | uniq -u | xargs --delimiter '\n' --no-run-if-empty rm -r
81 }
82 
83 
84 # process commands
85 case "$action" in
86  help|--help)
87  echo "Usage: $(basename "$0")"
88  echo " [<platform>] - export build"
89  echo " --doc - generate documentation"
90  echo " --purge <keep-number-of-dirs> - purge old exports"
91  exit 0
92  ;;
93  doc|--doc)
94  export_doc
95  ;;
96  purge|--purge)
97  keep_builds=${2:-}
98  [ -z "$keep_builds" ] && echo >&2 "No number of non-purgeable builds given" && exit 2
99  echo "$keep_builds" | grep -q "[^0-9]" && echo >&2 "Number of non-purgeable builds contains non-digits: '$keep_builds'" && exit 3
100  [ "$keep_builds" -lt 1 ] && echo >&2 "Number of non-purgeable builds is too low: '$keep_builds'" && exit 4
101  purge_old_exports "$keep_builds"
102  ;;
103  *)
104  build_platform "$action"
105  ;;
106 esac
107 
108 exit 0