Skip to main content

Platform as a Service (PaaS)

Platform as a Service
========================================================

In simple words in this project we are going to develop a online coding webapp where you can just choose the programming language in which you want to code and Execute your code in the website itself.
The magic of using webserver and docker technology in PaaS is 
you can just connect all your device with the server and anyone connected with it will have access to the Coding Website we will develop.

1. First we will Develop the Web Page and Save it as  paas.html in the html folder of you apache server.

Write the Following code in that--


<form action ="http://192.168.43.80/cgi-bin/paas.py">
<br>

<pre>
    <h2>Select the Language</h2>   
<select name="languages">

  <option value="java">Java</option>
  <option value="python">Python</option>
  <option value="c">C</option>
  <option value="cpp">C++</option>
</br>

</select>

<h2>Enter the name of Program</h2>
<input type=text name="n">

<h2>Enter your code here</h2>
 <textarea name="message" rows="50" cols="80">

</textarea>

 <textarea name="input" rows="10" cols="30">

</textarea>
 <input type=submit value="Start">


</pre>
</form>



We can see the Form used in the html file tranfers the inputs from the user to the paas.py which is stored in the cgi-bin folder of your apache server.
The html coding done above is very simple.
The python file paas.py is explained below.

paas.py is as follows--


#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"

import os
import cgi,commands,re,cgitb
import mysql.connector as mariadb

x=cgi.FieldStorage()
lang=x.getvalue('languages')
name=x.getvalue('n')
mes=x.getvalue('message')
inp=x.getvalue('input')


r=os.chdir("/var/www/html/")

f = open("{}.java".format(name),"w")
f.write("{}".format(mes))
f.close()

f = open("i{}.txt".format(name),"w")
f.write("{}".format(inp))
f.close()



d=commands.getstatusoutput("sudo docker run --rm -v \"$PWD\":/usr/src/myapp -w /usr/src/myapp java:7 javac {}.java".format(name))

e=commands.getstatusoutput("sudo docker run --rm -v \"$PWD\":/usr/src/myapp -w /usr/src/myapp java:7 cat i{}.txt | java {}".format(name,name))


f = open("{}.txt".format(name),"w")
f.write("{}".format(e[1]))
f.close()

print "<META HTTP-EQUIV=refresh CONTENT=\"0; URL=http://192.168.43.80/{}.txt\">".format(name)


a=commands.getstatusoutput("rm {}.java {}.class
i{}.txt".format(name,name,name,name))


This looks complicated but yet simple. The python programming used here can be understand even you use some other programming language.
Only the commands written inside  commands.getstatusoutput() do the real magic.

The working of the python file is as follows:-

The selected language is stored in the lang varialble. The name of class or the program is stored in name variable and the whole program written by user  in the textarea field of html page is stored in the mes variable.
  
First we go to /var/www/html directory of our apache server on rhel by the command os.chdir("path").
We write all the command that are to be executed by rhel server(or redhat commands) inside the   commands.getstatusoutput() method and the store the output of these commands in a variable.

At first by using python file handling features, we create a file naming name.java. The name is provided by the user. Then we write the written program which is stored in mes variable in the name.java file if the language selected is java. Then we close the file.
The sign {} is used for a variable like we use %d or %s in printf in c language. It is later defines by the content in .format() function.

The two main command used here are-  
sudo docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp java:7 javac {}.java".format(name)  
sudo docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp java:7 java {}".format(name)
 Note that in python file we have used \ sign before the " to remove conflicts.

The two are docker commands 
This will add your current directory as a volume to the container, set the working directory to the volume, and run the command javac name.java which will tell Java to compile the code in name.java and output the Java class file to name.class.
Download the java dockerfiles from:-
https://hub.docker.com/_/java/ 

cat i{}.txt | java {}

In this code cat iname.txt | java name, We redirect the java programs standard input to the iname.txt file. Whenever in the program user writes some code to take input from the standard input, it get redirected to this text file.

We then stored the output of java in variable e and save it in a file name.txt.
In the last print command we redirect the output of html page to the name.txt file which stores the output of the java file.
Enjoy you PaaS. Ask any problems in comments.    

Comments

  1. how to install and set path for the docker image of openjdk?..please can u give me the full details..

    ReplyDelete
    Replies
    1. Hello,
      The simplest method is, if you have latest version of Docker 1.9.1 installed just make a java program and save it(example.java) and connect to internet
      and then try to run the following command-

      sudo docker run --rm -v $PWD:/usr/src/myapp -w /usr/src/myapp java:7 javac example.java

      This should automatically search for needed files and will download and install them as needed...
      Hope this works, if some problem still exists please mention...

      Delete

Post a Comment

Popular posts from this blog

Share applications with ssh (SaaS)

We are going to share our server's application programs to client system without actually installing it to client's pc. In this example we are going to use firefox as application program but any installed application(on server side) can be used for SaaS . Server- RHEL7.2 Client- Windows or any linux based OS For any type of service we need server applications which defines protocols that tells how the communication to client is going to happen. For SaaS we will use SSH protocol- Secure Shell ( SSH ) is a cryptographic network protocol for operating network services securely over an unsecured network. The server will be open-ssh -- OpenSSH is the connectivity tool for remote login with the SSH protocol. It encrypts all traffic to eliminate eavesdropping, connection hijacking. Step 1- Installing SSH server -- Open terminal and type- [root@www Desktop]# yum  install openssh-server This will install the open-ssh server to your rhel7.2 server. In case you don

The basics of cloud computing

First we are going to understand what cloud computing is all about. We will less focus on theory part and more on implementation part. Cloud computing-- Let's suppose you buy a brand new Laptop with a lots of RAMs and super fast processor speed with of course a lots of money. Now you ask the laptop dealer that till what duration this device is mine and the dealer said "Sir, it your till forever and infinity!". Well it's not that bad deal as i got a lots of ram and speed for this amount with a lifetime of use you may think, b ut consider it this way that-- Are you really going to use all this RAM and processor power all the time till it's lifetime. Of course not even you are going to use this laptop(or we can say operating system) all the time. So, why pay for it's 24*7 use if you just need it let's say 6 hours a day. This all idea may seem silly but that's what all I.T. companies use. Consider the situation that you opened a new company and