Version promtail version

This adds the promtail version used in the setup to a file and adds
an installation step downloading promtail, if the installation is not
run in `dryrun`-mode.

Change-Id: I1127220a57b2610b5c4458ce2205077706a860e6
diff --git a/Pipfile b/Pipfile
index 3568467..a313565 100644
--- a/Pipfile
+++ b/Pipfile
@@ -12,6 +12,7 @@
 pyyaml = "~=5.3"
 passlib = "~=1.7.2"
 python-gnupg = "~=0.4.5"
+requests = "~=2.23.0"
 
 [requires]
 python_version = "3.8"
diff --git a/Pipfile.lock b/Pipfile.lock
index b00c5cd..1fbfb22 100644
--- a/Pipfile.lock
+++ b/Pipfile.lock
@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "ac147eba23c329eeb6cf858f428692964ba3951ce309c64c121fa504b9198abc"
+            "sha256": "04289147b549a22aa59a430acffa91aec1ce904346f398e8e01093962f562cb7"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -16,6 +16,27 @@
         ]
     },
     "default": {
+        "certifi": {
+            "hashes": [
+                "sha256:1d987a998c75633c40847cc966fcf5904906c920a7f17ef374f5aa4282abd304",
+                "sha256:51fcb31174be6e6664c5f69e3e1691a2d72a1a12e90f872cbdb1567eb47b6519"
+            ],
+            "version": "==2020.4.5.1"
+        },
+        "chardet": {
+            "hashes": [
+                "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
+                "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
+            ],
+            "version": "==3.0.4"
+        },
+        "idna": {
+            "hashes": [
+                "sha256:7588d1c14ae4c77d74036e8c22ff447b26d0fde8f007354fd48a7814db15b7cb",
+                "sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa"
+            ],
+            "version": "==2.9"
+        },
         "passlib": {
             "hashes": [
                 "sha256:68c35c98a7968850e17f1b6892720764cc7eed0ef2b7cb3116a89a28e43fe177",
@@ -48,6 +69,21 @@
             ],
             "index": "pypi",
             "version": "==5.3.1"
+        },
+        "requests": {
+            "hashes": [
+                "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee",
+                "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6"
+            ],
+            "index": "pypi",
+            "version": "==2.23.0"
+        },
+        "urllib3": {
+            "hashes": [
+                "sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc",
+                "sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc"
+            ],
+            "version": "==1.25.8"
         }
     },
     "develop": {
diff --git a/promtail/VERSION b/promtail/VERSION
new file mode 100644
index 0000000..f0bb29e
--- /dev/null
+++ b/promtail/VERSION
@@ -0,0 +1 @@
+1.3.0
diff --git a/subcommands/install.py b/subcommands/install.py
index a03bdfb..d26f6b6 100644
--- a/subcommands/install.py
+++ b/subcommands/install.py
@@ -13,7 +13,11 @@
 # limitations under the License.
 
 import os.path
+import stat
 import subprocess
+import zipfile
+
+import requests
 import yaml
 
 from ._globals import HELM_CHARTS
@@ -93,6 +97,33 @@
     os.remove(os.path.join(output_dir, "promtail.yaml"))
 
 
+def _download_promtail(output_dir):
+    with open(os.path.abspath("./promtail/VERSION"), "r") as f:
+        promtail_version = f.readlines()[0].strip()
+
+    output_dir = os.path.join(output_dir, "promtail")
+    output_zip = os.path.join(output_dir, "promtail.zip")
+
+    response = requests.get(
+        "https://github.com/grafana/loki/releases/download/v%s/promtail-linux-amd64.zip"
+        % promtail_version,
+        stream=True,
+    )
+    with open(output_zip, "wb") as f:
+        for chunk in response.iter_content(chunk_size=512):
+            f.write(chunk)
+
+    with zipfile.ZipFile(output_zip) as f:
+        f.extractall(output_dir)
+
+    promtail_exe = os.path.join(output_dir, "promtail-linux-amd64")
+    os.chmod(
+        promtail_exe,
+        os.stat(promtail_exe).st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH,
+    )
+    os.remove(output_zip)
+
+
 def _run_ytt(config, output_dir):
     config_string = "#@data/values\n---\n"
     config_string += yaml.dump(config)
@@ -193,6 +224,7 @@
     _create_promtail_configs(output_dir)
 
     if not dryrun:
+        _download_promtail(output_dir)
         if update_repo:
             _update_helm_repos()
         _deploy_loose_resources(output_dir)