Compare commits

...

9 Commits

Author SHA1 Message Date
OpenGnSys Support Team 5cecb4f294 client: incorrect OS update after restoring image
refresh handler already updates the OS accordingly, remove broken code to
update OS that results in reporting no bootable OS after image restoration.
2025-02-13 19:19:31 +01:00
OpenGnSys Support Team 730d42a872 src: handle refresh response after image restore
use context to store disk, partition and image id that is requested to client
and handle refresh response from ogclient to update database, otherwise, an
explicit refresh from web frontend is required.
2025-02-13 13:24:42 +01:00
OpenGnSys Support Team 91c042ed61 client: incorrect partition type conversion to hexadecimal
partition code is already in decimal as integer, appending 0x to this decimal
results in an incorrect hexadecimal conversion when storing it to the database.

The web frontend bogusly displays "Empty" as partition type until a refresh
happens.
2025-02-12 13:42:48 +01:00
Alejandro Sirgo Rica acf9c07236 rest: send image/update to ogClient for update operations
Send image/update instead of only image/create for both update
and create operations. A failed update operation using the
error handler of OG_CMD_IMAGE_CREATE results in the deletion
of the image from database.

Fail if the image already exists for an image create operation.
2025-02-04 09:42:36 +01:00
OpenGnSys Support Team 4db69ad054 boot: set hostname in grub configuration
add missing hostname= to grub configuration.
2025-01-30 14:09:05 +01:00
OpenGnSys Support Team d8b271f503 cfg: rename user in ogserver.json
use default user in samba, otherwise it breaks ogrelive
2025-01-21 21:21:56 +01:00
Alejandro Sirgo Rica 2daa9cdb36 src: prevent unlinking image to a partition
Fix partition code parse from strng to integer in
og_update_client_disk_info(), forcing a positive partition format
detection every refresh. This caused the unlink of images in every
partition during a refresh command.

Remove image idimagen=0 SQL instruction from software inventory.
Prevent image unlink during a sofware inventory command for a specific
partition.
2025-01-14 14:59:11 +01:00
OpenGnSys Support Team 41c8dec51c rest: use http error code to report mac is in use
report 409 conflict if mac is in use
2025-01-13 12:30:45 +01:00
Alejandro Sirgo Rica 31d426b018 rest: prevent repo deletion if it is in use
Check if the repo is used by any client before the deletion in
/repository/delete
2024-12-16 11:59:15 +01:00
7 changed files with 97 additions and 41 deletions

View File

@ -12,7 +12,7 @@
"pass" : "mysql"
},
"samba" : {
"user" : "og",
"user" : "opengnsys",
"pass" : "test"
},
"wol" : {

View File

@ -20,6 +20,7 @@
#define OGRELIVE_TEMPLATE \
"# Autogenerated by ogserver on %s - do not edit this file!\n" \
"set oghostname=%s\n" \
"set ogserver=%s\n" \
"set ogrepo=%s\n" \
"set ogreliveversion=%s\n" \
@ -45,7 +46,7 @@
" insmod http\n" \
" insmod net\n" \
" net_bootp\n" \
" linux (http,$ogrepo)/$ogrelivedir/vmlinuz ro boot=live quiet splash oglivedir=$ogrelivedir ip=$ogserver ogrepo=$ogrepo username=$username passwd=$passwd fetch=http://$ogrepo/$ogrelivedir/filesystem.squashfs\n" \
" linux (http,$ogrepo)/$ogrelivedir/vmlinuz ro boot=live quiet splash oglivedir=$ogrelivedir hostname=$oghostname ip=$ogserver ogrepo=$ogrepo username=$username passwd=$passwd fetch=http://$ogrepo/$ogrelivedir/filesystem.squashfs\n" \
" initrd (http,$ogrepo)/$ogrelivedir/initrd.img\n" \
" boot\n" \
"}\n"
@ -92,7 +93,7 @@ int ogrelive_generate_grub2_file(const struct og_boot_cfg *cfg, const char *mac)
}
fprintf(fp, OGRELIVE_TEMPLATE, date,
cfg->ogserver, cfg->ogrepo, cfg->ogrelivedir, cfg->username, cfg->passwd);
cfg->hostname, cfg->ogserver, cfg->ogrepo, cfg->ogrelivedir, cfg->username, cfg->passwd);
fclose(fp);

View File

@ -2,6 +2,7 @@
#define _OG_BOOT_H_
struct og_boot_cfg {
const char *hostname;
const char *ogserver;
const char *ogrepo;
const char *ogrelivedir;

View File

@ -673,7 +673,7 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
partitions[i].size, i + 1, __func__, __LINE__);
return false;
}
if (safe_strtoull(partitions[i].code, &part_code, 32, UINT32_MAX) < 0) {
if (safe_strtoull(partitions[i].code, &part_code, 16, UINT32_MAX) < 0) {
syslog(LOG_ERR, "failed to parse partition code %s for partition %d (%s:%d)\n",
partitions[i].code, i + 1, __func__, __LINE__);
return false;
@ -693,7 +693,7 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
"idordenador, numdisk, numpar, codpar, tamano, uso, "
"idsistemafichero, idnombreso, idimagen, "
"used_size, free_size)"
" VALUES(%d,%s,%s,0x%d,%s,0,%d,%d,0,%lu,%lu)",
" VALUES(%d,%s,%s,%d,%s,0,%d,%d,0,%lu,%lu)",
computer_id,
partitions[i].disk,
partitions[i].number,
@ -1154,37 +1154,17 @@ static int og_resp_image_create(json_t *data, struct og_client *cli)
static int og_resp_image_restore(json_t *data, struct og_client *cli)
{
struct og_software_legacy soft_legacy;
uint32_t disk, partition, image_id;
struct og_image_legacy img_legacy;
struct og_computer computer = {};
const char *partition = NULL;
const char *image_id = NULL;
const char *disk = NULL;
const char *msglog;
struct og_dbi *dbi;
dbi_result result;
const char *key;
json_t *value;
int err = 0;
if (json_typeof(data) != JSON_OBJECT)
return -1;
json_object_foreach(data, key, value) {
if (!strcmp(key, "partition"))
err = og_json_parse_string(value, &partition);
else if (!strcmp(key, "disk"))
err = og_json_parse_string(value, &disk);
else if (!strcmp(key, "image_id"))
err = og_json_parse_string(value, &image_id);
if (err < 0)
return err;
}
if (!partition || !disk || !image_id) {
syslog(LOG_ERR, "malformed response json\n");
return -1;
}
err = og_resp_refresh(data, cli);
if (err < 0)
return err;
dbi = og_dbi_open(&ogconfig.db);
if (!dbi) {
@ -1193,9 +1173,13 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli)
return -1;
}
image_id = cli->last_cmd.ctx.image_restore.id;
partition = cli->last_cmd.ctx.image_restore.part;
disk = cli->last_cmd.ctx.image_restore.disk;
result = dbi_conn_queryf(dbi->conn,
"SELECT idperfilsoft FROM imagenes "
" WHERE idimagen='%s'", image_id);
" WHERE idimagen='%u'", image_id);
if (!result) {
og_dbi_close(dbi);
syslog(LOG_ERR, "failed to query database\n");
@ -1204,7 +1188,8 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli)
if (!dbi_result_next_row(result)) {
dbi_result_free(result);
og_dbi_close(dbi);
syslog(LOG_ERR, "software profile does not exist in database\n");
syslog(LOG_ERR, "software profile for image id %u does not exist in database\n",
image_id);
return -1;
}
snprintf(img_legacy.software_id, sizeof(img_legacy.software_id),
@ -1217,20 +1202,19 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli)
return -1;
}
snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s",
snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%u",
image_id);
snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition);
snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk);
snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id);
snprintf(img_legacy.part, sizeof(img_legacy.part), "%u", partition);
snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%u", disk);
snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%u", computer.id);
result = dbi_conn_queryf(dbi->conn,
"UPDATE ordenadores_particiones"
" SET idimagen=%s, idperfilsoft=%s, fechadespliegue=NOW(),"
" revision=(SELECT revision FROM imagenes WHERE idimagen=%s),"
" idnombreso=IFNULL((SELECT idnombreso FROM perfilessoft WHERE idperfilsoft=%s),0)"
" revision=(SELECT revision FROM imagenes WHERE idimagen=%s)"
" WHERE idordenador=%s AND numdisk=%s AND numpar=%s",
img_legacy.image_id, img_legacy.software_id,
img_legacy.image_id, img_legacy.software_id,
img_legacy.image_id,
soft_legacy.id, img_legacy.disk, img_legacy.part);
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
@ -1404,6 +1388,9 @@ int og_agent_state_process_response(struct og_client *cli)
if (err < 0)
og_resp_image_create_error(cli);
break;
case OG_CMD_IMAGE_UPDATE:
err = og_resp_image_create(root, cli);
break;
case OG_CMD_IMAGE_RESTORE:
err = og_resp_image_restore(root, cli);
if (!err)

View File

@ -575,7 +575,7 @@ bool cuestionPerfilSoftware(struct og_dbi *dbi, char *idc, char *ido,
if (idperfilsoftware != nwidperfilsoft) { // No coinciden los perfiles
// Actualiza el identificador del perfil software del ordenador
result = dbi_conn_queryf(dbi->conn,
"UPDATE ordenadores_particiones SET idperfilsoft=%d,idimagen=0"
"UPDATE ordenadores_particiones SET idperfilsoft=%d"
" WHERE idordenador=%s AND numpar=%s", nwidperfilsoft, ido, par);
if (!result) { // Error al insertar
dbi_conn_error(dbi->conn, &msglog);

View File

@ -1721,6 +1721,7 @@ static int og_set_client_mode(struct og_dbi *dbi, const char *client_ip,
return -1;
}
boot_cfg.hostname = boot_params.nombreordenador;
boot_cfg.ogserver = boot_params.server_ip;
boot_cfg.ogrepo = boot_params.repo_ip;
boot_cfg.ogrelivedir = boot_params.oglivedir;
@ -3032,6 +3033,7 @@ static int og_cmd_post_client_add(struct og_rest_ctx *ctx)
computer.ip, computer_ip, computer_name, room_name, hwaddr);
dbi_result_free(result);
og_dbi_close(dbi);
ctx->http_error = OG_HTTP_409_CONFLICT;
return -1;
}
dbi_result_free(result);
@ -3834,6 +3836,7 @@ static int og_cmd_add_image(struct og_rest_ctx *ctx, bool update)
char repository_ip[OG_DB_IP_MAXLEN + 1];
char new_image_id[OG_DB_INT_MAXLEN + 1];
struct og_cmd_ctx cmd_ctx = {};
enum og_cmd_type cmd_type;
struct og_dbi *dbi;
json_t *body;
int err = 0;
@ -3886,6 +3889,13 @@ static int og_cmd_add_image(struct og_rest_ctx *ctx, bool update)
snprintf(new_image_id, sizeof(new_image_id), "%lu", params->image.id);
params->id = new_image_id;
} else {
if (!update) {
syslog(LOG_ERR, "cannot create image file `%s', it already exists\n",
params->image.name);
ctx->http_error = OG_HTTP_409_CONFLICT;
og_dbi_close(dbi);
return -1;
}
syslog(LOG_INFO, "updating existing image `%s'\n", params->image.name);
snprintf(new_image_id, sizeof(new_image_id), "%lu", params->image.id);
params->id = new_image_id;
@ -3905,7 +3915,12 @@ static int og_cmd_add_image(struct og_rest_ctx *ctx, bool update)
cmd_ctx.image.id = params->image.id;
return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_CREATE, params,
if (update)
cmd_type = OG_CMD_IMAGE_UPDATE;
else
cmd_type = OG_CMD_IMAGE_CREATE;
return og_send_request(OG_METHOD_POST, cmd_type, params,
body, &cmd_ctx);
}
@ -3966,6 +3981,8 @@ static int og_cmd_restore_image(struct og_rest_ctx *ctx)
{
char repository_ip[OG_DB_IP_MAXLEN + 1] = {};
struct og_msg_params *params = &ctx->params;
struct og_cmd_ctx cmd_ctx = {};
uint64_t image_id, disk, part;
struct og_dbi *dbi;
json_t *body;
int err;
@ -3999,12 +4016,33 @@ static int og_cmd_restore_image(struct og_rest_ctx *ctx)
if (err < 0)
return err;
if (safe_strtoull(params->id, &image_id, 10, UINT32_MAX) < 0) {
syslog(LOG_ERR, "failed to image id %s (%s:%d)\n",
params->id, __func__, __LINE__);
return -1;
}
cmd_ctx.image_restore.id = image_id;
if (safe_strtoull(params->disk, &disk, 10, UINT32_MAX) < 0) {
syslog(LOG_ERR, "failed to parse disk %s (%s:%d)\n",
params->disk, __func__, __LINE__);
return -1;
}
cmd_ctx.image_restore.disk = disk;
if (safe_strtoull(params->partition, &part, 10, UINT32_MAX) < 0) {
syslog(LOG_ERR, "failed to parse partition %s (%s:%d)\n",
params->partition, __func__, __LINE__);
return -1;
}
cmd_ctx.image_restore.part = part;
body = json_copy(ctx->json);
json_object_del(body, "clients");
json_object_set_new(body, "repository", json_string(repository_ip));
return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_RESTORE, params,
body, NULL);
body, &cmd_ctx);
}
static int og_cmd_delete_image(struct og_rest_ctx *ctx)
@ -5233,6 +5271,30 @@ static int og_cmd_post_repository_delete(struct og_rest_ctx *ctx)
return -1;
}
result = dbi_conn_queryf(dbi->conn,
"SELECT idrepositorio FROM ordenadores "
"WHERE idrepositorio=%u",
repo_id);
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
__func__, __LINE__, msglog);
og_dbi_close(dbi);
ctx->http_error = OG_HTTP_500_INTERNAL_SERVER_ERROR;
return -1;
}
if (dbi_result_get_numrows(result) > 0) {
syslog(LOG_ERR, "Repository of id %u in use by clients\n",
repo_id);
dbi_result_free(result);
og_dbi_close(dbi);
ctx->http_error = OG_HTTP_423_LOCKED;
return -1;
}
dbi_result_free(result);
result = dbi_conn_queryf(dbi->conn,
"DELETE FROM repositorios "
"WHERE idrepositorio=%u OR alias=%u",

View File

@ -56,6 +56,11 @@ struct og_cmd_ctx {
struct {
uint32_t id;
} image;
struct {
uint32_t id;
uint32_t disk;
uint32_t part;
} image_restore;
};
};