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
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.
========================================================
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.
how to install and set path for the docker image of openjdk?..please can u give me the full details..
ReplyDeleteHello,
DeleteThe 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...