Thomas Vachuska
Committed by Gerrit Code Review

Initial implementation of shared test cell warden.

Change-Id: Ia973d514fe1dd11ffe4cdb7c902cc43a9c2eb626
...@@ -103,10 +103,41 @@ function setPrimaryInstance { ...@@ -103,10 +103,41 @@ function setPrimaryInstance {
103 echo $OCI 103 echo $OCI
104 } 104 }
105 105
106 +# ON.Lab shared test cell warden address
107 +export CELL_WARDEN="10.254.1.19"
108 +
106 # Applies the settings in the specified cell file or lists current cell definition 109 # Applies the settings in the specified cell file or lists current cell definition
107 # if no cell file is given. 110 # if no cell file is given.
108 function cell { 111 function cell {
109 - if [ -n "$1" ]; then 112 + cell=$1
113 + case "$cell" in
114 + "borrow")
115 + aux="/tmp/cell-$$"
116 + curl -sS -X POST "http://$CELL_WARDEN:4321/?user=$(id -un)&duration=${2:-60}" \
117 + -d "$(cat ~/.ssh/id_rsa.pub)" > $aux
118 + . $aux
119 + rm -f $aux
120 + export ONOS_INSTANCES=$(env | grep 'OC[0-9]*=' | sort | cut -d= -f2)
121 + setPrimaryInstance 1 >/dev/null
122 + cell
123 + ;;
124 + "return")
125 + curl -sS -X DELETE "http://$CELL_WARDEN:4321/?user=$(id -un)"
126 + ;;
127 +
128 + "status")
129 + curl -sS "http://$CELL_WARDEN:4321/"
130 + ;;
131 +
132 + "")
133 + env | egrep "ONOS_CELL"
134 + env | egrep "OCI"
135 + env | egrep "OC[0-9]+" | sort
136 + env | egrep "OC[NT]"
137 + env | egrep "ONOS_" | egrep -v 'ONOS_ROOT|ONOS_CELL|ONOS_INSTANCES' | sort
138 + ;;
139 +
140 + *)
110 [ ! -f $ONOS_ROOT/tools/test/cells/$1 ] && \ 141 [ ! -f $ONOS_ROOT/tools/test/cells/$1 ] && \
111 echo "No such cell: $1" >&2 && return 1 142 echo "No such cell: $1" >&2 && return 1
112 unset ONOS_CELL ONOS_NIC ONOS_IP ONOS_APPS ONOS_BOOT_FEATURES 143 unset ONOS_CELL ONOS_NIC ONOS_IP ONOS_APPS ONOS_BOOT_FEATURES
...@@ -121,16 +152,10 @@ function cell { ...@@ -121,16 +152,10 @@ function cell {
121 export ONOS_INSTANCES=$(env | grep 'OC[0-9]*=' | sort | cut -d= -f2) 152 export ONOS_INSTANCES=$(env | grep 'OC[0-9]*=' | sort | cut -d= -f2)
122 setPrimaryInstance 1 >/dev/null 153 setPrimaryInstance 1 >/dev/null
123 cell 154 cell
124 - else 155 + esac
125 - env | egrep "ONOS_CELL"
126 - env | egrep "OCI"
127 - env | egrep "OC[0-9]+" | sort
128 - env | egrep "OC[NT]"
129 - env | egrep "ONOS_" | egrep -v 'ONOS_ROOT|ONOS_CELL|ONOS_INSTANCES' | sort
130 - fi
131 } 156 }
132 157
133 -cell $ONOS_CELL > /dev/null 158 +[ -n "$ONOS_CELL" ] && cell $ONOS_CELL > /dev/null
134 159
135 # Lists available cells 160 # Lists available cells
136 function cells { 161 function cells {
......
...@@ -40,7 +40,7 @@ complete -F _stl-opts stl ...@@ -40,7 +40,7 @@ complete -F _stl-opts stl
40 function _cell-opts () { 40 function _cell-opts () {
41 local cur=${COMP_WORDS[COMP_CWORD]} 41 local cur=${COMP_WORDS[COMP_CWORD]}
42 if [ $COMP_CWORD -eq 1 ]; then 42 if [ $COMP_CWORD -eq 1 ]; then
43 - COMPREPLY=( $( compgen -W "$(cd $ONOS_ROOT/tools/test/cells && ls -1)" -- $cur ) ) 43 + COMPREPLY=( $( compgen -W "$(cd $ONOS_ROOT/tools/test/cells && ls -1) borrow return status" -- $cur ) )
44 fi 44 fi
45 } 45 }
46 46
......
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015-present Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<project xmlns="http://maven.apache.org/POM/4.0.0"
18 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20 + <modelVersion>4.0.0</modelVersion>
21 +
22 + <parent>
23 + <groupId>org.onosproject</groupId>
24 + <artifactId>onlab-utils</artifactId>
25 + <version>1.6.0-SNAPSHOT</version>
26 + <relativePath>../pom.xml</relativePath>
27 + </parent>
28 +
29 + <artifactId>onlab-warden</artifactId>
30 + <packaging>jar</packaging>
31 +
32 + <description>System Test Cell Warden</description>
33 +
34 + <dependencies>
35 + <dependency>
36 + <groupId>org.eclipse.jetty</groupId>
37 + <artifactId>jetty-server</artifactId>
38 + <version>8.1.18.v20150929</version>
39 + </dependency>
40 + <dependency>
41 + <groupId>org.eclipse.jetty</groupId>
42 + <artifactId>jetty-servlet</artifactId>
43 + <version>8.1.18.v20150929</version>
44 + </dependency>
45 + </dependencies>
46 +
47 + <build>
48 + <plugins>
49 + <plugin>
50 + <groupId>org.apache.maven.plugins</groupId>
51 + <artifactId>maven-shade-plugin</artifactId>
52 + <version>2.4.3</version>
53 + <configuration>
54 + <transformers>
55 + <transformer
56 + implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
57 + <mainClass>org.onlab.warden.Main</mainClass>
58 + </transformer>
59 + </transformers>
60 + <filters>
61 + <filter>
62 + <artifact>*:*</artifact>
63 + <excludes>
64 + <exclude>META-INF/*.SF</exclude>
65 + <exclude>META-INF/*.DSA</exclude>
66 + <exclude>META-INF/*.RSA</exclude>
67 + </excludes>
68 + </filter>
69 + </filters>
70 + </configuration>
71 + <executions>
72 + <execution>
73 + <phase>package</phase>
74 + <goals>
75 + <goal>shade</goal>
76 + </goals>
77 + </execution>
78 + </executions>
79 + </plugin>
80 + </plugins>
81 + </build>
82 +
83 +</project>
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onlab.warden;
18 +
19 +import org.eclipse.jetty.server.Server;
20 +import org.eclipse.jetty.servlet.ServletHandler;
21 +import org.eclipse.jetty.util.log.Logger;
22 +
23 +/**
24 + * Main program for executing scenario test warden.
25 + */
26 +public final class Main {
27 +
28 + // Public construction forbidden
29 + private Main(String[] args) {
30 + }
31 +
32 + /**
33 + * Main entry point for the cell warden.
34 + *
35 + * @param args command-line arguments
36 + */
37 + public static void main(String[] args) {
38 + Main main = new Main(args);
39 + main.run();
40 + }
41 +
42 + // Runs the warden processing
43 + private void run() {
44 + startWebServer();
45 + }
46 +
47 + // Initiates a web-server.
48 + private static void startWebServer() {
49 + WardenServlet.warden = new Warden();
50 + org.eclipse.jetty.util.log.Log.setLog(new NullLogger());
51 + Server server = new Server(4321);
52 + ServletHandler handler = new ServletHandler();
53 + server.setHandler(handler);
54 + handler.addServletWithMapping(WardenServlet.class, "/*");
55 + try {
56 + server.start();
57 + } catch (Exception e) {
58 + print("Warden already active...");
59 + }
60 + }
61 +
62 + private static void print(String s) {
63 + System.out.println(s);
64 + }
65 +
66 + // Logger to quiet Jetty down
67 + private static class NullLogger implements Logger {
68 + @Override
69 + public String getName() {
70 + return "quiet";
71 + }
72 +
73 + @Override
74 + public void warn(String msg, Object... args) {
75 + }
76 +
77 + @Override
78 + public void warn(Throwable thrown) {
79 + }
80 +
81 + @Override
82 + public void warn(String msg, Throwable thrown) {
83 + }
84 +
85 + @Override
86 + public void info(String msg, Object... args) {
87 + }
88 +
89 + @Override
90 + public void info(Throwable thrown) {
91 + }
92 +
93 + @Override
94 + public void info(String msg, Throwable thrown) {
95 + }
96 +
97 + @Override
98 + public boolean isDebugEnabled() {
99 + return false;
100 + }
101 +
102 + @Override
103 + public void setDebugEnabled(boolean enabled) {
104 + }
105 +
106 + @Override
107 + public void debug(String msg, Object... args) {
108 + }
109 +
110 + @Override
111 + public void debug(Throwable thrown) {
112 + }
113 +
114 + @Override
115 + public void debug(String msg, Throwable thrown) {
116 + }
117 +
118 + @Override
119 + public Logger getLogger(String name) {
120 + return this;
121 + }
122 +
123 + @Override
124 + public void ignore(Throwable ignored) {
125 + }
126 + }
127 +
128 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onlab.warden;
18 +
19 +import static com.google.common.base.Preconditions.checkState;
20 +
21 +/**
22 + * Cell reservation record.
23 + */
24 +final class Reservation {
25 +
26 + final String cellName;
27 + final String userName;
28 + final long time;
29 + final int duration;
30 +
31 + // Creates a new reservation record
32 + Reservation(String cellName, String userName, long time, int duration) {
33 + this.cellName = cellName;
34 + this.userName = userName;
35 + this.time = time;
36 + this.duration = duration;
37 + }
38 +
39 + /**
40 + * Decodes reservation record from the specified line.
41 + *
42 + * @param line string line
43 + */
44 + Reservation(String line) {
45 + String[] fields = line.trim().split("\t");
46 + checkState(fields.length == 4, "Incorrect reservation encoding");
47 + this.cellName = fields[0];
48 + this.userName = fields[1];
49 + this.time = Long.parseLong(fields[2]);
50 + this.duration = Integer.parseInt(fields[3]);
51 + }
52 +
53 + /**
54 + * Encodes reservation record into a string line.
55 + *
56 + * @return encoded string
57 + */
58 + String encode() {
59 + return String.format("%s\t%s\t%s\t%s\n", cellName, userName, time, duration);
60 + }
61 +
62 +}
This diff is collapsed. Click to expand it.
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onlab.warden;
18 +
19 +import com.google.common.io.ByteStreams;
20 +import org.eclipse.jetty.server.Response;
21 +
22 +import javax.servlet.ServletException;
23 +import javax.servlet.http.HttpServlet;
24 +import javax.servlet.http.HttpServletRequest;
25 +import javax.servlet.http.HttpServletResponse;
26 +import java.io.IOException;
27 +import java.io.PrintWriter;
28 +import java.text.SimpleDateFormat;
29 +import java.util.Date;
30 +
31 +import static com.google.common.base.Strings.isNullOrEmpty;
32 +
33 +/**
34 + * Web socket servlet capable of creating web sockets for the STC monitor.
35 + */
36 +public class WardenServlet extends HttpServlet {
37 +
38 + static Warden warden;
39 +
40 + @Override
41 + protected void doGet(HttpServletRequest req, HttpServletResponse resp)
42 + throws ServletException, IOException {
43 + resp.setContentType("text/plain; charset=UTF-8");
44 + SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
45 +
46 + try (PrintWriter out = resp.getWriter()) {
47 + for (String cellName : warden.getCells()) {
48 + Reservation reservation = warden.currentCellReservation(cellName);
49 + if (reservation != null) {
50 + long expiration = reservation.time + reservation.duration * 60_000;
51 + out.println(String.format("%-10s\t%-10s\t%s\t%s\t%s minutes", cellName,
52 + reservation.userName,
53 + fmt.format(new Date(reservation.time)),
54 + fmt.format(new Date(expiration)),
55 + reservation.duration));
56 + } else {
57 + out.println(String.format("%-10s\t%-10s", cellName, "available"));
58 + }
59 + }
60 + } catch (Exception e) {
61 + resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
62 + e.printStackTrace();
63 + }
64 + }
65 +
66 + @Override
67 + protected void doPost(HttpServletRequest req, HttpServletResponse resp)
68 + throws ServletException, IOException {
69 + try (PrintWriter out = resp.getWriter()) {
70 + String sshKey = new String(ByteStreams.toByteArray(req.getInputStream()), "UTF-8");
71 + String userName = req.getParameter("user");
72 + String sd = req.getParameter("duration");
73 + int duration = isNullOrEmpty(sd) ? 60 : Integer.parseInt(sd);
74 + String cellDefinition = warden.borrowCell(userName, sshKey, duration);
75 + out.println(cellDefinition);
76 + } catch (Exception e) {
77 + resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
78 + e.printStackTrace();
79 + }
80 + }
81 +
82 + @Override
83 + protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
84 + throws ServletException, IOException {
85 + try (PrintWriter out = resp.getWriter()) {
86 + String userName = req.getParameter("user");
87 + warden.returnCell(userName);
88 + } catch (Exception e) {
89 + resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
90 + e.printStackTrace();
91 + }
92 + }
93 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Cell warden to coordinate borrowing and returning test cells.
19 + */
20 +package org.onlab.warden;
...\ No newline at end of file ...\ No newline at end of file