Revert "stateful_update: Deprecate/Delete"

This reverts commit 48c2e8a369c07d0b4ee1eaa2eba1e4ae13ee0123.

Reason for revert: autoupdater in Autotest is suspected to still need this, and this may be causing increased provision failures.  Revert since it's just removing an otherwise presumed unused file.

Original change's description:
> stateful_update: Deprecate/Delete
> 
> stateful_update has been chromitized. We don't need this script anymore.
> 
> BUG=chromium:1026933
> TEST=CQ passes
> 
> Cq-Depend: chromium:2054077
> Change-Id: Ibb707672a3d421bbc779c042afa9ddb143ef2efc
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2054755
> Tested-by: Amin Hassani <ahassani@chromium.org>
> Commit-Queue: Amin Hassani <ahassani@chromium.org>
> Reviewed-by: Mike Frysinger <vapier@chromium.org>

Bug: chromium:1026933
Change-Id: If45b6812d35b4cd8f67ee9451013269cbc260d62
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2133291
Reviewed-by: Allen Li <ayatane@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
Commit-Queue: Allen Li <ayatane@chromium.org>
Tested-by: Allen Li <ayatane@chromium.org>
diff --git a/Makefile b/Makefile
index 3cbc559..99c3309 100644
--- a/Makefile
+++ b/Makefile
@@ -22,12 +22,19 @@
 		setup_chromite.py \
 		"${DESTDIR}/usr/lib/devserver"
 
+	install -m 0755 stateful_update "${DESTDIR}/usr/bin"
+
 	# The content in /var/lib is only meaningful for installation in
 	# a chroot, not for Moblab.
 	mkdir -m0777 -p "${DESTDIR}/var/lib/devserver"
 	mkdir -m0777 -p "${DESTDIR}/var/lib/devserver/static"
 	mkdir -m0777 -p "${DESTDIR}/var/lib/devserver/static/cache"
 
+	# The dut-scripts content is only used when installed on Moblab.
+	# Mode 0644 for these files because they're for serving to DUTs
+	# over HTTP, not for local execution.
+	install -m 0644 stateful_update \
+		"${DESTDIR}/usr/lib/devserver/dut-scripts"
 	install -m 0644 quick-provision/quick-provision \
 		"${DESTDIR}/usr/lib/devserver/dut-scripts"
 
diff --git a/stateful_update b/stateful_update
new file mode 100755
index 0000000..a705402
--- /dev/null
+++ b/stateful_update
@@ -0,0 +1,152 @@
+#!/bin/sh
+
+# Copyright (c) 2012 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.
+
+# This scripts performs update of stateful partition directories useful for
+# dev_mode.
+
+# in order to support developers going from older images with the old shflags
+# we'll check for both new shflags and old shflags
+if [ -f /usr/share/misc/shflags ] ; then
+  . /usr/share/misc/shflags
+elif [ -f /usr/lib/shflags ] ; then
+  . /usr/lib/shflags
+else
+  echo >&2 "$0 Unable to source shflags"
+  exit 1
+fi
+
+# Constants for states.
+CLEAN_STATE="clean"
+OLD_STATE="old"
+RESET_STATE="reset"
+
+LSB_RELEASE="/etc/lsb-release"
+STATEFUL_DIR="/mnt/stateful_partition"
+UPDATE_STATE_FILE=".update_available"
+
+DEFINE_string stateful_change "${OLD_STATE}" \
+  "The state of the new stateful partition - used in update testing."
+
+FLAGS "$@" || exit 1
+
+# Die on error.
+set -e
+
+remove_quotes() {
+  echo "$1" | sed -e "s/^'//; s/'$//"
+}
+
+download_stateful_payload(){
+  # Download and unzip directories onto the stateful partition.
+  eval "curl --silent --max-time 300 \"${stateful_update_url}\"" |
+      tar --ignore-command-error --overwrite --directory=${STATEFUL_DIR} -xz
+}
+
+update_dev_image () {
+  local base_update_url devserver_url
+  local local_payload_path
+  local path=$(remove_quotes "${FLAGS_ARGV}")
+
+  if [ -z "${path}" ]; then
+    if [ -f "${STATEFUL_DIR}${LSB_RELEASE}" ]; then
+      devserver_url=$(grep CHROMEOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} |
+          cut -f 2 -d '=')
+    fi
+    if [ -z "${devserver_url}" ]; then
+      devserver_url=$(grep CHROMEOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=')
+    fi
+    # Sanity check.
+    if [ -z "${devserver_url}" ]; then
+      echo >&2 "No CHROMEOS_DEVSERVER URL found in lsb-release file."
+      exit 1
+    fi
+    # Devserver URL should never contain "/update"
+    devserver_url=$(echo ${devserver_url} | sed -e 's#/update##')
+    base_update_url="${devserver_url}/static"
+  # Determine whether the path is local.
+  elif [ -f "${path}" ] || [ "${path}" = "-" ]; then
+    local_payload_path=${path}
+  else
+    base_update_url=${path}
+  fi
+
+  if [ -n "${base_update_url}" ]; then
+    local stateful_update_url="${base_update_url}/stateful.tgz"
+    local download_exit_code=0
+    for i in `seq 1 2`; do
+      if [ $i -eq 1 ]; then
+        echo >&2 "Downloading stateful payload from ${stateful_update_url}"
+      else
+        echo >&2 "Downloading failed, retrying."
+      fi
+      if download_stateful_payload; then
+        download_exit_code=$?
+        echo >&2 "Downloading command returns code ${download_exit_code}."
+        break
+      else
+        download_exit_code=$?
+        echo >&2 "Downloading command returns code ${download_exit_code}."
+      fi
+    done
+    if [ ${download_exit_code} -ne 0 ]; then
+      return ${download_exit_code}
+    fi
+    echo >&2 "Successfully downloaded update."
+  else
+    echo >&2 "Reading local payload ${local_payload_path}"
+    # Set timeout to four minutes to avoid waiting on stdin indefinitely.
+    timeout 240s tar --ignore-command-error --overwrite \
+        --directory=${STATEFUL_DIR} -xzf ${local_payload_path}
+    exit_code=$?
+    if [ "${exit_code}" -ne 0 ]; then
+      echo >&2 "Failed to unzip and write the stateful update."
+      return "${exit_code}"
+    fi
+    echo >&2 "Successfully retrieved update."
+  fi
+
+  if [ ! -d "${STATEFUL_DIR}/var_new" ] ||
+      [ ! -d "${STATEFUL_DIR}/dev_image_new" ]; then
+    echo >&2 "Missing var or dev_image in stateful payload."
+    return 1
+  fi
+}
+
+reset_state () {
+  echo >&2 "Resetting stateful update state."
+  rm -f "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
+  rm -rf "${STATEFUL_DIR}/var_new"
+  rm -rf "${STATEFUL_DIR}/dev_image_new"
+}
+
+update_old_state () {
+  echo >&2 "Performing standard stateful update."
+  echo -n "" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
+}
+
+update_clean_state () {
+  echo >&2 "Restoring state to factory_install with dev_image."
+  echo -n "clobber" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
+}
+
+main () {
+  if [ "${FLAGS_stateful_change}" = "${RESET_STATE}" ]; then
+    reset_state
+  elif update_dev_image; then
+    if [ "${FLAGS_stateful_change}" = "${OLD_STATE}" ]; then
+      update_old_state
+    elif [ "${FLAGS_stateful_change}" = "${CLEAN_STATE}" ]; then
+      update_clean_state
+    else
+      echo >&2 "Invalid state given to stateful update.  Aborting..."
+      return 1
+    fi
+  else
+    return 1
+  fi
+}
+
+main $@