chiark / gitweb /
import: simplify dkr importer, by making use of generic import-job logic, used by...
[elogind.git] / src / import / import.c
index 3362f4a9efa2d547a4076bf613cb4643d07b3c02..4c36108c2efb8ed58cfaf7f82bc2efceae9a3a7e 100644 (file)
@@ -33,7 +33,7 @@
 
 static bool arg_force = false;
 static const char *arg_image_root = "/var/lib/machines";
-
+static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
 static const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL;
 
 static void on_tar_finished(TarImport *import, int error, void *userdata) {
@@ -42,10 +42,8 @@ static void on_tar_finished(TarImport *import, int error, void *userdata) {
 
         if (error == 0)
                 log_info("Operation completed successfully.");
-        else
-                log_error_errno(error, "Operation failed: %m");
 
-        sd_event_exit(event, error);
+        sd_event_exit(event, EXIT_FAILURE);
 }
 
 static int strip_tar_suffixes(const char *name, char **ret) {
@@ -53,10 +51,12 @@ static int strip_tar_suffixes(const char *name, char **ret) {
         char *s;
 
         e = endswith(name, ".tar");
+        if (!e)
+                e = endswith(name, ".tar.xz");
         if (!e)
                 e = endswith(name, ".tar.gz");
         if (!e)
-                e = endswith(name, ".tar.xz");
+                e = endswith(name, ".tar.bz2");
         if (!e)
                 e = endswith(name, ".tgz");
         if (!e)
@@ -137,7 +137,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to allocate importer: %m");
 
-        r = tar_import_pull(import, url, local, arg_force);
+        r = tar_import_pull(import, url, local, arg_force, arg_verify);
         if (r < 0)
                 return log_error_errno(r, "Failed to pull image: %m");
 
@@ -147,7 +147,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
 
         log_info("Exiting.");
 
-        return 0;
+        return r;
 }
 
 static void on_raw_finished(RawImport *import, int error, void *userdata) {
@@ -156,18 +156,19 @@ static void on_raw_finished(RawImport *import, int error, void *userdata) {
 
         if (error == 0)
                 log_info("Operation completed successfully.");
-        else
-                log_error_errno(error, "Operation failed: %m");
 
-        sd_event_exit(event, error);
+        sd_event_exit(event, EXIT_FAILURE);
 }
 
 static int strip_raw_suffixes(const char *p, char **ret) {
         static const char suffixes[] =
                 ".xz\0"
+                ".gz\0"
+                ".bz2\0"
                 ".raw\0"
                 ".qcow2\0"
-                ".img\0";
+                ".img\0"
+                ".bin\0";
 
         _cleanup_free_ char *q = NULL;
 
@@ -263,7 +264,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to allocate importer: %m");
 
-        r = raw_import_pull(import, url, local, arg_force);
+        r = raw_import_pull(import, url, local, arg_force, arg_verify);
         if (r < 0)
                 return log_error_errno(r, "Failed to pull image: %m");
 
@@ -273,7 +274,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
 
         log_info("Exiting.");
 
-        return 0;
+        return r;
 }
 
 static void on_dkr_finished(DkrImport *import, int error, void *userdata) {
@@ -282,10 +283,8 @@ static void on_dkr_finished(DkrImport *import, int error, void *userdata) {
 
         if (error == 0)
                 log_info("Operation completed successfully.");
-        else
-                log_error_errno(error, "Operation failed: %m");
 
-        sd_event_exit(event, error);
+        sd_event_exit(event, EXIT_FAILURE);
 }
 
 static int pull_dkr(int argc, char *argv[], void *userdata) {
@@ -299,6 +298,11 @@ static int pull_dkr(int argc, char *argv[], void *userdata) {
                 return -EINVAL;
         }
 
+        if (arg_verify != IMPORT_VERIFY_NO) {
+                log_error("Imports from dkr do not support image verification, please pass --verify=no.");
+                return -EINVAL;
+        }
+
         tag = strchr(argv[1], ':');
         if (tag) {
                 name = strndupa(argv[1], tag - argv[1]);
@@ -332,21 +336,20 @@ static int pull_dkr(int argc, char *argv[], void *userdata) {
                 local = NULL;
 
         if (local) {
-                const char *p;
-
                 if (!machine_name_is_valid(local)) {
                         log_error("Local image name '%s' is not valid.", local);
                         return -EINVAL;
                 }
 
-                p = strappenda(arg_image_root, "/", local);
-                if (laccess(p, F_OK) >= 0) {
-                        if (!arg_force) {
-                                log_info("Image '%s' already exists.", local);
-                                return 0;
+                if (!arg_force) {
+                        r = image_find(local, NULL);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
+                        else if (r > 0) {
+                                log_error_errno(EEXIST, "Image '%s' already exists.", local);
+                                return -EEXIST;
                         }
-                } else if (errno != ENOENT)
-                        return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local);
+                }
 
                 log_info("Pulling '%s' with tag '%s', saving as '%s'.", name, tag, local);
         } else
@@ -384,6 +387,8 @@ static int help(int argc, char *argv[], void *userdata) {
                "  -h --help                   Show this help\n"
                "     --version                Show package version\n"
                "     --force                  Force creation of image\n"
+               "     --verify=                Verify downloaded image, one of: 'no', 'sum'\n"
+               "                              'signature'.\n"
                "     --image-root=            Image root directory\n"
                "     --dkr-index-url=URL      Specify index URL to use for downloads\n\n"
                "Commands:\n"
@@ -402,6 +407,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_FORCE,
                 ARG_DKR_INDEX_URL,
                 ARG_IMAGE_ROOT,
+                ARG_VERIFY,
         };
 
         static const struct option options[] = {
@@ -410,6 +416,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "force",           no_argument,       NULL, ARG_FORCE           },
                 { "dkr-index-url",   required_argument, NULL, ARG_DKR_INDEX_URL   },
                 { "image-root",      required_argument, NULL, ARG_IMAGE_ROOT      },
+                { "verify",          required_argument, NULL, ARG_VERIFY          },
                 {}
         };
 
@@ -435,7 +442,7 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_DKR_INDEX_URL:
-                        if (!dkr_url_is_valid(optarg)) {
+                        if (!http_url_is_valid(optarg)) {
                                 log_error("Index URL is not valid: %s", optarg);
                                 return -EINVAL;
                         }
@@ -447,6 +454,15 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_image_root = optarg;
                         break;
 
+                case ARG_VERIFY:
+                        arg_verify = import_verify_from_string(optarg);
+                        if (arg_verify < 0) {
+                                log_error("Invalid verification setting '%s'", optarg);
+                                return -EINVAL;
+                        }
+
+                        break;
+
                 case '?':
                         return -EINVAL;