source: ogServer-Git/src/dbi.c

Last change on this file was 52a38d3, checked in by Javier Sánchez Parra <jsanchez@…>, 23 months ago

#915 Use the repository id on image creation

POST /image/create has two modes, image creation and update. You can
find more information about the "creation" mode in commit:
d2f20d0be06617f421eecca111449d94672695eb

On image creation, use the id to identify repositories instead of the
IP. This is a preparative commit to the support of repositories with
several IPs.

On image update, "repository_id" field is not needed because the image
already has the repository assigned.

This commit maintains backward compatibility with the Web Console (old
web interface), because it only use the "update" mode of /image/create.

Request POST /create/image:
{

"clients": [

"192.168.56.11"

],
"disk": "1",
"partition": "1",
"name": "archlinux",
"repository_id": 1,
"id": "0",
"code": "131",
"description": "This is a test",
"group_id": 0,
"center_id": 1

}
Response 200 OK

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 * Copyright (C) 2020-2021 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; either version 3 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <syslog.h>
11#include <netinet/in.h>
12#include <arpa/inet.h>
13#include <string.h>
14#include "dbi.h"
15#include <syslog.h>
16#include <string.h>
17#include <stdio.h>
18
19struct og_dbi *og_dbi_open(struct og_dbi_config *config)
20{
21        struct og_dbi *dbi;
22
23        dbi = (struct og_dbi *)malloc(sizeof(struct og_dbi));
24        if (!dbi)
25                return NULL;
26
27        dbi_initialize_r(NULL, &dbi->inst);
28        dbi->conn = dbi_conn_new_r("mysql", dbi->inst);
29        if (!dbi->conn) {
30                free(dbi);
31                return NULL;
32        }
33
34        dbi_conn_set_option(dbi->conn, "host", config->ip);
35        dbi_conn_set_option(dbi->conn, "port", config->port);
36        dbi_conn_set_option(dbi->conn, "username", config->user);
37        dbi_conn_set_option(dbi->conn, "password", config->pass);
38        dbi_conn_set_option(dbi->conn, "dbname", config->name);
39        dbi_conn_set_option(dbi->conn, "encoding", "UTF-8");
40
41        if (dbi_conn_connect(dbi->conn) < 0) {
42                dbi_shutdown_r(dbi->inst);
43                free(dbi);
44                return NULL;
45        }
46
47        return dbi;
48}
49
50void og_dbi_close(struct og_dbi *dbi)
51{
52        dbi_conn_close(dbi->conn);
53        dbi_shutdown_r(dbi->inst);
54        free(dbi);
55}
56
57int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
58                             struct in_addr addr)
59{
60        const char *msglog;
61        dbi_result result;
62
63        result = dbi_conn_queryf(dbi->conn,
64                                 "SELECT ordenadores.idordenador,"
65                                 "       ordenadores.nombreordenador,"
66                                 "       ordenadores.numserie,"
67                                 "       ordenadores.ip,"
68                                 "       ordenadores.mac,"
69                                 "       ordenadores.idaula,"
70                                 "       ordenadores.idperfilhard,"
71                                 "       ordenadores.idrepositorio,"
72                                 "       ordenadores.mascara,"
73                                 "       ordenadores.arranque,"
74                                 "       ordenadores.netiface,"
75                                 "       ordenadores.netdriver,"
76                                 "       ordenadores.idproautoexec,"
77                                 "       ordenadores.oglivedir,"
78                                 "       ordenadores.inremotepc,"
79                                 "       ordenadores.maintenance,"
80                                 "       centros.idcentro "
81                                 "FROM ordenadores "
82                                 "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula "
83                                 "INNER JOIN centros ON centros.idcentro=aulas.idcentro "
84                                 "WHERE ordenadores.ip='%s'", inet_ntoa(addr));
85        if (!result) {
86                dbi_conn_error(dbi->conn, &msglog);
87                syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
88                       __func__, __LINE__, msglog);
89                return -1;
90        }
91        if (!dbi_result_next_row(result)) {
92                syslog(LOG_ERR, "client does not exist in database (%s:%d)\n",
93                       __func__, __LINE__);
94                dbi_result_free(result);
95                return -1;
96        }
97
98        computer->id = dbi_result_get_uint(result, "idordenador");
99        snprintf(computer->name, sizeof(computer->name), "%s",
100                 dbi_result_get_string(result, "nombreordenador"));
101        snprintf(computer->serial_number, sizeof(computer->serial_number), "%s",
102                 dbi_result_get_string(result, "numserie"));
103        snprintf(computer->ip, sizeof(computer->ip), "%s",
104                 dbi_result_get_string(result, "ip"));
105        snprintf(computer->mac, sizeof(computer->mac), "%s",
106                 dbi_result_get_string(result, "mac"));
107        computer->room = dbi_result_get_uint(result, "idaula");
108        computer->center = dbi_result_get_uint(result, "idcentro");
109        computer->hardware_id = dbi_result_get_uint(result, "idperfilhard");
110        computer->repo_id = dbi_result_get_uint(result, "idrepositorio");
111        snprintf(computer->netmask, sizeof(computer->netmask), "%s",
112                 dbi_result_get_string(result, "mascara"));
113        snprintf(computer->boot, sizeof(computer->boot), "%s",
114                 dbi_result_get_string(result, "arranque"));
115        snprintf(computer->netiface, sizeof(computer->netiface), "%s",
116                 dbi_result_get_string(result, "netiface"));
117        snprintf(computer->netdriver, sizeof(computer->netdriver), "%s",
118                 dbi_result_get_string(result, "netdriver"));
119        computer->procedure_id = dbi_result_get_uint(result, "idproautoexec");
120        snprintf(computer->livedir, sizeof(computer->livedir), "%s",
121                 dbi_result_get_string(result, "oglivedir"));
122        computer->remote = dbi_result_get_uint(result, "inremotepc") != 0;
123        computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0;
124
125        dbi_result_free(result);
126
127        return 0;
128}
129
130#define OG_UNASSIGNED_SW_ID 0
131#define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */
132
133int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image)
134{
135        const char *msglog;
136        dbi_result result;
137
138        result = dbi_conn_queryf(dbi->conn,
139                                 "SELECT nombreca FROM imagenes WHERE nombreca = '%s'",
140                                 image->name);
141        if (!result) {
142                dbi_conn_error(dbi->conn, &msglog);
143                syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
144                       __func__, __LINE__, msglog);
145                return -1;
146        }
147
148        if (dbi_result_next_row(result)) {
149                syslog(LOG_ERR, "image creation attempt with already used image name (%s:%d)\n",
150                       __func__, __LINE__);
151                dbi_result_free(result);
152                return -1;
153        }
154        dbi_result_free(result);
155
156        result = dbi_conn_queryf(dbi->conn,
157                                 "INSERT INTO imagenes (nombreca, "
158                                 "descripcion, "
159                                 "idperfilsoft, "
160                                 "idcentro, "
161                                 "comentarios, "
162                                 "grupoid, "
163                                 "idrepositorio, "
164                                 "tipo, "
165                                 "ruta) "
166                                 "VALUES ('%s', '%s', %u, %lu, '', %u, %lu, %u, '')",
167                                 image->name, image->description,
168                                 OG_UNASSIGNED_SW_ID, image->center_id,
169                                 image->group_id, image->repo_id,
170                                 OG_IMAGE_DEFAULT_TYPE);
171
172        if (!result) {
173                dbi_conn_error(dbi->conn, &msglog);
174                syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n",
175                       __func__, __LINE__, msglog);
176                return -1;
177        }
178
179        dbi_result_free(result);
180        return dbi_conn_sequence_last(dbi->conn, NULL);
181}
182
183int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint64_t image_id,
184                             char *repository_ip)
185{
186        const char *msglog, *dbi_repository_ip;
187        dbi_result result;
188
189        result = dbi_conn_queryf(dbi->conn,
190                                 "SELECT r.ip FROM imagenes i "
191                                 "JOIN repositorios r USING (idrepositorio) "
192                                 "WHERE i.idimagen = %lu",
193                                 image_id);
194        if (!result) {
195                dbi_conn_error(dbi->conn, &msglog);
196                syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
197                       __func__, __LINE__, msglog);
198                return -1;
199        }
200
201        if (!dbi_result_next_row(result)) {
202                dbi_conn_error(dbi->conn, &msglog);
203                syslog(LOG_ERR,
204                      "image with id %lu has no repository (%s:%d) %s\n",
205                      image_id, __func__, __LINE__, msglog);
206                dbi_result_free(result);
207                return -1;
208        }
209
210        dbi_repository_ip = dbi_result_get_string(result, "ip");
211        snprintf(repository_ip, OG_DB_IP_MAXLEN + 1, "%s", dbi_repository_ip);
212
213        dbi_result_free(result);
214
215        return 0;
216}
Note: See TracBrowser for help on using the repository browser.