-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
158 lines (127 loc) · 3.59 KB
/
utils.sh
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
check_env_var() {
var_name=$1
if [ "${!var_name}" = "" ]; then
echo "You must set $1 before running these scripts."
exit 1
fi
}
announce() {
echo "++++++++++++++++++++++++++++++++++++++"
echo ""
echo "$@"
echo ""
echo "++++++++++++++++++++++++++++++++++++++"
}
environment_url() {
oc status | head -1 | egrep -o 'https?://[^ ]+' | awk -F: '{print $1":"$2}'
}
environment_domain() {
env_url=$(environment_url)
protocol="$(echo $env_url | grep :// | sed -e's,^\(.*://\).*,\1,g')"
echo ${env_url/$protocol/}
}
has_project() {
if [[ "$(oc projects | grep $1)" != "" ]]; then
true
else
false
fi
}
docker_tag_and_push() {
docker_tag="${DOCKER_REGISTRY_PATH}/$1/$2:$1"
docker tag $2:$1 $docker_tag
docker push $docker_tag
}
copy_file_to_container() {
local from=$1
local to=$2
local pod_name=$3
local source_file_path=$from
local source_file_name="$(basename "$source_file_path")"
local parent_path="$(dirname "$source_file_path")"
local parent_name="$(basename "$parent_path")"
local container_temp_path="/tmp"
oc exec $pod_name -- mkdir -p $container_temp_path
oc rsync --no-perms=true "$parent_path" "$pod_name:$container_temp_path"
oc exec "$pod_name" mv "$container_temp_path/$parent_name/$source_file_name" "$to"
oc exec "$pod_name" rm -- -rf "$container_temp_path/$parent_name"
}
get_conjur_cli_pod_name() {
pod_list=$(oc get pods -l app=conjur-cli --no-headers | awk '{ print $1 }')
echo $pod_list | awk '{print $1}'
}
get_master_pod_name() {
cont_list=$(docker ps -f "label=role=conjur_node" --format "{{.Names}}")
for i in $cont_list; do
if [[ $(docker exec $i evoke role) == master ]]; then
echo -n $i
exit 0
fi
done
}
get_cluster_leader_name() {
cont_list=$(docker ps -f "label=role=conjur_node" --format "{{.Names}}")
for i in $cont_list; do
leader_name=$(docker exec -it $i etcdctl member list | grep isLeader=true | awk '{ print $2 }' | cut -d = -f 2)
if [[ "$leader_name" != "" ]]; then
echo -n $leader_name
exit 0
fi
done
}
mastercmd() {
local current_project=$(oc projects | grep \* | awk '{ print $2 }')
set_project $CONJUR_NAMESPACE_NAME
local master_pod=$(oc get pod -l role=master --no-headers | awk '{ print $1 }')
local interactive=$1
if [ $interactive = '-i' ]; then
shift
oc exec -i $master_pod -- $@
else
oc exec $master_pod -- $@
fi
set_project "$current_project"
}
set_project() {
# general utility for switching projects/namespaces/contexts in openshift
# expects exactly 1 argument, a project name.
if [[ $# != 1 ]]; then
printf "Error in %s/%s - expecting 1 arg.\n" $(pwd) $0
exit -1
fi
oc project $1 > /dev/null
}
wait_for_node() {
wait_for_it -1 "oc describe pod $1 | grep Status: | grep -q Running"
}
function wait_for_it() {
local timeout=$1
local spacer=2
shift
if ! [ $timeout = '-1' ]; then
local times_to_run=$((timeout / spacer))
echo "Waiting for $@ up to $timeout s"
for i in $(seq $times_to_run); do
eval $@ && echo 'Success!' && break
echo -n .
sleep $spacer
done
eval $@
else
echo "Waiting for $@ forever"
while ! eval $@; do
echo -n .
sleep $spacer
done
echo 'Success!'
fi
}
rotate_api_key() {
set_project $CONJUR_NAMESPACE_NAME
master_pod_name=$(get_master_pod_name)
docker exec -it $master_pod_name conjur authn login -u admin -p $CONJUR_ADMIN_PASSWORD > /dev/null
api_key=$(docker exec -it $master_pod_name conjur user rotate_api_key)
docker exec -it $master_pod_name conjur authn logout > /dev/null
echo $api_key
}