-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcd-test.c
282 lines (254 loc) · 7.4 KB
/
etcd-test.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* =====================================================================================
*
* Filename: etcd-test.c
*
* Description:
*
* Version: 1.0
* Created: 05/07/2015 04:29:45 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "etcd-api.h"
void print_etcd_node(etcd_node_t *node){
int i;
if(node->key){
printf("key: %s ", node->key);
}
if(node->value){
printf("value: %s", node->value);
}
printf("\n");
if(node->dir){
for(i = 0; i < node->num_node; i++){
print_etcd_node((etcd_node_t*)node->nodes[i]);
}
}
}
void print_etcd_self(etcd_self_t *self){
printf("leader: %s \n", self->leader_id);
printf("self: %s \n", self->self_id);
printf("state: %s \n", self->state);
}
int
do_get (etcd_session sess, char *key){
etcd_node_t *node = NULL;
printf("getting %s\n",key);
node = etcd_get(sess,key);
if(node){
print_etcd_node(node);
free_etcd_node(node);
}
return 0;
}
int
do_watch (etcd_session sess, char *pfx, char *index_str){
etcd_node_t *node;
int index_i;
if (index_str) {
index_i = (int)strtol(index_str,NULL,10);
}
for (;;) {
node = etcd_watch(sess,pfx,&index_i);
if(node){
print_etcd_node(node);
free_etcd_node(node);
}
else{
printf("I don't know what happened\n");
}
sleep(1);
}
return 0;
}
int
do_set (etcd_session sess, char *key, char *value, etcd_prevcond_t *precond,
etcd_set_flag flag, char *ttl){
unsigned long ttl_num = 0;
printf("setting %s to %s\n",key,value);
if (precond) {
printf(" precond = %d %s\n",precond->type, precond->value);
}
if (ttl) {
ttl_num = strtoul(ttl,NULL,10);
printf(" ttl = %lu\n",ttl_num);
}
else {
ttl_num = 0;
}
if (etcd_set(sess,key,value,precond,flag,ttl_num) != ETCD_OK) {
fprintf(stderr,"etcd_set failed\n");
return 0;
}
return 0;
}
int do_set_dir(etcd_session sess, char *key, char *ttl){
unsigned long ttl_num = 0;
printf("setting directory %s\n",key);
if (ttl) {
ttl_num = strtoul(ttl,NULL,10);
printf(" ttl = %lu\n",ttl_num);
}
else {
ttl_num = 0;
}
if (etcd_set_dir(sess,key,ttl_num) != ETCD_OK) {
fprintf(stderr,"etcd_setdir failed\n");
return 0;
}
return 0;
}
int
do_delete (etcd_session sess, char *key){
printf("deleting %s\n",key);
if (etcd_delete(sess,key) != ETCD_OK) {
fprintf(stderr,"etcd_delete failed\n");
return !0;
}
return 0;
}
int
do_leader (etcd_session sess){
etcd_self_t *self = etcd_self(sess);
if (!self) {
fprintf(stderr,"etcd_delete failed\n");
return !0;
}
print_etcd_self(self);
free_etcd_self(self);
return 0;
}
struct option my_opts[] = {
{ "index", required_argument, NULL, 'w' },
{ "precond", required_argument, NULL, 'p' },
{ "servers", required_argument, NULL, 's' },
{ "ttl", required_argument, NULL, 't' },
{ NULL }
};
int
print_usage (char *prog){
fprintf (stderr, "Usage: %s [-s server-list] command ...\n",prog);
fprintf (stderr, "Valid commands:\n");
fprintf (stderr, " get KEY\n");
fprintf (stderr, " set [-p precond] [-t ttl] KEY VALUE\n");
fprintf (stderr, " delete KEY\n");
fprintf (stderr, " watch [-i index] KEY\n");
fprintf (stderr, " leader\n");
fprintf (stderr, " lock -t ttl [-i index] KEY\n");
fprintf (stderr, " unlock -i index KEY\n");
fprintf (stderr, "Server list is host:port pairs separated by comma,\n"
"semicolon, or white space. If not given on the\n"
"command line, ETCD_SERVERS will be used from the\n"
"environment instead.\n");
return !0;
}
int main(int argc, char **argv){
int opt;
char *servers = getenv("ETCD_SERVERS");
char *command;
etcd_prevcond_t *precond = NULL;
char *ttl = NULL;
char *index_str = NULL;
int parsed = 0;
etcd_session sess;
int res = !0;
int flag = 0;
for (;;) {
opt = getopt_long(argc,argv,"i:p:v:s:t:f:",my_opts,NULL);
if (opt == (-1)) {
break;
}
switch (opt) {
case 'i':
index_str = optarg;
break;
case 'p':
if(!precond){
precond = malloc(sizeof(etcd_prevcond_t));
}
precond->type = atoi(optarg);
break;
case 'v':
if(!precond){
precond = malloc(sizeof(etcd_prevcond_t));
}
precond->value = optarg;
break;
case 's':
servers = optarg;
break;
case 't':
ttl = optarg;
break;
case 'f':
flag = atoi(optarg);
break;
default:
return print_usage(argv[0]);
}
}
if (!servers) {
return print_usage(argv[0]);
}
if (optind == argc) {
return print_usage(argv[0]);
}
sess = etcd_open_str(strdup(servers));
if (!sess) {
fprintf(stderr,"etcd_open failed\n");
return 0;
}
command = argv[optind++];
if (!strcasecmp(command,"get")) {
if (((argc-optind) == 1) && !precond && !ttl && !index_str) {
parsed = 1;
do_get(sess,argv[optind]);
res = 0;
}
}
else if (!strcasecmp(command,"set")) {
if (((argc-optind) == 2) && !index_str) {
parsed = 1;
res = do_set (sess, argv[optind], argv[optind+1],
precond, flag, ttl);
if(precond){
free(precond);
}
}
}
else if (!strcasecmp(command,"setdir")) {
if (((argc-optind) == 1) && !index_str) {
parsed = 1;
res = do_set_dir(sess, argv[optind], ttl);
}
}
else if (!strcasecmp(command,"delete")) {
if (((argc-optind) == 1) && !precond && !ttl && !index_str) {
parsed = 1;
res = do_delete(sess,argv[optind]);
}
}
else if (!strcasecmp(command,"watch")) {
if (((argc-optind) == 1) && !precond && !ttl) {
parsed = 1;
res = do_watch(sess,argv[optind],index_str);
}
}
else if (!strcasecmp(command,"leader")) {
parsed = 1;
do_leader(sess);
}
etcd_close_str(sess);
return parsed ? res : print_usage(argv[0]);
}