This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions-dev
138 lines (115 loc) · 3.75 KB
/
functions-dev
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
#!/bin/bash
shopt -s expand_aliases
############
# WEBRH HELPER FUNCTIONS
# General: repositories must live in the same parent folder
#
# dependencies:
# -- USER variable; from bash_profile
# a string containing the user name
# -- REPOS array; from bash_profile
# an array of valid project repositories
# -- REPO_HOME variable; from bash_profile
# the path where all the repos exist together
# -- DRUPAL_LOC variable; from bash_profile
# path on your sandbox where Drupal is installed
# -- sandbox function required; from bash_profile
# returns sandbox path
############
for r in "${REPOS[@]}"; do
alias ${r}="cd ${REPO_HOME}/${r}"
done
function echoCmd {
echo $'\n-----------' && $@ && echo $'------------\n'
}
function getIndex {
search=$1; shift
array=( $* )
len=$(expr ${#array[@]})
i=$( echo ${array[@]/$search//} | cut -d/ -f1 | wc -w | tr -d ' ' )
[ $i -lt $len ] && echo $i || echo -1
}
function sshsandbox {
cmd="ssh $(sandbox ${1:-$USER})"
eval $cmd
}
# Run grunt watcher in the background
function bgwatch {
grunt watcher &>/dev/null &
return $!
}
function getRepo {
this=
for repo in ${REPOS[@]}; do
test=$(pwd | grep ${repo})
[ $? -eq 0 ] && this="${repo}"
done
# if neither repo
[ -z "$this" ] && echo "You are not in a valid repository." && return 1
echo "$this" && return 0
}
function topofrepo {
oldIFS=$IFS
repo=$(getRepo)
[ $? -gt 0 ] && echo "Must be in a valid project repository." && return
# Check full path and move to root of repository
path="$(pwd)"
path=${path#\/}
IFS='/' declare -a 'arr=( ${path%\/} )'
i=$( getIndex $repo ${arr[@]} )
root=
[[ $i -lt ${#arr[@]} ]] && root=$(IFS='/'; shift; echo "/${arr[*]:0:$i+1}") && cd $root
IFS=$oldIFS
}
function spin {
printf "\r${1}: [ ${sp:sc++:1} ] "
((sc==${#sp})) && sc=0
}
function endspin {
printf "\r%s\n" "$@"
}
function getBranchByRepo {
repo=$1
[[ $( getIndex $repo ${REPOS[@]} ) -le 0 ]] && echo "Please provide a valid repository name as an input." && return 1
start=$( pwd ) && eval "${repo}" && branch=$( currbranch ) && cd $start && echo $branch
}
# Kick off a jenkins build
function buildstatus {
sandbox=$(sandbox $1)
JOB_URL="${sandbox/root\@/http\:\/\/}:8080/job/webdrupal-update"
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json
GREP_RETURN_CODE=0
# Test if url is accessible
test=$(curl -s --max-time 5 --head $JOB_STATUS_URL | head -n 1 | tr -d '[:space:]')
if [[ ${test: -2} == "OK" ]]; then
# Poll every thirty seconds until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]; do
spin "Please wait for the Jenkins build to finish"
sleep 5
# Grep will return 0 while the build is running:
status=$(curl -s $JOB_STATUS_URL | grep result\":null)
GREP_RETURN_CODE=$?
done
endspin
success=$(curl -s $JOB_STATUS_URL | grep "result\":\"SUCCESS")
[ $? -eq 0 ] && echo "Build was successful!"
fail=$(curl -s $JOB_STATUS_URL | grep "result\":\"FAILURE")
[ $? -eq 0 ] && echo "Build failed :("
else
echo "Sandbox url is not available"
fi
}
function buildbox {
sandbox=$(sandbox $1)
JOB_URL="${sandbox/root\@/http\:\/\/}:8080/job/webdrupal-update"
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json
GREP_RETURN_CODE=0
# Check if a build is already running
test=$(curl -s $JOB_STATUS_URL | grep result\":null)
GREP_RETURN_CODE=$?
[[ $GREP_RETURN_CODE == 0 ]] && read -e -p "It looks like a build is already running; are you sure you want to kick off another one? [y/n]" choice
# Start the build
[[ $choice == "y" || $GREP_RETURN_CODE > 0 ]] && curl $JOB_URL/buildWithParameters?delay=0sec&GIT_BRANCH=$( getBranchByRepo webdrupal ) && open "${JOB_URL}"
# Poll every thirty seconds until the build is finished
#buildstatus $1
}