autotest: close tunnel when using atest creating host.

This CL calls cleanup stuff after creating a host with atest.

Also fixes pylint warnings and errors.

BUG=chromium:659799
TEST=Ran atest create host locally and verified tunnel is closed.
Close a host twice in atest create & Schedule a job on a host with local
autotest to verify this CL.

Change-Id: Ieb1e90b83c338d9ae848bbe9b1cf30bd854612cf
Reviewed-on: https://chromium-review.googlesource.com/418165
Commit-Ready: Xixuan Wu <xixuan@chromium.org>
Tested-by: Xixuan Wu <xixuan@chromium.org>
Reviewed-by: danny chan <dchan@chromium.org>
Reviewed-by: Richard Barnette <jrbarnette@chromium.org>
diff --git a/cli/host.py b/cli/host.py
index 1ddceb9..9374705 100644
--- a/cli/host.py
+++ b/cli/host.py
@@ -154,6 +154,7 @@
 
 
     def execute(self):
+        """Execute 'atest host list'."""
         filters = {}
         check_results = {}
         if self.hosts:
@@ -194,6 +195,10 @@
 
 
     def output(self, results):
+        """Print output of 'atest host list'.
+
+        @param results: the results to be printed.
+        """
         if results:
             # Remove the platform from the labels.
             for result in results:
@@ -213,6 +218,7 @@
     usage_action = 'stat'
 
     def execute(self):
+        """Execute 'atest host stat'."""
         results = []
         # Convert wildcards into real host stats.
         existing_hosts = []
@@ -243,6 +249,10 @@
 
 
     def output(self, results):
+        """Print output of 'atest host stat'.
+
+        @param results: the results to be printed.
+        """
         for stats, acls, labels, attributes in results:
             print '-'*5
             self.print_fields(stats,
@@ -275,6 +285,7 @@
 
 
     def execute(self):
+        """Execute 'atest host jobs'."""
         results = []
         real_hosts = []
         for host in self.hosts:
@@ -305,6 +316,10 @@
 
 
     def output(self, results):
+        """Print output of 'atest host jobs'.
+
+        @param results: the results to be printed.
+        """
         for host, jobs in results:
             print '-'*5
             print 'Hostname: %s' % host
@@ -314,6 +329,7 @@
                                                 'status'])
 
 class BaseHostModCreate(host):
+    """The base class for host_mod and host_create"""
     # Matches one attribute=value pair
     attribute_regex = r'(?P<attribute>\w+)=(?P<value>.+)?'
 
@@ -389,8 +405,9 @@
                     raise topic_common.CliError('Attribute must be in key=value '
                                                 'syntax.')
                 elif m.group('attribute') in self.attributes:
-                    raise topic_common.CliError('Multiple values provided for '
-                                                'attribute %s.' % attribute)
+                    raise topic_common.CliError(
+                            'Multiple values provided for attribute '
+                            '%s.' % m.group('attribute'))
                 self.attributes[m.group('attribute')] = m.group('value')
 
         self.platform = options.platform
@@ -511,6 +528,7 @@
 
 
     def execute(self):
+        """Execute 'atest host mod'."""
         successes = []
         for host in self.execute_rpc('get_hosts', hostname__in=self.hosts):
             self.host_ids[host['hostname']] = host['id']
@@ -548,6 +566,10 @@
 
 
     def output(self, hosts):
+        """Print output of 'atest host mod'.
+
+        @param hosts: the host list to be printed.
+        """
         for msg in self.messages:
             self.print_wrapped(msg, hosts)
 
@@ -643,8 +665,13 @@
                     adb_serial = self.attributes.get('serials')
                     host_dut = hosts.create_host(machine,
                                                  adb_serial=adb_serial)
+
                 host_info = HostInfo(host, host_dut.get_platform(),
                                      host_dut.get_labels())
+                # Clean host to make sure nothing left after calling it,
+                # e.g. tunnels.
+                if hasattr(host_dut, 'close'):
+                    host_dut.close()
             else:
                 # Can't ping the host, use default information.
                 host_info = HostInfo(host, None, [])
@@ -685,6 +712,7 @@
 
 
     def execute(self):
+        """Execute 'atest host create'."""
         successful_hosts = []
         for host in self.hosts:
             try:
@@ -704,6 +732,10 @@
 
 
     def output(self, hosts):
+        """Print output of 'atest host create'.
+
+        @param hosts: the added host list to be printed.
+        """
         self.print_wrapped('Added host', hosts)