Some progress on service configuration

This commit is contained in:
Ziver Koc 2015-06-03 15:08:41 +00:00
parent 29def452b6
commit ab7296198a
10 changed files with 138 additions and 84 deletions

View file

@ -108,8 +108,10 @@ public class WAAbstractPage implements HttpPage{
Templator content = null;
if(page != null)
content = page.htmlResponse(context, client_info, session, cookie, request);
if(content != null)
if(content != null) {
tmpl.set("nav", context.getBreadcrumb().get(1));
tmpl.set("content", content.compile());
}
out.print(tmpl.compile());
}

View file

@ -29,6 +29,9 @@ public class WebAdminServer {
HttpServer http = new HttpServer(80);
http.setPage("/", new WAAbstractPage(pluginManager));
//http.setPage("status", new StatusPage(pluginManager));
//http.setPage("service", new ServicePage(pluginManager));
//http.setPage("servicestatus", new ServiceStatusPage(pluginManager));
http.setDefaultPage(new HttpFilePage(FileUtil.find("WebContent/")));
http.start();

View file

@ -25,6 +25,7 @@ package wa.server.page;
import wa.server.WAContext;
import wa.server.page.struct.WANavigation;
import wa.server.plugin.WAService;
import wa.server.plugin.WAServiceStatus;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeaderParser;
@ -43,16 +44,21 @@ import java.util.logging.Logger;
*/
public class ServicePage implements WAPage {
private static final Logger log = LogUtil.getLogger();
public static final String NAVIGATION_NAME = "Services";
private static final String TMPL_FILE = "WebContent/page/ServicePage.tmpl";
private ArrayList<WAService> plugins;
private ArrayList<WAService> services;
private ArrayList<ServiceStatusPage> statuses;
public ServicePage(PluginManager pluginManager){
this.plugins = pluginManager.toArray(WAService.class);
this.services = pluginManager.toArray(WAService.class);
this.statuses = new ArrayList<>();
WANavigation nav = new WANavigation("Services");
for(WAService plugin : plugins)
WANavigation nav = new WANavigation(NAVIGATION_NAME, this);
for(WAService plugin : services) {
nav.addSubNav(new WANavigation(plugin.getName()));
statuses.add(new ServiceStatusPage(plugin.getStatus()));
}
WANavigation.addRootNav(nav);
}
@ -65,13 +71,17 @@ public class ServicePage implements WAPage {
Map<String, String> request) {
try {
WAService obj = getPlugin(context);
int index = services.indexOf(context.getBreadcrumb().get(1).getResource());
if(index < 0)
return null;
WAService obj = services.get(index);
ServiceStatusPage statusPage = statuses.get(index);
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
tmpl.set("service_status",
statusPage.htmlResponse(context, client_info, session, cookie, request).compile());
return tmpl;
if (obj != null) {
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
tmpl.set("nav", context.getBreadcrumb().get(1));
return tmpl;
}
}catch (IOException e){
log.log(Level.SEVERE, null, e);
}
@ -88,13 +98,4 @@ public class ServicePage implements WAPage {
}
private WAService getPlugin(WAContext context){
if(context.getBreadcrumb().size() >= 2){
int i = plugins.indexOf(context.getBreadcrumb().get(1).getResource());
if(i >= 0)
return plugins.get(i);
}
return null;
}
}

View file

@ -24,7 +24,7 @@ package wa.server.page;
import wa.server.WAContext;
import wa.server.page.struct.WANavigation;
import wa.server.plugin.WAService;
import wa.server.plugin.WAServiceStatus;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeaderParser;
@ -32,6 +32,7 @@ import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.plugin.PluginManager;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
@ -43,17 +44,21 @@ import java.util.logging.Logger;
*/
public class ServiceStatusPage implements WAPage {
private static final Logger log = LogUtil.getLogger();
public static final String NAVIGATION_NAME = "Service Status";
private static final String TMPL_FILE = "WebContent/page/ServiceStatusPage.tmpl";
private ArrayList<WAService> plugins;
private ArrayList<WAServiceStatus> services;
public ServiceStatusPage(WAServiceStatus ss){
services = new ArrayList<>();
services.add(ss);
}
public ServiceStatusPage(PluginManager pluginManager){
this.plugins = pluginManager.toArray(WAService.class);
this.services = pluginManager.toArray(WAServiceStatus.class);
WANavigation nav = new WANavigation("Services");
for(WAService plugin : plugins)
nav.addSubNav(new WANavigation(plugin.getName()));
WANavigation.addRootNav(nav);
WANavigation.getRootNav(ServicePage.NAVIGATION_NAME).addSubNav(
new WANavigation(NAVIGATION_NAME, this));
}
@ -65,13 +70,9 @@ public class ServiceStatusPage implements WAPage {
Map<String, String> request) {
try {
WAService obj = getPlugin(context);
if (obj != null) {
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
tmpl.set("nav", context.getBreadcrumb().get(1));
return tmpl;
}
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
tmpl.set("services", services);
return tmpl;
}catch (IOException e){
log.log(Level.SEVERE, null, e);
}
@ -83,18 +84,23 @@ public class ServiceStatusPage implements WAPage {
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request){
return null;
}
private WAService getPlugin(WAContext context){
if(context.getBreadcrumb().size() >= 2){
int i = plugins.indexOf(context.getBreadcrumb().get(1).getResource());
if(i >= 0)
return plugins.get(i);
DataNode root = new DataNode(DataNode.DataType.Map);
if (request.containsKey("service_id") && request.containsKey("service_action")) {
int index = Integer.parseInt(request.get("ServiceId"));
if(0 > index || index > services.size()) {
root.set("error", "Invalid service_id");
}
else {
WAServiceStatus service = services.get(index);
switch (request.get("ServiceAction")) {
case "start": service.start(); break;
case "stop": service.stop(); break;
default:
root.set("error", "Unknown action");
}
}
}
return null;
return root;
}
}

View file

@ -38,12 +38,13 @@ import java.util.Map;
* Created by Ziver on 2015-04-06.
*/
public class StatusPage implements WAPage {
public static final String NAVIGATION_NAME = "Status";
private ArrayList<WAStatus> plugins;
public StatusPage(PluginManager pluginManager){
this.plugins = pluginManager.toArray(WAStatus.class);
WANavigation nav = new WANavigation("Status", this);
WANavigation nav = new WANavigation(NAVIGATION_NAME, this);
for(WAStatus plugin : plugins)
nav.addSubNav(new WANavigation(plugin.getName(), plugin));
WANavigation.addRootNav(nav);
@ -60,9 +61,7 @@ public class StatusPage implements WAPage {
WAStatus obj = getPlugin(context);
if(obj != null) {
Templator tmpl = new Templator(obj.html());
tmpl.set("nav", context.getBreadcrumb().get(1));
return tmpl;
return new Templator(obj.html());
}
return null;
}

View file

@ -68,6 +68,9 @@ public class WANavigation {
return null;
}
public String getName(){
return name;
}
public Object getResource(){
return resource;
}
@ -93,6 +96,13 @@ public class WANavigation {
public static List<WANavigation> getRootNav(){
return root_nav;
}
public static WANavigation getRootNav(String name){
for(WANavigation nav : root_nav){
if(nav.getName().equals(name))
return nav;
}
return null;
}
public static List<WANavigation> getNavResource(Map<String, String> request) {
LinkedList list = new LinkedList();

View file

@ -22,15 +22,34 @@
package wa.server.plugin;
public interface WAServiceStatus {
public abstract class WAServiceStatus {
public enum ServiceStatusType{
RUNNING,
NOT_RESPONDING,
UNRESPONSIVE,
STOPPED,
UNKNOWN
}
};
public void start();
public void stop();
public ServiceStatusType getStatus();
public boolean isRunning(){
return getStatus() == ServiceStatusType.RUNNING;
}
public boolean isUnresponsive(){
return getStatus() == ServiceStatusType.UNRESPONSIVE;
}
public boolean isStopped(){
return getStatus() == ServiceStatusType.STOPPED;
}
public boolean isUnknown(){
return getStatus() == ServiceStatusType.UNKNOWN;
}
public abstract String getName();
public abstract void start();
public abstract void stop();
public abstract ServiceStatusType getStatus();
}

View file

@ -34,13 +34,14 @@ import java.util.logging.Logger;
*/
public class ApacheService implements WAService {
private static Logger log = LogUtil.getLogger();
public static final String SERVICE_NAME = "Apache2";
private ApacheStatus status;
private ApacheInstaller installer;
@Override
public String getName() {
return "Apache2";
return SERVICE_NAME;
}
@Override

View file

@ -33,11 +33,18 @@ import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ApacheStatus implements WAServiceStatus {
public class ApacheStatus extends WAServiceStatus {
private static final Logger log = LogUtil.getLogger();
private static OSAbstractionLayer os = OSAbstractionLayer.getInstance();
private static final String PID_FILE = "/var/run/apache2.pid";
private static OSAbstractionLayer os = OSAbstractionLayer.getInstance();
@Override
public String getName() {
return ApacheService.SERVICE_NAME;
}
@Override
public void start() {
os.runCommand("service apache2 start");

View file

@ -60,35 +60,41 @@ public class HDDStatus implements WAStatus {
@Override
public void jsonUpdate(Map<String, String> request, DataNode root) {
DataNode hdd_root = new DataNode(DataNode.DataType.List);
root.set("hdd", hdd_root);
Sigar sigar = new Shell().getSigar();
try {
FileSystem[] hdds = sigar.getFileSystemList();
for (FileSystem hdd : hdds) {
if (hdd.getType() != FileSystem.TYPE_LOCAL_DISK)
continue;
DataNode node = new DataNode(DataNode.DataType.Map);
if (request.containsKey("hdd")) {
DataNode hdd_root = new DataNode(DataNode.DataType.List);
root.set("hdd", hdd_root);
Sigar sigar = new Shell().getSigar();
try {
FileSystem[] hdds = sigar.getFileSystemList();
for (FileSystem hdd : hdds) {
if (hdd.getType() != FileSystem.TYPE_LOCAL_DISK)
continue;
DataNode node = new DataNode(DataNode.DataType.Map);
String device = new String(hdd.getDevName().getBytes(), "UTF-8");
node.set("device", device);
node.set("filesystem", hdd.getSysTypeName());
node.set("mount", hdd.getDirName());
String device = new String(hdd.getDevName().getBytes(), "UTF-8");
node.set("device", device);
node.set("filesystem", hdd.getSysTypeName());
node.set("mount", hdd.getDirName());
if(idMap.containsKey(device))
node.set("id", idMap.get(device));
else{
idMap.put(device, nextId);
node.set("id", nextId++);
if (idMap.containsKey(device))
node.set("id", idMap.get(device));
else {
idMap.put(device, nextId);
node.set("id", nextId++);
}
FileSystemUsage hdd_use = sigar.getFileSystemUsage(hdd.getDirName());
node.set("size_total", hdd_use.getTotal() * 1000);
node.set("size_used", (hdd_use.getTotal() - hdd_use.getFree()) * 1000);
node.set("size_free", hdd_use.getFree() * 1000);
hdd_root.add(node);
}
FileSystemUsage hdd_use = sigar.getFileSystemUsage(hdd.getDirName());
node.set("size_total", hdd_use.getTotal() * 1000);
node.set("size_used", (hdd_use.getTotal() - hdd_use.getFree())*1000);
node.set("size_free", hdd_use.getFree() * 1000);
hdd_root.add(node);
} catch (SigarException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}catch(SigarException e){} catch (UnsupportedEncodingException e) {}
}
}
}