Airhead 8 лет назад
Родитель
Сommit
390bca4ce2
23 измененных файлов с 1117 добавлено и 25 удалено
  1. 24 0
      hos-agent/.gitignore
  2. BIN
      hos-agent/.mvn/wrapper/maven-wrapper.jar
  3. 1 0
      hos-agent/.mvn/wrapper/maven-wrapper.properties
  4. 233 0
      hos-agent/mvnw
  5. 145 0
      hos-agent/mvnw.cmd
  6. 94 0
      hos-agent/pom.xml
  7. 42 0
      hos-agent/src/main/java/com/yihu/hos/agent/HosAgentApplication.java
  8. 17 0
      hos-agent/src/main/java/com/yihu/hos/agent/camelrouter/AdminProxyRouter.java
  9. 43 0
      hos-agent/src/main/java/com/yihu/hos/agent/configuration/AgentConfiguration.java
  10. 10 0
      hos-agent/src/main/java/com/yihu/hos/agent/constant/FlowConstant.java
  11. 9 0
      hos-agent/src/main/java/com/yihu/hos/agent/constant/MycatConstant.java
  12. 14 0
      hos-agent/src/main/java/com/yihu/hos/agent/constant/ShellConstant.java
  13. 11 0
      hos-agent/src/main/java/com/yihu/hos/agent/controller/ProxyController.java
  14. 75 0
      hos-agent/src/main/java/com/yihu/hos/agent/service/FlowService.java
  15. 60 0
      hos-agent/src/main/java/com/yihu/hos/agent/service/MycatService.java
  16. 76 0
      hos-agent/src/main/java/com/yihu/hos/agent/service/ShellService.java
  17. 128 0
      hos-agent/src/main/java/com/yihu/hos/agent/util/SSHLinuxTool.java
  18. 25 0
      hos-agent/src/main/resources/application.yml
  19. 16 0
      hos-agent/src/test/java/com/yihu/hos/HosAgentApplicationTests.java
  20. 20 1
      src/main/java/com/yihu/hos/ESBApplication.java
  21. 16 0
      src/main/java/com/yihu/hos/config/BeanConfig.java
  22. 28 11
      src/main/java/com/yihu/hos/services/ServiceFlowEventService.java
  23. 30 13
      src/main/java/com/yihu/hos/services/ServiceShellEventService.java

+ 24 - 0
hos-agent/.gitignore

@ -0,0 +1,24 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

BIN
hos-agent/.mvn/wrapper/maven-wrapper.jar


+ 1 - 0
hos-agent/.mvn/wrapper/maven-wrapper.properties

@ -0,0 +1 @@
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip

+ 233 - 0
hos-agent/mvnw

@ -0,0 +1,233 @@
#!/bin/sh
# ----------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Maven2 Start Up Batch script
#
# Required ENV vars:
# ------------------
#   JAVA_HOME - location of a JDK home dir
#
# Optional ENV vars
# -----------------
#   M2_HOME - location of maven2's installed home dir
#   MAVEN_OPTS - parameters passed to the Java VM when running Maven
#     e.g. to debug Maven itself, use
#       set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
#   MAVEN_SKIP_RC - flag to disable loading of mavenrc files
# ----------------------------------------------------------------------------
if [ -z "$MAVEN_SKIP_RC" ] ; then
  if [ -f /etc/mavenrc ] ; then
    . /etc/mavenrc
  fi
  if [ -f "$HOME/.mavenrc" ] ; then
    . "$HOME/.mavenrc"
  fi
fi
# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
mingw=false
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  MINGW*) mingw=true;;
  Darwin*) darwin=true
           #
           # Look for the Apple JDKs first to preserve the existing behaviour, and then look
           # for the new JDKs provided by Oracle.
           #
           if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
           fi
           if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
           fi
           if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then
             #
             # Oracle JDKs
             #
             export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
           fi
           if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=`/usr/libexec/java_home`
           fi
           ;;
esac
if [ -z "$JAVA_HOME" ] ; then
  if [ -r /etc/gentoo-release ] ; then
    JAVA_HOME=`java-config --jre-home`
  fi
fi
if [ -z "$M2_HOME" ] ; then
  ## resolve links - $0 may be a link to maven's home
  PRG="$0"
  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
      PRG="$link"
    else
      PRG="`dirname "$PRG"`/$link"
    fi
  done
  saveddir=`pwd`
  M2_HOME=`dirname "$PRG"`/..
  # make it fully qualified
  M2_HOME=`cd "$M2_HOME" && pwd`
  cd "$saveddir"
  # echo Using m2 at $M2_HOME
fi
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$M2_HOME" ] &&
    M2_HOME=`cygpath --unix "$M2_HOME"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$CLASSPATH" ] &&
    CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi
# For Migwn, ensure paths are in UNIX format before anything is touched
if $mingw ; then
  [ -n "$M2_HOME" ] &&
    M2_HOME="`(cd "$M2_HOME"; pwd)`"
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
  # TODO classpath?
fi
if [ -z "$JAVA_HOME" ]; then
  javaExecutable="`which javac`"
  if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
    # readlink(1) is not available as standard on Solaris 10.
    readLink=`which readlink`
    if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
      if $darwin ; then
        javaHome="`dirname \"$javaExecutable\"`"
        javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
      else
        javaExecutable="`readlink -f \"$javaExecutable\"`"
      fi
      javaHome="`dirname \"$javaExecutable\"`"
      javaHome=`expr "$javaHome" : '\(.*\)/bin'`
      JAVA_HOME="$javaHome"
      export JAVA_HOME
    fi
  fi
fi
if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD="`which java`"
  fi
fi
if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly." >&2
  echo "  We cannot execute $JAVACMD" >&2
  exit 1
fi
if [ -z "$JAVA_HOME" ] ; then
  echo "Warning: JAVA_HOME environment variable is not set."
fi
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  [ -n "$M2_HOME" ] &&
    M2_HOME=`cygpath --path --windows "$M2_HOME"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
  [ -n "$CLASSPATH" ] &&
    CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
fi
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
  local basedir=$(pwd)
  local wdir=$(pwd)
  while [ "$wdir" != '/' ] ; do
    if [ -d "$wdir"/.mvn ] ; then
      basedir=$wdir
      break
    fi
    wdir=$(cd "$wdir/.."; pwd)
  done
  echo "${basedir}"
}
# concatenates all lines of a file
concat_lines() {
  if [ -f "$1" ]; then
    echo "$(tr -s '\n' ' ' < "$1")"
  fi
}
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)}
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
export MAVEN_CMD_LINE_ARGS
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
exec "$JAVACMD" \
  $MAVEN_OPTS \
  -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
  "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
  ${WRAPPER_LAUNCHER} "$@"

+ 145 - 0
hos-agent/mvnw.cmd

@ -0,0 +1,145 @@
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements.  See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership.  The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License.  You may obtain a copy of the License at
@REM
@REM    http://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied.  See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Maven2 Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@REM
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM     e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
@REM ----------------------------------------------------------------------------
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
@echo off
@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on'
@if "%MAVEN_BATCH_ECHO%" == "on"  echo %MAVEN_BATCH_ECHO%
@REM set %HOME% to equivalent of $HOME
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
@REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
:skipRcPre
@setlocal
set ERROR_CODE=0
@REM To isolate internal variables from possible post scripts, we use another setlocal
@setlocal
@REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
echo.
echo Error: JAVA_HOME not found in your environment. >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto init
echo.
echo Error: JAVA_HOME is set to an invalid directory. >&2
echo JAVA_HOME = "%JAVA_HOME%" >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
@REM ==== END VALIDATION ====
:init
set MAVEN_CMD_LINE_ARGS=%*
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
@REM Fallback to current working directory if not found.
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
set EXEC_DIR=%CD%
set WDIR=%EXEC_DIR%
:findBaseDir
IF EXIST "%WDIR%"\.mvn goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set WDIR=%CD%
goto findBaseDir
:baseDirFound
set MAVEN_PROJECTBASEDIR=%WDIR%
cd "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
cd "%EXEC_DIR%"
:endDetectBaseDir
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
:endReadAdditionalConfig
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar""
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS%
if ERRORLEVEL 1 goto error
goto end
:error
set ERROR_CODE=1
:end
@endlocal & set ERROR_CODE=%ERROR_CODE%
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
@REM check for post script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
:skipRcPost
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
if "%MAVEN_BATCH_PAUSE%" == "on" pause
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
exit /B %ERROR_CODE%

+ 94 - 0
hos-agent/pom.xml

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.yihu.hos</groupId>
	<artifactId>hos-agent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>hos-agent</name>
	<description>hos agent</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
        <camel.version>2.18.0</camel.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <dependency>
            <groupId>org.zbus</groupId>
            <artifactId>zbus</artifactId>
            <version>7.2.0</version>
        </dependency>
		<dependency>
			<groupId>com.jcraft</groupId>
			<artifactId>jsch</artifactId>
			<version>0.1.53</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mousio/etcd4j -->
		<dependency>
			<groupId>org.mousio</groupId>
			<artifactId>etcd4j</artifactId>
			<version>2.13.0</version>
		</dependency>
        <dependency>
            <groupId>com.yihu.hos</groupId>
            <artifactId>hos-core</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot</artifactId>
            <version>${camel.version}</version> <!-- use the same version as your Camel core version -->
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jetty</artifactId>
            <version>${camel.version}</version> <!-- use the same version as your Camel core version -->
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>${camel.version}</version> <!-- use the same version as your Camel core version -->
        </dependency>
    </dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

+ 42 - 0
hos-agent/src/main/java/com/yihu/hos/agent/HosAgentApplication.java

@ -0,0 +1,42 @@
package com.yihu.hos.agent;
import com.yihu.hos.agent.service.MycatService;
import com.yihu.hos.agent.service.FlowService;
import com.yihu.hos.agent.service.ShellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HosAgentApplication implements CommandLineRunner {
    private FlowService flowService;
    private ShellService shellService;
    private MycatService mycatService;
    public static void main(String[] args) {
        SpringApplication.run(HosAgentApplication.class, args);
    }
    @Override
    public void run(String... strings) throws Exception {
        flowService.server();
        shellService.server();
        mycatService.server();
    }
    @Autowired
    public void setFlowService(FlowService flowService) {
        this.flowService = flowService;
    }
    @Autowired
    public void setShellService(ShellService shellService) {
        this.shellService = shellService;
    }
    @Autowired
    public void setMycatService(MycatService mycatService) {
        this.mycatService = mycatService;
    }
}

+ 17 - 0
hos-agent/src/main/java/com/yihu/hos/agent/camelrouter/AdminProxyRouter.java

@ -0,0 +1,17 @@
package com.yihu.hos.agent.camelrouter;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
/**
 * @author Airhead
 * @since 2017/1/20.
 */
@Component
public class AdminProxyRouter extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("jetty://0.0.0.0:{{agent.proxy.port}}/esb?matchOnUriPrefix=true")
                .to("jetty:http://127.0.0.57:8080/esb?bridgeEndpoint=true&amp;throwExceptionOnFailure=false");
    }
}

+ 43 - 0
hos-agent/src/main/java/com/yihu/hos/agent/configuration/AgentConfiguration.java

@ -0,0 +1,43 @@
package com.yihu.hos.agent.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zbus.broker.ZbusBroker;
import java.io.IOException;
/**
 * @author Airhead
 * @since 2017/1/19.
 */
@Configuration
public class AgentConfiguration {
    @Value("${agent.zbus.url}")
    private String zbusBrokerURL;
    @Value("${agent.tenant.name}")
    private String tenant;
    private ZbusBroker zbusBroker;
    @Bean
    public ZbusBroker getZbusBroker() {
        try {
            zbusBroker = new ZbusBroker(this.zbusBrokerURL);
            return zbusBroker;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public String getZbusBrokerURL() {
        return zbusBrokerURL;
    }
    public String getTenant() {
        return tenant;
    }
}

+ 10 - 0
hos-agent/src/main/java/com/yihu/hos/agent/constant/FlowConstant.java

@ -0,0 +1,10 @@
package com.yihu.hos.agent.constant;
/**
 * @author Airhead
 * @since 2017/1/20.
 */
public interface FlowConstant {
    String PREFIX = "ssh";
    String ENDPOINT = "service.flow.event:queue:configuration.service.flow";
}

+ 9 - 0
hos-agent/src/main/java/com/yihu/hos/agent/constant/MycatConstant.java

@ -0,0 +1,9 @@
package com.yihu.hos.agent.constant;
/**
 * @author Airhead
 * @since 2017/1/20.
 */
public interface MycatConstant {
    String PREFIX = "mycat";
}

+ 14 - 0
hos-agent/src/main/java/com/yihu/hos/agent/constant/ShellConstant.java

@ -0,0 +1,14 @@
package com.yihu.hos.agent.constant;
/**
 * @author Airhead
 * @since 2017/1/20.
 */
public interface ShellConstant {
    String PREFIX_IN =  "shell_arbiter";
    String PREFIX_OUT = "shell_admin";
    // shell 请求命令,对列名称
    String QUEUE = "configuration.service.shell";
    String ENDPOINT = "service.shell.event:queue:configuration.service.shell";
}

+ 11 - 0
hos-agent/src/main/java/com/yihu/hos/agent/controller/ProxyController.java

@ -0,0 +1,11 @@
package com.yihu.hos.agent.controller;
import org.springframework.stereotype.Controller;
/**
 * @author Airhead
 * @since 2017/1/20.
 */
@Controller
public class ProxyController {
}

+ 75 - 0
hos-agent/src/main/java/com/yihu/hos/agent/service/FlowService.java

@ -0,0 +1,75 @@
package com.yihu.hos.agent.service;
import com.yihu.hos.agent.configuration.AgentConfiguration;
import com.yihu.hos.agent.constant.FlowConstant;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zbus.broker.ZbusBroker;
import org.zbus.mq.Consumer;
import org.zbus.net.http.Message;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
 * @author Airhead
 * @since 2016/12/22.
 */
@Service
public class FlowService {
    private static final Logger logger = LogManager.getLogger(ShellService.class);
    private ZbusBroker zbusBroker;
    private Consumer consumer;
    private AgentConfiguration configuration;
    private CamelContext camelContext;
    @Autowired
    public void setZbusBroker(ZbusBroker zbusBroker) {
        this.zbusBroker = zbusBroker;
    }
    @Autowired
    public void setConfiguration(AgentConfiguration configuration) {
        this.configuration = configuration;
    }
    @Autowired
    public void setCamelContext(CamelContext camelContext) {
        this.camelContext = camelContext;
    }
    public void handle(Message message, Consumer consumer) {
        Map<String, Object> header = new HashMap<>();
        header.put("event", message.getHead("event"));
        ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
        producerTemplate.sendBodyAndHeaders(FlowConstant.ENDPOINT, message.getBodyString(), header);
    }
    public void server() {
        if (zbusBroker == null) {
            logger.error("zbusBroker is null");
            return;
        }
        consumer = new Consumer(zbusBroker, FlowConstant.PREFIX + "@" + configuration.getTenant());
        try {
            consumer.start(this::handle);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }
    @Override
    protected void finalize() throws Throwable {
        consumer.close();
        super.finalize();
    }
}

+ 60 - 0
hos-agent/src/main/java/com/yihu/hos/agent/service/MycatService.java

@ -0,0 +1,60 @@
package com.yihu.hos.agent.service;
import com.yihu.hos.agent.configuration.AgentConfiguration;
import com.yihu.hos.agent.constant.FlowConstant;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zbus.broker.ZbusBroker;
import org.zbus.mq.Consumer;
import org.zbus.net.http.Message;
import java.io.IOException;
/**
 * @author Airhead
 * @since 2017/1/19.
 */
@Service
public class MycatService {
    private static final Logger logger = LogManager.getLogger(ShellService.class);
    private ZbusBroker zbusBroker;
    private Consumer consumer;
    private AgentConfiguration configuration;
    @Autowired
    public void setZbusBroker(ZbusBroker zbusBroker) {
        this.zbusBroker = zbusBroker;
    }
    @Autowired
    public void setConfiguration(AgentConfiguration configuration) {
        this.configuration = configuration;
    }
    @Override
    protected void finalize() throws Throwable {
        consumer.close();
        super.finalize();
    }
    public void server(){
        if (zbusBroker == null) {
            logger.error("zbusBroker is null");
            return;
        }
        consumer = new Consumer(zbusBroker, FlowConstant.PREFIX + "@" + configuration.getTenant());
        try {
            consumer.start(this::handle);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }
    public void handle(Message message, Consumer consumer) {
    }
}

+ 76 - 0
hos-agent/src/main/java/com/yihu/hos/agent/service/ShellService.java

@ -0,0 +1,76 @@
package com.yihu.hos.agent.service;
import com.yihu.hos.agent.configuration.AgentConfiguration;
import com.yihu.hos.agent.constant.ShellConstant;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zbus.broker.ZbusBroker;
import org.zbus.mq.Consumer;
import org.zbus.net.http.Message;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/1/6.
 */
@Service
public class ShellService {
    private static final Logger logger = LogManager.getLogger(ShellService.class);
    private ZbusBroker zbusBroker;
    private Consumer consumer;
    private AgentConfiguration configuration;
    private CamelContext camelContext;
    @Autowired
    public void setZbusBroker(ZbusBroker zbusBroker) {
        this.zbusBroker = zbusBroker;
    }
    @Autowired
    public void setConfiguration(AgentConfiguration configuration) {
        this.configuration = configuration;
    }
    @Autowired
    public void setCamelContext(CamelContext camelContext) {
        this.camelContext = camelContext;
    }
    public void handle(Message message, Consumer consumer) {
        Map<String, Object> header = new HashMap<>();
        header.put("event", message.getHead("event"));
        ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
        producerTemplate.sendBodyAndHeaders(ShellConstant.ENDPOINT, message.getBodyString(), header);
    }
    public void server() {
        if (zbusBroker == null) {
            logger.error("zbusBroker is null");
            return;
        }
        consumer = new Consumer(zbusBroker, ShellConstant.PREFIX_IN + "@" + configuration.getTenant());
        try {
            consumer.start(this::handle);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }
    @Override
    protected void finalize() throws Throwable {
        consumer.close();
        super.finalize();
    }
}

+ 128 - 0
hos-agent/src/main/java/com/yihu/hos/agent/util/SSHLinuxTool.java

@ -0,0 +1,128 @@
package com.yihu.hos.agent.util;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * ssh 链接操作测试类
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/1/3.
 */
@Service("SSHLinuxTool")
public class SSHLinuxTool {
    public static final String BEAN_ID = "SSHLinuxTool";
    @Value("${hos.jcraft.host}")
    private String host;
    @Value("${hos.jcraft.port}")
    private Integer port;
    @Value("${hos.jcraft.user}")
    private String user;
    @Value("${hos.jcraft.password}")
    private String password;
    public static Channel channel = null;
    /**
     * //TODO 密钥密码等验证
     * 建立一个shell连接会话
     *
     * @return
     * @throws JSchException
     */
    public Session getsessionConn() throws JSchException {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setConfig("StrictHostKeyChecking", "no");
        //    java.util.Properties config = new java.util.Properties();
        //   config.put("StrictHostKeyChecking", "no");
        session.setTimeout(600000); // 设置timeout时间
        session.setPassword(password);
        session.connect();
        return session;
    }
//    public String exeCommand(Session session, String command, boolean exit) throws JSchException, IOException {
//
//        if (!session.isConnected()) {
//            session = getsessionConn();
//        }
//
//        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
//        InputStream in = channelExec.getInputStream();
//        channelExec.setCommand(command);
//        channelExec.setErrStream(System.err);
//        channelExec.connect();
//        String out = IOUtils.toString(in, "UTF-8");
//        if (exit) {
//            channelExec.disconnect();
//        }
//        return out;
//    }
    /**
     * 利用JSch实现远程主机SHELL命令执行
     */
    public  String sshShell(Session session, String command, boolean exit) throws Exception {
        //如果服务器连接不上,则抛出异常
        if (session == null) {
            throw new Exception("session is null");
        }
        try {
            //创建sftp通信通道
            if (channel == null) {
                channel = session.openChannel("shell");
                channel.connect(3000);
            }
            //获取输入流和输出流
            InputStream instream = channel.getInputStream();
            OutputStream outstream = channel.getOutputStream();
            //发送需要执行的SHELL命令,需要用\n结尾,表示回车
            String shellCommand = command + " \n";
            outstream.write(shellCommand.getBytes());
            outstream.flush();
            //获取命令执行的结果
            Thread.sleep(2000);
            if (instream.available() > 0) {
                byte[] data = new byte[instream.available()];
                int nLen = instream.read(data);
                if (nLen < 0) {
                    throw new Exception("network error.");
                }
                //转换输出结果并打印出来
                String temp = new String(data, 0, nLen, "iso8859-1");
                System.out.println(temp);
                return temp;
            }
            outstream.close();
            instream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (exit) {
                channel.disconnect();
                session.disconnect();
            }
        }
        return null;
    }
}

+ 25 - 0
hos-agent/src/main/resources/application.yml

@ -0,0 +1,25 @@
spring:
  application:
    name: HosAgent
server:
  port: 9080
security:
  basic:
    enabled: false
# the name of Camel
camel:
  springboot:
    name: HosAgent
---
spring:
  profiles: lfq
agent:
  zbus:
    url: 127.0.0.1:15555
  agent:
    name: jzkl
  proxy:
    port: 9090

+ 16 - 0
hos-agent/src/test/java/com/yihu/hos/HosAgentApplicationTests.java

@ -0,0 +1,16 @@
package com.yihu.hos;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HosAgentApplicationTests {
	@Test
	public void contextLoads() {
	}
}

+ 20 - 1
src/main/java/com/yihu/hos/ESBApplication.java

@ -1,25 +1,35 @@
package com.yihu.hos;
import com.yihu.hos.config.BeanConfig;
import com.yihu.hos.interceptor.WebMvcInterceptor;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import com.yihu.hos.web.framework.util.GridFSUtil;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.zbus.mq.server.MqServer;
import org.zbus.mq.server.MqServerConfig;
import javax.jms.Queue;
@SpringBootApplication
public class ESBApplication extends WebMvcConfigurerAdapter {
public class ESBApplication extends WebMvcConfigurerAdapter  implements CommandLineRunner {
    private BeanConfig configuration;
    @Autowired
    private GridFsOperations operations;
    @Autowired
    public void setConfiguration(BeanConfig configuration) {
        this.configuration = configuration;
    }
    public static void main(String[] args) throws Exception {
        SpringApplication application = new SpringApplication(ESBApplication.class);
        application.run(args);
@ -41,4 +51,13 @@ public class ESBApplication extends WebMvcConfigurerAdapter {
    public Queue shellQueue() {
        return new ActiveMQQueue(ServiceFlowConstant.SHELL_EVENT_QUEUE);
    }
    @Override
    public void run(String... strings) throws Exception {
        MqServerConfig config = new MqServerConfig();
        config.serverPort = configuration.getZbusPort();
        config.storePath = configuration.getZbusStore();
        final MqServer server = new MqServer(config);
        server.start();
    }
}

+ 16 - 0
src/main/java/com/yihu/hos/config/BeanConfig.java

@ -42,6 +42,10 @@ public class BeanConfig {
    @Value("${hos.zbus.url}")
    private String zbusUrl;
    @Value("${hos.zbus.port}")
    private Integer zbusPort;
    @Value("${hos.zbus.store}")
    private String zbusStore;
    private ZbusBroker zbusBroker;
@ -56,6 +60,18 @@ public class BeanConfig {
        return null;
    }
    public String getZbusUrl() {
        return zbusUrl;
    }
    public Integer getZbusPort() {
        return zbusPort;
    }
    public String getZbusStore() {
        return zbusStore;
    }
    @Override
    protected void finalize() throws Throwable {
        if (zbusBroker != null) {

+ 28 - 11
src/main/java/com/yihu/hos/services/ServiceFlowEventService.java

@ -1,6 +1,5 @@
package com.yihu.hos.services;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.core.log.Logger;
@ -11,10 +10,12 @@ import com.yihu.hos.web.framework.model.bo.ServiceFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import org.zbus.broker.ZbusBroker;
import org.zbus.mq.Producer;
import org.zbus.net.http.Message;
import javax.jms.Queue;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
/**
 * @created Airhead 2016/8/2.
@ -31,6 +32,13 @@ public class ServiceFlowEventService {
    @Autowired
    private ObjectMapper objectMapper;
    private ZbusBroker zbusBroker;
    @Autowired
    public void setZbusBroker(ZbusBroker zbusBroker) {
        this.zbusBroker = zbusBroker;
    }
    /**
     * admin启动时,触发一次所有流程更新事件,用于重启整个服务的情况。
     * 同时解决Broker中启动多个采集任务的问题。
@ -72,17 +80,26 @@ public class ServiceFlowEventService {
    private void sendMsg(String event, ServiceFlow serviceFlow) {
        if (zbusBroker == null) {
            logger.error("zbusBroker is null.");
            return;
        }
        try {
            String msg = objectMapper.writeValueAsString(serviceFlow);
            Map<String, Object> header = new HashMap<>();
            String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
            serviceFlow.setTenant(attachment);
            header.put("tenant", attachment);
            header.put("event", event);
                    this.jmsMessagingTemplate.convertAndSend(this.queue, msg, header);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            String tenant = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
            Producer producer = new Producer(zbusBroker, ServiceFlowConstant.SSH + "@" + tenant);
            producer.createMQ();    //确定为创建消息队列需要显示调用
            Message message = new Message();
            message.setHead("event", event);
            message.setHead("tenant", tenant);
            message.setMethod("POST");
            message.setBody(msg);
            producer.sendSync(message);
        } catch (IOException | InterruptedException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }
}

+ 30 - 13
src/main/java/com/yihu/hos/services/ServiceShellEventService.java

@ -1,6 +1,5 @@
package com.yihu.hos.services;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.core.log.Logger;
@ -11,14 +10,17 @@ import com.yihu.hos.web.framework.model.bo.ServiceShell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import org.zbus.broker.ZbusBroker;
import org.zbus.mq.Producer;
import org.zbus.net.http.Message;
import javax.annotation.Resource;
import javax.jms.Queue;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
/**
 *  发送shell命令消息到MQ
 * 发送shell命令消息到MQ
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/1/5.
@ -35,6 +37,13 @@ public class ServiceShellEventService {
    @Autowired
    private ObjectMapper objectMapper;
    private ZbusBroker zbusBroker;
    @Autowired
    public void setZbusBroker(ZbusBroker zbusBroker) {
        this.zbusBroker = zbusBroker;
    }
    public void serviceShellSend(ServiceShell serviceShell) {
        serviceShell.setType(ServiceFlowConstant.ARBITER_SHELL_SEND);
        this.sendMsg(ServiceFlowConstant.ARBITER_SHELL_SEND, serviceShell);
@ -47,20 +56,28 @@ public class ServiceShellEventService {
    }
    private void sendMsg(String event, ServiceShell serviceShell) {
        if (zbusBroker == null) {
            logger.error("zbusBroker is null.");
            return;
        }
        try {
            Map<String, Object> header = new HashMap<>();
            String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
            serviceShell.setTenant(attachment);
            String msg = objectMapper.writeValueAsString(serviceShell);
            header.put("tenant", attachment);
            header.put("event", event);
            this.jmsMessagingTemplate.convertAndSend(this.shellQueue, msg, header);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            String tenant = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
            Producer producer = new Producer(zbusBroker, ServiceFlowConstant.SHELL_ARBITER + "@" + tenant);
            producer.createMQ();    //确定为创建消息队列需要显示调用
            Message message = new Message();
            message.setHead("event", event);
            message.setHead("tenant", tenant);
            message.setMethod("POST");
            message.setBody(msg);
            producer.sendSync(message);
        } catch (IOException | InterruptedException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }
}