Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java verifier #3

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
Expand Down Expand Up @@ -52,6 +51,3 @@ docs/_build/

# PyBuilder
target/


verifiers/angularjs/node_modules/
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.0
0.7.1
31 changes: 16 additions & 15 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function forwardrule_exist() {

function group_exist() {
echo -n "Checking if managed instance group named '$1' in zone '$2' already exists..."
found $(gcloud preview managed-instance-groups --zone "$2" list -l | awk '$1=="'$1'" {print $1}')
found $(gcloud compute instance-groups managed list --zone "$2" | awk '$1=="'$1'" {print $1}')
return $?
}

Expand Down Expand Up @@ -104,10 +104,13 @@ function template_exist() {


function create_autoscaler() {
gcloud preview autoscaler --zone "$3" create "$1" \
--min-num-replicas "$4" \
--max-num-replicas "$5" \
--target "$2"
gcloud compute instance-groups managed set-autoscaling \
$1 \
--zone "$2"\
--min-num-replicas "$3" \
--max-num-replicas "$4" \
--target-cpu-utilization 0.75 \
--cool-down-period 180
}


Expand All @@ -129,9 +132,9 @@ function create_forwardrule() {
function create_group() {
group_exist $1 $2
if [[ $? -ne 0 ]]; then
gcloud preview managed-instance-groups \
gcloud compute instance-groups managed create \
"$1" \
--zone "$2" \
create "$1" \
--base-instance-name "$3" \
--size "$4"\
--template "$5" \
Expand Down Expand Up @@ -211,16 +214,15 @@ function check_image_builder_instance_status() {

echo "Image builder instance IP:" $ip

status=$(curl http://${ip}/status.txt)
status_fetched=$?
while [[ $status_fetched -ne 0 ]]; do
code=$(curl -s -o /dev/null -w "%{http_code}" http://${ip}/status.txt)
while [[ $code -ne 200 ]]; do
echo "Failed to fetch image builder instance status..."
echo "will try again in 30s"
sleep 30
status=$(curl http://${ip}/status.txt)
status_fetched=$?
code=$(curl -s -o /dev/null -w "%{http_code}" http://${ip}/status.txt)
done

status=$(curl http://${ip}/status.txt)
if [[ "$status" == "done" ]]; then
echo "Image builder instance is ready."
elif [[ "$status" == "failed" ]]; then
Expand Down Expand Up @@ -305,7 +307,7 @@ function start_cluster() {

### Autoscaler
echo -e "\n\nCreating autoscaler..."
create_autoscaler "$AUTOSCALER_NAME" "$INSTANCE_GROUP_NAME" "$ZONE" "$CLUSTER_NODE_MIN_COUNT" "$CLUSTER_NODE_MAX_COUNT"
create_autoscaler "$INSTANCE_GROUP_NAME" "$ZONE" "$CLUSTER_NODE_MIN_COUNT" "$CLUSTER_NODE_MAX_COUNT"

cluster_ip=$(gcloud compute forwarding-rules list codeverifier-rule --regions us-central1 | cat -n | awk '$1>1{print $4}')
echo "Test the cluster at http://${cluster_ip}/console/"
Expand All @@ -314,8 +316,7 @@ function start_cluster() {


function stop_cluster() {
gcloud preview autoscaler --zone "$ZONE" delete "$AUTOSCALER_NAME"
gcloud preview managed-instance-groups --zone "$ZONE" delete "$INSTANCE_GROUP_NAME"
gcloud compute instance-groups managed delete --zone "$ZONE" "$INSTANCE_GROUP_NAME"
}


Expand Down
2 changes: 2 additions & 0 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ push-images: images
run-image: images
${docker} rm -f $(${docker} ps -aq) > /dev/null 2>&1 || echo "no container running."
cd ../verifiers/angularjs; make images version=${version} tag=${tag}
cd ../verifiers/java; make images version=${version} tag=${tag}
cd ../verifiers/python3; make images version=${version} tag=${tag}
cd ../verifiers/javascript; make images version=${version} tag=${tag}
CLUSTER_VERSION=${version} SKIP_CLUSTER_IPTABLE_CONFIGURATION=true DOCKER="docker" ./bin/startup_run.sh
.PHONY: images push-images run-image

push-deploy-images: push-images
cd ../verifiers/java; make push-images version=${version} tag=${tag}
cd ../verifiers/python3; make push-images version=${version} tag=${tag}
cd ../verifiers/javascript; make push-images version=${version} tag=${tag}
cd ../verifiers/angularjs; make push-images version=${version} tag=${tag}
Expand Down
4 changes: 4 additions & 0 deletions server/bin/startup_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ IPTABLES=${IPTABLES:="sudo iptables"}
SELENIUM_VERSION="2.44.0"

SERVER_IMAGE="singpath/verifier-server"
JAVA_IMAGE="singpath/verifier-java"
PYTHON_IMAGE="singpath/verifier-python3"
JAVASCRIPT_IMAGE="singpath/verifier-javascript"
ANGULARJS_IMAGE="singpath/verifier-angularjs"
Expand All @@ -14,6 +15,7 @@ SELENIUM_IMAGE="selenium/hub"
SELENIUM_PHANTOMJS_IMAGE="singpath/verifier-angularjs-phantomjs"

SERVER_CONTAINER="server"
JAVA_CONTAINER="java"
PYTHON_CONTAINER="python"
JAVASCRIPT_CONTAINER="javascript"
ANGULARJS_CONTAINER="angularjs"
Expand Down Expand Up @@ -52,6 +54,7 @@ fi


# start verifier containers
$DOCKER run -d --name "$JAVA_CONTAINER" --restart="always" "$JAVA_IMAGE":"$CLUSTER_VERSION"
$DOCKER run -d --name "$PYTHON_CONTAINER" --restart="always" "$PYTHON_IMAGE":"$CLUSTER_VERSION"
$DOCKER run -d --name "$JAVASCRIPT_CONTAINER" --restart="always" "$JAVASCRIPT_IMAGE":"$CLUSTER_VERSION"
$DOCKER run -d --name "$ANGULARJS_STATIC_CONTAINER" --restart="always" -v /www/_protractor "$ANGULARJS_STATIC_IMAGE":"$CLUSTER_VERSION"
Expand All @@ -62,6 +65,7 @@ $DOCKER run -d --name "$ANGULARJS_CONTAINER" --restart="always" --link "$SELENIU

# start the nginx proxy
$DOCKER run -d --name "$SERVER_CONTAINER" -p 80:80 --restart="always" \
--link "$JAVA_CONTAINER":java \
--link "$PYTHON_CONTAINER":python \
--link "$JAVASCRIPT_CONTAINER":javascript \
--link "$ANGULARJS_CONTAINER":angularjs \
Expand Down
2 changes: 2 additions & 0 deletions server/bin/startup_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SELENIUM_VERSION="2.44.0"

SERVER_IMAGE="singpath/verifier-server"
JAVA_IMAGE="singpath/verifier-java"
PYTHON_IMAGE="singpath/verifier-python3"
JAVASCRIPT_IMAGE="singpath/verifier-javascript"
ANGULARJS_IMAGE="singpath/verifier-angularjs"
Expand All @@ -29,6 +30,7 @@ if [[ -z "$CLUSTER_VERSION" ]]; then
status_server "failed"
else
sudo docker pull "$PYTHON_IMAGE":"$CLUSTER_VERSION"
sudo docker pull "$JAVA_IMAGE":"$CLUSTER_VERSION"
sudo docker pull "$JAVASCRIPT_IMAGE":"$CLUSTER_VERSION"
sudo docker pull "$SERVER_IMAGE":"$CLUSTER_VERSION"
sudo docker pull "$ANGULARJS_IMAGE":"$CLUSTER_VERSION"
Expand Down
6 changes: 6 additions & 0 deletions server/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ http {
gzip on;
gzip_disable "msie6";

limit_req_zone $binary_remote_addr zone=java:10m rate=5r/s;
limit_req_zone $binary_remote_addr zone=python:10m rate=5r/s;
limit_req_zone $binary_remote_addr zone=javascript:10m rate=5r/s;
limit_req_zone "all" zone=angularjs:10m rate=5r/s;
Expand All @@ -28,6 +29,11 @@ http {

location / {}

location /java {
proxy_pass http://java:5000;
limit_req zone=java burst=5;
}

location /python {
proxy_pass http://python:5000;
limit_req zone=python burst=5;
Expand Down
12 changes: 12 additions & 0 deletions server/www/console/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ <h1 class="md-toolbar-tools">Verifier Console</h1>
<li>
<a href="" ng-click="ctrl.setVerifier('python3')">Python 3</a>
</li>

<li>
<a href="" ng-click="ctrl.setVerifier('java')">Java</a>
</li>
</ul>
</md-content>

Expand Down Expand Up @@ -93,6 +97,14 @@ <h2 class="md-toolbar-tools">{{ctrl.verifier.name}}</h2>
.controller('Verifier', function($http) {
var self = this;
var verifiers = {
java: {
name: 'Java',
endpoint: '/java',
payload: {
solution: 'int foo = 1',
tests: 'assertEquals(1, foo)'
}
},
python3: {
name: 'Python 3',
endpoint: '/python3',
Expand Down
3 changes: 2 additions & 1 deletion verifiers/angularjs/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.images
.images
node_modules
3 changes: 3 additions & 0 deletions verifiers/java/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/*
.images/*
out/*
5 changes: 5 additions & 0 deletions verifiers/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
.images
out
build
java-verifier.iml
22 changes: 22 additions & 0 deletions verifiers/java/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM java:8-jdk

RUN mkdir -p /app/build/dist && \
mkdir -p /app/build/tests && \
adduser --system --home /playground verifier

COPY ./lib /app/lib
COPY ./src /app/src
ENV CLASSPATH /app/lib/*:/app/build/dist
RUN javac -sourcepath /app/src -d /app/build/dist -g /app/src/com/singpath/server/Main.java

COPY ./tests /app/tests
RUN javac -sourcepath /app/tests -d /app/build/tests -g /app/tests/com/singpath/TestSuite.java

COPY ./log4j2.xml /app/log4j2.xml

USER verifier
WORKDIR /playground

EXPOSE 5000

CMD ["java", "-Xmx70m", "-Dlog4j.configurationFile=/app/log4j2.xml", "com.singpath.server.Main"]
39 changes: 39 additions & 0 deletions verifiers/java/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cwd = $(shell pwd)

repo-name ?= singpath/verifier-java
tag ?= latest
version ?= $(shell cat ../../VERSION)

docker ?= docker

src-files = $(shell find src -name "*.java")
test-files = $(shell find src -name "*.java")

.images/${repo-name}.${tag}: .images/${repo-name}.${version}
mkdir -p .images/singpath
${docker} rmi ${repo-name}:${tag} > /dev/null 2>&1 || echo "latest tag not set yet."
${docker} tag -f ${repo-name}:${version} ${repo-name}:${tag}
touch $@

.images/${repo-name}.${version}: Dockerfile lib/*.jar log4j2.xml ${src-files} ${test-files}
mkdir -p .images/singpath
${docker} build --rm=true -t ${repo-name}:${version} .
touch $@

images: .images/${repo-name}.${tag}
.PHONY: images

push-images: images
${docker} push ${repo-name}:${version}
.PHONY: push-images

run-image: images
${docker} run -ti --rm -p 5000:5000 ${repo-name}:${version}
.PHONY: run-image

test: .images/${repo-name}.${version}
$(docker) run -ti --rm ${repo-name}:${version} java -classpath /app/lib/*:/app/build/dist:/app/build/tests org.junit.runner.JUnitCore com.singpath.TestSuite
.PHONY: test

bash: .images/${repo-name}.${version}
${docker} run -ti --rm -p 5000:5000 ${repo-name}:${version} ls -la /app/src
62 changes: 62 additions & 0 deletions verifiers/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Java verifier

The Java verifier needs a Java JDK docker image to compile the server and run
it. We are currently using "java:8-jdk" (OpenJDK).


## Requirements

- [Docker](https://docs.docker.com/installation/) (or docker-machine on OS X
and windows);
- [gcloud](https://cloud.google.com/sdk/#Quick_Start);
- bash and make (you will need to install something [cygwin](http://cygwin.com/)
or [MSYS](http://www.mingw.org/wiki/MSYS) on windows).

Note:

On Linux, you might have to run the commands running sudo or add your user
to the docker group.


## Building the docker images

```shell
make images
```


## Running the server

```shell
make run-image
```

To try to POST a JSON solution you can use curl or the Postman chrome extension.
E.g. using curl on OS X (with the docker host IP usually being 192.168.99.100):
```shell
curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"solution":"\npublic class SingPath {\n public Double two() {\n return 2.0;\n }\n} \n", "tests":"SingPath sp = new SingPath();\nassertEquals(2.0 , sp.two());"}' \
http://192.168.99.100:5000/java
```


## Test

The run the tests in docker:

```shell
make test
```


## Pushing image

You should be part of the
[Singpath organization](https://registry.hub.docker.com/repos/singpath/)
to publish the code verifier on docker hub.

```
make push-images
```
Binary file added verifiers/java/lib/accessors-smart-1.1.jar
Binary file not shown.
Binary file added verifiers/java/lib/asm-5.0.3.jar
Binary file not shown.
Binary file added verifiers/java/lib/bsh-2.0b5.jar
Binary file not shown.
Binary file added verifiers/java/lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added verifiers/java/lib/json-smart-2.2.jar
Binary file not shown.
Binary file added verifiers/java/lib/junit-4.12.jar
Binary file not shown.
Binary file added verifiers/java/lib/log4j-api-2.4.1.jar
Binary file not shown.
Binary file added verifiers/java/lib/log4j-core-2.4.1.jar
Binary file not shown.
Binary file added verifiers/java/lib/org.osgi.core-4.3.1.jar
Binary file not shown.
13 changes: 13 additions & 0 deletions verifiers/java/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Loading