]> git.ekhem.eu.org Git - turnup.git/commitdiff
Add a script for downloading a file from Google Drive.
authorJakub Czajka <jakub@ekhem.eu.org>
Wed, 27 Sep 2023 19:05:30 +0000 (21:05 +0200)
committerJakub Czajka <jczajka@google.com>
Sun, 24 Dec 2023 18:46:45 +0000 (19:46 +0100)
download.yaml [new file with mode: 0644]

diff --git a/download.yaml b/download.yaml
new file mode 100644 (file)
index 0000000..5d05cd4
--- /dev/null
@@ -0,0 +1,63 @@
+# Copyright (c) 2023 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+- name: Download from Google Drive
+  get_url:
+    url: "https://www.googleapis.com/drive/v3/files/{{ item.uid }}?alt=media"
+    headers:
+      Authorization: "Bearer {{ access_token }}"
+    dest: "/tmp/{{ item.uid }}"
+- name: Decrypt
+  command: |
+    python3 -c
+    "
+    from cryptography.fernet import Fernet
+
+    encryption_key = Fernet('{{ key }}')
+    with open('/tmp/{{ item.uid }}', 'rb') as in_file:
+      token = encryption_key.decrypt(in_file.read())
+      with open('/tmp/{{ item.name }}.zip', 'wb+') as outfile:
+        outfile.write(token)
+    "
+- name: Unarchive if necessary
+  command: |
+    python3 -c
+    "
+    import shutil
+    import zipfile
+
+    name = '/tmp/{{ item.name }}'
+    archive = name + '.zip'
+    if zipfile.is_zipfile(archive):
+      shutil.unpack_archive(archive, extract_dir='/tmp', format='zip')
+    else:
+      shutil.move(archive, name)
+    "
+- name: Ensure destination exists
+  file:
+    path: "{{ item.dest }}"
+    state: directory
+  become: true
+- name: Move to destination
+  command: mv "/tmp/{{ item.name }}" "{{ item.dest }}/"
+  become: true
+- name: Stat destination
+  stat:
+    path: "{{ item.dest }}/{{ item.name }}"
+  register: dest_file
+- name: Recursively set permissions at destination
+  file:
+    path: "{{ item.dest }}/{{ item.name }}"
+    owner: "{{ item.owner }}"
+    group: "{{ item.group }}"
+    state: directory
+    recurse: yes
+  become: true
+  when: dest_file.stat.isdir
+- name: Set permissions at destination
+  file:
+    path: "{{ item.dest }}/{{ item.name }}"
+    owner: "{{ item.owner }}"
+    group: "{{ item.group }}"
+  become: true
+  when: not dest_file.stat.isdir