Skip to content

Commit

Permalink
in_calyptia_fleet: fix registering agent after first invocation.
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Whelan <[email protected]>
  • Loading branch information
pwhelan authored and edsiper committed Nov 7, 2024
1 parent 43f71df commit e039c56
Showing 1 changed file with 92 additions and 5 deletions.
97 changes: 92 additions & 5 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx,
#define new_fleet_config_filename(a) fleet_config_filename((a), "new")
#define cur_fleet_config_filename(a) fleet_config_filename((a), "cur")
#define old_fleet_config_filename(a) fleet_config_filename((a), "old")
#define hdr_fleet_config_filename(a) fleet_config_filename((a), "header")

static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
const char *url,
Expand Down Expand Up @@ -388,7 +389,6 @@ static int is_timestamped_fleet_config_path(struct flb_in_calyptia_fleet_config
errno = 0;
val = strtol(fname, &end, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) {
flb_errno();
return FLB_FALSE;
}

Expand Down Expand Up @@ -480,6 +480,23 @@ static int exists_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
return ret;
}

static int exists_header_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
int ret = FLB_FALSE;
flb_sds_t cfgheadername;

cfgheadername = hdr_fleet_config_filename(ctx);
if (cfgheadername == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}

ret = access(cfgheadername, F_OK) == 0 ? FLB_TRUE : FLB_FALSE;
flb_sds_destroy(cfgheadername);

return ret;
}

static void *do_reload(void *data)
{
struct reload_ctx *reload = (struct reload_ctx *)data;
Expand Down Expand Up @@ -1665,12 +1682,68 @@ static void fleet_config_get_properties(flb_sds_t *buf, struct mk_list *props)
}
}

static flb_sds_t get_fleet_id_from_header(struct flb_in_calyptia_fleet_config *ctx)
{
struct mk_list *head;
struct flb_cf_section *section;
flb_sds_t fleet_id;
flb_sds_t name;
struct flb_cf *cf_hdr;


if (exists_header_fleet_config(ctx)) {
cf_hdr = flb_cf_create_from_file(NULL, hdr_fleet_config_filename(ctx));

if (cf_hdr == NULL) {
flb_cf_destroy(cf_hdr);
return NULL;
}

mk_list_foreach(head, &cf_hdr->sections) {
section = mk_list_entry(head, struct flb_cf_section, _head);

if (strcasecmp(section->name, "custom") != 0) {
continue;
}

name = flb_cf_section_property_get_string(cf_hdr, section, "name");

if (!name) {
flb_plg_error(ctx->ins, "no name in fleet header");
flb_cf_destroy(cf_hdr);
return NULL;
}

if (strcasecmp(name, "calyptia") != 0) {
flb_sds_destroy(name);
continue;
}
flb_sds_destroy(name);

fleet_id = flb_cf_section_property_get_string(cf_hdr, section, "fleet_id");

if (!fleet_id) {
flb_plg_error(ctx->ins, "no fleet_id in fleet header");
flb_cf_destroy(cf_hdr);
return NULL;
}

flb_cf_destroy(cf_hdr);
return fleet_id;
}
}

flb_cf_destroy(cf_hdr);
return NULL;
}

flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
{
flb_sds_t buf;
struct mk_list *head;
struct flb_custom_instance *c_ins;
flb_ctx_t *flb = flb_context_get();
flb_sds_t fleet_id = NULL;


buf = flb_sds_create_size(2048);
Expand All @@ -1679,7 +1752,6 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
return NULL;
}

/* [INPUT] */
mk_list_foreach(head, &flb->config->customs) {
c_ins = mk_list_entry(head, struct flb_custom_instance, _head);
if (strcasecmp(c_ins->p->name, "calyptia")) {
Expand All @@ -1690,8 +1762,21 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)

fleet_config_get_properties(&buf, &c_ins->properties);

if (ctx->fleet_id && flb_config_prop_get("fleet_id", &c_ins->properties) == NULL) {
flb_sds_printf(&buf, " fleet_id %s\n", ctx->fleet_id);
if (flb_config_prop_get("fleet_id", &c_ins->properties) == NULL) {
if (ctx->fleet_id != NULL) {
flb_sds_printf(&buf, " fleet_id %s\n", ctx->fleet_id);
}
else {
fleet_id = get_fleet_id_from_header(ctx);

if (fleet_id == NULL) {
flb_plg_error(ctx->ins, "unable to get fleet_id from header");
return NULL;
}

flb_sds_printf(&buf, " fleet_id %s\n", fleet_id);
flb_sds_destroy(fleet_id);
}
}
}
flb_sds_printf(&buf, "\n");
Expand Down Expand Up @@ -2215,7 +2300,9 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in,
/* refresh calyptia settings before attempting to load the fleet
* configuration file.
*/
create_fleet_header(ctx);
if (exists_header_fleet_config(ctx) == FLB_TRUE) {
create_fleet_header(ctx);
}

/* if we load a new configuration then we will be reloaded anyways */
if (load_fleet_config(ctx) == FLB_TRUE) {
Expand Down

0 comments on commit e039c56

Please sign in to comment.