From dc3c795732e843fd82db4bf71fbbc3306c94b7ca Mon Sep 17 00:00:00 2001 From: Jakub Czajka Date: Wed, 27 Sep 2023 21:05:30 +0200 Subject: [PATCH] Add a script for downloading a file from Google Drive. --- download.yaml | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 download.yaml diff --git a/download.yaml b/download.yaml new file mode 100644 index 0000000..5d05cd4 --- /dev/null +++ b/download.yaml @@ -0,0 +1,63 @@ +# Copyright (c) 2023 Jakub Czajka +# 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 -- 2.39.5