Skip to content

Fix Networking #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,32 @@ cd src
docker build -t tinyfaas-mgmt .
```

Then start the container with:
Then start the management service container with:

```bash
docker run -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 --name tinyfaas-mgmt -d tinyfaas-mgmt tinyfaas-mgmt
docker run -v /var/run/docker.sock:/var/run/docker.sock -p 5000:8080 --name tinyfaas-mgmt -d tinyfaas-mgmt tinyfaas-mgmt
```

This ensures that the management service has access to Docker on the host and it will then expose port 8080 to accept incoming request.
This ensures that the management service has access to Docker on the host and it will then expose port 5000 to accept incoming request.
When starting the management service, it will first build and deploy the reverse proxy as a second Docker container.
Depending on the performance of your host, this can take between a few seconds and up to a minute (on a Raspberry Pi 3B+).

To deploy a function (e.g. the "Sieve of Erasthostenes"), run:

```bash
curl http://localhost:8080 --data '{"path": "sieve-of-erasthostenes", "resource": "/sieve/primes", "entry": "sieve.js", "threads": 4}' -v
curl http://localhost:5000 --data '{"path": "sieve-of-erasthostenes", "resource": "/sieve/primes", "entry": "sieve.js", "threads": 4}' -v
```

The reverse proxy will then expose this service on port 5683 (default CoAP port) as `coap://localhost:5683/sieve/primes`.
The reverse proxy will then expose this service on port 5683 (default CoAP port) as `coap://localhost:5683/sieve/primes`.
To change the default port, use the additional port parameter when running the tinyFaaS management service (e.g., to change the endpoint port to 7000):

```bash
docker run -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 --name tinyfaas-mgmt -d tinyfaas-mgmt tinyfaas-mgmt 7000
```

To stop and remove all containers on your system (including, **but not limited to**, containers managed by tinyFaaS), use:

```bash
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
```
27 changes: 19 additions & 8 deletions src/management-service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
endpoint_container = {}
function_handlers = {}

def create_endpoint(meta_container):
def create_endpoint(meta_container, port):
client = docker.from_env()
endpoint_image = client.images.build(path='./reverse-proxy/', rm=True)[0]

# remove old endpoint-net networks
for n in client.networks.list(names=['endpoint-net']):
n.remove()

endpoint_network = client.networks.create('endpoint-net', driver='bridge')

endpoint_container['container'] = client.containers.run(endpoint_image, network=endpoint_network.name, ports={'5683/udp': 5683}, detach=True)
endpoint_container['container'] = client.containers.run(endpoint_image, network=endpoint_network.name, ports={'5683/udp': port}, detach=True)
# getting IP address of the handler container by inspecting the network and converting CIDR to IPv4 address notation (very dirtily, removing the last 3 chars -> i.e. '/20', so let's hope we don't have a /8 subnet mask)
endpoint_container['ipaddr'] = docker.APIClient().inspect_network(endpoint_network.id)['Containers'][endpoint_container['container'].id]['IPv4Address'][:-3]

Expand Down Expand Up @@ -106,20 +110,27 @@ async def post(self):
raise

def main(args):
# read config data
# exactly one argument should be provided: meta_container
if len(args) != 2:
raise ValueError('Too many or too little arguments provided:\n' + json.dumps(args))

# default coap port is 5683
port = 5683

if len(args) == 3:
try:
port = int(args[2])
except ValueError:
raise ValueError('Could not parse port number:\n' + json.dumps(args) + '\nUsage: management-service.py [tinyfaas-mgmt container name] <endpoint port>')
elif len(args) != 2:
raise ValueError('Too many or too little arguments provided:\n' + json.dumps(args) + '\nUsage: management-service.py [tinyfaas-mgmt container name] <endpoint port>')

meta_container = args[1]

try:
docker.from_env().containers.get(meta_container)
except:
raise ValueError('Provided container name does not match a running container')
raise ValueError('Provided container name does not match a running container' + '\nUsage: management-service.py [tinyfaas-mgmt container name] <endpoint port>')

# create endpoint
create_endpoint(meta_container)
create_endpoint(meta_container, port)

# accept incoming configuration requests and create handlers based on that
app = tornado.web.Application([
Expand Down