File: ecflow_ui_create_jira_issue.sh.in

package info (click to toggle)
ecflow 5.13.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 51,304 kB
  • sloc: cpp: 259,208; python: 22,228; sh: 3,216; perl: 770; xml: 333; f90: 199; ansic: 141; makefile: 71
file content (140 lines) | stat: -rwxr-xr-x 4,128 bytes parent folder | download | duplicates (4)
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
#!/bin/bash

#============================================================================
# Copyright 2009- ECMWF.
# This software is licensed under the terms of the Apache Licence version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#============================================================================


if [[ $# -ne 6 ]] ; then
    echo "Error: wrong number of arguments = $# (must be 6)"
    if [[ $# -eq 1 ]] ; then
        echo "This script should only be called from within ecflow_ui."
    fi
    exit 1
fi

outFile=$1
host=$2
port=$3
nodePath=$4
nodeStatus=$5
project=$6

jiraExe="jira"
lastLines=50
pattern="--abort"
foundPattern=0
hasOutFile=0
hasLog=0
exitCode=0

#-------------------
# get node log
#-------------------
logTmp=$(mktemp)
ecflow_client --host=${host} --port=${port} --edit_history ${nodePath} > ${logTmp} 2>&1
if [[ $? -eq 0 && -s ${logTmp} ]] ; then
    hasLog=1
fi

#-------------------
# get task output
#-------------------
if [[ "${outFile}" != "_undef_" && -s ${outFile}  ]] ; then
    hasOutFile=1
    outTmp=$(mktemp)

    #line number of the last occurence of pattern in the output file
    lineNum=$(grep -n -e ${pattern}  $outFile | tail -n 1 | cut -d : -f 1)

    #extract lines preceding last occurence of pattern from out file
    if [ $? -eq 0 ] ; then
        foundPattern=1
        lineNum=$((lineNum-lastLines))
        lineNum=$((lineNum+1))
        tail -n+${lineNum} ${outFile} | head -n${lastLines} > ${outTmp}
    else
        tail -${lastLines} ${lastLines} > ${outTmp}
    fi
fi

#-------------------
# build command
#-------------------

#define issue description
desc="This report was generated automatically from ecflowUI at $(date). \\\\Server: ${host}@${port} \\\\Node: ${nodePath}"

#jobout
if [[ ${hasOutFile} -eq 1 ]] ; then

    if [ "${foundPattern}" = "1" ] ; then
        descOutFile="These are the last ${lastLines} lines preceding pattern=${pattern} from the node job output file: {code}$(cat ${outTmp}) {code}"
    else
        descOutFile="These are the last ${lastLines} lines from the node job output file:  {code}$(cat ${outTmp}) {code}"
    fi
else
    descOutFile="Could not locate job output file."
fi


#log
if [[ ${hasLog} -eq 1 ]] ; then
    descLog="These are the entries for the given node in the ecflow log: {code}$(cat ${logTmp}) {code}"
else
    descLog="No entries for the given node were found in the ecflow log"
fi

desc="${desc}\\\\ \\\\${descOutFile} \\\\${descLog}"

#-------------------
# run command
#-------------------

#create jira issue
jiraTmp=$(mktemp)

if [[ $project != "_undef_" ]] ; then

    jiraCmdStr="${jiraExe} -S \"${host}@${port}:${nodePath} status=${nodeStatus}\" -D \${desc} --justdoit +${project}"
    ${jiraExe} -S "${nodePath} status=${nodeStatus}" -D "${desc}" --justdoit +${project} > ${jiraTmp} 2>&1

else
    #if no project is defined the defaultproject is taken from the .jirarc file!
    jiraCmdStr="${jiraExe} -S \"${host}@${port}:${nodePath} status=${nodeStatus}\" -D \${desc} --justdoit +"
    ${jiraExe} -S "${nodePath} status=${nodeStatus}" -D "${desc}" --justdoit + > ${jiraTmp} 2>&1
fi

cmdFailed=$?

#the exit code is still 0 for certain errors so we check
#if the output contains the string error
if [[ $cmdFailed -eq 0  && -s ${jiraTmp} ]] ; then
    if [[  $(grep -c -e "ERROR:" ${jiraTmp}) -gt 0  ]] ; then
        cmdFailed=1
    fi
fi

#if there is an error the command stdout and stderr is written to the stderr
if [[ $cmdFailed -ne 0 ]] ; then
    echo -e "Generating JIRA issue with command: \n   ${jiraCmdStr}\nfailed!" >&2
    if [[ -s ${jiraTmp} ]] ; then
        cat ${jiraTmp} >&2
        exitCode=1
    fi
else
    echo -e "Sucessfully generated JIRA issue with command: \n   ${jiraCmdStr}\n"
fi


#delete tmp files
[[ -f ${logTmp} ]] && rm -f ${logTmp}
[[ -f ${outTmp} ]] && rm -f ${outTmp}
[[ -f ${jiraTmp} ]] && rm -f ${jiraTmp}

exit ${exitCode}