Vagrant.configure("2") do |config|
config.vm.define "backend" do |backend|
backend.vm.box = "bento/ubuntu-16.04"
backend.vm.network :forwarded_port, guest: 80, host: 4568
backend.vm.provider "virtualbox" do |vb|
vb.memory = '512'
end
#backend.vm.network "private_network", type: "dhcp"
backend.vm.network "private_network", ip: '10.1.1.15'
backend.vm.provision "shell", path: "provisioners/install_python_nameko.sh"
end
config.vm.define "frontend" do |frontend|
frontend.vm.box = "bento/ubuntu-16.04"
frontend.vm.network "forwarded_port", guest: 80, host: 4567
frontend.vm.provider "virtualbox" do |vb|
vb.memory = '512'
end
#frontend.vm.network "private_network", type: "dhcp"
frontend.vm.network "private_network", ip: '10.1.1.30'
frontend.vm.provision "shell", path: "provisioners/install_python_nameko.sh"
#frontend.vm.provision "shell", path: "provisioners/install-node.sh"
end
end
import os
import urllib
from nameko.dependency_providers import Config
from nameko.web.handlers import http
from urllib.request import urlopen
import sys
import psutil
class HttpService:
name = "connect_to_client"
config = Config()
# Hello User!
@http('GET', '/')
def get_method0(self, request):
print("\n")
ip = self.config.get('IP')
print("\n")
print("IP: " + str(ip))
print ("Calling Method 1 with python version: ",sys.version_info[:])
print("\n")
sys.stdout.flush()
# Format output for client
msg='''
Hello User!
'''
return msg
# Trailing slash required to avoid 301 redirect which fails. Not resolved.
@http('GET', '/grab/<string:cmd>/')
def get_method1(self, request, cmd):
ip = config.get('IP')
print("\n")
print("IP: " + ip)
print("\n")
cip = "http://"+ip + "/" + cmd
print("\n")
print("IP: " + cip)
print("\n")
with urllib.request.urlopen(cip) as response:
html = response.read()
html = html.decode("utf8")
response.close()
return html
import os
import urllib
from nameko.web.handlers import http
from urllib.request import urlopen
import sys
import psutil
class HttpService:
name = "psutils_client"
# Print commands
@http('GET', '/')
def get_method1(self, request):
print("\n")
print ("Calling Method 1 with python version: ",sys.version_info[:])
print("\n")
cmds = '''
COMMANDS:
cpu_times
virtual_memory
swap_memory
net_if_addrs
'''
sys.stdout.flush()
return cmds
@http('GET', '/cpu_times')
def get_cpu_times(self, request):
result = str(psutil.cpu_times()) + '''
'''
return result
@http('GET', '/virtual_memory')
def get_virtual_memory(self, request):
result = str(psutil.virtual_memory()) + '''
'''
return result
@http('GET', '/swap_memory')
def get_swap_memory(self, request):
result = str(psutil.swap_memory()) + '''
'''
return result
@http('GET', '/net_if_addrs')
def get_net_if_addrs(self, request):
result = str(psutil.net_if_addrs()) + '''
'''
return result
I do not know what command should be for the file retrieval.
Create a simple file retrieval service that consists of two Microservices and a Client. The following depicts the architecture:
Client (Bash shell script) --> FrontEnd Microservice --> BackEnd Microservice
BackEnd Micorservice
The backend, BackEnd, is a simple Python program running in Nameko that has one method:
getFileContents(name-of-file) – return the contents of a file
In a subdirectory named “files”, which resides under the directory where BackEnd.py is, create 4 text files: a.txt, b.txt, c.txt, d.txt
Put different text into each file.
To get the contents of file a.txt, a caller calls getFileContents(“a.txt”), to get the contents of b.txt, the caller calls getFileContents(“b.txt”), etc.
The BackEnd microservice must run in a Vagrant-based Ubuntu 16.04 VM. So, besides writing the code for the microservice, you must also write the vagrant file and the provisioning code needed to install Python 3, Nameko, and any other dependencies that are needed.
FrontEnd Microservice
The frontend microservice, FrontEnd, is a simple Python-based HTTP object that receives an HTTP request to get a specific file – a.txt, b.txt, c.txt, d.txt. This Python module is integrated into Nameko to receive the HTTP requests.
You only have to handle requests for files that exist. When a request comes in, the FrontEnd code needs to parse the request and call getFileContents in the BackEnd microservice, to get the requested file
When the FrontEnd microservice gets the return from the BackEnd microservice, it returns the file contents (returned by getFileContents) within the HTTP response it sends back to the client.
The FrontEnd microservice must run in a Vagrant-based Ubuntu 16.04 VM. So, besides writing the code for the microservice, you must also write the vagrant file and the provisioning code needed to install Python 3, Nameko, and any other dependencies that are needed.
Client
The Client is a simple bash script that runs in the host machine. It uses wget or curl to get each file.