1
+ import pytest
2
+ import docker
3
+ import atexit
4
+ import time
5
+ import boto3
6
+ import os
7
+ import requests
8
+
9
+ AWS_S3_DEFAULT_BUCKET = "test"
10
+ DOCKER_GW_IP = "172.17.0.1" # will override below if found
11
+
12
+ myContainers = list ()
13
+ dockerClient = docker .DockerClient (base_url = 'unix://var/run/docker.sock' , version = "auto" )
14
+ for network in dockerClient .networks .list ():
15
+ if (network .attrs ["Scope" ] == "local" and network .attrs ["Driver" ] == "bridge" ):
16
+ DOCKER_GW_IP = network .attrs ["IPAM" ]["Config" ][0 ]["Gateway" ]
17
+ break
18
+
19
+ i = 0
20
+
21
+ def startContainer (image , command = None , ** kwargs ):
22
+ container = dockerClient .containers .run (
23
+ image ,
24
+ command ,
25
+ auto_remove = True ,
26
+ detach = True ,
27
+ ** kwargs ,
28
+ )
29
+ myContainers .append (container )
30
+
31
+ while container .status != "running" :
32
+ time .sleep (1 )
33
+ container .reload ()
34
+ print (container .status )
35
+ return container
36
+
37
+ _minioCache = None
38
+ def getMinio ():
39
+ global _minioCache
40
+ if _minioCache :
41
+ return _minioCache
42
+
43
+ container = startContainer (
44
+ "minio/minio" ,
45
+ "server /data --console-address :9011" ,
46
+ ports = {9000 :9010 ,9011 :9011 },
47
+ )
48
+
49
+ endpoint_url = f"http://{ DOCKER_GW_IP } :9010"
50
+
51
+ while True :
52
+ time .sleep (1 )
53
+ response = None
54
+ try :
55
+ print (endpoint_url + "/minio/health/live" )
56
+ response = requests .get (endpoint_url + "/minio/health/live" )
57
+ except Exception as error :
58
+ print (error )
59
+
60
+ if response and response .status_code == 200 :
61
+ break
62
+
63
+ s3 = boto3 .client (
64
+ 's3' ,
65
+ endpoint_url = endpoint_url ,
66
+ config = boto3 .session .Config (signature_version = 's3v4' ),
67
+ aws_access_key_id = 'minioadmin' ,
68
+ aws_secret_access_key = 'minioadmin' ,
69
+ aws_session_token = None ,
70
+ # verify=False,
71
+ )
72
+
73
+ s3 .create_bucket (Bucket = AWS_S3_DEFAULT_BUCKET )
74
+
75
+ result = {
76
+ "container" : container ,
77
+ "endpoint_url" : endpoint_url ,
78
+ "s3" : s3 ,
79
+ }
80
+ _minioCache = result
81
+ return result
82
+
83
+ _ddaCache = None
84
+ def getDDA (minio = None ):
85
+ global _ddaCache
86
+ if _ddaCache :
87
+ return _ddaCache
88
+
89
+ PORT = 8010
90
+
91
+ environment = {
92
+ "HF_AUTH_TOKEN" : os .getenv ("HF_AUTH_TOKEN" ),
93
+ "http_proxy" : os .getenv ("DDA_http_proxy" ),
94
+ "https_proxy" : os .getenv ("DDA_https_proxy" ),
95
+ "REQUESTS_CA_BUNDLE" : os .getenv ("DDA_http_proxy" ) and "/usr/local/share/ca-certificates/squid-self-signed.crt"
96
+ }
97
+
98
+ if minio :
99
+ environment .update ({
100
+ "AWS_ACCESS_KEY_ID" : "minioadmin" ,
101
+ "AWS_SECRET_ACCESS_KEY" : "minioadmin" ,
102
+ "AWS_DEFAULT_REGION" : "" ,
103
+ "AWS_S3_DEFAULT_BUCKET" : "test" ,
104
+ "AWS_S3_ENDPOINT_URL" : minio ["endpoint_url" ],
105
+ })
106
+
107
+ container = startContainer (
108
+ "gadicc/diffusers-api" ,
109
+ ports = {8000 :PORT },
110
+ device_requests = [
111
+ docker .types .DeviceRequest (count = - 1 , capabilities = [['gpu' ]])
112
+ ],
113
+ environment = environment ,
114
+ )
115
+
116
+ url = f"http://{ DOCKER_GW_IP } :{ PORT } /"
117
+
118
+ while True :
119
+ time .sleep (1 )
120
+ response = None
121
+ try :
122
+ # print(url + "healthcheck")
123
+ response = requests .get (url + "healthcheck" )
124
+ except Exception as error :
125
+ # print(error)
126
+ continue
127
+
128
+ if response :
129
+ if response .status_code == 200 :
130
+ result = response .json ()
131
+ if (result ["state" ] == "healthy" and result ["gpu" ] == True ):
132
+ print ("Ready" )
133
+ break
134
+ else :
135
+ print (response )
136
+ print (response .text )
137
+ else :
138
+ raise Exception ("Unexpected status code from dda/healthcheck" )
139
+
140
+ data = {
141
+ "container" : container ,
142
+ "minio" : minio ,
143
+ "url" : url ,
144
+ }
145
+
146
+ _ddaCache = data
147
+ return data
148
+
149
+ def cleanup ():
150
+ print ("cleanup" )
151
+ for container in myContainers :
152
+ print ("Stopping" )
153
+ print (container )
154
+ container .stop ()
155
+
156
+ atexit .register (cleanup )
0 commit comments