Mercurial > dovecot > core-2.2
changeset 19078:7ac9ec913a39
doveadm director update command added.
The difference to "doveadm director add" is that if the IP doesn't already
exist, it's not added.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 03 Sep 2015 16:04:26 +0300 |
parents | f17065f4f9f2 |
children | 83fe8ab89c0e |
files | src/director/doveadm-connection.c src/doveadm/doveadm-director.c |
diffstat | 2 files changed, 53 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/src/director/doveadm-connection.c Thu Sep 03 14:10:36 2015 +0300 +++ b/src/director/doveadm-connection.c Thu Sep 03 16:04:26 2015 +0300 @@ -246,7 +246,8 @@ } static bool -doveadm_cmd_host_set(struct doveadm_connection *conn, const char *line) +doveadm_cmd_host_set_or_update(struct doveadm_connection *conn, const char *line, + bool update) { struct director *dir = conn->dir; const char *const *args, *ip_str, *tag = ""; @@ -264,8 +265,10 @@ ip_str = t_strdup_until(ip_str, tag++); } if (ip_str == NULL || net_addr2ip(ip_str, &ip) < 0 || - (args[1] != NULL && str_to_uint(args[1], &vhost_count) < 0)) { - i_error("doveadm sent invalid HOST-SET parameters: %s", line); + (args[1] != NULL && str_to_uint(args[1], &vhost_count) < 0) || + (args[1] == NULL && update)) { + i_error("doveadm sent invalid %s parameters: %s", + update ? "HOST-UPDATE" : "HOST-SET", line); return FALSE; } if (vhost_count > MAX_VALID_VHOST_COUNT && vhost_count != UINT_MAX) { @@ -273,11 +276,16 @@ return TRUE; } host = mail_host_lookup(dir->mail_hosts, &ip); - if (host == NULL) + if (host == NULL) { + if (update) { + o_stream_nsend_str(conn->output, "NOTFOUND\n"); + return TRUE; + } host = mail_host_add_ip(dir->mail_hosts, &ip, tag); + } if (vhost_count != UINT_MAX) mail_host_set_vhost_count(dir->mail_hosts, host, vhost_count); - /* NOTE: we don't supporting changing a tag for an existing host. + /* NOTE: we don't support changing a tag for an existing host. it needs to be removed first. otherwise it would be a bit ugly to handle. */ director_update_host(dir, dir->self_host, NULL, host); @@ -287,6 +295,18 @@ } static bool +doveadm_cmd_host_set(struct doveadm_connection *conn, const char *line) +{ + return doveadm_cmd_host_set_or_update(conn, line, FALSE); +} + +static bool +doveadm_cmd_host_update(struct doveadm_connection *conn, const char *line) +{ + return doveadm_cmd_host_set_or_update(conn, line, TRUE); +} + +static bool doveadm_cmd_host_updown(struct doveadm_connection *conn, bool down, const char *line) { @@ -616,6 +636,8 @@ ret = doveadm_cmd_director_remove(conn, args); else if (strcmp(cmd, "HOST-SET") == 0) ret = doveadm_cmd_host_set(conn, args); + else if (strcmp(cmd, "HOST-UPDATE") == 0) + ret = doveadm_cmd_host_update(conn, args); else if (strcmp(cmd, "HOST-UP") == 0) ret = doveadm_cmd_host_updown(conn, FALSE, args); else if (strcmp(cmd, "HOST-DOWN") == 0)
--- a/src/doveadm/doveadm-director.c Thu Sep 03 14:10:36 2015 +0300 +++ b/src/doveadm/doveadm-director.c Thu Sep 03 16:04:26 2015 +0300 @@ -407,7 +407,9 @@ pool_unref(&pool); } -static void cmd_director_add(int argc, char *argv[]) +static void +cmd_director_add_or_update(int argc, char *argv[], doveadm_command_t *cmd_func, + const char *director_cmd) { struct director_context *ctx; struct ip_addr *ips; @@ -415,18 +417,20 @@ const char *host, *line; string_t *cmd; - ctx = cmd_director_init(argc, argv, "a:t:", cmd_director_add); + ctx = cmd_director_init(argc, argv, "a:t:", cmd_func); if (ctx->tag != NULL && ctx->tag[0] == '\0') ctx->tag = NULL; host = argv[optind++]; if (host == NULL) - director_cmd_help(cmd_director_add); + director_cmd_help(cmd_func); if (argv[optind] != NULL) { if (str_to_uint(argv[optind++], &vhost_count) < 0) - director_cmd_help(cmd_director_add); - } + director_cmd_help(cmd_func); + } else if (strcmp(director_cmd, "HOST-UPDATE") == 0) + director_cmd_help(cmd_func); + if (argv[optind] != NULL) - director_cmd_help(cmd_director_add); + director_cmd_help(cmd_func); if (ctx->tag == NULL) { ctx->tag = strchr(host, '@'); @@ -437,7 +441,7 @@ cmd = t_str_new(128); for (i = 0; i < ips_count; i++) { str_truncate(cmd, 0); - str_printfa(cmd, "HOST-SET\t%s", net_ip2addr(&ips[i])); + str_printfa(cmd, "%s\t%s", director_cmd, net_ip2addr(&ips[i])); if (ctx->tag != NULL) str_printfa(cmd, "@%s", ctx->tag); if (vhost_count != UINT_MAX) @@ -449,7 +453,9 @@ line = i_stream_read_next_line(ctx->input); if (line == NULL || strcmp(line, "OK") != 0) { fprintf(stderr, "%s: %s\n", net_ip2addr(&ips[i]), - line == NULL ? "failed" : line); + line == NULL ? "failed" : + strcmp(line, "NOTFOUND") == 0 ? + "doesn't exist" : line); doveadm_exit_code = EX_TEMPFAIL; } else if (doveadm_verbose) { printf("%s: OK\n", net_ip2addr(&ips[i])); @@ -458,6 +464,16 @@ director_disconnect(ctx); } +static void cmd_director_add(int argc, char *argv[]) +{ + cmd_director_add_or_update(argc, argv, cmd_director_add, "HOST-SET"); +} + +static void cmd_director_update(int argc, char *argv[]) +{ + cmd_director_add_or_update(argc, argv, cmd_director_update, "HOST-UPDATE"); +} + static void cmd_director_ipcmd(const char *cmd_name, doveadm_command_t *cmd, const char *success_result, int argc, char *argv[]) @@ -808,6 +824,8 @@ "[-a <director socket path>] [-f <users file>] [-h | -u] [<host>]" }, { cmd_director_add, "director add", "[-a <director socket path>] [-t <tag>] <host> [<vhost count>]" }, + { cmd_director_update, "director update", + "[-a <director socket path>] [-t <tag>] <host> <vhost count>" }, { cmd_director_up, "director up", "[-a <director socket path>] <host>" }, { cmd_director_down, "director down",