import.c revision 0c7bf33a989a58922b3eb9aaa96abd773c8754c4
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering This file is part of systemd.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Copyright 2014 Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is free software; you can redistribute it and/or modify it
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering under the terms of the GNU Lesser General Public License as published by
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering (at your option) any later version.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is distributed in the hope that it will be useful, but
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Lesser General Public License for more details.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering You should have received a copy of the GNU Lesser General Public License
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic bool arg_force = false;
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poetteringstatic const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic void on_finished(DkrImport *import, int error, void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_info("Operation completed successfully.");
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering log_info_errno(error, "Operation failed: %m");
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic int pull_dkr(int argc, char *argv[], void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering _cleanup_(dkr_import_unrefp) DkrImport *import = NULL;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek _cleanup_event_unref_ sd_event *event = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Please specify an index URL with --dkr-index-url=");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Remote name '%s' is not valid.", name);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Tag name '%s' is not valid.", tag);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *p;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt log_error("Local image name '%s' is not valid.", local);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering p = strappenda("/var/lib/container/", local);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_info("Image '%s' already exists.", local);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_info("Pulling '%s' with tag '%s', saving as '%s'.", name, tag, local);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_info("Pulling '%s' with tag '%s'.", name, tag);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return log_error_errno(r, "Failed to allocate event loop: %m");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = dkr_import_new(&import, event, arg_dkr_index_url, on_dkr_finished, event);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return log_error_errno(r, "Failed to allocate importer: %m");
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek r = dkr_import_pull(import, name, tag, local, arg_force);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to pull image: %m");
e3b9d9c8027a7c4c55cf1614e0fe9423fad69e8fZbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to run event loop: %m");
e3b9d9c8027a7c4c55cf1614e0fe9423fad69e8fZbigniew Jędrzejewski-Szmekstatic int help(int argc, char *argv[], void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering printf("%s [OPTIONS...] {COMMAND} ...\n\n"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "Import container or virtual machine image.\n\n"
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek " -h --help Show this help\n"
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek " --version Show package version\n"
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek " --force Force creation of image\n"
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek " --dkr-index-url=URL Specify index URL to use for downloads\n\n"
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek " pull-dkr REMOTE [NAME] Download an image\n",
4b8268f843b0da1cfe1995d93a0b1f95faccc454Zbigniew Jędrzejewski-Szmekstatic int parse_argv(int argc, char *argv[]) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek static const struct option options[] = {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek { "help", no_argument, NULL, 'h' },
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek { "version", no_argument, NULL, ARG_VERSION },
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek { "force", no_argument, NULL, ARG_FORCE },
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek { "dkr-index-url", required_argument, NULL, ARG_DKR_INDEX_URL },
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek if (!dkr_url_is_valid(optarg)) {
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek log_error("Index URL is not valid: %s", optarg);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidtstatic int import_main(int argc, char *argv[]) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek { "help", VERB_ANY, VERB_ANY, 0, help },
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek { "pull-dkr", 2, 3, 0, pull_dkr },
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return dispatch_verb(argc, argv, verbs, NULL);