Since its inception, Python has curved a niche for itself by making software development process easy for developers. Initially, it was not used much and its usage was limited to writing scripts to automate the processes and in prototyping different applications that will be implemented in different languages later on.
However, the scenario is completely different now. Currently, Python is one of the most favorite languages among software developers, Data scientists and Devops Engineers. It is the key contributor to machine learning intelligence and big data analytics now.
Why I am learning and using Python
I have been a full-time developer for one year now and have mostly been doing Native android development . However, I have also been writing RESTful APIs and doing some Cloud work using Amazon Web Services(AWS) EC2. Mostly, EC2 instances run on Linux and yes, you guessed that right; I have created EC2 instances that run on Ubuntu. Python has assisted me to automate some repetitive tasks during deployment of the API such as database creation, creation of directories, copying of files and directories and much more.I did not know any Python before even writing these automation scripts. I found Python easy to learn and very quick to pickup, provided you have the basic programming knowledge. So I chose a very awesome Python framework called Django to do the job.
Single-Tenant Application Architecture
I had not discovered Multi-tenant application architecture design, so I used a single-tenant application architecture design. The picture below highlights the difference between these two application architectures.As you can see above, a single-tenant application architecture design can be very tedious and hectic when setting up a new tenant. Imagine you have 100 different clients (tenants) who each need to access your App. You will have to set up 100 application instances, and 100 databases. Problem!!! Headache!! What if you have already set up the clients (tenants) and have an update for the app, maybe you added a new feature and you want all your clients (tenants) to use the new feature. Nightmare!!
My solution using Python and Django
Each time you setup a new client (tenant), you will need to create a new app and copy all the files for your app inside there. You will also have to edit the database configuration files inside the new app instance. You also need to create a new database. Now if you have 100 clients (tenants), you repeat this process for each client (tenant) which is boring.Since my application ran on the cloud (Amazon EC2), I created a simple Django (the web framework for perfectionists with deadlines) application to automate the boring stuff. Python OS Module provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
Now that we use Ubuntu on our cloud, we just need to import the OS module and put our Linux terminal commands inside os.system('sudo command') method. The terminal is a very powerful tool so I am executing the commands needed to perform the actions directly from the terminal as shown in the code snippet below..I edited the exact commands for my case.
import os
folder_name = "newTenant"
def add_tenant(request):
try:
#create a new directory with write and read permissions
os.system('sudo mkdir -m 777 /your new instance location/' + folder_name)
#copy files from the original instance to the new instance created above
os.system(
'sudo cp -a /original instance location/. /your new instance location/' + folder_name + '/')
#edit the database configuration file in the new app instance
os.system(
'sudo sed -i s/original instance database name/' + folder_name + '/g /your new instance location/' + folder_name + '/your database configuration file')
#create a MySql database using the terminal
os.system('sudo mysqladmin -uYourDatabaseServerUsername -pYourDatabasePassword create ' + folder_name)
except OSError:
print("Creation of the tenant failed")
else:
print("Successfully created the tenant")
As you can see above, I did this using just very few lines of code. You will however need to edit the terminal commands to suit your problem. Of course, I have created a user interface separately using Django and Bootstrap.
No comments:
Post a Comment