From c90538595f2aafb31c4cd0c9fa8e587be3e701b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20M=C3=BCller?= <daniel.q.mueller@stud.h-da.de>
Date: Wed, 24 Nov 2021 17:10:19 +0000
Subject: [PATCH] Add tensorflow installer

- Add task to check if tensorflow is installed alread
- Add script to download and install prebuilt wheel
---
 .../files/tensorflow-install.sh               | 50 +++++++++++++++++++
 roles/ml-python-packages/tasks/main.yml       | 18 +++++++
 2 files changed, 68 insertions(+)
 create mode 100644 roles/ml-python-packages/files/tensorflow-install.sh

diff --git a/roles/ml-python-packages/files/tensorflow-install.sh b/roles/ml-python-packages/files/tensorflow-install.sh
new file mode 100644
index 0000000..4158bb3
--- /dev/null
+++ b/roles/ml-python-packages/files/tensorflow-install.sh
@@ -0,0 +1,50 @@
+#!/bin/bash
+
+gdownload() {
+  URL=$1
+  OUTFILE=$2
+  COOKIES="/tmp/${RANDOM}.cookie"
+
+  if [ -z "$URL" -o -z "$OUTFILE" ]; then
+    return 1
+  fi
+
+  # Get cookie with skip verification code
+  curl -sc "$COOKIES" "$URL" &> /dev/null
+
+  # Extract skip verification code from cookie
+  CODE="$(awk '/_warning_/ {print $NF}' $COOKIES)"
+
+  echo "Code: $CODE"
+
+  # Download the actual file
+  echo "Using link: " "$URL&confirm=$CODE"
+  curl -Lb "$COOKIES" "$URL&confirm=$CODE" -o "$OUTFILE"
+
+  # Cleanup cookie file
+  rm "$COOKIES"
+
+  return $?
+}
+
+install() {
+  DLFILE="/tmp/tensorflow-2.7.0-cp39-none-linux_aarch64.whl"
+
+  # Try to download the wheel file from google drive
+  if gdownload "https://drive.google.com/uc?export=download&id=1D3R7bzkuFdY_gGTkSmnF9IbRNlj9wssB" "$DLFILE"; then
+    # Install the downloaded wheel
+    pip3 install "$DLFILE"
+
+    # Save the pip3 install return code (0 => success)
+    PIPRC="$?"
+
+    # Cleanup the downloaded wheel
+    rm "$DLFILE"
+
+    return $PIPRC
+  fi
+
+  return 1
+}
+
+install
diff --git a/roles/ml-python-packages/tasks/main.yml b/roles/ml-python-packages/tasks/main.yml
index ccfc690..124ed9b 100644
--- a/roles/ml-python-packages/tasks/main.yml
+++ b/roles/ml-python-packages/tasks/main.yml
@@ -20,3 +20,21 @@
     virtualenv_command: /usr/bin/python3 -m venv
     requirements: /tmp/ml-requirements.txt
     virtualenv: "/home/{{ target_user }}/.ml-venv"
+
+
+
+- name: Check if tensorflow is installed
+  shell: ". /home/{{ target_user }}/.ml-venv/bin/activate && pip3 freeze | grep -q tensorflow==2.7"
+  register: tfinstalled
+  changed_when: "tfinstalled.rc != 0"
+  failed_when: false
+
+- name: Copy tf install script to remote fs
+  copy:
+    src: files/tensorflow-install.sh
+    dest: /tmp/tensorflow-install.sh
+  when: "tfinstalled.rc != 0"
+
+- name: Download and install prebuilt tensorflow wheel
+  shell: ". /home/{{ target_user }}/.ml-venv/bin/activate && sh /tmp/tensorflow-install.sh"
+  when: "tfinstalled.rc != 0"
-- 
GitLab