Modify /version to match the new /etc/pihole/versions file format

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2023-01-09 20:31:14 +01:00
parent d8e95f4e46
commit 8bbd49acf4
4 changed files with 210 additions and 67 deletions
+92 -35
View File
@@ -21,40 +21,97 @@ components:
version:
type: object
properties:
web:
version:
type: object
properties:
branch:
type: string
description: web branch
example: "master"
tag:
type: string
description: web tag
example: "v6.0"
core:
type: object
properties:
branch:
type: string
description: core branch
example: "master"
tag:
type: string
description: core tag
example: "v6.0"
ftl:
type: object
properties:
branch:
type: string
description: FTL branch
example: "master"
tag:
type: string
description: FTL tag
example: "v6.0"
date:
type: string
description: FTL build time
example: "2021-05-12 21:04:55 +0200"
core:
type: object
properties:
local:
type: object
properties:
branch:
type: string
description: Local Pi-hole Core branch
example: "development"
version:
type: string
description: Local Pi-hole Core version
example: "v6.1"
hash:
type: string
description: Local Pi-hole Core hash
example: "955e36a9"
remote:
type: object
properties:
version:
type: string
description: Remote (Github) Pi-hole Core version
example: "v6.1"
hash:
type: string
description: Remote (Github) Pi-hole Core hash
example: "955e36a9"
web:
type: object
properties:
local:
type: object
properties:
branch:
type: string
description: Local Pi-hole Web branch
example: "devel"
version:
type: string
description: Local Pi-hole Web version
example: "v6.1"
hash:
type: string
description: Local Pi-hole Web hash
example: "f69f7e88"
remote:
type: object
properties:
version:
type: string
description: Remote (Github) Pi-hole Web version
example: "v6.1"
hash:
type: string
description: Remote (Github) Pi-hole Web hash
example: "f69f7e88"
ftl:
type: object
properties:
local:
type: object
properties:
branch:
type: string
description: Local Pi-hole FTL branch
example: "development"
version:
type: string
description: Local Pi-hole FTL version
example: "v6.1"
hash:
type: string
description: Local Pi-hole FTL hash
example: "64441ed6-dirty"
date:
type: string
description: Build time of your local Pi-hole FTL
example: "2023-01-09 20:25:24 +0100"
remote:
type: object
properties:
version:
type: string
description: Remote (Github) Pi-hole FTL version
example: "v6.1"
hash:
type: string
description: Remote (Github) Pi-hole FTL hash
example: "64441ed6"
+86 -32
View File
@@ -8,52 +8,106 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "../FTL.h"
#include "../webserver/http-common.h"
#include "../webserver/json_macros.h"
#include "FTL.h"
#include "webserver/http-common.h"
#include "webserver/json_macros.h"
#include "api.h"
// get_FTL_version()
#include "../log.h"
#include "../version.h"
#include "log.h"
#include "version.h"
// prase_line()
#include "files.h"
#define VERSIONS_FILE "/etc/pihole/versions"
int api_version(struct ftl_conn *api)
{
cJSON *json = JSON_NEW_OBJECT();
char *line = NULL;
size_t len = 0;
ssize_t read;
char *key, *value;
cJSON *core_local = JSON_NEW_OBJECT();
cJSON *web_local = JSON_NEW_OBJECT();
cJSON *ftl_local = JSON_NEW_OBJECT();
cJSON *core_remote = JSON_NEW_OBJECT();
cJSON *web_remote = JSON_NEW_OBJECT();
cJSON *ftl_remote = JSON_NEW_OBJECT();
FILE* file;
char coreversion[256] = "N/A", webversion[256] = "N/A";
if((file = fopen("/etc/pihole/localversions", "r")) != NULL)
FILE *fp = fopen(VERSIONS_FILE, "r");
if(!fp)
return send_json_error(api, 500,
"internal_error",
"Failed to read " VERSIONS_FILE,
NULL);
// Loop over KEY=VALUE parts in the versions file
while((read = getline(&line, &len, fp)) != -1)
{
igr(fscanf(file, "%255s %255s", coreversion, webversion));
fclose(file);
}
char corebranch[256] = "N/A", webbranch[256] = "N/A";
if((file = fopen("/etc/pihole/localbranches", "r")) != NULL)
{
igr(fscanf(file, "%255s %255s", corebranch, webbranch));
fclose(file);
if (parse_line(line, &key, &value))
continue;
if(strcmp(key, "CORE_BRANCH") == 0)
JSON_COPY_STR_TO_OBJECT(core_local, "branch", value);
else if(strcmp(key, "WEB_BRANCH") == 0)
JSON_COPY_STR_TO_OBJECT(web_local, "branch", value);
// Added below from the running FTL binary itself
//else if(strcmp(key, "FTL_BRANCH") == 0)
// JSON_COPY_STR_TO_OBJECT(ftl_local, "branch", value);
else if(strcmp(key, "CORE_VERSION") == 0)
JSON_COPY_STR_TO_OBJECT(core_local, "version", value);
else if(strcmp(key, "WEB_VERSION") == 0)
JSON_COPY_STR_TO_OBJECT(web_local, "version", value);
// Added below from the running FTL binary itself
//else if(strcmp(key, "FTL_VERSION") == 0)
// JSON_COPY_STR_TO_OBJECT(ftl_local, "version", value);
else if(strcmp(key, "GITHUB_CORE_VERSION") == 0)
JSON_COPY_STR_TO_OBJECT(core_remote, "version", value);
else if(strcmp(key, "GITHUB_WEB_VERSION") == 0)
JSON_COPY_STR_TO_OBJECT(web_remote, "version", value);
else if(strcmp(key, "GITHUB_FTL_VERSION") == 0)
JSON_COPY_STR_TO_OBJECT(ftl_remote, "version", value);
else if(strcmp(key, "CORE_HASH") == 0)
JSON_COPY_STR_TO_OBJECT(core_local, "hash", value);
else if(strcmp(key, "WEB_HASH") == 0)
JSON_COPY_STR_TO_OBJECT(web_local, "hash", value);
else if(strcmp(key, "FTL_HASH") == 0)
JSON_COPY_STR_TO_OBJECT(ftl_local, "hash", value);
else if(strcmp(key, "GITHUB_CORE_HASH") == 0)
JSON_COPY_STR_TO_OBJECT(core_remote, "hash", value);
else if(strcmp(key, "GITHUB_WEB_HASH") == 0)
JSON_COPY_STR_TO_OBJECT(web_remote, "hash", value);
else if(strcmp(key, "GITHUB_FTL_HASH") == 0)
JSON_COPY_STR_TO_OBJECT(ftl_remote, "hash", value);
}
// Build web object
cJSON *web = JSON_NEW_OBJECT();
JSON_REF_STR_IN_OBJECT(web, "branch", webbranch);
JSON_REF_STR_IN_OBJECT(web, "tag", webversion);
JSON_ADD_ITEM_TO_OBJECT(json, "web", web);
// Free allocated memory and release file pointer
free(line);
fclose(fp);
// Add remaining properties to ftl object
JSON_REF_STR_IN_OBJECT(ftl_local, "branch", GIT_BRANCH);
JSON_REF_STR_IN_OBJECT(ftl_local, "version", get_FTL_version());
JSON_REF_STR_IN_OBJECT(ftl_local, "date", GIT_DATE);
cJSON *version = JSON_NEW_OBJECT();
// Build core object
cJSON *core = JSON_NEW_OBJECT();
JSON_REF_STR_IN_OBJECT(core, "branch", corebranch);
JSON_REF_STR_IN_OBJECT(core, "tag", coreversion);
JSON_ADD_ITEM_TO_OBJECT(json, "core", core);
JSON_ADD_ITEM_TO_OBJECT(core, "local", core_local);
JSON_ADD_ITEM_TO_OBJECT(core, "remote", core_remote);
JSON_ADD_ITEM_TO_OBJECT(version, "core", core);
cJSON *web = JSON_NEW_OBJECT();
JSON_ADD_ITEM_TO_OBJECT(web, "local", web_local);
JSON_ADD_ITEM_TO_OBJECT(web, "remote", web_remote);
JSON_ADD_ITEM_TO_OBJECT(version, "web", web);
// Build ftl object
cJSON *ftl = JSON_NEW_OBJECT();
JSON_REF_STR_IN_OBJECT(ftl, "branch", GIT_BRANCH);
const char *version = get_FTL_version();
JSON_REF_STR_IN_OBJECT(ftl, "tag", version);
JSON_REF_STR_IN_OBJECT(ftl, "date", GIT_DATE);
JSON_ADD_ITEM_TO_OBJECT(json, "ftl", ftl);
JSON_ADD_ITEM_TO_OBJECT(ftl, "local", ftl_local);
JSON_ADD_ITEM_TO_OBJECT(ftl, "remote", ftl_remote);
JSON_ADD_ITEM_TO_OBJECT(version, "ftl", ftl);
// Send reply
cJSON *json = JSON_NEW_OBJECT();
JSON_ADD_ITEM_TO_OBJECT(json, "version", version);
JSON_SEND_OBJECT(json);
}
+30
View File
@@ -217,3 +217,33 @@ int get_filepath_usage(const char *file, char buffer[64])
// Get percentage of disk usage at this path
return get_path_usage(path, buffer);
}
// Credits: https://stackoverflow.com/a/55410469
static char *trim(char *str)
{
char *start = str;
char *end = str + strlen(str);
while(*start && isspace(*start))
start++;
while(end > start && isspace(*(end - 1)))
end--;
*end = '\0';
return start;
}
// Credits: https://stackoverflow.com/a/55410469
int parse_line(char *line, char **key, char **value)
{
char *ptr = strchr(line, '=');
if (ptr == NULL)
return -1;
*ptr++ = '\0';
*key = trim(line);
*value = trim(ptr);
return 0;
}
+2
View File
@@ -19,4 +19,6 @@ void ls_dir(const char* path);
int get_path_usage(const char *path, char buffer[64]);
int get_filepath_usage(const char *file, char buffer[64]);
int parse_line(char *line, char **key, char **value);
#endif //FILE_H