archive_hwqual & call_autoserv: move to crostestutils

archive_hwqual deals with hardware/test functionality that would
be more at home in platform/crostestutils/. call_autoserv.py is
only used by archive_hwqual, and so will move with it.

This commit will not work on its own. It is a straight script move,
and so the scripts still assume they are in src/scripts.

BUG=chromium:875007
TEST=None
CQ-DEPEND=CL:1318140, CL:1318197, CL:1318472

Change-Id: Iaa203dd043ae332b939a1d17533430fee7aae507
Reviewed-on: https://chromium-review.googlesource.com/1318471
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Alex Klein <saklein@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/archive_hwqual b/archive_hwqual
new file mode 100755
index 0000000..d26b276
--- /dev/null
+++ b/archive_hwqual
@@ -0,0 +1,106 @@
+#!/bin/bash
+
+# Copyright (c) 2010 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.
+
+# Script to take an archived build result and prepare a hwqual release.
+
+SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
+. "${SCRIPT_ROOT}/common.sh" || exit 1
+
+DEFAULT_PRIVATE_KEY="${SCRIPTS_DIR}/mod_for_test_scripts/ssh_keys/testing_rsa"
+
+# Flags
+DEFINE_string from "" "Directory with autotest tarball"
+DEFINE_string to "" "Directory to receive packaged hwqual"
+DEFINE_string output_tag "chromeos-hwqual" "Name used in tar"
+DEFINE_string image_dir "" "Directory containing test image."
+DEFINE_string image "${CHROMEOS_TEST_IMAGE_NAME}" "Name of image file to use."
+DEFINE_string ssh_private_key "${DEFAULT_PRIVATE_KEY}" \
+  "Path to the private ssh key to use for testing"
+
+TMP=$(mktemp -d "/tmp/image.XXXX")
+
+cleanup() {
+  rm -rf "${TMP}"
+}
+
+main() {
+  assert_outside_chroot
+  assert_not_root_user
+
+  # Parse command line
+  FLAGS "$@" || exit 1
+  eval set -- "${FLAGS_ARGV}"
+  switch_to_strict_mode
+
+  if [[ -z "${FLAGS_from}" ]]; then
+    die "Please specify --from directory"
+  fi
+
+  if [[ -z "${FLAGS_image_dir}" ]]; then
+    die "Please specify --image_dir directory"
+  fi
+
+  FLAGS_from=$(readlink -f "${FLAGS_from}")
+
+  if [[ -z "${FLAGS_to}" ]]; then
+    FLAGS_to="${FLAGS_from}"
+  fi
+
+  local script_dir=${SCRIPTS_DIR}
+
+  script_dir=$(readlink -f "${script_dir}")
+
+  trap cleanup EXIT
+
+  cd "${TMP}"
+
+  echo "Extracting build artifacts..."
+  mkdir -p "tarball/${FLAGS_output_tag}"
+
+  if which pbzip2 >/dev/null 2>/dev/null; then
+    TAR_BZIP2="tar --use-compress-program=pbzip2"
+  else
+    TAR_BZIP2="tar --bzip2"
+  fi
+
+  echo "Extracting autotest from archived autotest tarball..."
+  ${TAR_BZIP2} -xf "${FLAGS_from}/autotest.tar.bz2"
+
+  cd "${TMP}"
+
+  echo "Copying image..."
+  cp "${FLAGS_image_dir}/${FLAGS_image}" \
+      "tarball/${FLAGS_output_tag}/chromeos-hwqual-usb.img"
+
+  echo "Inserting documentation and autotest to tarball..."
+  ln -s \
+    "${FLAGS_output_tag}/autotest/client/site_tests/suite_HWQual/README.txt" \
+    tarball
+  ln -s autotest/client/site_tests/suite_HWQual/manual \
+    "tarball/${FLAGS_output_tag}"
+  cp $(readlink -f "${FLAGS_ssh_private_key}") \
+    "tarball/${FLAGS_output_tag}/testing_rsa"
+  chmod 0400 "tarball/${FLAGS_output_tag}/testing_rsa"
+  mv autotest "tarball/${FLAGS_output_tag}"
+  # Copy call_autoserv.py to tarball.
+  cp "${script_dir}/call_autoserv.py" "tarball/${FLAGS_output_tag}/."
+  cp "${script_dir}/generate_test_report" \
+    "tarball/${FLAGS_output_tag}/generate_test_report"
+  # Copy python lib used in generate_test_report.
+  cp -a "${GCLIENT_ROOT}/chromite" "tarball/${FLAGS_output_tag}"
+  echo "Creating ${FLAGS_to}/${FLAGS_output_tag}.tar.bz2..."
+  mkdir -p "${FLAGS_to}"
+  cd tarball
+  ${TAR_BZIP2} -cf "${FLAGS_to}/${FLAGS_output_tag}.tar.bz2" .
+
+  trap - EXIT
+  cleanup
+
+  echo "Done."
+  cd "${script_dir}"
+}
+
+main "$@"
diff --git a/call_autoserv.py b/call_autoserv.py
new file mode 100755
index 0000000..65c00a5
--- /dev/null
+++ b/call_autoserv.py
@@ -0,0 +1,150 @@
+#!/usr/bin/python
+
+# Copyright (c) 2011 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.
+
+"""Script to run client or server tests on a live remote image.
+
+This script can be used to save results of each test run in timestamped
+unique results directory.
+
+"""
+
+import datetime
+import glob
+import logging
+import os
+import sys
+import time
+from optparse import OptionParser
+
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.INFO)
+conlog = logging.StreamHandler()
+conlog.setLevel(logging.INFO)
+formatter = logging.Formatter("%(asctime)s %(levelname)s | %(message)s")
+conlog.setFormatter(formatter)
+logger.addHandler(conlog)
+
+def ConnectRemoteMachine(machine_ip):
+  os.system("eval `ssh-agent -s`")
+  os.system("ssh-add testing_rsa")
+  username = os.environ['USER']
+
+  # Removing the machine IP entry from known hosts to avoid identity clash.
+  logger.info("Removing machine IP entry from known hosts to avoid identity"
+              " clash.")
+  host_list = open("/home/%s/.ssh/known_hosts" % username, "r").readlines()
+  index = 0
+  for host in host_list:
+    if machine_ip in host:
+      del host_list[index]
+      break
+    index += 1
+
+  open("/home/%s/.ssh/known_hosts" % username, "w").writelines(host_list)
+
+  # Starting ssh connection to remote test machine.
+  logger.info("Starting ssh connection to remote test machine.")
+  os.system("ssh root@%s true; echo $? > ssh_result_file" % machine_ip)
+  ssh_result = open("ssh_result_file", "r").read()
+  logger.info("Status of ssh connection to remote machine : %s" % ssh_result)
+
+  if ssh_result.strip() != '0':
+    logger.error("Ssh connection to remote test machine FAILED. Exiting the"
+                 " test.")
+    sys.exit()
+
+def TestSearch(suite_path, test_name):
+  test_path = ""
+  filenames = glob.glob(os.path.join(suite_path, test_name))
+  for filename in filenames:
+    if filename == ("%s/%s" % (suite_path, test_name)):
+      test_path = filename
+      break
+  return test_path
+
+def TriggerTest(test_name, machine_ip):
+  # Creating unique time stamped result folder name.
+  current_time = datetime.datetime.now()
+  result_name = "results." + test_name + current_time.strftime("_%d-%m-%y"
+                                                               "_%H:%M")
+
+  # Setting the test path location based on the test_name.
+  suite_path = "./autotest/client/site_tests/suite_HWQual"
+  test_path = TestSearch(suite_path, "control.%s" % test_name)
+
+  # Looking for test_name under client/site_tests if not under suite_HWQual.
+  if test_path == "":
+    suite_path = ("./autotest/client/site_tests/%s" % test_name)
+    test_path = TestSearch(suite_path, "control")
+
+  # Looking for test_name under server/site_tests if not present under client.
+  if test_path == "":
+    suite_path = ("./autotest/server/site_tests/%s" % test_name)
+    test_path = TestSearch(suite_path, "control")
+    # Looking for test_name under server/site_tests/suites.
+    if test_path == "":
+      suite_path = "./autotest/server/site_tests/suites"
+      test_path = TestSearch(suite_path, "control.%s" % test_name)
+    # Setting command for server tests.
+    run_command = ("./autotest/server/autoserv -r ./autotest/%s -m %s"
+                   " -s %s" % (result_name, machine_ip, test_path))
+  else:
+    run_command = ("./autotest/server/autoserv -r ./autotest/%s -m %s "
+                   "-c %s" % (result_name, machine_ip, test_path))
+
+  if test_path == "":
+    logger.error("Test not found under client or server directories! Check the "
+                 "name of test and do not prefix 'control.' to test name.")
+    sys.exit()
+
+  # Making the call to HWQual test.
+  logger.info("Starting the HWQual test : %s" % test_path)
+  os.system(run_command)
+
+  # Displaying results on test completion.
+  test_result = os.system("./generate_test_report ./autotest/%s" % result_name)
+
+  result_path = ("./autotest/%s" % result_name)
+  if test_result != 0:
+    # Grabbing the results directory as test failed & return value nonzero.
+    log_name = ("%s.tar.bz2" % result_path)
+    os.system("tar cjf %s %s" % (log_name, result_path))
+    logger.info("Logs for the failed test at : %s" % log_name)
+
+  logger.info("Results of test run at : %s" % result_path)
+
+def main(argv):
+  # Checking the arguments for help, machine ip and test name.
+  parser = OptionParser(usage="USAGE : ./%prog [options]")
+
+  parser.add_option("--ip", dest="dut_ip",
+                    help="accepts IP address of device under test <DUT>.")
+  parser.add_option("--test", dest="test_name",
+                    help="accepts HWQual test name without prefix 'control.'")
+
+  (options, args) = parser.parse_args()
+
+  # Checking for presence of both ip and test parameters.
+  if (options.dut_ip == None) or (options.test_name == None):
+    parser.error("Argument missing! Both --ip and --test arguments required.")
+
+  # Checking for blank values of both ip and test parameters.
+  arg_ip, arg_testname = options.dut_ip, options.test_name
+  if (arg_ip == "") or (arg_testname == ""):
+    parser.error("Blank values are not accepted for arguments.")
+
+  logger.info("HWQual test to trigger : %s" % arg_testname)
+  logger.info("Remote test machine IP : %s" % arg_ip)
+
+  # Setting up ssh connection to remote machine.
+  ConnectRemoteMachine(arg_ip)
+
+  # Triggerring the HWQual test and result handling.
+  TriggerTest(arg_testname, arg_ip)
+
+
+if __name__ == '__main__':
+  main(sys.argv)