-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
69 lines (58 loc) · 1.89 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
import click
import docker
import json
@click.group()
def cli():
pass
@cli.command()
@click.argument('metadata')
def run(metadata):
container = client.containers.list()[0]
ip_address = str(container.attrs['NetworkSettings']['IPAddress'])
ip_address = "127.0.0.1"
run_metadata = json.load(open(metadata))
files = {
"metadata.json": open(metadata, 'rb')
}
for input in run_metadata['inputs']:
typ = run_metadata['inputs'][input]['type']
location = run_metadata['inputs'][input]['location']
files[str(input)] = open(location, 'rb')
# debug
print(files)
print(container.attrs)
print(ip_address)
# send the request, get back a NetCDF
netCDF = requests.post('http://' + ip_address + ':4000/', files=files)
# printing the file that the client recieved back from the srever in the response message
print(netCDF.content)
@cli.command()
@click.argument('filepath')
@click.argument('tagname')
def build_server(filepath, tagname):
# Build the image first
print('Building http server image if not built already...\n')
client.images.build(path=filepath, tag=tagname)
#build(filepath, tagname)
print('Finished building the image...\n')
# Running the container now
#The port number, as an integer. For example, {'2222/tcp': 3333}
#will expose port 2222 inside the container as port 3333 on the host.
print('Now running the container...\n')
port = {'80/tcp':4000}
cont = client.containers.run('http-server', ports=port, tty=True, stdin_open=True, detach=True)
print(cont.logs().decode('utf-8'))
# command mostly for testing
# will stop all containers and prune them
@cli.command()
def clean():
for container in client.containers.list():
container.stop()
client.containers.prune()
def main():
global client
client = docker.from_env()
cli()
if __name__ == '__main__':
main()