Thomas Vachuska

Removed warden; moved to separate repo.

Change-Id: I76ae777891ad3256f1bb30f11c17c0aebfc1ed5d
#!/bin/bash
# Creates a cell definition from the given name and LXC info
name="$1"
apps="${2:-drivers,openflow,proxyarp,mobility,pathpainter}"
echo "export ONOS_CELL=borrow"
echo "export ONOS_NIC=\"10.128.11.*\""
sudo lxc-ls -F "name,ipv4" --fancy | grep $name | \
sed "s/^$name-/OC/" | tr "[:lower:]" "[:upper:]" | \
sed -r 's/[ ]+/\=/;s/^/export /'
echo "export OCT=\"10.128.11.1\""
echo "export ONOS_USER=sdn"
echo "export ONOS_USE_SSH=true"
echo "export ONOS_APPS=${apps}"
echo "export ONOS_WEB_USER=onos"
echo "export ONOS_WEB_PASS=rocks"
#!/bin/bash
# Creates a new node from the base image.
base="$1"
ip="$2"
name="$3"
shift 3
key="$@"
sudo lxc-clone -o $base -n $name
sudo chmod 777 /var/lib/lxc
sudo chmod 777 /var/lib/lxc/$name
sudo chmod 666 /var/lib/lxc/$name/config
sudo cat >>/var/lib/lxc/$name/config <<EOF
lxc.network.ipv4 = ${ip}/16
lxc.network.ipv4.gateway = 10.128.0.1
EOF
sudo chmod 644 /var/lib/lxc/$name/config
sudo chmod 750 /var/lib/lxc/$name
sudo chmod 700 /var/lib/lxc
sudo lxc-start -d -n $name
sudo lxc-attach -n $name -- ping -c1 8.8.8.8
sudo lxc-attach -n $name -- bash -c "echo $key >> /home/sdn/.ssh/authorized_keys"
sudo lxc-attach -n $name -- bash -c "sed -i \"s/127.0.1.1.*/127.0.1.1 $name/\" /etc/hosts"
#!/bin/bash
# Creates a new cell.
name="$1"
ipx="$2"
spec="$3"
shift 3
key="$@"
cd $(dirname $0)
nodes=${spec%+*}
mininet=${spec#*+}
sudo lxc-attach -n bit-proxy -- bash -c "grep -qF \"$key\" /home/sdn/.ssh/authorized_keys || echo $key >> /home/sdn/.ssh/authorized_keys"
if [ $mininet -ge 1 ]; then
./clone-node base-mininet ${ipx/x/0} $name-n "$key"
fi
let n=1
while [ $n -le $nodes ]; do
./clone-node base-onos ${ipx/x/$n} $name-$n "$key"
let n=n+1
done
#!/bin/bash
# Destroys an LXC cell with the specified name.
name=$1
spec=$2
nodes=${spec%+*}
mininet=${spec#*+}
cd $(dirname $0)
if [ $mininet -ge 1 ]; then
./destroy-node $name-n
fi
let n=1
while [ $n -le $nodes ]; do
./destroy-node $name-$n
let n=n+1
done
#!/bin/bash
# Destroys an LXC node with the specified name.
name=$1
[ "$name" = "onos-base" ] && echo "Don't do that!" && exit 1
[ "$name" = "mininet-base" ] && echo "Don't do that!" && exit 1
sudo lxc-stop -n $name
sudo lxc-destroy -n $name
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2015-present Open Networking Laboratory
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onlab-utils</artifactId>
<version>1.6.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onlab-warden</artifactId>
<packaging>jar</packaging>
<description>System Test Cell Warden</description>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.18.v20150929</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.1.18.v20150929</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-misc</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.onlab.warden.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.warden;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.util.log.Logger;
/**
* Main program for executing scenario test warden.
*/
public final class Main {
// Public construction forbidden
private Main(String[] args) {
}
/**
* Main entry point for the cell warden.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
Main main = new Main(args);
main.run();
}
// Runs the warden processing
private void run() {
startWebServer();
}
// Initiates a web-server.
private static void startWebServer() {
WardenServlet.warden = new Warden();
org.eclipse.jetty.util.log.Log.setLog(new NullLogger());
Server server = new Server(4321);
ServletHandler handler = new ServletHandler();
server.setHandler(handler);
handler.addServletWithMapping(WardenServlet.class, "/*");
try {
server.start();
} catch (Exception e) {
print("Warden already active...");
}
}
private static void print(String s) {
System.out.println(s);
}
// Logger to quiet Jetty down
private static class NullLogger implements Logger {
@Override
public String getName() {
return "quiet";
}
@Override
public void warn(String msg, Object... args) {
}
@Override
public void warn(Throwable thrown) {
}
@Override
public void warn(String msg, Throwable thrown) {
}
@Override
public void info(String msg, Object... args) {
}
@Override
public void info(Throwable thrown) {
}
@Override
public void info(String msg, Throwable thrown) {
}
@Override
public boolean isDebugEnabled() {
return false;
}
@Override
public void setDebugEnabled(boolean enabled) {
}
@Override
public void debug(String msg, Object... args) {
}
@Override
public void debug(Throwable thrown) {
}
@Override
public void debug(String msg, Throwable thrown) {
}
@Override
public Logger getLogger(String name) {
return this;
}
@Override
public void ignore(Throwable ignored) {
}
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.warden;
import static com.google.common.base.Preconditions.checkState;
/**
* Cell reservation record.
*/
final class Reservation {
final String cellName;
final String userName;
final long time;
final int duration;
final String cellSpec;
// Creates a new reservation record
Reservation(String cellName, String userName, long time, int duration, String cellSpec) {
this.cellName = cellName;
this.userName = userName;
this.time = time;
this.duration = duration;
this.cellSpec = cellSpec;
}
/**
* Decodes reservation record from the specified line.
*
* @param line string line
*/
Reservation(String line) {
String[] fields = line.trim().split("\t");
checkState(fields.length == 5, "Incorrect reservation encoding");
this.cellName = fields[0];
this.userName = fields[1];
this.time = Long.parseLong(fields[2]);
this.duration = Integer.parseInt(fields[3]);
this.cellSpec = fields[4];
}
/**
* Encodes reservation record into a string line.
*
* @return encoded string
*/
String encode() {
return String.format("%s\t%s\t%s\t%s\t%s\n", cellName, userName, time, duration, cellSpec);
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.warden;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.*;
/**
* Warden for tracking use of shared test cells.
*/
class Warden {
private static final String CELL_NOT_NULL = "Cell name cannot be null";
private static final String USER_NOT_NULL = "User name cannot be null";
private static final String KEY_NOT_NULL = "User key cannot be null";
private static final String UTF_8 = "UTF-8";
private static final long TIMEOUT = 10; // 10 seconds
private static final int MAX_MINUTES = 240; // 4 hours max
private static final int MINUTE = 60_000; // 1 minute
private static final int DEFAULT_MINUTES = 60;
private static final String DEFAULT_SPEC = "3+1";
private final File log = new File("warden.log");
// Allow overriding these for unit tests.
static String cmdPrefix = "";
static File root = new File(".");
private final File cells = new File(root, "cells");
private final File supported = new File(cells, "supported");
private final File reserved = new File(cells, "reserved");
private final Random random = new Random();
/**
* Creates a new cell warden.
*/
Warden() {
reserved.mkdirs();
random.setSeed(System.currentTimeMillis());
Timer timer = new Timer("cell-pruner", true);
timer.schedule(new Reposessor(), MINUTE / 4, MINUTE / 2);
}
/**
* Returns list of names of supported cells.
*
* @return list of cell names
*/
Set<String> getCells() {
String[] list = supported.list();
return list != null ? ImmutableSet.copyOf(list) : ImmutableSet.of();
}
/**
* Returns list of names of available cells.
*
* @return list of cell names
*/
Set<String> getAvailableCells() {
Set<String> available = new HashSet<>(getCells());
available.removeAll(getReservedCells());
return ImmutableSet.copyOf(available);
}
/**
* Returns list of names of reserved cells.
*
* @return list of cell names
*/
Set<String> getReservedCells() {
String[] list = reserved.list();
return list != null ? ImmutableSet.copyOf(list) : ImmutableSet.of();
}
/**
* Returns the host name on which the specified cell is hosted.
*
* @param cellName cell name
* @return host name where the cell runs
*/
String getCellHost(String cellName) {
return getCellInfo(cellName).hostName;
}
/**
* Returns reservation for the specified user.
*
* @param userName user name
* @return cell reservation record or null if user does not have one
*/
Reservation currentUserReservation(String userName) {
checkNotNull(userName, USER_NOT_NULL);
for (String cellName : getReservedCells()) {
Reservation reservation = currentCellReservation(cellName);
if (reservation != null && userName.equals(reservation.userName)) {
return reservation;
}
}
return null;
}
/**
* Returns the name of the user who reserved the given cell.
*
* @param cellName cell name
* @return cell reservation record or null if cell is not reserved
*/
Reservation currentCellReservation(String cellName) {
checkNotNull(cellName, CELL_NOT_NULL);
File cellFile = new File(reserved, cellName);
if (!cellFile.exists()) {
return null;
}
try (InputStream stream = new FileInputStream(cellFile)) {
return new Reservation(new String(ByteStreams.toByteArray(stream), "UTF-8"));
} catch (IOException e) {
throw new IllegalStateException("Unable to get current user for cell " + cellName, e);
}
}
/**
* Reserves a cell for the specified user and their public access key.
*
* @param userName user name
* @param sshKey user ssh public key
* @param minutes optional number of minutes for reservation
* @param cellSpec optional cell specification string
* @return reserved cell definition
*/
synchronized String borrowCell(String userName, String sshKey, int minutes,
String cellSpec) {
checkNotNull(userName, USER_NOT_NULL);
checkArgument(userName.matches("[\\w.-]+"), "Invalid user name %s", userName);
checkNotNull(sshKey, KEY_NOT_NULL);
checkArgument(minutes < MAX_MINUTES, "Number of minutes must be less than %d", MAX_MINUTES);
checkArgument(minutes >= 0, "Number of minutes must be non-negative");
checkArgument(cellSpec == null || cellSpec.matches("[\\d]+\\+[0-1]"),
"Invalid cell spec string %s", cellSpec);
Reservation reservation = currentUserReservation(userName);
if (reservation == null) {
// If there is no reservation for the user, create one
String cellName = findAvailableCell();
reservation = new Reservation(cellName, userName, System.currentTimeMillis(),
minutes == 0 ? DEFAULT_MINUTES : minutes,
cellSpec == null ? DEFAULT_SPEC : cellSpec);
} else if (minutes == 0) {
// If minutes are 0, simply return the cell definition
return getCellDefinition(reservation.cellName);
} else {
// If minutes are > 0, update the existing cell reservation
reservation = new Reservation(reservation.cellName, userName,
System.currentTimeMillis(), minutes,
reservation.cellSpec);
}
reserveCell(reservation);
createCell(reservation, sshKey);
log(userName, reservation.cellName, reservation.cellSpec,
"borrowed for " + reservation.duration + " minutes");
return getCellDefinition(reservation.cellName);
}
/**
* Returns name of an available cell. Cell is chosen based on the load
* of its hosting server; a random one will be chosen from the set of
* cells hosted by the least loaded server.
*
* @return name of an available cell
*/
private String findAvailableCell() {
Set<String> cells = getAvailableCells();
checkState(!cells.isEmpty(), "No cells are presently available");
Map<String, ServerInfo> load = Maps.newHashMap();
cells.stream().map(this::getCellInfo)
.forEach(info -> load.compute(info.hostName, (k, v) -> v == null ?
new ServerInfo(info.hostName) : v.bumpLoad(info)));
List<ServerInfo> servers = new ArrayList<>(load.values());
servers.sort((a, b) -> b.load - a.load);
ServerInfo server = servers.get(0);
return server.cells.get(random.nextInt(server.cells.size())).cellName;
}
/**
* Returns the specified cell for the specified user and their public access key.
*
* @param userName user name
*/
synchronized void returnCell(String userName) {
checkNotNull(userName, USER_NOT_NULL);
Reservation reservation = currentUserReservation(userName);
checkState(reservation != null, "User %s has no cell reservations", userName);
unreserveCell(reservation);
destroyCell(reservation);
log(userName, reservation.cellName, reservation.cellSpec, "returned");
}
/**
* Reserves the specified cell for the user the source file and writes the
* specified content to the target file.
*
* @param reservation cell reservation record
*/
private void reserveCell(Reservation reservation) {
File cellFile = new File(reserved, reservation.cellName);
try (FileOutputStream stream = new FileOutputStream(cellFile)) {
stream.write(reservation.encode().getBytes(UTF_8));
} catch (IOException e) {
throw new IllegalStateException("Unable to reserve cell " + reservation.cellName, e);
}
}
/**
* Returns the cell definition of the specified cell.
*
* @param cellName cell name
* @return cell definition
*/
private String getCellDefinition(String cellName) {
CellInfo cellInfo = getCellInfo(cellName);
return exec(String.format("ssh %s warden/bin/cell-def %s",
cellInfo.hostName, cellInfo.cellName));
}
/**
* Cancels the specified reservation.
*
* @param reservation reservation record
*/
private void unreserveCell(Reservation reservation) {
checkState(new File(reserved, reservation.cellName).delete(),
"Unable to return cell %s", reservation.cellName);
}
/**
* Creates the cell for the specified user SSH key.
*
* @param reservation cell reservation
* @param sshKey ssh key
*/
private void createCell(Reservation reservation, String sshKey) {
CellInfo cellInfo = getCellInfo(reservation.cellName);
String cmd = String.format("ssh %s warden/bin/create-cell %s %s %s %s",
cellInfo.hostName, cellInfo.cellName,
cellInfo.ipPrefix, reservation.cellSpec, sshKey);
exec(cmd);
}
/**
* Destroys the specified cell.
*
* @param reservation reservation record
*/
private void destroyCell(Reservation reservation) {
CellInfo cellInfo = getCellInfo(reservation.cellName);
exec(String.format("ssh %s warden/bin/destroy-cell %s %s",
cellInfo.hostName, cellInfo.cellName, reservation.cellSpec));
}
/**
* Reads the information about the specified cell.
*
* @param cellName cell name
* @return cell information
*/
private CellInfo getCellInfo(String cellName) {
File cellFile = new File(supported, cellName);
try (InputStream stream = new FileInputStream(cellFile)) {
String[] fields = new String(ByteStreams.toByteArray(stream), UTF_8).split(" ");
return new CellInfo(cellName, fields[0], fields[1]);
} catch (IOException e) {
throw new IllegalStateException("Unable to definition for cell " + cellName, e);
}
}
// Executes the specified command.
private String exec(String command) {
try {
Process process = Runtime.getRuntime().exec(cmdPrefix + command);
String output = new String(ByteStreams.toByteArray(process.getInputStream()), UTF_8);
process.waitFor(TIMEOUT, TimeUnit.SECONDS);
return process.exitValue() == 0 ? output : null;
} catch (Exception e) {
throw new IllegalStateException("Unable to execute " + command);
}
}
// Creates an audit log entry.
private void log(String userName, String cellName, String cellSpec, String action) {
try (FileOutputStream fos = new FileOutputStream(log, true);
PrintWriter pw = new PrintWriter(fos)) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
pw.println(String.format("%s\t%s\t%s-%s\t%s", format.format(new Date()),
userName, cellName, cellSpec, action));
pw.flush();
} catch (IOException e) {
throw new IllegalStateException("Unable to log reservation action", e);
}
}
// Carrier of cell information
private final class CellInfo {
final String cellName;
final String hostName;
final String ipPrefix;
private CellInfo(String cellName, String hostName, String ipPrefix) {
this.cellName = cellName;
this.hostName = hostName;
this.ipPrefix = ipPrefix;
}
}
// Carrier of cell server information
private final class ServerInfo {
final String hostName;
int load = 0;
List<CellInfo> cells = Lists.newArrayList();
private ServerInfo(String hostName) {
this.hostName = hostName;
}
private ServerInfo bumpLoad(CellInfo info) {
cells.add(info);
load++; // TODO: bump by cell size later
return this;
}
}
// Task for re-possessing overdue cells
private final class Reposessor extends TimerTask {
@Override
public void run() {
long now = System.currentTimeMillis();
for (String cellName : getReservedCells()) {
Reservation reservation = currentCellReservation(cellName);
if (reservation != null &&
(reservation.time + reservation.duration * MINUTE) < now) {
try {
returnCell(reservation.userName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.warden;
import com.google.common.io.ByteStreams;
import org.eclipse.jetty.server.Response;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static com.google.common.base.Strings.isNullOrEmpty;
/**
* Web socket servlet capable of creating web sockets for the STC monitor.
*/
public class WardenServlet extends HttpServlet {
static Warden warden;
private SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain; charset=UTF-8");
try (PrintWriter out = resp.getWriter()) {
if (req.getPathInfo().endsWith("data")) {
String userName = req.getParameter("user");
if (userName != null) {
printUserInfo(out, userName);
} else {
printAvailability(out);
}
} else {
printAvailabilityText(out);
}
} catch (Exception e) {
resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
}
private void printUserInfo(PrintWriter out, String userName) {
Reservation reservation = warden.currentUserReservation(userName);
out.println(getCellStatus(null, reservation));
}
private void printAvailability(PrintWriter out) {
List<String> list = new ArrayList<>(warden.getCells());
list.sort(String::compareTo);
for (String cellName : list) {
Reservation reservation = warden.currentCellReservation(cellName);
out.println(getCellStatus(cellName, reservation));
}
}
private String getCellStatus(String cellName, Reservation reservation) {
if (reservation != null) {
long expiration = reservation.time + reservation.duration * 60_000;
long remaining = (expiration - System.currentTimeMillis()) / 60_000;
return String.format("%s,%s,%s,%s", reservation.cellName,
reservation.cellSpec, reservation.userName, remaining);
} else if (cellName != null) {
return String.format("%s", cellName);
}
return null;
}
private void printAvailabilityText(PrintWriter out) {
List<String> list = new ArrayList<>(warden.getCells());
list.sort(String::compareTo);
for (String cellName : list) {
Reservation reservation = warden.currentCellReservation(cellName);
if (reservation != null) {
long expiration = reservation.time + reservation.duration * 60_000;
long remaining = (expiration - System.currentTimeMillis()) / 60_000;
out.println(String.format("%-14s\t%-10s\t%s\t%s\t%s mins (%s remaining)",
cellName + "-" + reservation.cellSpec,
reservation.userName,
fmt.format(new Date(reservation.time)),
fmt.format(new Date(expiration)),
reservation.duration, remaining));
} else {
out.println(String.format("%-10s\t%-10s", cellName, "available"));
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try (PrintWriter out = resp.getWriter()) {
String sshKey = new String(ByteStreams.toByteArray(req.getInputStream()), "UTF-8");
String userName = req.getParameter("user");
String sd = req.getParameter("duration");
String spec = req.getParameter("spec");
int duration = isNullOrEmpty(sd) ? 0 : Integer.parseInt(sd);
String cellDefinition = warden.borrowCell(userName, sshKey, duration, spec);
out.println(cellDefinition);
} catch (Exception e) {
resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
}
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try (PrintWriter out = resp.getWriter()) {
String userName = req.getParameter("user");
warden.returnCell(userName);
} catch (Exception e) {
resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Cell warden to coordinate borrowing and returning test cells.
*/
package org.onlab.warden;
\ No newline at end of file
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.warden;
import com.google.common.io.Files;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.util.Tools;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import static org.junit.Assert.*;
/**
* Suite of tests for the cell warden.
*/
public class WardenTest {
private Warden warden;
private File cells;
private File supportedCells;
@Before
public void setUp() throws IOException {
// Setup warden to be tested
Warden.root = Files.createTempDir();
Warden.cmdPrefix = "echo ";
cells = new File(Warden.root, "cells");
supportedCells = new File(cells, "supported");
warden = new Warden();
// Setup test cell information
createCell("alpha", "foo");
createCell("bravo", "foo");
createCell("charlie", "foo");
createCell("delta", "bar");
createCell("echo", "bar");
createCell("foxtrot", "bar");
new File("warden.log").deleteOnExit();
}
private void createCell(String cellName, String hostName) throws IOException {
File cellFile = new File(supportedCells, cellName);
Files.createParentDirs(cellFile);
Files.write((hostName + " " + cellName).getBytes(), cellFile);
}
@After
public void tearDown() throws IOException {
Tools.removeDirectory(Warden.root);
}
@Test
public void basics() {
assertEquals("incorrect number of cells", 6, warden.getCells().size());
validateSizes(6, 0);
String cellDefinition = warden.borrowCell("dude", "the-key", 0, null);
assertTrue("incorrect definition", cellDefinition.contains("cell-def"));
validateSizes(5, 1);
Reservation dudeCell = warden.currentUserReservation("dude");
validateCellState(dudeCell);
warden.borrowCell("dolt", "a-key", 0, "4+1");
Reservation doltCell = warden.currentUserReservation("dolt");
validateCellState(doltCell);
validateSizes(4, 2);
assertFalse("cells should not be on the same host",
Objects.equals(warden.getCellHost(dudeCell.cellName),
warden.getCellHost(doltCell.cellName)));
warden.returnCell("dude");
validateSizes(5, 1);
warden.borrowCell("dolt", "a-key", 30, null);
validateSizes(5, 1);
warden.returnCell("dolt");
validateSizes(6, 0);
}
private void validateSizes(int available, int reserved) {
assertEquals("incorrect number of available cells", available,
warden.getAvailableCells().size());
assertEquals("incorrect number of reserved cells", reserved,
warden.getReservedCells().size());
}
private void validateCellState(Reservation reservation) {
assertFalse("cell should not be available",
warden.getAvailableCells().contains(reservation.cellName));
assertTrue("cell should be reserved",
warden.getReservedCells().contains(reservation.cellName));
}
}
\ No newline at end of file
#! /bin/bash
# -----------------------------------------------------------------------------
# init.d script to run ON.Lab test cell warden
## -----------------------------------------------------------------------------
### BEGIN INIT INFO
# Provides: warden
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ON.Lab Test Cell Warden
# Description: Warden is a broker for sharing test cell infrastructure among ON.Lab developers.
### END INIT INFO
WARDEN_USER="sdn"
WARDEN_HOME="/home/$WARDEN_USER/warden"
WARDEN_VERSION="1.6.0-SNAPSHOT"
DEBUG="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
cd $WARDEN_HOME
start () {
# Start warden if it's not already running
if ! status >/dev/null; then
echo "Starting Warden"
startwarden
else
echo "Warden is already running"
fi
}
startwarden () {
start-stop-daemon --signal INT --start --chuid $WARDEN_USER \
--pidfile $WARDEN_HOME/warden.pid --make-pidfile \
--background --chdir $WARDEN_HOME \
--exec /usr/bin/java -- -jar onlab-warden-$WARDEN_VERSION.jar \
&>$WARDEN_HOME/std.log
}
stop () {
if status >/dev/null; then
echo "Stopping Warden"
start-stop-daemon --signal INT --stop --chuid $WARDEN_USER \
--pidfile $WARDEN_HOME/warden.pid
rm warden.pid
else
echo "Warden is not running"
fi
}
restart () {
stop
start
}
status () {
start-stop-daemon --signal INT --status --chuid $WARDEN_USER \
--pidfile $WARDEN_HOME/warden.pid
}
case $1 in
start)
start
;;
stop | force-stop)
stop
;;
restart)
shift
restart "$@"
;;
status)
status && echo "Warden is running" || echo "Warden is stopped"
exit $?
;;
*)
echo "Usage: $0 {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0