|
1 | 1 | import asyncio
|
2 |
| -import os |
3 | 2 | import uuid
|
4 | 3 | import json
|
5 | 4 |
|
6 |
| -import sqlite3 |
7 | 5 | from typing import Optional, Any, Dict
|
8 | 6 |
|
9 | 7 | import dask
|
10 |
| -from dask.utils import tmpfile |
11 | 8 | from dask_cloudprovider.generic.vmcluster import (
|
12 | 9 | VMCluster,
|
13 | 10 | VMInterface,
|
@@ -628,9 +625,11 @@ def __init__(
|
628 | 625 | "gpu_instance": self.gpu_instance,
|
629 | 626 | "bootstrap": self.bootstrap,
|
630 | 627 | "auto_shutdown": self.auto_shutdown,
|
631 |
| - "preemptible": preemptible |
632 |
| - if preemptible is not None |
633 |
| - else self.config.get("preemptible"), |
| 628 | + "preemptible": ( |
| 629 | + preemptible |
| 630 | + if preemptible is not None |
| 631 | + else self.config.get("preemptible") |
| 632 | + ), |
634 | 633 | "instance_labels": instance_labels or self.config.get("instance_labels"),
|
635 | 634 | "service_account": service_account or self.config.get("service_account"),
|
636 | 635 | }
|
@@ -658,37 +657,30 @@ def __init__(self, service_account_credentials: Optional[dict[str, Any]] = None)
|
658 | 657 | self._compute = self.refresh_client()
|
659 | 658 |
|
660 | 659 | def refresh_client(self):
|
661 |
| - if os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", False): |
| 660 | + if self.service_account_credentials: |
662 | 661 | import google.oauth2.service_account # google-auth
|
663 | 662 |
|
664 |
| - creds = google.oauth2.service_account.Credentials.from_service_account_file( |
665 |
| - os.environ["GOOGLE_APPLICATION_CREDENTIALS"], |
666 |
| - scopes=["https://www.googleapis.com/auth/cloud-platform"], |
667 |
| - ) |
668 |
| - elif self.service_account_credentials: |
669 |
| - import google.oauth2.service_account # google-auth |
670 |
| - |
671 |
| - creds = google.oauth2.service_account.Credentials.from_service_account_info( |
672 |
| - self.service_account_credentials, |
673 |
| - scopes=["https://www.googleapis.com/auth/cloud-platform"], |
| 663 | + credentials = ( |
| 664 | + google.oauth2.service_account.Credentials.from_service_account_info( |
| 665 | + self.service_account_credentials, |
| 666 | + scopes=["https://www.googleapis.com/auth/cloud-platform"], |
| 667 | + ) |
674 | 668 | )
|
675 | 669 | else:
|
676 |
| - import google.auth.credentials # google-auth |
| 670 | + import google.auth |
677 | 671 |
|
678 |
| - path = os.path.join( |
679 |
| - os.path.expanduser("~"), ".config/gcloud/credentials.db" |
680 |
| - ) |
681 |
| - if not os.path.exists(path): |
682 |
| - raise GCPCredentialsError() |
683 |
| - conn = sqlite3.connect(path) |
684 |
| - creds_rows = conn.execute("select * from credentials").fetchall() |
685 |
| - with tmpfile() as f: |
686 |
| - with open(f, "w") as f_: |
687 |
| - # take first row |
688 |
| - f_.write(creds_rows[0][1]) |
689 |
| - creds, _ = google.auth.load_credentials_from_file(filename=f) |
| 672 | + # Obtain Application Default Credentials (ADC) |
| 673 | + try: |
| 674 | + credentials, _ = google.auth.default() |
| 675 | + except google.auth.exceptions.DefaultCredentialsError as e: |
| 676 | + raise GCPCredentialsError() from e |
| 677 | + |
| 678 | + # Use the credentials to build a service client |
690 | 679 | return googleapiclient.discovery.build(
|
691 |
| - "compute", "v1", credentials=creds, requestBuilder=build_request(creds) |
| 680 | + "compute", |
| 681 | + "v1", |
| 682 | + credentials=credentials, |
| 683 | + requestBuilder=build_request(credentials), |
692 | 684 | )
|
693 | 685 |
|
694 | 686 | def instances(self):
|
|
0 commit comments