1
+ import logging
2
+ import os
3
+ from pathlib import Path
4
+
5
+ from distributed import WorkerPlugin
6
+
7
+ logger = logging .getLogger (__name__ )
8
+
9
+ class UploadAWSCredentials (WorkerPlugin ):
10
+ # """Automatically upload a GCP key to the worker."""
11
+
12
+ name = "upload_aws_credentials"
13
+
14
+ def __init__ (self ):
15
+ """
16
+ Initialize the plugin by reading in the data from the given file.
17
+ """
18
+ config_path = os .getenv ("AWS_CONFIG_FILE" , Path .home () / Path (".aws/config" ))
19
+ credentials_path = os .getenv (
20
+ "AWS_SHARED_CREDENTIALS_FILE" , Path .home () / Path (".aws/credentials" )
21
+ )
22
+ config_path , credentials_path = Path (config_path ), Path (credentials_path )
23
+
24
+ if not config_path .exists ():
25
+ raise ValueError (
26
+ f"Config file { config_path } does not exist. If you store AWS config "
27
+ "in a different location, please set AWS_CONFIG_FILE environment variable."
28
+ )
29
+
30
+ if not credentials_path .exists ():
31
+ raise ValueError (
32
+ f"Credentials file { credentials_path } does not exist. If you store AWS credentials "
33
+ "in a different location, please set AWS_SHARED_CREDENTIALS_FILE environment variable."
34
+ )
35
+
36
+ self .config_filename = config_path .name
37
+ self .credentials_filename = credentials_path .name
38
+
39
+ with open (config_path , "rb" ) as f :
40
+ self .config = f .read ()
41
+ with open (credentials_path , "rb" ) as f :
42
+ self .credentials = f .read ()
43
+
44
+ async def setup (self , worker ):
45
+ await worker .upload_file (
46
+ filename = self .config_filename , data = self .config , load = False
47
+ )
48
+ worker_config_path = Path (worker .local_directory ) / self .config_filename
49
+ os .environ ["AWS_CONFIG_FILE" ] = str (worker_config_path )
50
+
51
+ await worker .upload_file (
52
+ filename = self .credentials_filename , data = self .credentials , load = False
53
+ )
54
+ worker_credentials_path = (
55
+ Path (worker .local_directory ) / self .credentials_filename
56
+ )
57
+ os .environ ["AWS_SHARED_CREDENTIALS_FILE" ] = str (worker_credentials_path )
0 commit comments