[satlab]: Add cli to force update Satlab containers and system

Adding commands to satlab-remote-access

1 ./update_system: This will force update the Satlab DHB OS and reboot system
2 ./update_satalb: Updates the Satlab docker containers, no reboot required.


Change-Id: Iaac2d01d7f10989fa5e3fc1bb1eedaa86b9cc3cf
Bug:b:219549038
Test Manually ran these commands on Satlab
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/satlab/+/3490328
Commit-Queue: Prasad Vuppalapu <prasadv@chromium.org>
Tested-by: Prasad Vuppalapu <prasadv@chromium.org>
Reviewed-by: Anh Le <anhdle@chromium.org>
diff --git a/src/dockerfiles/satlab_remote_access/Dockerfile b/src/dockerfiles/satlab_remote_access/Dockerfile
index ea13388..bdb593b 100644
--- a/src/dockerfiles/satlab_remote_access/Dockerfile
+++ b/src/dockerfiles/satlab_remote_access/Dockerfile
@@ -20,6 +20,10 @@
 WORKDIR /
 RUN /sbin/apk add git
 
+COPY dockerfiles/satlab_remote_access/tools/host_command.py /usr/local/bin
+COPY dockerfiles/satlab_remote_access/tools/get_host_identifier /usr/local/bin
+COPY dockerfiles/satlab_remote_access/tools/update_system /usr/local/bin
+COPY dockerfiles/satlab_remote_access/tools/update_satlab /usr/local/bin
 COPY dockerfiles/satlab_remote_access/tools/satlab_setup /usr/local/bin
 COPY dockerfiles/satlab_remote_access/tools/get_dns_hosts /usr/local/bin
 COPY dockerfiles/satlab_remote_access/tools/force-update-cipd /usr/local/bin
@@ -56,7 +60,7 @@
 RUN  sed -i s/AllowTcpForwarding\ no/AllowTcpForwarding\ yes/ /etc/ssh/sshd_config
 
 # Change welcoming message.
-COPY dockerfiles/satlab_remote_access/motd /etc/
+COPY dockerfiles/satlab_remote_access/tools/motd /etc/
 RUN chmod 666 /etc/motd
 
 # Add jq to get service account key id from json file.
diff --git a/src/dockerfiles/satlab_remote_access/tools/get_host_identifier b/src/dockerfiles/satlab_remote_access/tools/get_host_identifier
new file mode 100644
index 0000000..bcec131
--- /dev/null
+++ b/src/dockerfiles/satlab_remote_access/tools/get_host_identifier
@@ -0,0 +1,13 @@
+#!/bin/sh
+# Copyright 2021 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# get_host_identifier gets the serial number for the satlab as a whole.
+#
+# sample output:
+#   1XXXXXXX1341333A
+
+set -eu
+
+exec python3 /usr/local/bin/host_command.py "$0"
diff --git a/src/dockerfiles/satlab_remote_access/tools/host_command.py b/src/dockerfiles/satlab_remote_access/tools/host_command.py
new file mode 100644
index 0000000..2e2d600
--- /dev/null
+++ b/src/dockerfiles/satlab_remote_access/tools/host_command.py
@@ -0,0 +1,85 @@
+# Copyright 2021 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import docker
+import logging
+import os
+import sys
+
+from moblab_common import host_connector
+
+DOCKER_REGISTRY = "gcr.io/chromeos-partner-moblab/"
+
+def reboot():
+    """Reboot the docker host box."""
+    host_connector.HostServicesConnector().reboot()
+
+
+def factory_reset():
+    """Run a factory reset on the docker host box."""
+    host_connector.HostServicesConnector.factory_reset()
+
+
+def get_host_identifier():
+    """Get the serial number for the satlab as a whole"""
+    print(host_connector.HostServicesConnector.get_host_identifier())
+
+
+def update_satlab():
+    """
+        Sets off an update of all docker containers, using watchtower
+
+    Returns: String result message.
+    """
+    client = docker.from_env(timeout=600)
+
+    try:
+        container = client.containers.get("update")
+        if container.status != "running":
+            container.remove(force=True)
+        else:
+            print("Already updating please wait.")
+            return
+    except docker.errors.NotFound:
+        pass
+
+    label = os.environ.get("WATCHTOWER_TAG", "release")
+    container = client.containers.run(
+        DOCKER_REGISTRY + "watchtower:%s" % label,
+        volumes=["/var/run/docker.sock:/var/run/docker.sock"],
+        name="update",
+        detach=True,
+        remove=True,
+        command="--run-once --include-restarting --include-stopped --revive-stopped -d",
+    )
+
+    try:
+        container.wait(timeout=60)
+    except requests.exceptions.ReadTimeout:
+        logging.exception("Watchtower update raised an exception.")
+        container.stop()
+    finally:
+        container.remove()
+    print("Update finished.")
+    return
+
+def update_system():
+    """
+        Initiates system update if it is available.
+    Returns:
+        An error message.
+    """
+    try:
+        response = (
+            host_connector.HostServicesConnector.install_system_update()
+        )
+        return response.error
+    except host_connector.HostServicesException:
+        logging.exception("Install system update raised an exception.")
+        return "Failed to install update."
+
+if __name__ == "__main__":
+    if len(sys.argv) > 1:
+        f = os.path.basename(sys.argv[1])
+        locals()[f]()
diff --git a/src/dockerfiles/satlab_remote_access/motd b/src/dockerfiles/satlab_remote_access/tools/motd
similarity index 100%
rename from src/dockerfiles/satlab_remote_access/motd
rename to src/dockerfiles/satlab_remote_access/tools/motd
diff --git a/src/dockerfiles/satlab_remote_access/tools/update_satlab b/src/dockerfiles/satlab_remote_access/tools/update_satlab
new file mode 100644
index 0000000..f48dfe7
--- /dev/null
+++ b/src/dockerfiles/satlab_remote_access/tools/update_satlab
@@ -0,0 +1,30 @@
+#!/bin/sh
+# Copyright 2022 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# update_satlab updates the satlab containers by running watchtower container once.
+
+set -eu
+
+cat << EOF
+###############################################################################
+IMPORTANT: Please read below information
+###############################################################################
+
+This operation will update and restart Satlab software, any tests currently in
+progress will be impacted.
+
+###############################################################################
+
+EOF
+while true; do
+    read -p "Do you want to continue with the Satlab update(y/n)?" yn
+    case $yn in
+        [Yy]* ) break;;
+        [Nn]* ) echo "Satlab update aborted!"; exit;;
+        * ) echo "Please answer yes or no.";;
+    esac
+done
+
+exec python3 /usr/local/bin/host_command.py "$0"
\ No newline at end of file
diff --git a/src/dockerfiles/satlab_remote_access/tools/update_system b/src/dockerfiles/satlab_remote_access/tools/update_system
new file mode 100644
index 0000000..ae4162f
--- /dev/null
+++ b/src/dockerfiles/satlab_remote_access/tools/update_system
@@ -0,0 +1,30 @@
+#!/bin/sh
+# Copyright 2022 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# update_system updates the ChromeOS image on the Satlab DHB.
+
+set -eu
+
+cat << EOF
+###############################################################################
+IMPORTANT: Please read below information
+###############################################################################
+
+This operation will update and reboot your Satlab Chromebox, any tests currently in
+progress will be impacted.
+
+###############################################################################
+
+EOF
+while true; do
+    read -p "Do you want to continue with the System update(y/n)?" yn
+    case $yn in
+        [Yy]* ) break;;
+        [Nn]* ) echo "System update aborted!"; exit;;
+        * ) echo "Please answer yes or no.";;
+    esac
+done
+
+exec python3 /usr/local/bin/host_command.py "$0"
\ No newline at end of file