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

Overview

Namaste!   Welcome to my blog.. Here we are soon going to develop a own cloud infrastructure from the scratch. If you study here from the beginning  to end with self practice at your home I can assure you that within maximum of 2 months you can develop your own cloud. In our cloud project we will mainly include following features -- #################################################################################################### Software as a Service (SaaS) Storage as a Service (StaaS)[Object and Block] Infrastructure  as a Service (IaaS) Container as a Service (Caas) Platform as a Service (PaaS) #################################################################################################### The tools and technologies we are going to work on are-- Redhat Dockers KVM hypervisor Mariadb Python Python CGI HTML, CSS, Java Script The above names sure looks bit strange and you may think that it's very complicated and not for me to learn. But i can say