Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / build_view
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         build_view
5 # Description:  Builds in a view
6 # Author:       Andrew@DeFaria.com
7 # Created:      Wed Jun 11 13:22:11 PDT 2003
8 # Language:     Bash Shell
9 # Modifications:06/17/2003 AD: Changed 2310_onu -> onu2310 as the card directory
10 #               has changed. Also added -to and -cc options.
11 #
12 # (c) Copyright 2003, Andrew@DeFaria.com, all rights reserved
13 #
14 ################################################################################
15 # Set me to command name
16 me=$(basename $0)
17
18 # Set adm_base
19 adm_base="$SITE_TOOLS_PATH/adm"
20
21 # Set adm_fpath
22 adm_fpath=${adm_fpath:-$adm_base/functions}
23
24 # Source functions
25 . $adm_fpath/common
26
27 function usage {
28   if [ ! -z "$1" ]; then
29     error "$1\n"
30   fi
31   display "Usage: $me: [-to <email_address>] [-cc <email_address>]"
32   display "\t\t   [-q|uiet ] [-v|erbose] [-d|ebug] [-usage]"
33   display "\t\t   [-noupdate] <viewtag> [ <viewtag>... ]"
34   display
35   display "Where:"
36   display "\t-to:\t\tEmail address where to mail the build log"
37   display "\t-cc:\t\tEmail address where to cc the build log"
38   display "\t-quiet:\t\tBe quiet"
39   display "\t-verbose:\tTurn on verbose mode"
40   display "\t-debug:\t\tTurn on debug mode"
41   display "\t-usage:\t\tDisplay usage"
42   display "\t-noupdate:\tSkip updating of snapshot views"
43   display "\t\t\t(default: Snapshot views are updated)"
44   display "\t<viewtag>\tView tag(s) to build"
45   exit 1
46 } # usage
47
48 # Commands used
49 ssmtp=/usr/sbin/ssmtp
50
51 # Get cards definition
52 . $SITE_TOOLS_PATH/adm/etc/cards
53
54 declare -i build_status=0
55
56 function send_message {
57   msg="$1"
58
59   # Perform accounting
60   accounting
61
62   # Compose message
63   if [ -z "$cc" ]; then
64     display "From: ccadmin\nTo: $to\nSubject: Build of $view_to_build $msg" > /tmp/msg.$$
65   else
66     display "From: ccadmin\nTo: $to\ncc: $cc\nSubject: Build of $view_to_build $msg" > /tmp/msg.$$
67   fi
68
69   # Mail it
70   cat /tmp/msg.$$ $logfile.acct $logfile | $ssmtp -t
71
72   # Clean up accounting file
73   rm -f $logfile.acct
74
75   if [ $? -eq 0 ]; then
76     rm -f /tmp/msg.$$
77   fi
78 } # send_message
79
80 function accounting {
81   # Cheap accounting...
82   display "\n\tCard Summary"             >> $logfile.acct
83   display "****************************" >> $logfile.acct
84   for card in $cards; do
85     if [ -f "$card.build.log" ]; then
86       warnings=$(grep -i warning $card.build.log | grep -vc "Warnings: 0")
87       errors=$(grep -i error $card.build.log | grep -v "Errors: 0" | grep -vc "cli_errors.c")
88       display "$card:\tWarnings: $warnings\tErrors: $errors" >> $logfile.acct
89     fi
90   done
91 } # accounting
92
93 # Get parameters                                                                
94 update_view="yes"
95 while [ $# -ge 1 ]; do                                                          
96   case "$1" in                                                                  
97       -u|-usage)
98         usage
99       ;;
100
101       -v|-verbose)
102         verbose=yes
103       ;;                                                                        
104
105       -d|-debug)
106         debug=yes
107       ;;                                                                        
108
109       -q|-quiet)
110         quiet=yes
111       ;;
112
113       -to)
114         shift
115         to="$1"
116       ;;
117
118       -cc)
119         shift;
120         cc="$1"
121       ;;
122
123       -noupdate)
124         update_view="no"
125       ;;
126
127       *)
128         views_to_build="$views_to_build $1"
129       ;;
130   esac
131   shift
132 done
133
134 # Who to notify
135 if [ -z "$to" ]; then
136   to="$SITE_ADMIN_EMAIL"
137 fi
138
139 for view_to_build in $views_to_build; do
140   # First check to see if there is a snapshot view
141   if [ -d "$SITE_SNAPSHOT_VIEW_PATH/$view_to_build" ]; then
142     view_path="$SITE_SNAPSHOT_VIEW_PATH"
143
144     if [ $update_view = "yes" ]; then
145       verbose "Updating view $view_to_build..."
146       $SITE_TOOLS_PATH/bin/update_view -q $view_to_build
147
148       if [ $? -ne 0 ]; then
149         error "Failed to update view $view_to_build" 1
150       else
151         verbose "Updated view $view_to_build"
152       fi
153     fi
154   else # Now check ot see if it's a dynamic view
155     view_path="$SITE_DYNAMIC_VIEW_PATH"
156     if [ ! -d $view_path/$view_to_build ]; then
157       # View not started - start it
158       verbose "Starting view $view_to_build..."
159       cleartool startview $view_to_build > /dev/null 2>&1
160
161       if [ $? -ne 0 ]; then
162         error "Unable to start view $view_to_build" 2
163       fi
164     fi
165   fi
166
167   if [ -d "$view_path/$view_to_build" ]; then
168     logfile=/tmp/$view_to_build.build.log
169     verbose "Start build of $view_to_build @ $(date)"
170     echo "Start build of $view_to_build @ $(date)" > $logfile
171
172     # Perform build
173     cd $view_path/$view_to_build/salira/neopon/build
174     $SITE_TOOLS_PATH/bin/build -nomonitor -sequential -clean -quiet -tee $logfile
175
176     build_status=$?
177
178     if [ $build_status -eq 0 ]; then
179       msg="succeeded"
180     else 
181       msg="failed"
182     fi
183    
184     display "\nEnd build of $view_to_build $msg @ $(date)"       >> $logfile
185     display "Logfile: $(hostname):/tmp/$view_to_build.build.log" >> $logfile
186
187     send_message $msg
188
189     # Check for errors and notify the right people
190     if [ "$quiet" = "yes" ]; then
191       # If quiet then do not notify
192       :
193     else
194       $SITE_TOOLS_PATH/bin/notify_err
195     fi
196   else
197     warning "Cannot find view $view_path/$view_to_build"
198   fi
199 done