Skip to content

Commit

Permalink
in_calyptia_fleet: create fleet directory with machine_id.
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Whelan <[email protected]>
  • Loading branch information
pwhelan committed Aug 22, 2023
1 parent 9e1a5e4 commit 57008dd
Showing 1 changed file with 61 additions and 12 deletions.
73 changes: 61 additions & 12 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,13 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx,

cfgname = flb_sds_create_size(4096);
if (ctx->fleet_name != NULL) {
flb_sds_printf(&cfgname, "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s.ini", ctx->config_dir, ctx->fleet_name, fname);
flb_sds_printf(&cfgname,
"%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s.ini",
ctx->config_dir, ctx->machine_id, ctx->fleet_name, fname);
} else {
flb_sds_printf(&cfgname, "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s.ini", ctx->config_dir, ctx->fleet_id, fname);
flb_sds_printf(&cfgname,
"%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s.ini",
ctx->config_dir, ctx->machine_id, ctx->fleet_id, fname);
}

return cfgname;
Expand Down Expand Up @@ -769,31 +773,76 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
FLB_INPUT_RETURN(ret);
}

static void create_fleet_directory(struct flb_in_calyptia_fleet_config *ctx)
// recursively create directories, based on:
// https://stackoverflow.com/a/2336245
// who found it at:
// http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html
static int _mkdir(const char *dir, int perms) {
char tmp[255];
char *p = NULL;
size_t len;
int rc;

rc = snprintf(tmp, sizeof(tmp),"%s",dir);
if (rc > sizeof(tmp)) {
return -1;
}

len = strlen(tmp);
if (tmp[len - 1] == '/') {
tmp[len - 1] = 0;
}

for (p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = 0;
if (access(tmp, F_OK) != 0) {
rc = mkdir(tmp, perms);
if (rc != 0) {
return rc;
}
}
*p = '/';
}
}
return mkdir(tmp, perms);
}

static int create_fleet_directory(struct flb_in_calyptia_fleet_config *ctx)
{
flb_sds_t myfleetdir;

if (access(ctx->config_dir, F_OK)) {
mkdir(ctx->config_dir, 0700);
if (access(ctx->config_dir, F_OK) != 0) {
if (_mkdir(ctx->config_dir, 0700) != 0) {
return -1;
}
}

myfleetdir = flb_sds_create_size(256);
if (ctx->fleet_name != NULL) {
flb_sds_printf(&myfleetdir, "%s" PATH_SEPARATOR "%s", ctx->config_dir, ctx->fleet_name);
flb_sds_printf(&myfleetdir, "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s",
ctx->config_dir, ctx->machine_id, ctx->fleet_name);
} else {
flb_sds_printf(&myfleetdir, "%s" PATH_SEPARATOR "%s", ctx->config_dir, ctx->fleet_id);
flb_sds_printf(&myfleetdir, "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s",
ctx->config_dir, ctx->machine_id, ctx->fleet_id);
}
if (access(myfleetdir, F_OK)) {
mkdir(myfleetdir, 0700);
if (access(myfleetdir, F_OK) != 0) {
if (_mkdir(myfleetdir, 0700) !=0) {
return -1;
}
}
flb_sds_destroy(myfleetdir);
return 0;
}

static void load_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
static int load_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
flb_ctx_t *flb_ctx = flb_context_get();


create_fleet_directory(ctx);
if (create_fleet_directory(ctx) != 0) {
return -1;
}

// check if we are already using the fleet configuration file.
if (is_fleet_config(ctx, flb_ctx->config) == FLB_FALSE) {
Expand All @@ -804,6 +853,7 @@ static void load_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
execute_reload(ctx, new_fleet_config_filename(ctx));
}
}
return 0;
}

static int in_calyptia_fleet_init(struct flb_input_instance *in,
Expand Down Expand Up @@ -897,7 +947,6 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in,
ctx->collect_fd = ret;

load_fleet_config(ctx);

return 0;
}

Expand Down

0 comments on commit 57008dd

Please sign in to comment.