]>
Commit | Line | Data |
---|---|---|
72648326 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <getopt.h> | |
21 | ||
22 | #include "sd-event.h" | |
3f6fd1ba | 23 | |
b5efdb8a | 24 | #include "alloc-util.h" |
25300b5a | 25 | #include "hostname-util.h" |
85dbc41d | 26 | #include "import-util.h" |
3f6fd1ba | 27 | #include "machine-image.h" |
6bedfcbb | 28 | #include "parse-util.h" |
3f6fd1ba LP |
29 | #include "pull-raw.h" |
30 | #include "pull-tar.h" | |
31 | #include "signal-util.h" | |
07630cea | 32 | #include "string-util.h" |
3f6fd1ba | 33 | #include "verbs.h" |
49cf4170 | 34 | #include "web-util.h" |
72648326 LP |
35 | |
36 | static bool arg_force = false; | |
5f129649 | 37 | static const char *arg_image_root = "/var/lib/machines"; |
8f695058 | 38 | static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE; |
9854730b | 39 | static bool arg_settings = true; |
91f4347e | 40 | |
3d7415f4 LP |
41 | static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
42 | log_notice("Transfer aborted."); | |
43 | sd_event_exit(sd_event_source_get_event(s), EINTR); | |
44 | return 0; | |
45 | } | |
46 | ||
dc2c282b | 47 | static void on_tar_finished(TarPull *pull, int error, void *userdata) { |
56ebfaf1 | 48 | sd_event *event = userdata; |
dc2c282b | 49 | assert(pull); |
56ebfaf1 LP |
50 | |
51 | if (error == 0) | |
52 | log_info("Operation completed successfully."); | |
56ebfaf1 | 53 | |
3d7415f4 | 54 | sd_event_exit(event, abs(error)); |
56ebfaf1 LP |
55 | } |
56 | ||
57 | static int pull_tar(int argc, char *argv[], void *userdata) { | |
dc2c282b | 58 | _cleanup_(tar_pull_unrefp) TarPull *pull = NULL; |
4afd3348 | 59 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
56ebfaf1 LP |
60 | const char *url, *local; |
61 | _cleanup_free_ char *l = NULL, *ll = NULL; | |
62 | int r; | |
63 | ||
64 | url = argv[1]; | |
65 | if (!http_url_is_valid(url)) { | |
66 | log_error("URL '%s' is not valid.", url); | |
67 | return -EINVAL; | |
68 | } | |
69 | ||
70 | if (argc >= 3) | |
71 | local = argv[2]; | |
72 | else { | |
85dbc41d | 73 | r = import_url_last_component(url, &l); |
56ebfaf1 LP |
74 | if (r < 0) |
75 | return log_error_errno(r, "Failed get final component of URL: %m"); | |
76 | ||
77 | local = l; | |
78 | } | |
79 | ||
80 | if (isempty(local) || streq(local, "-")) | |
81 | local = NULL; | |
82 | ||
83 | if (local) { | |
3d7415f4 | 84 | r = tar_strip_suffixes(local, &ll); |
56ebfaf1 LP |
85 | if (r < 0) |
86 | return log_oom(); | |
87 | ||
88 | local = ll; | |
89 | ||
90 | if (!machine_name_is_valid(local)) { | |
91 | log_error("Local image name '%s' is not valid.", local); | |
92 | return -EINVAL; | |
93 | } | |
94 | ||
95 | if (!arg_force) { | |
96 | r = image_find(local, NULL); | |
97 | if (r < 0) | |
98 | return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); | |
99 | else if (r > 0) { | |
92dfd88b | 100 | log_error("Image '%s' already exists.", local); |
56ebfaf1 LP |
101 | return -EEXIST; |
102 | } | |
103 | } | |
104 | ||
105 | log_info("Pulling '%s', saving as '%s'.", url, local); | |
106 | } else | |
107 | log_info("Pulling '%s'.", url); | |
108 | ||
109 | r = sd_event_default(&event); | |
110 | if (r < 0) | |
111 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
112 | ||
72c0a2c2 LP |
113 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0); |
114 | (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL); | |
115 | (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL); | |
56ebfaf1 | 116 | |
dc2c282b | 117 | r = tar_pull_new(&pull, event, arg_image_root, on_tar_finished, event); |
56ebfaf1 | 118 | if (r < 0) |
dc2c282b | 119 | return log_error_errno(r, "Failed to allocate puller: %m"); |
56ebfaf1 | 120 | |
9854730b | 121 | r = tar_pull_start(pull, url, local, arg_force, arg_verify, arg_settings); |
56ebfaf1 LP |
122 | if (r < 0) |
123 | return log_error_errno(r, "Failed to pull image: %m"); | |
124 | ||
125 | r = sd_event_loop(event); | |
126 | if (r < 0) | |
127 | return log_error_errno(r, "Failed to run event loop: %m"); | |
128 | ||
129 | log_info("Exiting."); | |
3d7415f4 | 130 | return -r; |
56ebfaf1 LP |
131 | } |
132 | ||
dc2c282b | 133 | static void on_raw_finished(RawPull *pull, int error, void *userdata) { |
90199220 | 134 | sd_event *event = userdata; |
dc2c282b | 135 | assert(pull); |
90199220 LP |
136 | |
137 | if (error == 0) | |
138 | log_info("Operation completed successfully."); | |
90199220 | 139 | |
3d7415f4 | 140 | sd_event_exit(event, abs(error)); |
edce2aed LP |
141 | } |
142 | ||
aceac2f0 | 143 | static int pull_raw(int argc, char *argv[], void *userdata) { |
dc2c282b | 144 | _cleanup_(raw_pull_unrefp) RawPull *pull = NULL; |
4afd3348 | 145 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
edce2aed | 146 | const char *url, *local; |
0d6e763b | 147 | _cleanup_free_ char *l = NULL, *ll = NULL; |
90199220 LP |
148 | int r; |
149 | ||
150 | url = argv[1]; | |
a2e03378 | 151 | if (!http_url_is_valid(url)) { |
90199220 LP |
152 | log_error("URL '%s' is not valid.", url); |
153 | return -EINVAL; | |
154 | } | |
155 | ||
8620a9a3 LP |
156 | if (argc >= 3) |
157 | local = argv[2]; | |
158 | else { | |
85dbc41d | 159 | r = import_url_last_component(url, &l); |
0d6e763b LP |
160 | if (r < 0) |
161 | return log_error_errno(r, "Failed get final component of URL: %m"); | |
90199220 | 162 | |
0d6e763b | 163 | local = l; |
90199220 LP |
164 | } |
165 | ||
8620a9a3 LP |
166 | if (isempty(local) || streq(local, "-")) |
167 | local = NULL; | |
90199220 | 168 | |
8620a9a3 | 169 | if (local) { |
3d7415f4 | 170 | r = raw_strip_suffixes(local, &ll); |
edce2aed LP |
171 | if (r < 0) |
172 | return log_oom(); | |
173 | ||
0d6e763b | 174 | local = ll; |
8620a9a3 LP |
175 | |
176 | if (!machine_name_is_valid(local)) { | |
177 | log_error("Local image name '%s' is not valid.", local); | |
178 | return -EINVAL; | |
179 | } | |
180 | ||
0d6e763b LP |
181 | if (!arg_force) { |
182 | r = image_find(local, NULL); | |
183 | if (r < 0) | |
184 | return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); | |
185 | else if (r > 0) { | |
92dfd88b | 186 | log_error("Image '%s' already exists.", local); |
0d6e763b | 187 | return -EEXIST; |
8620a9a3 | 188 | } |
0d6e763b | 189 | } |
90199220 | 190 | |
8620a9a3 LP |
191 | log_info("Pulling '%s', saving as '%s'.", url, local); |
192 | } else | |
193 | log_info("Pulling '%s'.", url); | |
90199220 LP |
194 | |
195 | r = sd_event_default(&event); | |
196 | if (r < 0) | |
197 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
198 | ||
72c0a2c2 LP |
199 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0); |
200 | (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL); | |
201 | (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL); | |
90199220 | 202 | |
dc2c282b | 203 | r = raw_pull_new(&pull, event, arg_image_root, on_raw_finished, event); |
90199220 | 204 | if (r < 0) |
dc2c282b | 205 | return log_error_errno(r, "Failed to allocate puller: %m"); |
90199220 | 206 | |
9854730b | 207 | r = raw_pull_start(pull, url, local, arg_force, arg_verify, arg_settings); |
90199220 LP |
208 | if (r < 0) |
209 | return log_error_errno(r, "Failed to pull image: %m"); | |
210 | ||
211 | r = sd_event_loop(event); | |
212 | if (r < 0) | |
213 | return log_error_errno(r, "Failed to run event loop: %m"); | |
214 | ||
215 | log_info("Exiting."); | |
3d7415f4 | 216 | return -r; |
90199220 LP |
217 | } |
218 | ||
72648326 LP |
219 | static int help(int argc, char *argv[], void *userdata) { |
220 | ||
221 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" | |
587fec42 | 222 | "Download container or virtual machine images.\n\n" |
72648326 LP |
223 | " -h --help Show this help\n" |
224 | " --version Show package version\n" | |
91f4347e | 225 | " --force Force creation of image\n" |
9854730b LP |
226 | " --verify=MODE Verify downloaded image, one of: 'no',\n" |
227 | " 'checksum', 'signature'\n" | |
228 | " --settings=BOOL Download settings file with image\n" | |
b43d75c3 | 229 | " --image-root=PATH Image root directory\n\n" |
72648326 | 230 | "Commands:\n" |
aa9bd499 | 231 | " tar URL [NAME] Download a TAR image\n" |
b43d75c3 | 232 | " raw URL [NAME] Download a RAW image\n", |
72648326 LP |
233 | program_invocation_short_name); |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | static int parse_argv(int argc, char *argv[]) { | |
239 | ||
240 | enum { | |
241 | ARG_VERSION = 0x100, | |
242 | ARG_FORCE, | |
087682d1 | 243 | ARG_IMAGE_ROOT, |
8f695058 | 244 | ARG_VERIFY, |
9854730b | 245 | ARG_SETTINGS, |
72648326 LP |
246 | }; |
247 | ||
248 | static const struct option options[] = { | |
249 | { "help", no_argument, NULL, 'h' }, | |
250 | { "version", no_argument, NULL, ARG_VERSION }, | |
251 | { "force", no_argument, NULL, ARG_FORCE }, | |
087682d1 | 252 | { "image-root", required_argument, NULL, ARG_IMAGE_ROOT }, |
8f695058 | 253 | { "verify", required_argument, NULL, ARG_VERIFY }, |
9854730b | 254 | { "settings", required_argument, NULL, ARG_SETTINGS }, |
72648326 LP |
255 | {} |
256 | }; | |
257 | ||
9854730b | 258 | int c, r; |
72648326 LP |
259 | |
260 | assert(argc >= 0); | |
261 | assert(argv); | |
262 | ||
263 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) | |
264 | ||
265 | switch (c) { | |
266 | ||
267 | case 'h': | |
7eeeb28e | 268 | return help(0, NULL, NULL); |
72648326 LP |
269 | |
270 | case ARG_VERSION: | |
3f6fd1ba | 271 | return version(); |
72648326 LP |
272 | |
273 | case ARG_FORCE: | |
274 | arg_force = true; | |
275 | break; | |
276 | ||
087682d1 LP |
277 | case ARG_IMAGE_ROOT: |
278 | arg_image_root = optarg; | |
279 | break; | |
280 | ||
8f695058 LP |
281 | case ARG_VERIFY: |
282 | arg_verify = import_verify_from_string(optarg); | |
283 | if (arg_verify < 0) { | |
284 | log_error("Invalid verification setting '%s'", optarg); | |
285 | return -EINVAL; | |
286 | } | |
287 | ||
288 | break; | |
289 | ||
9854730b LP |
290 | case ARG_SETTINGS: |
291 | r = parse_boolean(optarg); | |
292 | if (r < 0) | |
293 | return log_error_errno(r, "Failed to parse --settings= parameter '%s'", optarg); | |
294 | ||
295 | arg_settings = r; | |
296 | break; | |
297 | ||
72648326 LP |
298 | case '?': |
299 | return -EINVAL; | |
300 | ||
301 | default: | |
302 | assert_not_reached("Unhandled option"); | |
303 | } | |
304 | ||
305 | return 1; | |
306 | } | |
307 | ||
dc2c282b | 308 | static int pull_main(int argc, char *argv[]) { |
72648326 | 309 | |
7eeeb28e | 310 | static const Verb verbs[] = { |
aa9bd499 LP |
311 | { "help", VERB_ANY, VERB_ANY, 0, help }, |
312 | { "tar", 2, 3, 0, pull_tar }, | |
313 | { "raw", 2, 3, 0, pull_raw }, | |
72648326 LP |
314 | {} |
315 | }; | |
316 | ||
317 | return dispatch_verb(argc, argv, verbs, NULL); | |
318 | } | |
319 | ||
320 | int main(int argc, char *argv[]) { | |
321 | int r; | |
322 | ||
323 | setlocale(LC_ALL, ""); | |
324 | log_parse_environment(); | |
325 | log_open(); | |
326 | ||
327 | r = parse_argv(argc, argv); | |
328 | if (r <= 0) | |
329 | goto finish; | |
330 | ||
ce30c8dc | 331 | (void) ignore_signals(SIGPIPE, -1); |
b6e676ce | 332 | |
dc2c282b | 333 | r = pull_main(argc, argv); |
72648326 LP |
334 | |
335 | finish: | |
336 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
337 | } |