|
35 | 35 | # |
36 | 36 | # TODO - files should be owned by www-data |
37 | 37 |
|
38 | | -- name: "Download {{ iiab_map_host_url }}/{{ item }} via .meta4 file" |
39 | | - # cd to the temp directory so that the aria2c file, data file, and (perhaps |
40 | | - # eventually) torrent file all end up there. |
41 | | - shell: | |
42 | | - set -euo pipefail |
| 38 | +- block: |
| 39 | + - name: "Fetching file size for {{ iiab_map_host_url }}/{{ item }} via .meta4 file" |
| 40 | + shell: | |
| 41 | + import sys, xmltodict, requests, os |
43 | 42 |
|
44 | | - cd /library/downloads/maps/ |
| 43 | + if os.path.exists("{{ dest_path }}"): |
| 44 | + sys.exit(0) |
45 | 45 |
|
46 | | - aria2c \ |
47 | | - --connect-timeout="{{ download_timeout }}" \ |
48 | | - --log-level=warn \ |
49 | | - --console-log-level=warn \ |
50 | | - --summary-interval=0 \ |
51 | | - --show-console-readout=false \ |
52 | | - --download-result=hide \ |
53 | | - --follow-metalink=mem \ |
54 | | - --max-connection-per-server=4 \ |
55 | | - --file-allocation=falloc \ |
56 | | - --enable-http-pipelining=true \ |
57 | | - --seed-time=0 \ |
58 | | - "{{ iiab_map_host_url }}/{{ item }}.meta4" |
| 46 | + size = int(xmltodict.parse( |
| 47 | + requests.get("{{ iiab_map_host_url }}/{{ item }}.meta4").content |
| 48 | + )['metalink']['file']['size']) |
| 49 | + KiB = 1024 |
| 50 | + MiB = KiB * 1024 |
| 51 | + GiB = MiB * 1024 |
59 | 52 |
|
60 | | - chmod 644 "{{ item }}" |
61 | | - mv "{{ item }}" "{{ dest_path }}" |
62 | | - args: |
63 | | - executable: /bin/bash |
64 | | - creates: "{{ dest_path }}" |
65 | | - vars: |
66 | | - # Where the file eventually goes |
67 | | - dest_path: "{{ dest_base_path }}/{{ item }}" |
| 53 | + if size < MiB: |
| 54 | + size_disp = str(round(size / KiB, 2)) + ' KiB' |
| 55 | + elif size < GiB: |
| 56 | + size_disp = str(round(size / MiB, 2)) + ' MiB' |
| 57 | + else: |
| 58 | + size_disp = str(round(size / GiB, 2)) + ' GiB' |
| 59 | + print(' (' + size_disp + ') To check progress, run: "tail {{ working_dir }}/{{ log_file }}"') |
| 60 | + args: |
| 61 | + executable: /usr/bin/python3 |
| 62 | + vars: |
| 63 | + # Where the file eventually goes |
| 64 | + dest_path: "{{ dest_base_path }}/{{ item }}" |
| 65 | + |
| 66 | + log_file: "{{ item }}.log" |
| 67 | + working_dir: "/library/downloads/maps" |
| 68 | + register: download_file_size |
| 69 | + |
| 70 | + - name: "Download {{ item }}{{ download_file_size.stdout_lines.0 | default(' (done)')}}" |
| 71 | + # cd to the temp directory so that the aria2c file, data file, and (perhaps |
| 72 | + # eventually) torrent file all end up there. |
| 73 | + shell: | |
| 74 | + set -euo pipefail |
| 75 | +
|
| 76 | + cd {{ working_dir }} |
| 77 | +
|
| 78 | + echo "Starting download..." > "{{ log_file }}" |
| 79 | + aria2c \ |
| 80 | + --connect-timeout="{{ download_timeout }}" \ |
| 81 | + --log-level=warn \ |
| 82 | + --console-log-level=warn \ |
| 83 | + --summary-interval=60 \ |
| 84 | + --download-result=hide \ |
| 85 | + --follow-metalink=mem \ |
| 86 | + --max-connection-per-server=4 \ |
| 87 | + --file-allocation=falloc \ |
| 88 | + --show-console-readout=false \ |
| 89 | + --enable-http-pipelining=true \ |
| 90 | + --seed-time=0 \ |
| 91 | + "{{ iiab_map_host_url }}/{{ item }}.meta4" \ |
| 92 | + >> "{{ log_file }}" |
| 93 | +
|
| 94 | + chmod 644 "{{ item }}" |
| 95 | + mv "{{ item }}" "{{ dest_path }}" |
| 96 | + rm "{{ log_file }}" |
| 97 | + args: |
| 98 | + executable: /bin/bash |
| 99 | + creates: "{{ dest_path }}" |
| 100 | + vars: |
| 101 | + # Where the file eventually goes |
| 102 | + dest_path: "{{ dest_base_path }}/{{ item }}" |
| 103 | + |
| 104 | + log_file: "{{ item }}.log" |
| 105 | + working_dir: "/library/downloads/maps" |
| 106 | + rescue: |
| 107 | + # We output summaries to a log file for the user's benefit, |
| 108 | + # but all of the errors necessarily show up there as well. |
| 109 | + # Here, we parse out the error message, ignoring almost all of |
| 110 | + # the summary lines, which could be a lot. |
| 111 | + # Finally, we exit 1 so Ansible shows this error. |
| 112 | + - name: "Error downloading {{ item }}" |
| 113 | + shell: | |
| 114 | + set -euo pipefail |
| 115 | +
|
| 116 | + cd {{ working_dir }} |
| 117 | +
|
| 118 | + echo "aria2c error:" 1>&2 |
| 119 | + tac "{{ log_file }}" | sed '/'"Download Progress Summary as of"'/q' | tac 1>&2 |
| 120 | + exit 1 |
| 121 | + vars: |
| 122 | + log_file: "{{ item }}.log" |
| 123 | + working_dir: "/library/downloads/maps" |
0 commit comments