-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_pulsar_topic.sh
executable file
·145 lines (117 loc) · 3.1 KB
/
create_pulsar_topic.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
#!/bin/bash
BINARY="pulsar-admin"
ADMIN_URL="http://10.0.2.100:8080"
validate_binary_path() {
if [ -x $1/$BINARY ]; then
return 0
fi
return 1
}
create_tenant() {
echo "creating tenant..."
# check if the tenant already exists.
$1 --admin-url $ADMIN_URL tenants list | grep ^\"$2\"$
if [ $? -eq 0 ]; then
return 0
fi
$1 --admin-url $ADMIN_URL tenants create $2
return $?
}
create_namespace() {
echo "creating namespace..."
# check if the namespace already exists.
$1 --admin-url $ADMIN_URL namespaces list $2 | grep ^\"$2/$3\"$
if [ $? -eq 0 ]; then
return 0
fi
$1 --admin-url $ADMIN_URL namespaces create $2/$3
return $?
}
create_partitioned_topic() {
$1 --admin-url $ADMIN_URL topics create-partitioned-topic \
persistent://$2/$3/$4 --partitions $5
}
create_non_partitioned_topic() {
$1 --admin-url $ADMIN_URL topics create \
persistent://$2/$3/$4
}
create_topic() {
echo "creating topic..."
if [ $# -eq 5 ]; then
if [[ $5 =~ ^[0-9]+$ ]]; then
create_partitioned_topic "$@"
return $?
else
usage "invalid argument - $5"
return 2
fi
else
create_non_partitioned_topic "$@"
return $?
fi
}
update_broker_configuration() {
$1 --admin-url $ADMIN_URL brokers update-dynamic-config \
--config $2 \
--value $3
return $?
}
set_retention_limit() {
if [ $# -lt 5 ]; then
usage "not enough arguments"
return 1
fi
$1 --admin-url $ADMIN_URL namespaces set-retention \
$2/$3 --size $4 --time $5
return $?
}
usage() {
echo "error: $1"
echo "Usage: $0 binary-path tenant namespace topic [number-of-partitions]" >&2
echo "Optons: \
binary-path - path to pulsar directory that contains the binaries.
tenant - valid tenant name.
namespace - valid namespace name.
topic - valid topic name.
number-of-partitions - number of partitions (integer)
"
}
# main
if [ $# -lt 4 ]; then
usage "not enough arguments"
exit 1
fi
# check whether the binary exists or not in the given path
validate_binary_path "$1"
if [ $? -eq 1 ]; then
echo "error: binary does not exists"
exit 1
fi
# pulsar-admin command
cmd="$1/$BINARY"
# update_broker_configuration $cmd "brokerDeleteInactiveTopicsEnabled" "false"
# if [ ! $? -eq 0 ]; then
# echo "error: failed to update broker configuration"
# exit 1
# fi
create_tenant "$cmd" "$2"
if [ ! $? -eq 0 ]; then
echo "error: failed to create tenant - $2"
exit 1
fi
create_namespace "$cmd" "$2" "$3"
if [ ! $? -eq 0 ]; then
echo "error: failed to create namespace - $2/$3"
exit 1
fi
set_retention_limit "$cmd" "$2" "$3" -1 -1
if [ ! $? -eq 0 ]; then
echo "error: failed to set retention limits on - $2/$3"
exit 1
fi
create_topic "$cmd" ${@:2}
if [ ! $? -eq 0 ]; then
echo "error: failed to create topic - $2/$3/$4"
exit 1
fi
echo "topic created successfully!"