Skip to content

Commit 7594954

Browse files
Merge pull request #44 from sachingade20/log-utility
Added log utility to copy logs from CBCD deployments
2 parents 56253a0 + 89f5de6 commit 7594954

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# Simple Log Utility to fetch/copy Logs for CBCD components from deployed kubernetes/openshift namespaces.
3+
4+
## Prerequisite:
5+
1. CD/RO or Software Delivery Automation deployed on Kubernetes platform.
6+
2. Install kubectl https://kubernetes.io/docs/tasks/tools/
7+
8+
9+
## How it works
10+
11+
$> bash get-logs.sh -help
12+
13+
Usage:
14+
-n Namespace: The name of the Kubernetes namespace/project where CD/RO or Software Delivery Automation is deployed
15+
-c (Optional) Component: The name of component to fetch logs. defaults to all
16+
all, flow-server, flow-web, flow-devopsinsight, flow-bound-agent, flow-agent
17+
-o (Optional)Output path to copy logs. default is /tmp.
18+
-k (Optional)Kubeconfig file path to connect to k8s cluster.
19+
20+
## Examples:
21+
./get-logs.sh -n <namespace> -c <Component> -o <output-path> -k <kube-config path>
22+
./get-logs.sh -n flow-demo -c all -o /tmp/flow-logs
23+
./get-logs.sh -n flow-demo -c flow-server -o /tmp/flow-logs
24+
./get-logs.sh -n flow-demo -c flow-server -k /home/foo/kubeconfigfile
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#! /bin/bash
2+
3+
4+
kctl() {
5+
if [ ! -z $KUBE_CONFIG_PATH ] ; then
6+
kubectl --kubeconfig $KUBE_CONFIG_PATH -n $NAMESPACE "$@"
7+
else
8+
kubectl -n $NAMESPACE "$@"
9+
fi
10+
}
11+
12+
function get_logs
13+
{
14+
C_FETCH="flow"
15+
if [ $COMPONENT == "all" ] ; then
16+
C_FETCH="flow"
17+
else
18+
C_FETCH=$COMPONENT
19+
fi
20+
for i in $(kctl get po|awk '{print $1}'|awk 'NR!=1'|grep $C_FETCH)
21+
do
22+
echo "Fetching logs for $i"
23+
echo "Creating Directory $OUTPUT/$i/logs"
24+
mkdir -p $OUTPUT/$i/logs
25+
echo "Copying logs from $i to $OUTPUT/$i/logs"
26+
kctl cp $i:/opt/cbflow/logs $OUTPUT/$i/logs
27+
done
28+
29+
}
30+
function initScript
31+
{
32+
NAMESPACE=""
33+
COMPONENT="all"
34+
OUTPUT="/tmp"
35+
KUBE_CONFIG_PATH=""
36+
while getopts h:n:c:o:k: opt
37+
do
38+
case "$opt" in
39+
h) usage "";exit 1;;
40+
n) NAMESPACE=$OPTARG;;
41+
c) COMPONENT=$OPTARG;;
42+
o) OUTPUT=$OPTARG;;
43+
k) KUBE_CONFIG_PATH=$OPTARG;;
44+
\?) usage "";exit 1;;
45+
esac
46+
done
47+
if [ -z $NAMESPACE ] ;
48+
then
49+
echo "$(date) Make sure you provide valid -n NAMESPACE"
50+
usage ""
51+
exit 1;
52+
fi
53+
54+
}
55+
56+
function usage
57+
{
58+
cat <<EOF
59+
60+
Usage:
61+
./get-logs.sh -n <Namespace> -c <Component> -o <Output> -k <kubeconfig-file-path>
62+
63+
Options:
64+
-n Namespace: The name of the Kubernetes namespace/project where CD/RO or Software Delievery Automation is deployed
65+
-c (Optional) Component: The name of component to fetch logs. defaults to all
66+
all, flow-server, flow-web, flow-devopsinsight, flow-bound-agent, flow-agent
67+
-o (Optional)Output path to copy logs. default is /tmp.
68+
-k (Optional)kubeconfig file path to connect to k8s cluster.
69+
70+
Examples:
71+
./get-logs.sh -n flow-demo
72+
./get-logs.sh -n flow-demo -c all -o /tmp/flow-logs
73+
./get-logs.sh -n flow-demo -c flow-server -o /tmp/flow-logs
74+
./get-logs.sh -n flow-demo -c flow-server -k /home/foo/kubeconfigfile
75+
EOF
76+
}
77+
78+
function main
79+
{
80+
initScript "$@"
81+
kctl get ns $NAMESPACE
82+
exit_code=$?
83+
if [ $exit_code != 0 ];
84+
then
85+
echo "$(date) Please set valid namespace "
86+
echo "$(date) Please check kubectl configuration is set correctly. Logging utility not able to reach to k8s cluster"
87+
echo "$(date) Please set valid kubeconfig file if exist "
88+
exit 1;
89+
fi
90+
get_logs "$@"
91+
}
92+
93+
main "$@"

0 commit comments

Comments
 (0)