You need to sign in or sign up before continuing.
image.c 42.4 KB
Newer Older
O
overweight 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
 * iSulad licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 * Author: tanyifeng
 * Create: 2017-11-22
 * Description: provide image functions
 ******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <sys/utsname.h>
#include <ctype.h>

#include "image.h"
L
LiuHao 已提交
25
#include "libisulad.h"
O
overweight 已提交
26 27 28 29
#include "log.h"
#include "utils.h"

#include "ext_image.h"
D
dogsheng 已提交
30
#include "filters.h"
O
overweight 已提交
31 32

#ifdef ENABLE_OCI_IMAGE
D
dogsheng 已提交
33 34
#include "isula_image.h"
#include "oci_images_store.h"
O
overweight 已提交
35 36 37 38 39 40 41 42 43
#endif

#ifdef ENABLE_EMBEDDED_IMAGE
#include "embedded_image.h"
#include "db_all.h"

/* embedded */
static const struct bim_ops g_embedded_ops = {
    .init = embedded_init,
D
dogsheng 已提交
44
    .clean_resource = NULL,
O
overweight 已提交
45 46 47 48 49 50
    .detect = embedded_detect,

    .prepare_rf = embedded_prepare_rf,
    .mount_rf = embedded_mount_rf,
    .umount_rf = embedded_umount_rf,
    .delete_rf = embedded_delete_rf,
D
dogsheng 已提交
51
    .export_rf = NULL,
O
overweight 已提交
52 53 54 55 56

    .merge_conf = embedded_merge_conf,
    .get_user_conf = embedded_get_user_conf,

    .list_ims = embedded_list_images,
D
dogsheng 已提交
57
    .get_image_count = NULL,
O
overweight 已提交
58 59 60
    .rm_image = embedded_remove_image,
    .inspect_image = embedded_inspect_image,
    .resolve_image_name = embedded_resolve_image_name,
D
dogsheng 已提交
61 62 63 64
    .container_fs_usage = embedded_filesystem_usage,
    .get_filesystem_info = NULL,
    .get_storage_status = NULL,
    .image_status = NULL,
O
overweight 已提交
65 66
    .load_image = embedded_load_image,
    .pull_image = NULL,
D
dogsheng 已提交
67 68 69 70 71

    .login = NULL,
    .logout = NULL,

    .health_check = NULL,
O
overweight 已提交
72 73 74
};
#endif

D
dogsheng 已提交
75
/* isula image server */
O
overweight 已提交
76
#ifdef ENABLE_OCI_IMAGE
D
dogsheng 已提交
77 78 79
static const struct bim_ops g_isula_ops = {
    .init = isula_init,
    .clean_resource = isula_exit,
O
overweight 已提交
80 81
    .detect = oci_detect,

D
dogsheng 已提交
82 83 84 85 86
    .prepare_rf = isula_prepare_rf,
    .mount_rf = isula_mount_rf,
    .umount_rf = isula_umount_rf,
    .delete_rf = isula_delete_rf,
    .export_rf = isula_export_rf,
O
overweight 已提交
87

D
dogsheng 已提交
88
    .merge_conf = isula_merge_conf_rf,
O
overweight 已提交
89 90 91
    .get_user_conf = oci_get_user_conf,

    .list_ims = oci_list_images,
D
dogsheng 已提交
92 93
    .get_image_count = oci_images_store_size,
    .rm_image = isula_rmi,
O
overweight 已提交
94 95
    .inspect_image = oci_inspect_image,
    .resolve_image_name = oci_resolve_image_name,
D
dogsheng 已提交
96 97 98 99 100 101 102 103 104 105
    .container_fs_usage = isula_container_filesystem_usage,
    .get_filesystem_info = isula_get_filesystem_info,
    .get_storage_status = isula_get_storage_status,
    .image_status = oci_status_image,
    .load_image = isual_load_image,
    .pull_image = isula_pull_rf,
    .login = isula_login,
    .logout = isula_logout,

    .health_check = isula_health_check,
O
overweight 已提交
106 107 108 109 110 111
};
#endif

/* external */
static const struct bim_ops g_ext_ops = {
    .init = ext_init,
D
dogsheng 已提交
112
    .clean_resource = NULL,
O
overweight 已提交
113 114 115 116 117 118
    .detect = ext_detect,

    .prepare_rf = ext_prepare_rf,
    .mount_rf = ext_mount_rf,
    .umount_rf = ext_umount_rf,
    .delete_rf = ext_delete_rf,
D
dogsheng 已提交
119
    .export_rf = NULL,
O
overweight 已提交
120 121 122 123 124

    .merge_conf = ext_merge_conf,
    .get_user_conf = ext_get_user_conf,

    .list_ims = ext_list_images,
D
dogsheng 已提交
125
    .get_image_count = NULL,
O
overweight 已提交
126 127 128
    .rm_image = ext_remove_image,
    .inspect_image = ext_inspect_image,
    .resolve_image_name = ext_resolve_image_name,
D
dogsheng 已提交
129 130 131 132
    .container_fs_usage = ext_filesystem_usage,
    .image_status = NULL,
    .get_filesystem_info = NULL,
    .get_storage_status = NULL,
O
overweight 已提交
133 134 135 136
    .load_image = ext_load_image,
    .pull_image = NULL,
    .login = ext_login,
    .logout = ext_logout,
D
dogsheng 已提交
137 138

    .health_check = NULL,
O
overweight 已提交
139 140 141 142 143 144
};

static const struct bim_type g_bims[] = {
#ifdef ENABLE_OCI_IMAGE
    {
        .image_type = IMAGE_TYPE_OCI,
D
dogsheng 已提交
145
        .ops = &g_isula_ops,
O
overweight 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    },
#endif
    { .image_type = IMAGE_TYPE_EXTERNAL, .ops = &g_ext_ops },
#ifdef ENABLE_EMBEDDED_IMAGE
    { .image_type = IMAGE_TYPE_EMBEDDED, .ops = &g_embedded_ops },
#endif
};

static const size_t g_numbims = sizeof(g_bims) / sizeof(struct bim_type);

static const struct bim_type *bim_query(const char *image_name)
{
    size_t i;
    char *temp = NULL;

    for (i = 0; i < g_numbims; i++) {
D
dogsheng 已提交
162 163 164 165
        if (g_bims[i].ops->resolve_image_name == NULL) {
            WARN("Unimplements resolve image name in %s", g_bims[i].image_type);
            continue;
        }
O
overweight 已提交
166 167
        temp = g_bims[i].ops->resolve_image_name(image_name);
        if (temp == NULL) {
L
LiuHao 已提交
168
            isulad_append_error_message("Failed to resovle image name%s", image_name);
O
overweight 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            return NULL;
        }
        int r = g_bims[i].ops->detect(temp);

        free(temp);
        temp = NULL;

        if (r != 0) {
            break;
        }
    }

    if (i == g_numbims) {
        return NULL;
    }
    return &g_bims[i];
}

static const struct bim_type *get_bim_by_type(const char *image_type)
{
    size_t i;

    for (i = 0; i < g_numbims; i++) {
        if (strcmp(g_bims[i].image_type, image_type) == 0) {
            return &g_bims[i];
        }
    }

    ERROR("Backing store %s unknown but not caught earlier\n", image_type);
    return NULL;
}

static void bim_put(struct bim *bim)
{
    if (bim == NULL) {
        return;
    }

    free(bim->image_name);
    bim->image_name = NULL;
    free(bim->ext_config_image);
    bim->ext_config_image = NULL;
    free(bim->container_id);
    bim->container_id = NULL;
    free(bim);
}

static struct bim *bim_get(const char *image_type, const char *image_name, const char *ext_config_image,
                           const char *container_id)
{
    struct bim *bim = NULL;
    const struct bim_type *q = NULL;

    if (image_type == NULL) {
        return NULL;
    }

    q = get_bim_by_type(image_type);
    if (q == NULL) {
        return NULL;
    }

    bim = util_common_calloc_s(sizeof(struct bim));
    if (bim == NULL) {
        return NULL;
    }

    bim->ops = q->ops;
    bim->type = q->image_type;

    if (image_name != NULL) {
        bim->image_name = bim->ops->resolve_image_name(image_name);
        if (bim->image_name == NULL) {
L
LiuHao 已提交
242
            isulad_append_error_message("Failed to resovle image name%s", image_name);
O
overweight 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255
            bim_put(bim);
            return NULL;
        }
    }
    if (ext_config_image != NULL) {
        bim->ext_config_image = util_strdup_s(ext_config_image);
    }
    if (container_id != NULL) {
        bim->container_id = util_strdup_s(container_id);
    }
    return bim;
}

D
dogsheng 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
int im_resolv_image_name(const char *image_type, const char *image_name, char **resolved_name)
{
    int ret = -1;
    const struct bim_type *q = NULL;

    if (image_type == NULL) {
        ERROR("Image type is required");
        goto out;
    }
    q = get_bim_by_type(image_type);
    if (q == NULL) {
        goto out;
    }
    if (q->ops->resolve_image_name == NULL) {
        ERROR("Get resolve image name umimplements");
        goto out;
    }

    *resolved_name = q->ops->resolve_image_name(image_name);
    if (*resolved_name == NULL) {
        goto out;
    }

    ret = 0;
out:
    return ret;
}

int im_get_storage_status(const char *image_type, im_storage_status_response **response)
{
    int ret = -1;
    const struct bim_type *q = NULL;

    if (image_type == NULL) {
        ERROR("Image type is required");
        goto out;
    }
    q = get_bim_by_type(image_type);
    if (q == NULL) {
        goto out;
    }
    if (q->ops->get_storage_status == NULL) {
        ERROR("Get storage status umimplements");
        goto out;
    }

    ret = q->ops->get_storage_status(response);
    if (ret != 0) {
        ERROR("Get storage status failed");
        free_im_storage_status_response(*response);
        *response = NULL;
        goto out;
    }

out:
    return ret;
}

int im_get_filesystem_info(const char *image_type, im_fs_info_response **response)
{
    int ret = -1;
    const struct bim_type *q = NULL;

    if (image_type == NULL) {
        ERROR("Image type is required");
        goto out;
    }

    q = get_bim_by_type(image_type);
    if (q == NULL) {
        goto out;
    }
    if (q->ops->get_filesystem_info == NULL) {
        ERROR("Get filesystem info umimplements");
        goto out;
    }

    EVENT("Event: {Object: get image filesystem info, Type: inspecting}");
    ret = q->ops->get_filesystem_info(response);
    if (ret != 0) {
        if (response != NULL && *response != NULL) {
            ERROR("Get filesystem info failed: %s", (*response)->errmsg);
        } else {
            ERROR("Get filesystem info failed");
        }
        goto out;
    }
    EVENT("Event: {Object: get image filesystem info, Type: inspected}");

out:
    return ret;
}

int im_health_check(const char *image_type)
{
    int ret = -1;
    const struct bim_type *q = NULL;

    if (image_type == NULL) {
        ERROR("Image type is required");
        goto out;
    }

    q = get_bim_by_type(image_type);
    if (q == NULL) {
        ERROR("Get bim failed");
        goto out;
    }

    if (q->ops->health_check == NULL) {
        ERROR("Health check umimplement");
        goto out;
    }

    ret = q->ops->health_check();

out:
    return ret;
}

O
overweight 已提交
376 377 378 379 380
int im_get_container_filesystem_usage(const char *image_type, const char *id, imagetool_fs_info **fs_usage)
{
    int ret = 0;
    imagetool_fs_info *filesystemusage = NULL;
    const struct bim_type *q = NULL;
D
dogsheng 已提交
381
    im_container_fs_usage_request *request = NULL;
O
overweight 已提交
382 383 384 385 386 387 388 389 390 391 392 393

    if (image_type == NULL || id == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    q = get_bim_by_type(image_type);
    if (q == NULL) {
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
394 395 396 397
    if (q->ops->container_fs_usage == NULL) {
        ERROR("Unimplements filesystem usage in %s", image_type);
        goto out;
    }
O
overweight 已提交
398

D
dogsheng 已提交
399 400 401
    request = util_common_calloc_s(sizeof(im_container_fs_usage_request));
    if (request == NULL) {
        ERROR("Out of memory");
O
overweight 已提交
402 403 404 405 406
        ret = -1;
        goto out;
    }

    if (id != NULL) {
D
dogsheng 已提交
407
        request->name_id = util_strdup_s(id);
O
overweight 已提交
408 409
    }

D
dogsheng 已提交
410 411
    EVENT("Event: {Object: container \'%s\' filesystem info, Type: inspecting}", id != NULL ? id : "");
    ret = q->ops->container_fs_usage(request, &filesystemusage);
O
overweight 已提交
412 413 414 415 416 417 418
    if (ret != 0) {
        ERROR("Failed to get filesystem usage for container %s", id);
        ret = -1;
        goto out;
    }

    *fs_usage = filesystemusage;
D
dogsheng 已提交
419
    EVENT("Event: {Object: container \'%s\' filesystem info, Type: inspected}", id != NULL ? id : "");
O
overweight 已提交
420 421

out:
D
dogsheng 已提交
422
    free_im_container_fs_usage_request(request);
O
overweight 已提交
423 424 425 426 427 428
    return ret;
}

int im_remove_container_rootfs(const char *image_type, const char *container_id)
{
    int ret = 0;
D
dogsheng 已提交
429
    im_delete_request *request = NULL;
O
overweight 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443
    struct bim *bim = NULL;

    if (container_id == NULL || image_type == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, NULL, NULL, container_id);
    if (bim == NULL) {
        ERROR("Failed to init bim for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
444 445 446 447 448 449 450 451 452 453 454 455
    if (bim->ops->delete_rf == NULL) {
        ERROR("Unimplements delete in %s", bim->type);
        goto out;
    }

    request = util_common_calloc_s(sizeof(im_delete_request));
    if (request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    request->name_id = util_strdup_s(container_id);
O
overweight 已提交
456

D
dogsheng 已提交
457 458
    EVENT("Event: {Object: %s, Type: removeing rootfs}", container_id);
    ret = bim->ops->delete_rf(request);
O
overweight 已提交
459 460 461 462 463
    if (ret != 0) {
        ERROR("Failed to delete rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
464
    EVENT("Event: {Object: %s, Type: removed rootfs}", container_id);
O
overweight 已提交
465 466 467

out:
    bim_put(bim);
D
dogsheng 已提交
468
    free_im_delete_request(request);
O
overweight 已提交
469 470 471
    return ret;
}

D
dogsheng 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
void free_im_container_fs_usage_request(im_container_fs_usage_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->name_id);
    request->name_id = NULL;
    free(request);
}

void free_im_prepare_request(im_prepare_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->image_name);
    request->image_name = NULL;
    free(request->container_id);
    request->container_id = NULL;
    free(request->ext_config_image);
    request->ext_config_image = NULL;

    free_json_map_string_string(request->storage_opt);
    request->storage_opt = NULL;

    free(request);
}

void free_im_mount_request(im_mount_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->name_id);
    request->name_id = NULL;
    free(request);
}

void free_im_delete_request(im_delete_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->name_id);
    request->name_id = NULL;
    free(request);
}

void free_im_umount_request(im_umount_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->name_id);
    request->name_id = NULL;
    free(request);
}

O
overweight 已提交
535 536 537 538
int im_umount_container_rootfs(const char *image_type, const char *image_name, const char *container_id)
{
    int ret = 0;
    struct bim *bim = NULL;
D
dogsheng 已提交
539
    im_umount_request *request = NULL;
O
overweight 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552

    if (container_id == NULL || image_type == NULL || image_name == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, image_name, NULL, container_id);
    if (bim == NULL) {
        ERROR("Failed to init bim for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
553 554 555 556
    if (bim->ops->umount_rf == NULL) {
        ERROR("Unimplements umount in %s", bim->type);
        goto out;
    }
O
overweight 已提交
557

D
dogsheng 已提交
558 559 560 561 562 563 564 565 566 567 568 569
    request = (im_umount_request *)util_common_calloc_s(sizeof(im_umount_request));
    if (request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    request->force = false;
    request->name_id = bim->container_id;
    bim->container_id = NULL;

    EVENT("Event: {Object: %s, Type: umounting rootfs}", container_id);
    ret = bim->ops->umount_rf(request);
O
overweight 已提交
570 571 572 573 574
    if (ret != 0) {
        ERROR("Failed to umount rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
575
    EVENT("Event: {Object: %s, Type: umounted rootfs}", container_id);
O
overweight 已提交
576 577 578

out:
    bim_put(bim);
D
dogsheng 已提交
579
    free_im_umount_request(request);
O
overweight 已提交
580 581 582 583 584 585 586
    return ret;
}

int im_mount_container_rootfs(const char *image_type, const char *image_name, const char *container_id)
{
    int ret = 0;
    struct bim *bim = NULL;
D
dogsheng 已提交
587
    im_mount_request *request = NULL;
O
overweight 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600

    if (image_name == NULL || container_id == NULL || image_type == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, image_name, NULL, container_id);
    if (bim == NULL) {
        ERROR("Failed to init bim for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
601 602 603 604 605 606 607 608 609 610 611 612
    if (bim->ops->mount_rf == NULL) {
        ERROR("Unimplements mount in %s", bim->type);
        goto out;
    }

    request = util_common_calloc_s(sizeof(im_mount_request));
    if (request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    request->name_id = util_strdup_s(container_id);
O
overweight 已提交
613

D
dogsheng 已提交
614 615
    EVENT("Event: {Object: %s, Type: mounting rootfs}", container_id);
    ret = bim->ops->mount_rf(request);
O
overweight 已提交
616 617 618 619 620
    if (ret != 0) {
        ERROR("Failed to mount rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
621
    EVENT("Event: {Object: %s, Type: mounted rootfs}", container_id);
O
overweight 已提交
622 623 624

out:
    bim_put(bim);
D
dogsheng 已提交
625
    free_im_mount_request(request);
O
overweight 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
    return ret;
}

char *im_get_image_type(const char *image, const char *external_rootfs)
{
    const char *image_name = NULL;
    const struct bim_type *bim_type = NULL;

    image_name = (external_rootfs != NULL) ? external_rootfs : image;
    if (image_name == NULL) {
        ERROR("Should specify the image name or external rootfs");
        return NULL;
    }

    bim_type = bim_query(image_name);
    if (bim_type == NULL) {
        ERROR("Failed to query type of image %s", image_name);
L
LiuHao 已提交
643
        isulad_set_error_message("No such image:%s", image_name);
O
overweight 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656
        return NULL;
    }

    return util_strdup_s(bim_type->image_type);
}

bool im_config_image_exist(const char *image_name)
{
    const struct bim_type *bim_type = NULL;

    bim_type = bim_query(image_name);
    if (bim_type == NULL) {
        ERROR("Config image %s not exist", image_name);
L
LiuHao 已提交
657
        isulad_set_error_message("Image %s not exist", image_name);
O
overweight 已提交
658 659 660 661 662 663 664
        return false;
    }

    return true;
}

int im_merge_image_config(const char *id, const char *image_type, const char *image_name,
665 666
                          const char *ext_config_image,
                          host_config *host_spec, container_config *container_spec,
O
overweight 已提交
667 668 669 670
                          char **real_rootfs)
{
    int ret = 0;
    struct bim *bim = NULL;
D
dogsheng 已提交
671
    im_prepare_request *request = NULL;
O
overweight 已提交
672

673
    if (real_rootfs == NULL || image_type == NULL) {
O
overweight 已提交
674 675 676 677 678 679 680 681 682 683 684
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, image_name, ext_config_image, id);
    if (bim == NULL) {
        ERROR("Failed to init bim of image %s", image_name);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
    if (bim->ops->merge_conf == NULL) {
        ERROR("Unimplements merge config in %s", bim->type);
        goto out;
    }

    request = util_common_calloc_s(sizeof(im_prepare_request));
    if (request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    request->container_id = util_strdup_s(id);
    request->image_name = util_strdup_s(image_name);
    request->ext_config_image = util_strdup_s(ext_config_image);
    if (host_spec != NULL) {
        request->storage_opt = host_spec->storage_opt;
    }

    EVENT("Event: {Object: %s, Type: preparing rootfs with image %s}", id, image_name);
O
overweight 已提交
704

705
    ret = bim->ops->merge_conf(host_spec, container_spec, request, real_rootfs);
D
dogsheng 已提交
706
    request->storage_opt = NULL;
O
overweight 已提交
707 708 709 710 711
    if (ret != 0) {
        ERROR("Failed to merge image %s config, config image is %s", image_name, ext_config_image);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
712 713
    EVENT("Event: {Object: %s, Type: prepared rootfs with image %s}", id, image_name);

O
overweight 已提交
714 715 716 717
    INFO("Use real rootfs: %s with type: %s", *real_rootfs, image_type);

out:
    bim_put(bim);
D
dogsheng 已提交
718
    free_im_prepare_request(request);
O
overweight 已提交
719 720 721 722
    return ret;
}

int im_get_user_conf(const char *image_type, const char *basefs, host_config *hc, const char *userstr,
723
                     defs_process_user *puser)
O
overweight 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
{
    int ret = 0;
    struct bim *bim = NULL;

    if (basefs == NULL || hc == NULL || image_type == NULL || puser == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image type: %s", image_type);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
740 741 742 743
    if (bim->ops->get_user_conf == NULL) {
        ERROR("Unimplements get user config in %s", bim->type);
        goto out;
    }
O
overweight 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

    ret = bim->ops->get_user_conf(basefs, hc, userstr, puser);
    if (ret != 0) {
        ERROR("Failed to get user config");
        ret = -1;
        goto out;
    }

out:
    bim_put(bim);
    return ret;
}

static int append_images_to_response(im_list_response *response, imagetool_images_list *images_in)
{
    int ret = 0;
    size_t images_num = 0;
    size_t old_num = 0;
    imagetool_image **tmp = NULL;
    size_t i = 0;
    size_t new_size = 0;
    size_t old_size = 0;

    if (images_in == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    if (response->images == NULL) {
        response->images = util_common_calloc_s(sizeof(imagetool_images_list));
        if (response->images == NULL) {
            ERROR("Memeory out");
            ret = -1;
            goto out;
        }
    }

    images_num = images_in->images_len;
    // no images need to append
    if (images_num == 0) {
        goto out;
    }
    if (images_num > SIZE_MAX / sizeof(imagetool_image *) - response->images->images_len) {
        ERROR("Too many images to append!");
        ret = -1;
        goto out;
    }

    old_num = response->images->images_len;

    new_size = (old_num + images_num) * sizeof(imagetool_image *);
    old_size = old_num * sizeof(imagetool_image *);
    ret = mem_realloc((void **)(&tmp), new_size, response->images->images, old_size);
    if (ret != 0) {
        ERROR("Failed to realloc memory for append images");
        ret = -1;
        goto out;
    }
    response->images->images = tmp;
    for (i = 0; i < images_num; i++) {
        response->images->images[old_num + i] = images_in->images[i];
        images_in->images[i] = NULL;
        images_in->images_len--;
        response->images->images_len++;
    }

out:
    return ret;
}
D
dogsheng 已提交
814
int im_list_images(const im_list_request *ctx, im_list_response **response)
O
overweight 已提交
815 816 817 818 819 820 821 822 823 824
{
    size_t i;
    imagetool_images_list *images_tmp = NULL;

    *response = util_common_calloc_s(sizeof(im_list_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

D
dogsheng 已提交
825
    EVENT("Event: {Object: list images, Type: listing}");
O
overweight 已提交
826 827

    for (i = 0; i < g_numbims; i++) {
D
dogsheng 已提交
828 829 830 831 832
        if (g_bims[i].ops->list_ims == NULL) {
            DEBUG("bim %s umimplements list images operator", g_bims[i].image_type);
            continue;
        }
        int ret = g_bims[i].ops->list_ims(ctx, &images_tmp);
O
overweight 已提交
833 834 835 836 837 838 839 840 841 842 843 844
        if (ret != 0) {
            ERROR("Failed to list all images with type:%s", g_bims[i].image_type);
            continue;
        }
        ret = append_images_to_response(*response, images_tmp);
        if (ret != 0) {
            ERROR("Failed to append images with type:%s", g_bims[i].image_type);
        }
        free_imagetool_images_list(images_tmp);
        images_tmp = NULL;
    }

D
dogsheng 已提交
845
    EVENT("Event: {Object: list images, Type: listed}");
O
overweight 已提交
846

L
LiuHao 已提交
847 848
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861
    }

    return 0;
}

void free_im_list_request(im_list_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->filter.image.image);
    ptr->filter.image.image = NULL;

D
dogsheng 已提交
862 863
    filters_args_free(ptr->image_filters);
    ptr->image_filters = NULL;
O
overweight 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    free(ptr);
}

void free_im_list_response(im_list_response *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free_imagetool_images_list(ptr->images);
    ptr->images = NULL;
    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

static bool check_im_pull_args(const im_pull_request *req, im_pull_response * const *resp)
{
    if (req == NULL || resp == NULL) {
        ERROR("Request or response is NULL");
        return false;
    }
    if (req->image == NULL) {
        ERROR("Empty image required");
L
LiuHao 已提交
888
        isulad_set_error_message("Empty image required");
O
overweight 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
        return false;
    }
    return true;
}

int im_pull_image(const im_pull_request *request, im_pull_response **response)
{
    int ret = -1;
    struct bim *bim = NULL;

    if (!check_im_pull_args(request, response)) {
        return ret;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type: %s", request->type);
        goto out;
    }

    if (bim->ops->pull_image == NULL) {
D
dogsheng 已提交
910
        ERROR("Unimplements pull image in %s", bim->type);
O
overweight 已提交
911 912 913
        goto out;
    }

D
dogsheng 已提交
914
    EVENT("Event: {Object: %s, Type: Pulling}", request->image);
O
overweight 已提交
915 916 917 918 919 920
    ret = bim->ops->pull_image(request, response);
    if (ret != 0) {
        ERROR("Pull image %s failed", request->image);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
921
    EVENT("Event: {Object: %s, Type: Pulled}", request->image);
O
overweight 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

out:
    bim_put(bim);
    return ret;
}

void free_im_pull_request(im_pull_request *req)
{
    if (req == NULL) {
        return;
    }
    free(req->type);
    req->type = NULL;
    free(req->image);
    req->image = NULL;
    free_sensitive_string(req->username);
    req->username = NULL;
    free_sensitive_string(req->password);
    req->password = NULL;
    free_sensitive_string(req->auth);
    req->auth = NULL;
    free_sensitive_string(req->server_address);
    req->server_address = NULL;
    free_sensitive_string(req->registry_token);
    req->registry_token = NULL;
    free_sensitive_string(req->identity_token);
    req->identity_token = NULL;
    free(req);
}

void free_im_pull_response(im_pull_response *resp)
{
    if (resp == NULL) {
        return;
    }
    free(resp->image_ref);
    resp->image_ref = NULL;
    free(resp->errmsg);
    resp->errmsg = NULL;
    free(resp);
}

D
dogsheng 已提交
964
int im_load_image(const im_load_request *request, im_load_response **response)
O
overweight 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
{
    int ret = -1;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_load_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->file == NULL) {
        ERROR("Load image requires image tarball file path");
L
LiuHao 已提交
982
        isulad_set_error_message("Load image requires image tarball file path");
O
overweight 已提交
983 984 985 986 987
        goto pack_response;
    }

    if (request->type == NULL) {
        ERROR("Missing image type");
L
LiuHao 已提交
988
        isulad_set_error_message("Missing image type");
O
overweight 已提交
989 990 991 992 993 994 995 996
        goto pack_response;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", request->type);
        goto pack_response;
    }
D
dogsheng 已提交
997 998 999 1000
    if (bim->ops->load_image == NULL) {
        ERROR("Unimplements load image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

    EVENT("Event: {Object: %s, Type: loading}", request->file);

    ret = bim->ops->load_image(request);
    if (ret != 0) {
        ERROR("Failed to load image from %s with tag %s and type %s", request->file, request->tag, request->type);
        ret = -1;
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: loaded}", request->file);

pack_response:
L
LiuHao 已提交
1014 1015
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    }

    bim_put(bim);
    return ret;
}

void free_im_load_request(im_load_request *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->file);
    ptr->file = NULL;

    free(ptr->tag);
    ptr->file = NULL;

    free(ptr->type);
    ptr->type = NULL;

    free(ptr);
}

void free_im_load_response(im_load_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1052
int im_login(const im_login_request *request, im_login_response **response)
O
overweight 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
{
    int ret = -1;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_login_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->server == NULL) {
        ERROR("Login requires server address");
L
LiuHao 已提交
1070
        isulad_set_error_message("Login requires server address");
O
overweight 已提交
1071 1072 1073 1074 1075
        goto pack_response;
    }

    if (request->type == NULL) {
        ERROR("Login requires image type");
L
LiuHao 已提交
1076
        isulad_set_error_message("Login requires image type");
O
overweight 已提交
1077 1078 1079 1080 1081
        goto pack_response;
    }

    if (request->username == NULL || request->password == NULL) {
        ERROR("Missing username or password");
L
LiuHao 已提交
1082
        isulad_set_error_message("Missing username or password");
O
overweight 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
        goto pack_response;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", request->type);
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: logining}", request->server);

    ret = bim->ops->login(request);
    if (ret != 0) {
        ERROR("Failed to login %s", request->server);
        ret = -1;
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: logined}", request->server);

pack_response:
L
LiuHao 已提交
1104 1105
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    }

    bim_put(bim);
    return ret;
}

void free_im_login_request(im_login_request *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free_sensitive_string(ptr->username);
    ptr->username = NULL;

    free_sensitive_string(ptr->password);
    ptr->password = NULL;

    free(ptr->type);
    ptr->type = NULL;

    free_sensitive_string(ptr->server);
    ptr->server = NULL;

    free(ptr);
}

void free_im_login_response(im_login_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1145
int im_logout(const im_logout_request *request, im_logout_response **response)
O
overweight 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
{
    int ret = -1;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_logout_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->server == NULL) {
        ERROR("Logout requires server address");
L
LiuHao 已提交
1163
        isulad_set_error_message("Logout requires server address");
O
overweight 已提交
1164 1165 1166 1167 1168
        goto pack_response;
    }

    if (request->type == NULL) {
        ERROR("Logout requires image type");
L
LiuHao 已提交
1169
        isulad_set_error_message("Logout requires image type");
O
overweight 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
        goto pack_response;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", request->type);
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: logouting}", request->server);

    ret = bim->ops->logout(request);
    if (ret != 0) {
        ERROR("Failed to logout %s", request->server);
        ret = -1;
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: logouted}", request->server);

pack_response:
L
LiuHao 已提交
1191 1192
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
    }

    bim_put(bim);
    return ret;
}

void free_im_logout_request(im_logout_request *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->type);
    ptr->type = NULL;

    free(ptr->server);
    ptr->server = NULL;

    free(ptr);
}

void free_im_logout_response(im_logout_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
int im_image_status(im_status_request *request, im_status_response **response)
{
    int ret = -1;
    char *image_ref = NULL;
    const struct bim_type *bim_type = NULL;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = (im_status_response *)util_common_calloc_s(sizeof(im_status_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->image.image == NULL) {
        ERROR("get image status requires image ref");
L
LiuHao 已提交
1246
        isulad_set_error_message("get image status requires image ref");
D
dogsheng 已提交
1247 1248 1249 1250 1251 1252 1253 1254
        goto pack_response;
    }

    image_ref = util_strdup_s(request->image.image);

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1255
        isulad_set_error_message("No such image:%s", image_ref);
D
dogsheng 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
        goto pack_response;
    }

    bim = bim_get(bim_type->image_type, image_ref, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image %s", image_ref);
        goto pack_response;
    }
    if (bim->ops->image_status == NULL) {
        ERROR("Unimplements image status in %s", bim->type);
        goto pack_response;
    }

    ret = bim->ops->image_status(request, response);
    if (ret != 0) {
        ERROR("Failed to get status of image %s", image_ref);
        ret = -1;
    }

pack_response:
L
LiuHao 已提交
1276
    if (g_isulad_errmsg != NULL) {
D
dogsheng 已提交
1277
        free((*response)->errmsg);
L
LiuHao 已提交
1278
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
D
dogsheng 已提交
1279 1280 1281 1282 1283 1284 1285
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

int im_rm_image(const im_remove_request *request, im_remove_response **response)
O
overweight 已提交
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
{
    int ret = -1;
    char *image_ref = NULL;
    const struct bim_type *bim_type = NULL;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_remove_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->image.image == NULL) {
        ERROR("remove image requires image ref");
L
LiuHao 已提交
1305
        isulad_set_error_message("remove image requires image ref");
O
overweight 已提交
1306 1307 1308 1309 1310
        goto pack_response;
    }

    image_ref = util_strdup_s(request->image.image);

D
dogsheng 已提交
1311
    EVENT("Event: {Object: %s, Type: image removing}", image_ref);
O
overweight 已提交
1312 1313 1314 1315

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1316
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1317 1318 1319 1320 1321 1322 1323 1324
        goto pack_response;
    }

    bim = bim_get(bim_type->image_type, image_ref, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image %s", image_ref);
        goto pack_response;
    }
D
dogsheng 已提交
1325 1326 1327 1328
    if (bim->ops->rm_image == NULL) {
        ERROR("Unimplements rm image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1329 1330 1331 1332 1333 1334 1335 1336

    ret = bim->ops->rm_image(request);
    if (ret != 0) {
        ERROR("Failed to remove image %s", image_ref);
        ret = -1;
        goto pack_response;
    }

D
dogsheng 已提交
1337
    EVENT("Event: {Object: %s, Type: image removed}", image_ref);
O
overweight 已提交
1338 1339

pack_response:
L
LiuHao 已提交
1340 1341
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

void free_im_remove_request(im_remove_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->image.image);
    ptr->image.image = NULL;

    free(ptr);
}

void free_im_remove_response(im_remove_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
static int do_im_inspect_image(struct bim *bim, char **inspected_json)
{
    int ret = 0;
    im_inspect_request *update_request = NULL;

    if (bim->ops->inspect_image == NULL) {
        ERROR("Unimplements inspect image in %s", bim->type);
        ret = -1;
        goto out;
    }
    update_request = util_common_calloc_s(sizeof(im_inspect_request));
    if (update_request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    update_request->image.image = bim->image_name;
    bim->image_name = NULL;

    ret = bim->ops->inspect_image(update_request, inspected_json);
    if (ret != 0) {
        ERROR("Failed to inspect image %s", update_request->image.image);
        ret = -1;
    }

out:
    free_im_inspect_request(update_request);
    return ret;
}

O
overweight 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
int im_inspect_image(const im_inspect_request *request, im_inspect_response **response)
{
    int ret = 0;
    char *image_ref = NULL;
    char *inspected_json = NULL;
    const struct bim_type *bim_type = NULL;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_inspect_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->image.image == NULL) {
        ERROR("inspect image requires image ref");
L
LiuHao 已提交
1422
        isulad_set_error_message("inspect image requires image ref");
O
overweight 已提交
1423 1424 1425 1426 1427 1428
        ret = -1;
        goto pack_response;
    }

    image_ref = util_strdup_s(request->image.image);

D
dogsheng 已提交
1429
    EVENT("Event: {Object: %s, Type: image inspecting}", image_ref);
O
overweight 已提交
1430 1431 1432 1433

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1434
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
        ret = -1;
        goto pack_response;
    }

    bim = bim_get(bim_type->image_type, image_ref, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image %s", image_ref);
        ret = -1;
        goto pack_response;
    }

D
dogsheng 已提交
1446
    ret = do_im_inspect_image(bim, &inspected_json);
O
overweight 已提交
1447 1448 1449 1450
    if (ret != 0) {
        goto pack_response;
    }

D
dogsheng 已提交
1451
    EVENT("Event: {Object: %s, Type: image inspected}", image_ref);
O
overweight 已提交
1452 1453

pack_response:
L
LiuHao 已提交
1454 1455
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    }
    if (inspected_json != NULL) {
        (*response)->im_inspect_json = util_strdup_s(inspected_json);
    }
    free(image_ref);
    free(inspected_json);
    bim_put(bim);
    return ret;
}

void free_im_inspect_request(im_inspect_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->image.image);
    ptr->image.image = NULL;

    free(ptr);
}

void free_im_inspect_response(im_inspect_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->im_inspect_json);
    ptr->im_inspect_json = NULL;

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
/*
 * parameters: image type
 * return: error return 0, success return image count
 * */
size_t im_get_image_count(const im_image_count_request *request)
{
    size_t ret = 0;
    const struct bim_type *q = NULL;

    if (request == NULL || request->type == NULL) {
        ERROR("Image type is required");
        goto out;
    }
    q = get_bim_by_type(request->type);
    if (q == NULL) {
        goto out;
    }
    if (q->ops->get_image_count == NULL) {
        ERROR("Get image count umimplements");
        goto out;
    }

    ret = q->ops->get_image_count();

out:
    return ret;
}

void free_im_image_count_request(im_image_count_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->type);
    ptr->type = NULL;
    free(ptr);
}

int im_container_export(const im_export_request *request)
{
    int ret = 0;
    int nret = 0;
    struct bim *bim = NULL;

    if (request == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    if (request->file == NULL) {
        ERROR("Container export requires output file path");
        ret = -1;
        goto out;
    }
    if (request->name_id == NULL) {
        ERROR("Container export requires container id");
        ret = -1;
        goto out;
    }

    if (request->type == NULL) {
        ERROR("Missing image type");
        ret = -1;
        goto out;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", request->type);
        ret = -1;
        goto out;
    }
    if (bim->ops->export_rf == NULL) {
        ERROR("Unimplements container export in %s", bim->type);
        ret = -1;
        goto out;
    }

    EVENT("Event: {Object: %s, Type: exporting}", request->file);

    nret = bim->ops->export_rf(request);
    if (nret != 0) {
        ERROR("Failed to export container from %s into file %s and type %s",
              request->name_id, request->file, request->type);
        ret = -1;
        goto out;
    }

    EVENT("Event: {Object: %s, Type: exported}", request->name_id);

out:
    bim_put(bim);
    return ret;
}

void free_im_export_request(im_export_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->type);
    ptr->type = NULL;
    free(ptr->file);
    ptr->file = NULL;
    free(ptr->name_id);
    ptr->name_id = NULL;
    free(ptr);
}

void free_im_configs(struct im_configs *conf)
{
    if (conf == NULL) {
        return;
    }
    free(conf->rootpath);
    conf->rootpath = NULL;
    free(conf->server_sock);
    conf->server_sock = NULL;
    free(conf);
}

static int bims_init(const struct im_configs *conf)
O
overweight 已提交
1614 1615 1616 1617 1618
{
    int ret = 0;
    size_t i;

    for (i = 0; i < g_numbims; i++) {
D
dogsheng 已提交
1619
        ret = g_bims[i].ops->init(conf);
O
overweight 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
        if (ret != 0) {
            ERROR("Failed to init bim %s", g_bims[i].image_type);
            break;
        }
    }

    return ret;
}

int image_module_init(const char *rootpath)
{
D
dogsheng 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
    struct im_configs *conf;
    int ret = -1;

    conf = (struct im_configs *)util_common_calloc_s(sizeof(struct im_configs));
    if (conf == NULL) {
        ERROR("Out of memory");
        return ret;
    }
    conf->rootpath = util_strdup_s(rootpath);
    ret = bims_init(conf);

    free_im_configs(conf);
    return ret;
}

void image_module_exit()
{
    size_t i;

    for (i = 0; i < g_numbims; i++) {
        if (g_bims[i].ops->clean_resource == NULL) {
            continue;
        }
        g_bims[i].ops->clean_resource();
    }
O
overweight 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
}

int map_to_key_value_string(const json_map_string_string *map, char ***array, size_t *array_len)
{
    char **strings = NULL;
    size_t strings_len = 0;
    size_t i;
    int ret;

    if (map == NULL) {
        return 0;
    }
    for (i = 0; i < map->len; i++) {
        char *str = NULL;
        size_t len;
        if (strlen(map->keys[i]) > (SIZE_MAX - strlen(map->values[i])) - 2) {
            ERROR("Invalid keys/values");
            goto cleanup;
        }
        len = strlen(map->keys[i]) + strlen(map->values[i]) + 2;
        str = util_common_calloc_s(len);
        if (str == NULL) {
            ERROR("Out of memory");
            goto cleanup;
        }
O
openeuler-iSula 已提交
1681 1682
        ret = snprintf(str, len, "%s=%s", map->keys[i], map->values[i]);
        if (ret < 0 || (size_t)ret >= len) {
O
overweight 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
            ERROR("Failed to print string");
            free(str);
            goto cleanup;
        }
        ret = util_array_append(&strings, str);
        free(str);
        if (ret != 0) {
            ERROR("Failed to append array");
            goto cleanup;
        }
        strings_len++;
    }
    *array = strings;
    *array_len = strings_len;
    return 0;

cleanup:
    util_free_array(strings);
    return -1;
}

D
dogsheng 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
void free_im_status_request(im_status_request *req)
{
    if (req == NULL) {
        return;
    }
    free(req->image.image);
    req->image.image = NULL;

    free(req);
}

void free_im_status_response(im_status_response *req)
{
    if (req == NULL) {
        return;
    }
    free_imagetool_image_status(req->image_info);
    req->image_info = NULL;
    free(req->errmsg);
    req->errmsg = NULL;

    free(req);
}

void free_im_fs_info_response(im_fs_info_response *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free_imagetool_fs_info(ptr->fs_info);
    ptr->fs_info = NULL;
    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

void free_im_storage_status_response(im_storage_status_response *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->backing_fs);
    ptr->backing_fs = NULL;
W
WangFengTu 已提交
1748 1749
    free(ptr->status);
    ptr->status = NULL;
D
dogsheng 已提交
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
    free(ptr);
}

void im_sync_containers_isuladkit(void)
{
    DEBUG("Sync containers...");
#ifdef ENABLE_OCI_IMAGE
    if (isula_sync_containers() != 0) {
        WARN("Sync containers with remote failed!!");
    }
#endif
}