Initial commit

This commit is contained in:
Ziver Koc 2015-04-09 21:22:47 +00:00
commit aa4e110832
69 changed files with 17898 additions and 0 deletions

View file

@ -0,0 +1,36 @@
package wa.server;
import java.io.File;
import zutil.osal.OSAbstractionLayer;
import zutil.osal.OSAbstractionLayer.OSType;
public class WAConstants {
public static final String DB_TABLE_PREFIX = "wa";
public static String WA_BASE_CONFIG_PATH;
public static final String WA_BASE_CONFIG_PATH_LINUX = "/etc/webadmin";
public static final String WA_BASE_CONFIG_PATH_WINDOWS = ".";
public static final String WA_SSL_CERT = "cert/server.crt";
public static final String WA_SSL_KEY = "cert/server.key";
public static final String WA_CONFIG_BOUNDARY = "---- WebAdmin Configuration ----";
static{
OSAbstractionLayer os = OSAbstractionLayer.getInstance();
if(os.getOSType() == OSType.Linux){
WA_BASE_CONFIG_PATH = WA_BASE_CONFIG_PATH_LINUX;
}
else if(os.getOSType() == OSType.Windows){
WA_BASE_CONFIG_PATH = WA_BASE_CONFIG_PATH_WINDOWS;
}
}
public static File getConfigFile(String name){
return new File(WA_BASE_CONFIG_PATH, name);
}
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server;
import wa.server.page.struct.WAAlert;
import wa.server.page.struct.WANavigation;
import java.util.ArrayList;
/**
* Created by Ziver on 2015-04-06.
*/
public class WAContext {
private WANavigation[] nav;
private ArrayList<WAAlert> alerts;
public WAContext(){
nav = new WANavigation[]{
new WANavigation("Status"),
new WANavigation("Services", new WANavigation[]{
new WANavigation("Apache"),
new WANavigation("Tomcat"),
new WANavigation("Samba")
}),
new WANavigation("Configure")
};
alerts = new ArrayList<WAAlert>();
}
public WANavigation[] getNavigation(){
return nav;
}
public ArrayList<WAAlert> getAlerts() {
return alerts;
}
}

View file

@ -0,0 +1,40 @@
package wa.server;
import wa.server.page.AbstractPage;
import wa.server.page.StatusPage;
import zutil.io.file.FileUtil;
import zutil.log.CompactLogFormatter;
import zutil.log.LogUtil;
import zutil.net.http.HttpServer;
import zutil.net.http.pages.HttpFilePage;
import zutil.plugin.PluginManager;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WebAdminServer {
private static Logger log = LogUtil.getLogger();
private PluginManager pluginManager;
public static void main(String[] args){
LogUtil.setGlobalLevel(Level.FINEST);
LogUtil.setGlobalFormatter(new CompactLogFormatter());
new WebAdminServer();
}
public WebAdminServer(){
try {
pluginManager = new PluginManager();
HttpServer http = new HttpServer(80);
http.setPage("/", new StatusPage(pluginManager));
http.setDefaultPage(new HttpFilePage(FileUtil.find("WebContent/")));
http.start();
}catch(Exception e){
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,110 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.page;
import wa.server.WAContext;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream;
import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.parser.json.JSONWriter;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by Ziver on 2015-04-02.
*/
public abstract class AbstractPage implements HttpPage{
private static final Logger log = LogUtil.getLogger();
private static final String TMPL_FILE = "WebContent/index.tmpl";
private Templator tmpl;
public AbstractPage() {
try {
tmpl = new Templator(FileUtil.find(TMPL_FILE));
} catch(IOException e){
log.log(Level.SEVERE, null, e);
tmpl = new Templator(e.getMessage());
}
}
@Override
public final synchronized void respond(HttpPrintStream out,
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
WAContext context = (WAContext)session.get("context");
if(context == null){
context = new WAContext();
}
if(("application/json").equals(client_info.getHeader("ContentType"))){
DataNode node = jsonResponse(context, client_info, session, cookie, request);
if(node != null) {
out.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(out);
writer.write(node);
writer.close();
}
}
else {
tmpl.clear();
tmpl.set("title", "WebAdmin");
tmpl.set("top-nav", context.getNavigation());
tmpl.set("side-nav-show", true);
tmpl.set("side-nav", context.getNavigation()[1].getSubNav());
tmpl.set("alerts", context.getAlerts());
//tmpl.set("footer", null);
Templator content = htmlResponse(context, client_info, session, cookie, request);
if(content != null)
tmpl.set("content", content.compile());
out.print(tmpl.compile());
}
}
public abstract Templator htmlResponse(WAContext context,
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request);
public DataNode jsonResponse(WAContext context,
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request){
return null;
}
}

View file

@ -0,0 +1,81 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.page;
import wa.server.WAContext;
import wa.server.plugin.WAStatus;
import zutil.net.http.HttpHeaderParser;
import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.plugin.PluginManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
/**
* Created by Ziver on 2015-04-06.
*/
public class StatusPage extends AbstractPage{
private ArrayList<WAStatus> plugins;
public StatusPage(PluginManager pluginManager){
this.plugins = pluginManager.toArray(WAStatus.class);
}
@Override
public Templator htmlResponse(WAContext context,
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
if(request.containsKey("i")) {
WAStatus obj = getPlugin(Integer.parseInt(request.get("i")));
if(obj != null)
return new Templator(obj.html());
}
return null;
}
public DataNode jsonResponse(WAContext context,
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request){
if(request.containsKey("i")) {
WAStatus obj = getPlugin(Integer.parseInt(request.get("i")));
DataNode root = new DataNode(DataNode.DataType.Map);
obj.jsonUpdate(root);
return root;
}
return null;
}
private WAStatus getPlugin(int i){
if(0 >= i && i < plugins.size())
return plugins.get(i);
return null;
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.page.struct;
import java.sql.Struct;
/**
* Created by Ziver on 2015-04-03.
*/
public class WAAlert {
public enum AlertType{
DANGER, WARNING, INFO, SUCCESS;
public String toString(){
return super.toString().toLowerCase();
}
}
private String message;
private AlertType type;
public WAAlert(String message, AlertType type) {
this.message = message;
this.type = type;
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.page.struct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by Ziver on 2015-04-02.
*/
public class WANavigation {
private String name;
private List<WANavigation> sub_nav;
public WANavigation(String name) {
this.name = name;
}
public WANavigation(String name, WANavigation[] sub_nav) {
this.name = name;
this.sub_nav = Arrays.asList(sub_nav);
}
public Object getSubNav() {
return sub_nav;
}
}

View file

@ -0,0 +1,11 @@
package wa.server.plugin;
import java.io.IOException;
import java.sql.SQLException;
import zutil.db.DBConnection;
public interface WAConfigurator {
public void read(DBConnection db) throws SQLException;
public void save(DBConnection db) throws IOException;
}

View file

@ -0,0 +1,5 @@
package wa.server.plugin;
public interface WAFrontend {
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2014 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.plugin;
/**
* Created by Ziver on 2014-11-09.
*/
public interface WAInstaller {
public void install();
public void uninstall();
}

View file

@ -0,0 +1,14 @@
package wa.server.plugin;
public interface WAService {
public enum WAServiceStatus{
RUNNING,
NOT_RESPONDING,
UNAVAILABLE,
UNKNOWN
}
public void start();
public void stop();
public WAServiceStatus getStatus();
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.plugin;
import zutil.parser.DataNode;
/**
* Created by Ziver on 2015-04-06.
*/
public interface WAStatus {
public String getName();
public String html();
public void jsonUpdate(DataNode root);
}

View file

@ -0,0 +1,40 @@
package wa.server.plugin.apache;
import wa.server.WAConstants;
import zutil.db.bean.DBBean;
import zutil.db.bean.DBBean.DBTable;;
@DBTable(WAConstants.DB_TABLE_PREFIX+"_apache_vhost")
public class ApacheConfigVirtualHost extends DBBean{
protected String domain;
protected String path;
protected boolean ssl;
protected boolean tomcat;
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isSSL() {
return ssl;
}
public void setSSL(boolean ssl) {
this.ssl = ssl;
}
public boolean isTomcatApp() {
return tomcat;
}
public void setTomcatApp(boolean tomcat) {
this.tomcat = tomcat;
}
}

View file

@ -0,0 +1,104 @@
package wa.server.plugin.apache;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import wa.server.WAConstants;
import wa.server.plugin.WAConfigurator;
import wa.server.util.ConfigFileUtil;
import zutil.db.DBConnection;
import zutil.io.file.FileUtil;
public class ApacheConfigurator implements WAConfigurator {
private static final String APACHE_CONF_FILE = "wa_apache_vhost.conf";
private static final String APACHE_MAIN_CONFIG_FILE = "/etc/apache2/apache2.conf";
private static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config";
// Configuration data
List<ApacheConfigVirtualHost> vhosts;
public ApacheConfigurator(){
vhosts = new LinkedList<ApacheConfigVirtualHost>();
}
@Override
public void read(DBConnection db) throws SQLException {
vhosts = ApacheConfigVirtualHost.load(db, ApacheConfigVirtualHost.class);
}
@Override
public void save(DBConnection db) throws IOException {
File file = WAConstants.getConfigFile(APACHE_CONF_FILE);
// Update Man configuration file
ConfigFileUtil.writeBetweenBoundary(
new File(APACHE_MAIN_CONFIG_FILE),
"#",
"Include "+file.getAbsolutePath());
// Write Vhost configuration
PrintStream out = new PrintStream(file);
out.println(FileUtil.getContent(new File(STATIC_PRE_CONF)));
out.println("######################################");
out.println("# vhost.php");
for(ApacheConfigVirtualHost vhost : vhosts){
if(vhost.isTomcatApp())
writeTomcatVhost(out, vhost);
else if(vhost.isSSL())
writeSSLVhost(out, vhost);
else
writeVhost(out, vhost);
}
out.close();
}
private void writeVhost(PrintStream out, ApacheConfigVirtualHost conf) throws IOException{
out.println("<VirtualHost *:80>");
out.println(" ServerName "+conf.getDomain()+":80");
out.println(" DocumentRoot "+conf.getPath());
out.println("</VirtualHost>");
out.println("");
}
private void writeSSLVhost(PrintStream out, ApacheConfigVirtualHost conf) throws IOException{
out.println("<VirtualHost *:80>");
out.println(" ServerName "+conf.getDomain()+":80");
out.println(" RewriteEngine On");
out.println(" RewriteCond %{SERVER_PORT} !^443$");
out.println(" RewriteRule ^(.*)$ https://server$1 [L,R]");
out.println("</VirtualHost>");
out.println("<VirtualHost *:443>");
out.println(" ServerName "+conf.getDomain()+":443");
out.println(" DocumentRoot "+conf.getPath());
out.println("");
out.println(" SSLEngine on");
out.println(" SSLCertificateFile "+WAConstants.getConfigFile(WAConstants.WA_SSL_CERT));
out.println(" SSLCertificateKeyFile "+WAConstants.getConfigFile(WAConstants.WA_SSL_KEY));
out.println("</VirtualHost>");
out.println("");
}
private void writeTomcatVhost(PrintStream out, ApacheConfigVirtualHost conf) throws IOException{
out.println("<VirtualHost *:80>");
out.println(" ServerName "+conf.getDomain()+":80");
out.println(" ");
out.println(" RewriteEngine On");
out.println(" RewriteRule ^/$ /"+conf.getPath()+" [R]");
out.println(" ProxyPreserveHost on");
out.println(" <Proxy *>");
out.println(" Order deny,allow");
out.println(" Allow from all");
out.println(" </Proxy>");
out.println(" ProxyPass / ajp://localhost:8009/");
out.println(" ProxyPassReverse / http://localhost:8080/");
out.println("</VirtualHost>");
out.println("");
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2014 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.plugin.apache;
import wa.server.plugin.WAInstaller;
import wa.server.util.AptGet;
import zutil.osal.OSAbstractionLayer;
/**
* Created by Ziver on 2014-11-09.
*/
public class ApacheInstaller implements WAInstaller {
@Override
public void install() {
AptGet.install("apache php5 php5-mcrypt php5-gd imagemagick");
}
@Override
public void uninstall() {
AptGet.purge("apache php5 php5-mcrypt php5-gd imagemagick");
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2014 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.plugin.apache;
import wa.server.plugin.WAService;
import wa.server.util.Ps;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.osal.OSAbstractionLayer;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by Ziver on 2014-12-23.
*/
public class ApacheService implements WAService {
private static Logger log = LogUtil.getLogger();
private static OSAbstractionLayer os = OSAbstractionLayer.getInstance();
private static final String PID_FILE = "/var/run/apache2.pid";
@Override
public void start() {
os.runCommand("service apache2 start");
}
@Override
public void stop() {
os.runCommand("service apache2 stop");
}
@Override
public WAServiceStatus getStatus() {
try {
int pid = Integer.parseInt(
FileUtil.getContent(new File(PID_FILE)));
if(Ps.isRunning(pid))
return WAServiceStatus.RUNNING;
return WAServiceStatus.UNAVAILABLE;
}catch(IOException e){
log.log(Level.WARNING, null, e);
}
return WAServiceStatus.UNKNOWN;
}
}

View file

@ -0,0 +1,91 @@
###################################
# default.php
ScriptAlias /cgi-bin/ /home/www/cgi-bin/
#NameVirtualHost *:80
NameVirtualHost *:443
ServerName koc.se
ServerAdmin ziver@koc.se
#<VirtualHost *:80>
# Redirect / http://koc.se
#</VirtualHost>
# WebAdmin {
<VirtualHost *:80>
ServerName admin.koc.se:80
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://admin.koc.se$1 [L,R]
</VirtualHost>
<VirtualHost *:443>
ServerName admin.koc.se:443
DocumentRoot ".$config["apache_www_path"]."/admin
SSLEngine on
SSLCertificateFile ".$config["apache_conf_path"]."/cert/server.crt
SSLCertificateKeyFile ".$config["apache_conf_path"]."/cert/server.key
</VirtualHost>
<VirtualHost *:80>
ServerName server:80
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://server$1 [L,R]
</VirtualHost>
<VirtualHost *:443>
ServerName server:443
DocumentRoot ".$config["apache_www_path"]."/admin
SSLEngine on
SSLCertificateFile ".$config["apache_conf_path"]."/cert/server.crt
SSLCertificateKeyFile ".$config["apache_conf_path"]."/cert/server.key
</VirtualHost>
<VirtualHost *:80>
ServerName localhost:80
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://localhost$1 [L,R]
</VirtualHost>
<VirtualHost *:443>
ServerName localhost:443
DocumentRoot ".$config["apache_www_path"]."/admin
SSLEngine on
SSLCertificateFile ".$config["apache_conf_path"]."/cert/server.crt
SSLCertificateKeyFile ".$config["apache_conf_path"]."/cert/server.key
</VirtualHost>
<VirtualHost *:80>
ServerName 192.168.0.2:80
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://192.168.0.2$1 [L,R]
</VirtualHost>
<VirtualHost *:443>
ServerName 192.168.0.2:443
DocumentRoot ".$config["apache_www_path"]."/admin
SSLEngine on
SSLCertificateFile ".$config["apache_conf_path"]."/cert/server.crt
SSLCertificateKeyFile ".$config["apache_conf_path"]."/cert/server.key
</VirtualHost>
#}
# Mythweb
#<VirtualHost *:80>
# ServerName mythtv.koc.se
# DirectoryIndex mythweb
# DocumentRoot /var/www
#
# # Include /etc/apache2/sites-available/mythweb.conf
#</VirtualHost>
# TvHeadend
<VirtualHost *:80>
ServerName tv.koc.se:80
ProxyPreserveHost on
ProxyPass / http://localhost:9981/
ProxyPassReverse / http://localhost:9981/
</VirtualHost>

View file

@ -0,0 +1,9 @@
{
"version": "1.0",
"name": "Apache Web Server",
"interfaces": {
"wa.server.plugin.WAInstaller": "wa.server.plugin.apache.ApacheInstaller",
"wa.server.plugin.WAConfigurator": "wa.server.plugin.apache.ApacheConfigurator",
"wa.server.plugin.WAService": "wa.server.plugin.apache.ApacheService"
}
}

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.plugin.hwstatus;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.cmd.Shell;
import wa.server.plugin.WAStatus;
import zutil.io.file.FileUtil;
import zutil.parser.DataNode;
import java.io.IOException;
/**
* Created by Ziver on 2015-04-07.
*/
public class CpuStatus implements WAStatus {
@Override
public String getName() {
return "Cpu Load";
}
@Override
public String html() {
try {
return FileUtil.getContent(FileUtil.findURL("wa/server/plugin/hwstatus/CpuStatus.tmpl"));
} catch (IOException e) {
return e.getMessage();
}
}
@Override
public void jsonUpdate(DataNode root) {
DataNode cpuNode = new DataNode(DataNode.DataType.List);
try{
Sigar sigar = new Shell().getSigar();
for(CpuInfo cpu_info : sigar.getCpuInfoList()){
for(CpuPerc cpu : sigar.getCpuPercList()){
cpuNode.add(cpu.getCombined());
}
}
} catch (SigarException e) {
e.printStackTrace();
}
root.set("cpu", cpuNode);
}
}

View file

@ -0,0 +1,39 @@
<div class="panel panel-default">
<div class="panel-heading">Cpu Status</div>
<div class="panel-body">
<div id="cpu_chart"></div>
</div>
</div>
<script language="javascript" type="text/javascript">
var cpu_data = [];
function update_cpu(data){
$(data).find("cpu").each(function() {
$(this).find("core").each(function() {
var cpu_nr = parseInt($(this).attr("id"));
if(cpu_data.length < (cpu_nr+1))
cpu_data.push( [] );
cpu_data[cpu_nr].push( [index, $(this).attr("load")] );
if(index>MAX_POINTS)
cpu_data[cpu_nr].shift();
});
});
var datasets = [];
for(i=0; i<cpu_data.length ;i++){
datasets[i] = {
label: "Cpu"+i,
data: cpu_data[i],
lines: { show: true },
points: { show: true }
};
}
var options = {
legend: { position: "ne" },
xaxis: { ticks: [] },
yaxis: { ticks: [0,20,40,60,80,100] }
};
$.plot($("#cpu_chart"), datasets, options);
}
</script>

View file

@ -0,0 +1,10 @@
{
"version": "1.0",
"name": "HW Status",
"interfaces": [
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.CpuStatus"},
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.MemStatus"},
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.HDDStatus"},
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.NetStatus"}
]
}

View file

@ -0,0 +1,54 @@
/*
* Copyright (c) 2014 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.util;
import zutil.log.LogUtil;
import zutil.osal.OSAbstractionLayer;
import java.io.IOException;
import java.util.logging.Logger;
/**
* Created by Ziver on 2014-11-09.
*/
public class AptGet {
public static final Logger log = LogUtil.getLogger();
private static long updateTimestamp;
public static void install(String pkg) {
update();
OSAbstractionLayer.runCommand("apt-get install " + pkg);
}
public static void update(){
// Only run every 5 min
if(updateTimestamp + 1000*60*5 >System.currentTimeMillis()){
OSAbstractionLayer.runCommand("apt-get update");
}
}
public static void purge(String pkg) {
OSAbstractionLayer.runCommand("apt-get --purge remove " + pkg);
}
}

View file

@ -0,0 +1,23 @@
package wa.server.util;
import java.io.File;
import java.io.IOException;
import wa.server.WAConstants;
import zutil.io.file.FileUtil;
public class ConfigFileUtil {
public static void writeBetweenBoundary(File file, String commentChar, String data) throws IOException{
String boundary = new StringBuilder().
append(commentChar).
append(WAConstants.WA_CONFIG_BOUNDARY).
append(commentChar).toString();
String dataWithComment = new StringBuilder().
append(commentChar).append(" This is auto generated configuration please\n").
append(commentChar).append(" do not edit this as it will be overwritten\n").
append(data).toString();
FileUtil.writeBetweenBoundary(file, boundary, dataWithComment);
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2014 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.util;
import zutil.osal.OSAbstractionLayer;
/**
* Created by Ziver on 2014-12-23.
*/
public class Ps {
private static OSAbstractionLayer os = OSAbstractionLayer.getInstance();
public static boolean isRunning(int pid){
String[] output = os.runCommand("ps -p "+pid);
return output.length > 1;
}
}