source: ogServer-Git/src/dbi.c @ b116081

Last change on this file since b116081 was d2f20d0, checked in by OpenGnSys Support Team <soporte-og@…>, 4 years ago

#942 Create DB image when calling POST /image/create

In case the DB entry for an image does not exist when POST /image/create
is called, this patch takes care of calling it.

This adds few optional json parameters to the POST /image/create API. If
optional parameters are included then this patch creates the DB entry,
otherwise it just creates the actual image and updates the existing
entry.

Request:
POST /image/create
{

"clients":192.168.56.11?,
"disk":"1",
"partition":"1",
"name":"archlinux",
"repository":"192.168.56.10",
"id":"24",
"code":"131",
"description":"This is a test",
"group_id":0,
"center_id":1

}
Response:
200 OK

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 * Copyright (C) 2020 Soleta Networks <info@soleta.eu>
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Affero General Public License as published by the
6 * Free Software Foundation, version 3.
7 */
8
9#include <syslog.h>
10#include <netinet/in.h>
11#include <arpa/inet.h>
12#include <string.h>
13#include "dbi.h"
14#include <syslog.h>
15#include <string.h>
16#include <stdio.h>
17
18struct og_dbi *og_dbi_open(struct og_dbi_config *config)
19{
20        struct og_dbi *dbi;
21
22        dbi = (struct og_dbi *)malloc(sizeof(struct og_dbi));
23        if (!dbi)
24                return NULL;
25
26        dbi_initialize_r(NULL, &dbi->inst);
27        dbi->conn = dbi_conn_new_r("mysql", dbi->inst);
28        if (!dbi->conn) {
29                free(dbi);
30                return NULL;
31        }
32
33        dbi_conn_set_option(dbi->conn, "host", config->ip);
34        dbi_conn_set_option(dbi->conn, "port", config->port);
35        dbi_conn_set_option(dbi->conn, "username", config->user);
36        dbi_conn_set_option(dbi->conn, "password", config->pass);
37        dbi_conn_set_option(dbi->conn, "dbname", config->name);
38        dbi_conn_set_option(dbi->conn, "encoding", "UTF-8");
39
40        if (dbi_conn_connect(dbi->conn) < 0) {
41                free(dbi);
42                return NULL;
43        }
44
45        return dbi;
46}
47
48void og_dbi_close(struct og_dbi *dbi)
49{
50        dbi_conn_close(dbi->conn);
51        dbi_shutdown_r(dbi->inst);
52        free(dbi);
53}
54
55int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
56                             struct in_addr addr)
57{
58        const char *msglog;
59        dbi_result result;
60
61        result = dbi_conn_queryf(dbi->conn,
62                                 "SELECT ordenadores.idordenador,"
63                                 "       ordenadores.nombreordenador,"
64                                 "       ordenadores.numserie,"
65                                 "       ordenadores.ip,"
66                                 "       ordenadores.mac,"
67                                 "       ordenadores.idaula,"
68                                 "       ordenadores.idperfilhard,"
69                                 "       ordenadores.idrepositorio,"
70                                 "       ordenadores.mascara,"
71                                 "       ordenadores.arranque,"
72                                 "       ordenadores.netiface,"
73                                 "       ordenadores.netdriver,"
74                                 "       ordenadores.idproautoexec,"
75                                 "       ordenadores.oglivedir,"
76                                 "       ordenadores.inremotepc,"
77                                 "       ordenadores.maintenance,"
78                                 "       centros.idcentro "
79                                 "FROM ordenadores "
80                                 "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula "
81                                 "INNER JOIN centros ON centros.idcentro=aulas.idcentro "
82                                 "WHERE ordenadores.ip='%s'", inet_ntoa(addr));
83        if (!result) {
84                dbi_conn_error(dbi->conn, &msglog);
85                syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
86                       __func__, __LINE__, msglog);
87                return -1;
88        }
89        if (!dbi_result_next_row(result)) {
90                syslog(LOG_ERR, "client does not exist in database (%s:%d)\n",
91                       __func__, __LINE__);
92                dbi_result_free(result);
93                return -1;
94        }
95
96        computer->id = dbi_result_get_uint(result, "idordenador");
97        snprintf(computer->name, sizeof(computer->name), "%s",
98                 dbi_result_get_string(result, "nombreordenador"));
99        snprintf(computer->serial_number, sizeof(computer->serial_number), "%s",
100                 dbi_result_get_string(result, "numserie"));
101        snprintf(computer->ip, sizeof(computer->ip), "%s",
102                 dbi_result_get_string(result, "ip"));
103        snprintf(computer->mac, sizeof(computer->mac), "%s",
104                 dbi_result_get_string(result, "mac"));
105        computer->room = dbi_result_get_uint(result, "idaula");
106        computer->hardware_id = dbi_result_get_uint(result, "idperfilhard");
107        computer->repo_id = dbi_result_get_uint(result, "idrepositorio");
108        snprintf(computer->netmask, sizeof(computer->netmask), "%s",
109                 dbi_result_get_string(result, "mascara"));
110        snprintf(computer->boot, sizeof(computer->boot), "%s",
111                 dbi_result_get_string(result, "arranque"));
112        snprintf(computer->netiface, sizeof(computer->netiface), "%s",
113                 dbi_result_get_string(result, "netiface"));
114        snprintf(computer->netdriver, sizeof(computer->netdriver), "%s",
115                 dbi_result_get_string(result, "netdriver"));
116        computer->procedure_id = dbi_result_get_uint(result, "idproautoexec");
117        snprintf(computer->livedir, sizeof(computer->livedir), "%s",
118                 dbi_result_get_string(result, "oglivedir"));
119        computer->remote = dbi_result_get_uint(result, "inremotepc") != 0;
120        computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0;
121
122        dbi_result_free(result);
123
124        return 0;
125}
126
127#define OG_UNASSIGNED_SW_ID 0
128#define OG_DEFAULT_REPO_ID 1
129#define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */
130
131int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image)
132{
133        const char *msglog;
134        dbi_result result;
135
136        result = dbi_conn_queryf(dbi->conn,
137                                 "INSERT INTO imagenes (nombreca, "
138                                 "descripcion, "
139                                 "idperfilsoft, "
140                                 "idcentro, "
141                                 "comentarios, "
142                                 "grupoid, "
143                                 "idrepositorio, "
144                                 "tipo, "
145                                 "ruta) "
146                                 "VALUES ('%s', '%s', %u, %lu, '', %u, %lu, %u, '')",
147                                 image->name, image->description,
148                                 OG_UNASSIGNED_SW_ID, image->center_id,
149                                 image->group_id, OG_DEFAULT_REPO_ID,
150                                 OG_IMAGE_DEFAULT_TYPE);
151
152        if (!result) {
153                dbi_conn_error(dbi->conn, &msglog);
154                syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n",
155                       __func__, __LINE__, msglog);
156                return -1;
157        }
158
159        dbi_result_free(result);
160        return dbi_conn_sequence_last(dbi->conn, NULL);
161}
Note: See TracBrowser for help on using the repository browser.