oci_image.c 11.0 KB
Newer Older
D
dogsheng 已提交
1 2
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
D
dogsheng 已提交
7 8 9
 * 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.
10
 * See the Mulan PSL v2 for more details.
D
dogsheng 已提交
11 12
* Author: liuhao
* Create: 2019-07-15
13
* Description: provide oci image operator definition
D
dogsheng 已提交
14
*******************************************************************************/
15
#include "oci_image.h"
D
dogsheng 已提交
16 17 18 19

#include <pthread.h>
#include <semaphore.h>

H
haozi007 已提交
20
#include "isula_libutils/log.h"
W
WangFengTu 已提交
21 22 23 24
#include "log.h"
#include "oci_pull.h"
#include "oci_login.h"
#include "oci_logout.h"
25
#include "registry.h"
D
dogsheng 已提交
26 27 28

#include "containers_store.h"

L
LiuHao 已提交
29
#include "isulad_config.h"
D
dogsheng 已提交
30
#include "utils.h"
31
#include "storage.h"
D
dogsheng 已提交
32 33 34

#define IMAGE_NOT_KNOWN_ERR "image not known"

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

static int storage_module_init_helper(const struct service_arguments *args)
{
    int ret = 0;
    struct storage_module_init_options *storage_opts = NULL;

    storage_opts = util_common_calloc_s(sizeof(struct storage_module_init_options));
    if (storage_opts == NULL) {
        ERROR("Memory out");
        ret = -1;
        goto out;
    }

    storage_opts->driver_name = util_strdup_s(args->json_confs->storage_driver);
    storage_opts->storage_root = util_path_join(args->json_confs->graph, GRAPH_ROOTPATH_NAME);
    if (storage_opts->storage_root == NULL) {
        ERROR("Failed to get storage root dir");
        ret = -1;
        goto out;
    }

    storage_opts->storage_run_root = util_path_join(args->json_confs->state, GRAPH_ROOTPATH_NAME);
    if (storage_opts->storage_run_root == NULL) {
        ERROR("Failed to get storage run root dir");
        ret = -1;
        goto out;
    }

    if (dup_array_of_strings((const char **)args->json_confs->storage_opts,
                             args->json_confs->storage_opts_len, &storage_opts->driver_opts, &storage_opts->driver_opts_len) != 0) {
        ERROR("Failed to get storage storage opts");
        ret = -1;
        goto out;
    }

    if (storage_module_init(storage_opts) != 0) {
        ERROR("Failed to init storage module");
        ret = -1;
        goto out;
    }

out:
    free_storage_module_init_options(storage_opts);
    return ret;
}

int oci_init(const struct service_arguments *args)
D
dogsheng 已提交
82
{
W
WangFengTu 已提交
83
    int ret = 0;
D
dogsheng 已提交
84

85
    if (args == NULL) {
D
dogsheng 已提交
86 87 88 89
        ERROR("Invalid image config");
        return ret;
    }

W
WangFengTu 已提交
90
    ret = registry_init();
91
    if (ret != 0) {
W
WangFengTu 已提交
92
        ret = -1;
93 94 95
        goto out;
    }

96 97
    if (storage_module_init_helper(args) != 0) {
        ret = -1;
D
dogsheng 已提交
98 99 100 101
        goto out;
    }

out:
W
WangFengTu 已提交
102

D
dogsheng 已提交
103 104 105
    return ret;
}

106
int oci_pull_rf(const im_pull_request *request, im_pull_response *response)
D
dogsheng 已提交
107
{
W
WangFengTu 已提交
108
    return oci_do_pull_image(request, response);
D
dogsheng 已提交
109 110
}

111
int oci_prepare_rf(const im_prepare_request *request, char **real_rootfs)
D
dogsheng 已提交
112 113 114 115 116 117
{
    if (request == NULL) {
        ERROR("Bim is NULL");
        return -1;
    }

118 119 120 121
    // TODO call storage rootfs prepare interface
    //return isula_rootfs_prepare_and_get_image_conf(request->container_id, request->image_name, request->storage_opt,
    //                                              real_rootfs, NULL);
    return 0;
D
dogsheng 已提交
122 123
}

124
int oci_merge_conf_rf(const host_config *host_spec, container_config *container_spec,
125
                      const im_prepare_request *request, char **real_rootfs)
D
dogsheng 已提交
126 127 128 129 130 131 132 133 134
{
    oci_image_spec *image = NULL;
    int ret = -1;

    if (request == NULL) {
        ERROR("Bim is NULL");
        return -1;
    }

135 136 137
    // TODO call storage rootfs prepare interface
    //ret = isula_rootfs_prepare_and_get_image_conf(request->container_id, request->image_name, host_spec->storage_opt,
    //                                              real_rootfs, &image);
D
dogsheng 已提交
138 139 140 141
    if (ret != 0) {
        ERROR("Get prepare rootfs failed of image: %s", request->image_name);
        goto out;
    }
142
    ret = oci_image_conf_merge_into_spec(request->image_name, container_spec);
D
dogsheng 已提交
143 144 145 146 147 148 149 150 151 152
    if (ret != 0) {
        ERROR("Failed to merge oci config for image: %s", request->image_name);
        goto out;
    }

out:
    free_oci_image_spec(image);
    return ret;
}

153
int oci_delete_rf(const im_delete_request *request)
D
dogsheng 已提交
154 155 156 157 158
{
    if (request == NULL) {
        ERROR("Request is NULL");
        return -1;
    }
159 160 161 162

    // TODO call storage rootfs remove interface
    //return isula_rootfs_remove(request->name_id);
    return 0;
D
dogsheng 已提交
163 164
}

165
int oci_mount_rf(const im_mount_request *request)
D
dogsheng 已提交
166 167 168 169 170
{
    if (request == NULL) {
        ERROR("Invalid arguments");
        return -1;
    }
171 172 173
    // TODO call storage rootfs mount interface
    //return isula_rootfs_mount(request->name_id);
    return 0;
D
dogsheng 已提交
174 175
}

176
int oci_umount_rf(const im_umount_request *request)
D
dogsheng 已提交
177 178 179 180 181
{
    if (request == NULL) {
        ERROR("Invalid arguments");
        return -1;
    }
182 183 184
    // TODO call storage rootfs umount interface
    //return isula_rootfs_umount(request->name_id, request->force);
    return 0;
D
dogsheng 已提交
185 186
}

187
int oci_rmi(const im_remove_request *request)
D
dogsheng 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
{
    int ret = -1;
    char *real_image_name = NULL;

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

    real_image_name = oci_resolve_image_name(request->image.image);
    if (real_image_name == NULL) {
        ERROR("Failed to resolve image name");
        goto out;
    }

L
LiFeng 已提交
203
    ret = storage_img_delete(real_image_name, true);
D
dogsheng 已提交
204
    if (ret != 0) {
L
LiFeng 已提交
205
        ERROR("Failed to remove image '%s'", real_image_name);
L
LiFeng 已提交
206
        goto out;
D
dogsheng 已提交
207
    }
L
LiFeng 已提交
208

D
dogsheng 已提交
209 210 211 212 213
out:
    free(real_image_name);
    return ret;
}

W
WangFengTu 已提交
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 242 243 244 245 246 247 248 249 250 251 252 253
int isula_import(const im_import_request *request, char **id)
{
    int ret = -1;
    char *dest_name = NULL;
    char *errmsg = NULL;

    if (request == NULL || request->file == NULL || request->tag == NULL || id == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    dest_name = oci_normalize_image_name(request->tag);
    if (dest_name == NULL) {
        ret = -1;
        ERROR("Failed to resolve image name");
        goto err_out;
    }

    ret = isula_do_import(request->file, dest_name, id);
    if (ret != 0) {
        goto err_out;
    }

    ret = register_new_oci_image_into_memory(dest_name);
    if (ret != 0) {
        ERROR("Register image %s into store failed", dest_name);
        goto err_out;
    }

    goto out;

err_out:
    free(*id);
    *id = NULL;
out:
    free(dest_name);
    free(errmsg);
    return ret;
}

L
LiFeng 已提交
254
int oci_tag(const im_tag_request *request)
W
WangFengTu 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
{
    int ret = -1;
    char *src_name = NULL;
    char *dest_name = NULL;
    char *errmsg = NULL;

    if (request == NULL || request->src_name.image == NULL || request->dest_name.image == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    src_name = oci_resolve_image_name(request->src_name.image);
    if (src_name == NULL) {
        ret = -1;
        ERROR("Failed to resolve source image name");
        goto out;
    }
    dest_name = oci_normalize_image_name(request->dest_name.image);
    if (src_name == NULL) {
        ret = -1;
        ERROR("Failed to resolve source image name");
        goto out;
    }

279 280
    // TODO call storage rootfs tag interface
    // ret = isula_image_tag(src_name, dest_name, &errmsg);
W
WangFengTu 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293
    if (ret != 0) {
        isulad_set_error_message("Failed to tag image with error: %s", errmsg);
        ERROR("Failed to tag image '%s' to '%s' with error: %s", src_name, dest_name, errmsg);
        goto out;
    }

out:
    free(src_name);
    free(dest_name);
    free(errmsg);
    return ret;
}

294
int oci_container_filesystem_usage(const im_container_fs_usage_request *request, imagetool_fs_info **fs_usage)
D
dogsheng 已提交
295 296 297 298 299 300 301 302 303 304
{
    int ret = 0;
    char *output = NULL;
    parser_error err = NULL;

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

305 306
    // TODO call storage container fs interface
    // ret = isula_container_fs_usage(request->name_id, &output);
D
dogsheng 已提交
307 308 309 310 311 312 313 314
    if (ret != 0) {
        ERROR("Failed to inspect container filesystem info");
        goto out;
    }

    *fs_usage = imagetool_fs_info_parse_data(output, NULL, &err);
    if (*fs_usage == NULL) {
        ERROR("Failed to parse output json: %s", err);
L
LiuHao 已提交
315
        isulad_set_error_message("Failed to parse output json:%s", err);
D
dogsheng 已提交
316 317 318 319 320 321 322 323
        ret = -1;
    }

out:
    free(output);
    return ret;
}

324
int oci_get_filesystem_info(im_fs_info_response **response)
D
dogsheng 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337
{
    int ret = -1;

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

    *response = (im_fs_info_response *)util_common_calloc_s(sizeof(im_fs_info_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }
L
LiFeng 已提交
338 339 340 341 342 343 344 345

    (*response)->fs_info = util_common_calloc_s(sizeof(imagetool_fs_info));
    if ((*response)->fs_info == NULL) {
        ERROR("Out of memory");
        goto err_out;
    }

    ret = storage_get_images_fs_usage((*response)->fs_info);
D
dogsheng 已提交
346 347 348 349 350 351
    if (ret != 0) {
        ERROR("Failed to inspect image filesystem info");
        goto err_out;
    }

    return 0;
L
LiFeng 已提交
352

D
dogsheng 已提交
353 354 355 356 357 358
err_out:
    free_im_fs_info_response(*response);
    *response = NULL;
    return -1;
}

359
int oci_load_image(const im_load_request *request)
D
dogsheng 已提交
360 361 362 363 364 365 366 367
{
    int ret = 0;

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

368 369
    // TODO call storage metadata load interface
    //ret = isula_image_load(request->file, request->tag, &refs);
D
dogsheng 已提交
370 371 372 373 374 375 376 377 378
    if (ret != 0) {
        ERROR("Failed to load image");
        goto out;
    }

out:
    return ret;
}

379
int oci_export_rf(const im_export_request *request)
D
dogsheng 已提交
380 381 382 383 384 385 386 387
{
    int ret = 0;

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

388 389
    // TODO call storage export load interface
    //ret = isula_container_export(request->name_id, request->file, 0, 0, 0);
D
dogsheng 已提交
390 391 392 393 394 395 396
    if (ret != 0) {
        ERROR("Failed to export container: %s", request->name_id);
    }

    return ret;
}

397
int oci_login(const im_login_request *request)
D
dogsheng 已提交
398 399 400 401 402 403 404 405
{
    int ret = 0;

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

W
WangFengTu 已提交
406
    ret = oci_do_login(request->server, request->username, request->password);
D
dogsheng 已提交
407 408 409 410 411 412 413
    if (ret != 0) {
        ERROR("Login failed");
    }

    return ret;
}

414
int oci_logout(const im_logout_request *request)
D
dogsheng 已提交
415 416 417 418 419 420 421 422
{
    int ret = 0;

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

W
WangFengTu 已提交
423
    ret = oci_do_logout(request->server);
D
dogsheng 已提交
424 425 426 427 428 429 430 431 432
    if (ret != 0) {
        ERROR("Logout failed");
    }

    return ret;
}

static inline void cleanup_container_rootfs(const char *name_id, bool mounted)
{
433 434 435 436 437 438 439 440 441
    // TODO call storage umount interface
    //if (mounted && isula_rootfs_umount(name_id, true) != 0) {
    //    WARN("Remove rootfs: %s failed", name_id);
    //}

    // TODO call storage rootfs rm interface
    //if (isula_rootfs_remove(name_id) != 0) {
    //    WARN("Remove rootfs: %s failed", name_id);
    //}
D
dogsheng 已提交
442
}