Repository: DataBiosphere/toil

Issue:
There is no zone environment variable for GCE or Azure
AWS has the option to export TOIL_AWS_ZONE. There should be one for GCE and Azure as well.

┆Issue is synchronized with this [Jira Story](https://ucsc-cgl.atlassian.net/browse/TOIL-69)
┆Issue Number: TOIL-69



Discussion:
GCE has the zone in its json file.

Resolving patch:
diff --git a/src/toil/provisioners/azure/__init__.py b/src/toil/provisioners/azure/__init__.py
--- a/src/toil/provisioners/azure/__init__.py
+++ b/src/toil/provisioners/azure/__init__.py
@@ -0,0 +1,27 @@
+# Copyright (C) 2015-2016 Regents of the University of California
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
+
+def getAzureZone(defaultZone=None):
+    """
+    Find an appropriate azure zone.
+
+    Look for an environment variable or return a default as provided.
+
+    :param defaultZone: The zone specified in the leader metadata.
+    :return zone:  The zone.
+    """
+
+    return os.environ.get('TOIL_AZURE_ZONE') or defaultZone
diff --git a/src/toil/provisioners/azure/azureProvisioner.py b/src/toil/provisioners/azure/azureProvisioner.py
--- a/src/toil/provisioners/azure/azureProvisioner.py
+++ b/src/toil/provisioners/azure/azureProvisioner.py
@@ -30,6 +30,7 @@
 from azure.common.credentials import ServicePrincipalCredentials
 from azure.mgmt.compute import ComputeManagementClient
 from azure.mgmt.network import NetworkManagementClient
+from toil.provisioners.azure import getAzureZone
 
 
 logger = logging.getLogger(__name__)
@@ -76,6 +77,7 @@ def __init__(self, clusterName, zone, nodeStorage):
         self._azureComputeClient = ComputeManagementClient(credentials, subscription)
         self._azureNetworkClient = NetworkManagementClient(credentials, subscription)
 
+        self._zone = getAzureZone() or zone
         self._onLeader = False
         if not clusterName:
             # If no clusterName, Toil must be running on the leader.
@@ -97,7 +99,7 @@ def _readClusterSettings(self):
         metadata = json.loads(dataStr)
 
         # set values from the leader meta-data
-        self._zone = metadata['compute']['location']
+        self._zone = self._zone or getAzureZone(metadata['compute']['location'])
         self.clusterName = metadata['compute']['resourceGroupName']
         tagsStr = metadata['compute']['tags']
         tags = dict(item.split(":") for item in tagsStr.split(";"))


Test patch:
diff --git a/src/toil/test/provisioners/azureProvisionerTest.py b/src/toil/test/provisioners/azureProvisionerTest.py
--- a/src/toil/test/provisioners/azureProvisionerTest.py
+++ b/src/toil/test/provisioners/azureProvisionerTest.py
@@ -154,7 +154,6 @@ def _test(self, preemptableJobs=False):
         # TODO: Add a check to make sure everything is cleaned up.
 
 
-
 @pytest.mark.timeout(3000)
 class AzureAutoscaleTest(AbstractAzureAutoscaleTest):