import.c revision 8620a9a32391fd74d70ddc07c9b79729ad4ec067
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering/***
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering This file is part of systemd.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering Copyright 2014 Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering (at your option) any later version.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering systemd is distributed in the hope that it will be useful, but
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering Lesser General Public License for more details.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering***/
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
4871690d9e32608bbd9b18505b5326c2079c9690Allin Cottrell#include <getopt.h>
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering#include "sd-event.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "event-util.h"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering#include "verbs.h"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering#include "build.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "import-gpt.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "import-dkr.h"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensenstatic bool arg_force = false;
a09abc4ae0bdc0200324eaa0416f23ff2170ec4eLennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic void on_gpt_finished(GptImport *import, int error, void *userdata) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering sd_event *event = userdata;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert(import);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek if (error == 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Operation completed successfully.");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering else
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info_errno(error, "Operation failed: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_exit(event, error);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering}
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic int pull_gpt(int argc, char *argv[], void *userdata) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering _cleanup_(gpt_import_unrefp) GptImport *import = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering const char *url, *local, *suffix;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering int r;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering url = argv[1];
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (!gpt_url_is_valid(url)) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("URL '%s' is not valid.", url);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return -EINVAL;
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (argc >= 3)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering local = argv[2];
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering else {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering const char *e, *p;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering e = url + strlen(url);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering while (e > url && e[-1] == '/')
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek e--;
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek p = e;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering while (p > url && p[-1] != '/')
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering p--;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering local = strndupa(p, e - p);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (isempty(local) || streq(local, "-"))
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek local = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (local) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering const char *p;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering suffix = endswith(local, ".gpt");
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering if (suffix)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering local = strndupa(local, suffix - local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (!machine_name_is_valid(local)) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Local image name '%s' is not valid.", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return -EINVAL;
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering p = strappenda("/var/lib/container/", local, ".gpt");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (laccess(p, F_OK) >= 0) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (!arg_force) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Image '%s' already exists.", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 0;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering } else if (errno != ENOENT)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Pulling '%s', saving as '%s'.", url, local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering } else
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Pulling '%s'.", url);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = sd_event_default(&event);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (r < 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(r, "Failed to allocate event loop: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = gpt_import_new(&import, event, on_gpt_finished, event);
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek if (r < 0)
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to allocate importer: %m");
8c0b803b97bb0ee6603d9be85fb6b69cd6081eafLennart Poettering
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek r = gpt_import_pull(import, url, local, arg_force);
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek if (r < 0)
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to pull image: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = sd_event_loop(event);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (r < 0)
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to run event loop: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Exiting.");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 0;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering}
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic void on_dkr_finished(DkrImport *import, int error, void *userdata) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event *event = userdata;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering assert(import);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (error == 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Operation completed successfully.");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering else
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info_errno(error, "Operation failed: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_exit(event, error);
12a717f8347f3daf0ae46a2b71c7d011d9c12feaZbigniew Jędrzejewski-Szmek}
92ee6447b1deef7c79962a8121fdf8e58acb3a83Zbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int pull_dkr(int argc, char *argv[], void *userdata) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_(dkr_import_unrefp) DkrImport *import = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering const char *name, *tag, *local;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering int r;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek if (!arg_dkr_index_url) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("Please specify an index URL with --dkr-index-url=");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return -EINVAL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering tag = strchr(argv[1], ':');
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (tag) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering name = strndupa(argv[1], tag - argv[1]);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering tag++;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering } else {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering name = argv[1];
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek tag = "latest";
a174f94d529c7ae9be589867308b669ec9b4dcc0Lennart Poettering }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (!dkr_name_is_valid(name)) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("Remote name '%s' is not valid.", name);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return -EINVAL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
2a0e0692565f0435657c93498e09cbb2d3517152Shawn Landden
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (!dkr_tag_is_valid(tag)) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("Tag name '%s' is not valid.", tag);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return -EINVAL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
2a0e0692565f0435657c93498e09cbb2d3517152Shawn Landden
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (argc >= 3)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering local = argv[2];
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering else {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering local = strchr(name, '/');
2a0e0692565f0435657c93498e09cbb2d3517152Shawn Landden if (local)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering local++;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering else
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering local = name;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
2a0e0692565f0435657c93498e09cbb2d3517152Shawn Landden if (isempty(local) || streq(local, "-"))
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering local = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (local) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering const char *p;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (!machine_name_is_valid(local)) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("Local image name '%s' is not valid.", local);
1f6b411372076426c0faf0bb350437fb4d82931fLennart Poettering return -EINVAL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
2a0e0692565f0435657c93498e09cbb2d3517152Shawn Landden
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering p = strappenda("/var/lib/container/", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (laccess(p, F_OK) >= 0) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (!arg_force) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Image '%s' already exists.", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 0;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering } else if (errno != ENOENT)
1f6b411372076426c0faf0bb350437fb4d82931fLennart Poettering return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_info("Pulling '%s' with tag '%s', saving as '%s'.", name, tag, local);
2a0e0692565f0435657c93498e09cbb2d3517152Shawn Landden } else
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_info("Pulling '%s' with tag '%s'.", name, tag);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_event_default(&event);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to allocate event loop: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = dkr_import_new(&import, event, arg_dkr_index_url, on_dkr_finished, event);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (r < 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(r, "Failed to allocate importer: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = dkr_import_pull(import, name, tag, local, arg_force);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (r < 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(r, "Failed to pull image: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = sd_event_loop(event);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (r < 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Exiting.");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 0;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering}
505b6a61c22d5565e9308045c7b9bf79f7d0517eLennart Poettering
fa1c4b518ec7d8ec2d647213ee651cde4d6c9d7eZbigniew Jędrzejewski-Szmekstatic int help(int argc, char *argv[], void *userdata) {
505b6a61c22d5565e9308045c7b9bf79f7d0517eLennart Poettering
505b6a61c22d5565e9308045c7b9bf79f7d0517eLennart Poettering printf("%s [OPTIONS...] {COMMAND} ...\n\n"
505b6a61c22d5565e9308045c7b9bf79f7d0517eLennart Poettering "Import container or virtual machine image.\n\n"
505b6a61c22d5565e9308045c7b9bf79f7d0517eLennart Poettering " -h --help Show this help\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " --version Show package version\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " --force Force creation of image\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " --dkr-index-url=URL Specify index URL to use for downloads\n\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering "Commands:\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " pull-dkr REMOTE [NAME] Download a DKR image\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " pull-gpt URL [NAME] Download a GPT image\n",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering program_invocation_short_name);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 0;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering}
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering enum {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering ARG_VERSION = 0x100,
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek ARG_FORCE,
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering ARG_DKR_INDEX_URL,
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering };
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek
a174f94d529c7ae9be589867308b669ec9b4dcc0Lennart Poettering static const struct option options[] = {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "help", no_argument, NULL, 'h' },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "version", no_argument, NULL, ARG_VERSION },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "force", no_argument, NULL, ARG_FORCE },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "dkr-index-url", required_argument, NULL, ARG_DKR_INDEX_URL },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering {}
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering };
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering int c;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering assert(argc >= 0);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering assert(argv);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek switch (c) {
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek case 'h':
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek return help(0, NULL, NULL);
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek case ARG_VERSION:
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering puts(PACKAGE_STRING);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering puts(SYSTEMD_FEATURES);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 0;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering case ARG_FORCE:
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering arg_force = true;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering break;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering case ARG_DKR_INDEX_URL:
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (!dkr_url_is_valid(optarg)) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen log_error("Index URL is not valid: %s", optarg);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen return -EINVAL;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering arg_dkr_index_url = optarg;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek break;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering case '?':
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return -EINVAL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering default:
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering assert_not_reached("Unhandled option");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering }
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 1;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering}
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic int import_main(int argc, char *argv[]) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering static const Verb verbs[] = {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "help", VERB_ANY, VERB_ANY, 0, help },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "pull-dkr", 2, 3, 0, pull_dkr },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "pull-gpt", 2, 3, 0, pull_gpt },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering {}
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering };
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering return dispatch_verb(argc, argv, verbs, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering}
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringint main(int argc, char *argv[]) {
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering int r;
1dfa7e79a60de680086b1d93fcc3629b463f58bdLennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering setlocale(LC_ALL, "");
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering log_parse_environment();
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering log_open();
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = parse_argv(argc, argv);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (r <= 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering goto finish;
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering r = import_main(argc, argv);
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering
73843b52585d42cc1a970a1c664818ece6942e9eLennart Poetteringfinish:
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering}
1dfa7e79a60de680086b1d93fcc3629b463f58bdLennart Poettering