master
			
			
		
		
						commit
						db8b40733b
					
				| @ -0,0 +1,20 @@ | ||||
| target/ | ||||
| pom.xml.tag | ||||
| pom.xml.releaseBackup | ||||
| pom.xml.versionsBackup | ||||
| pom.xml.next | ||||
| release.properties | ||||
| /.idea | ||||
| *.iml | ||||
| /.settings | ||||
| /bin | ||||
| /gen | ||||
| /build | ||||
| /gradle | ||||
| /classes | ||||
| .classpath | ||||
| .project | ||||
| *.gradle | ||||
| gradlew | ||||
| local.properties | ||||
| node_modules/ | ||||
| @ -0,0 +1,45 @@ | ||||
| #### 使用方法 | ||||
| 
 | ||||
| ```java | ||||
| <build> | ||||
|     <!-- 项目名称 --> | ||||
|     <finalName>${artifactId}</finalName> | ||||
|     <!-- 默认的主代码目录 --> | ||||
|     <sourceDirectory>src/main/java</sourceDirectory> | ||||
|     <!-- 默认的测试代码目录 --> | ||||
|     <testSourceDirectory>src/test/java</testSourceDirectory> | ||||
|     <plugins> | ||||
|         <!-- 一键部署 --> | ||||
|         <plugin> | ||||
|             <groupId>xyz.wbsite</groupId> | ||||
|             <artifactId>deployee-maven-plugin</artifactId> | ||||
|             <version>latest</version> | ||||
|             <configuration> | ||||
|                 <targetPath>${build.directory}</targetPath> | ||||
|                 <targetFile>${build.finalName}.${project.packaging}</targetFile> | ||||
|                 <servers> | ||||
|                     <!-- 服务部署信息 --> | ||||
|                     <server> | ||||
|                         <!-- 服务名称 --> | ||||
|                         <name>something</name> | ||||
|                         <!-- 服务器远程地址 --> | ||||
|                         <ip>10.0.0.1</ip> | ||||
|                         <!-- 服务器远程端口 --> | ||||
|                         <port>22</port> | ||||
|                         <!-- 服务器登录账户 --> | ||||
|                         <username>root</username> | ||||
|                         <!-- 服务器登录密码 --> | ||||
|                         <password>***</password> | ||||
|                         <!-- 部署环境 对应spring.profiles.active--> | ||||
|                         <deployActive>test</deployActive> | ||||
|                         <!-- 部署路径 --> | ||||
|                         <deployPath>/www</deployPath> | ||||
|                         <!-- 部署端口 --> | ||||
|                         <deployPort>8080</deployPort> | ||||
|                     </server> | ||||
|                 </servers> | ||||
|             </configuration> | ||||
|         </plugin> | ||||
|     </plugins> | ||||
| </build> | ||||
| ``` | ||||
| @ -0,0 +1,259 @@ | ||||
| package xyz.wbsite.deployee; | ||||
| 
 | ||||
| import cn.hutool.core.convert.Convert; | ||||
| import cn.hutool.core.io.resource.ResourceUtil; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
| import xyz.wbsite.deployee.config.Server; | ||||
| import xyz.wbsite.deployee.util.IOUtil; | ||||
| import xyz.wbsite.deployee.util.NetUtil; | ||||
| import xyz.wbsite.deployee.util.TaskUtil; | ||||
| import xyz.wbsite.deployee.util.ssh.JSshClient; | ||||
| import xyz.wbsite.deployee.util.ssh.SshSimpleProgress; | ||||
| import org.apache.maven.plugin.AbstractMojo; | ||||
| import org.apache.maven.plugin.MojoExecutionException; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.util.concurrent.Callable; | ||||
| import java.util.concurrent.TimeUnit; | ||||
| 
 | ||||
| /** | ||||
|  * @goal deploy | ||||
|  * @execute phase="package" | ||||
|  */ | ||||
| public class Deploy extends AbstractMojo { | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="${build.directory}" | ||||
|      */ | ||||
|     private String targetPath; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="${build.finalName}.${packaging}" | ||||
|      */ | ||||
|     private String targetFile; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter | ||||
|      */ | ||||
|     private Server[] servers; | ||||
| 
 | ||||
|     public void execute() throws MojoExecutionException { | ||||
|         printLog("------------------------------------------------------------------------"); | ||||
| 
 | ||||
|         printLog("TargetPath {} ", targetPath); | ||||
|         printLog("TargetFile {} ", targetFile); | ||||
| 
 | ||||
|         if (servers == null || servers.length == 0) { | ||||
|             throw new MojoExecutionException("Server information not configured"); | ||||
|         } | ||||
| 
 | ||||
|         for (Server server : servers) { | ||||
|             printLog("Server[{}:{}] start deploying", server.getIp(), server.getDeployPort()); | ||||
| 
 | ||||
|             File target = new File(targetPath, targetFile); | ||||
|             if (!target.exists()) { | ||||
|                 throw new MojoExecutionException("FileNotFoundException: " + target.getAbsolutePath()); | ||||
|             } | ||||
| 
 | ||||
|             JSshClient client = new JSshClient(server.getIp(), Convert.toInt(server.getPort()), server.getUsername(), server.getPassword()); | ||||
|             if (client.isLogin()) { | ||||
|                 printLog("server[{}] login success.", server.getIp()); | ||||
|             } else { | ||||
|                 throw new MojoExecutionException(StrUtil.format("server[{}] login failed.", server.getIp())); | ||||
|             } | ||||
| 
 | ||||
|             // 检测环境是否安装
 | ||||
|             printLog("test environment"); | ||||
|             String serverPath = server.getDeployPath() + "/" + server.getName(); | ||||
|             printLog(serverPath); | ||||
|             if (!client.exist(serverPath)) { | ||||
|                 try { | ||||
|                     printLog("start install jre"); | ||||
| 
 | ||||
|                     printLog("create dir " + serverPath); | ||||
|                     boolean dirs = client.createDirs(serverPath); | ||||
|                     if (dirs) { | ||||
|                         printLog("create dirs[{}] success.", serverPath); | ||||
|                     } else { | ||||
|                         throw new MojoExecutionException(StrUtil.format("create dirs[{}] failed.", serverPath)); | ||||
|                     } | ||||
| 
 | ||||
|                     InputStream stream = ResourceUtil.getStream("jre.tar.gz"); | ||||
| 
 | ||||
|                     client.putFile(stream, serverPath + "/jre.tar.gz", new SshSimpleProgress() { | ||||
|                         @Override | ||||
|                         public void update(int process) { | ||||
|                             System.out.print(StrUtil.format("[INFO] upload jre {}", process + "%\r")); | ||||
|                         } | ||||
|                     }); | ||||
|                     printLog(StrUtil.format("upload jre {}", "finish")); | ||||
| 
 | ||||
|                     printLog("unpackage [jre.tar.gz]"); | ||||
|                     client.exec("cd {}", serverPath); | ||||
|                     client.exec("tar -zxvf {} -C {}", serverPath + "/jre.tar.gz", serverPath); | ||||
| 
 | ||||
|                     printLog("delete [jre.tar.gz]"); | ||||
|                     client.deleteFile(serverPath + "/jre.tar.gz"); | ||||
| 
 | ||||
|                     printLog("create dir [jar]"); | ||||
|                     client.createDirs(serverPath + "/jar"); | ||||
|                     client.createFile(serverPath + "/jar/" + targetFile); | ||||
| 
 | ||||
|                     printLog("create sh script"); | ||||
|                     String sh = IOUtil.toString(ResourceUtil.getStream("service.sh"), StandardCharsets.UTF_8); | ||||
|                     sh = StrUtil.replace(sh, "{0}", serverPath); | ||||
|                     sh = StrUtil.replace(sh, "{1}", targetFile); | ||||
|                     sh = StrUtil.replace(sh, "{2}", server.getDeployActive()); | ||||
|                     sh = StrUtil.replace(sh, "{3}", server.getDeployPort()); | ||||
|                     if (StrUtil.isNotBlank(server.getDeployActive())) { | ||||
|                         client.write(serverPath + "/service-" + server.getDeployActive() + ".sh", sh); | ||||
|                         client.exec("chmod +x {}", serverPath + "/service-" + server.getDeployActive() + ".sh"); | ||||
|                         // 修复win和unix不兼容字符
 | ||||
|                         client.win2unix(serverPath + "/service-" + server.getDeployActive() + ".sh"); | ||||
|                     } else { | ||||
|                         client.write(serverPath + "/service.sh", sh); | ||||
|                         client.exec("chmod +x {}", serverPath + "/service.sh"); | ||||
|                         // 修复win和unix不兼容字符
 | ||||
|                         client.win2unix(serverPath + "/service.sh"); | ||||
|                     } | ||||
| 
 | ||||
|                     printLog("create service script"); | ||||
|                     String ss = IOUtil.toString(ResourceUtil.getStream("service.service"), StandardCharsets.UTF_8); | ||||
|                     ss = StrUtil.replace(ss, "{0}", serverPath); | ||||
|                     ss = StrUtil.replace(ss, "{1}", targetFile); | ||||
|                     if (StrUtil.isNotBlank(server.getDeployActive())) { | ||||
|                         ss = StrUtil.replace(ss, "{2}", "-" + server.getDeployActive()); | ||||
|                     } else { | ||||
|                         ss = StrUtil.replace(ss, "{2}", ""); | ||||
|                     } | ||||
|                     ss = StrUtil.replace(ss, "{3}", server.getDeployPort()); | ||||
|                     client.write("/usr/lib/systemd/system/" + server.getName() + ".service", ss); | ||||
|                     // 修复win和unix不兼容字符
 | ||||
|                     client.win2unix("/usr/lib/systemd/system/" + server.getName() + ".service"); | ||||
|                     printLog("create /usr/lib/systemd/system/{}.service success", server.getName()); | ||||
| 
 | ||||
|                     client.exec("systemctl daemon-reload"); | ||||
|                     printLog("daemon-reload"); | ||||
| 
 | ||||
|                     client.exec("systemctl start {}.service", server.getName()); | ||||
|                     printLog("start {}.service", server.getName()); | ||||
| 
 | ||||
|                     client.exec("systemctl enable {}.service", server.getName()); | ||||
|                     printLog("enable {}.service", server.getName()); | ||||
| 
 | ||||
|                     client.exec("firewall-cmd --add-service=http --permanent"); | ||||
|                     printLog("enable http service"); | ||||
| 
 | ||||
|                     client.exec("firewall-cmd --zone=public --add-port={}/tcp --permanent", server.getDeployPort()); | ||||
|                     printLog("release port {}", server.getDeployPort()); | ||||
| 
 | ||||
|                     client.exec("firewall-cmd --reload"); | ||||
|                     printLog("firewall-cmd --reload"); | ||||
| 
 | ||||
|                     printLog("jre[{}] install success", server.getName()); | ||||
|                 } catch (IOException e) { | ||||
|                     e.printStackTrace(); | ||||
|                     throw new MojoExecutionException("install jre error"); | ||||
|                 } | ||||
|             } else { | ||||
|                 printLog("jre[{}] has been installed", server.getName()); | ||||
|             } | ||||
| 
 | ||||
|             // 上传jar包
 | ||||
|             client.putFile(target.getAbsolutePath(), serverPath + "/jar/" + targetFile, new SshSimpleProgress() { | ||||
|                 @Override | ||||
|                 public void update(int process) { | ||||
|                     System.out.print(StrUtil.format("[INFO] upload package {}", process + "%\r")); | ||||
|                 } | ||||
|             }); | ||||
|             printLog(StrUtil.format("upload package {}", "finish")); | ||||
| 
 | ||||
|             printLog("stop service {}", server.getName()); | ||||
|             client.exec("systemctl stop {}.service", server.getName()); | ||||
| 
 | ||||
|             try { | ||||
|                 TimeUnit.SECONDS.sleep(1); | ||||
|             } catch (InterruptedException e) { | ||||
|                 e.printStackTrace(); | ||||
|             } | ||||
| 
 | ||||
|             printLog("start service {}", server.getName()); | ||||
|             client.exec("systemctl start {}.service", server.getName()); | ||||
| 
 | ||||
|             final int[] tagIndex = {0}; | ||||
|             TaskUtil.scheduleAtFixedRate(new Runnable() { | ||||
|                 @Override | ||||
|                 public void run() { | ||||
|                     System.out.print("[INFO] check service " + StrUtil.repeat('.', 1 + tagIndex[0] % 3) + "\r"); | ||||
|                     tagIndex[0]++; | ||||
|                 } | ||||
|             }, 0, 500); | ||||
| 
 | ||||
|             Object o = TaskUtil.retryTask(new Callable<Object>() { | ||||
|                 @Override | ||||
|                 public Object call() throws Exception { | ||||
|                     if (NetUtil.telnetPort(server.getIp(), Convert.toInt(server.getDeployPort()))) { | ||||
|                         TaskUtil.cancel(); | ||||
|                         System.out.println("[INFO] check service finish"); | ||||
|                         printLog("[{}] deploy success", server.getName()); | ||||
| 
 | ||||
|                         printLog("You can execute the following command to operate the service"); | ||||
| 
 | ||||
|                         printLog("========================================"); | ||||
|                         printLog(StrUtil.format("systemctl start  {}.service", server.getName())); | ||||
|                         printLog(StrUtil.format("systemctl stop   {}.service", server.getName())); | ||||
|                         printLog(StrUtil.format("systemctl status {}.service", server.getName())); | ||||
|                         printLog(StrUtil.format("systemctl reload {}.service", server.getName())); | ||||
|                         printLog("========================================"); | ||||
|                         return new Object(); | ||||
|                     } | ||||
|                     return null; | ||||
|                 } | ||||
|             }, 100); | ||||
|             if (o == null) { | ||||
|                 printLog("[{}] deploy failed", server.getName()); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         printLog("all deploy has finish"); | ||||
|         printLog("------------------------------------------------------------------------"); | ||||
|     } | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         System.out.println(StrUtil.format("systemctl start  {}.service", "tzwy")); | ||||
|         System.out.println(StrUtil.format("systemctl stop   {}.service", "tzwy")); | ||||
|         System.out.println(StrUtil.format("systemctl status {}.service", "tzwy")); | ||||
|         System.out.println(StrUtil.format("systemctl reload {}.service", "tzwy")); | ||||
|     } | ||||
| 
 | ||||
|     private void printLog(String context, Object... args) { | ||||
|         getLog().info(StrUtil.format(context, args)); | ||||
|     } | ||||
| 
 | ||||
|     public String getTargetPath() { | ||||
|         return targetPath; | ||||
|     } | ||||
| 
 | ||||
|     public void setTargetPath(String targetPath) { | ||||
|         this.targetPath = targetPath; | ||||
|     } | ||||
| 
 | ||||
|     public String getTargetFile() { | ||||
|         return targetFile; | ||||
|     } | ||||
| 
 | ||||
|     public void setTargetFile(String targetFile) { | ||||
|         this.targetFile = targetFile; | ||||
|     } | ||||
| 
 | ||||
|     public Server[] getServers() { | ||||
|         return servers; | ||||
|     } | ||||
| 
 | ||||
|     public void setServers(Server[] servers) { | ||||
|         this.servers = servers; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,96 @@ | ||||
| package xyz.wbsite.deployee; | ||||
| 
 | ||||
| import cn.hutool.core.convert.Convert; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
| import xyz.wbsite.deployee.config.Server; | ||||
| import xyz.wbsite.deployee.util.ssh.SshClient; | ||||
| import org.apache.maven.plugin.AbstractMojo; | ||||
| import org.apache.maven.plugin.MojoExecutionException; | ||||
| 
 | ||||
| /** | ||||
|  * @goal remove | ||||
|  */ | ||||
| public class Remove extends AbstractMojo { | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="${build.directory}" | ||||
|      */ | ||||
|     private String targetPath; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="${build.finalName}.${packaging}" | ||||
|      */ | ||||
|     private String targetFile; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter | ||||
|      */ | ||||
|     private Server[] servers; | ||||
| 
 | ||||
|     public void execute() throws MojoExecutionException { | ||||
|         printLog("------------------------------------------------------------------------"); | ||||
|         printLog("TargetPath " + targetPath); | ||||
|         printLog("TargetFile " + targetFile); | ||||
| 
 | ||||
|         if (servers == null || servers.length == 0) { | ||||
|             throw new MojoExecutionException("Server information not configured"); | ||||
|         } | ||||
| 
 | ||||
|         for (Server server : servers) { | ||||
|             printLog("Server[{}:{}] start remove", server.getIp(), server.getDeployPort()); | ||||
| 
 | ||||
|             SshClient client = new SshClient(server.getIp(), Convert.toInt(server.getPort()), server.getUsername(), server.getPassword()); | ||||
| 
 | ||||
|             printLog("stop service {}", server.getName()); | ||||
|             client.exec("systemctl stop {}.service", server.getName()); | ||||
| 
 | ||||
|             printLog("disable service {}", server.getName()); | ||||
|             client.exec("systemctl disable {}.service", server.getName()); | ||||
| 
 | ||||
|             printLog("remove service {}", server.getName()); | ||||
|             client.deleteFile("/usr/lib/systemd/system/" + server.getName() + ".service"); | ||||
| 
 | ||||
|             printLog("daemon-reload"); | ||||
|             client.exec("systemctl daemon-reload"); | ||||
| 
 | ||||
|             printLog("remove service {}", server.getName()); | ||||
| 
 | ||||
|             String serverPath = server.getDeployPath() + "/" + server.getName(); | ||||
|             printLog("delete dir {}", serverPath); | ||||
|             client.deleteDirs(serverPath); | ||||
| 
 | ||||
|             printLog("[{}] remove success", server.getName()); | ||||
|         } | ||||
| 
 | ||||
|         printLog("all remove has finish"); | ||||
|         printLog("------------------------------------------------------------------------"); | ||||
|     } | ||||
| 
 | ||||
|     private void printLog(String context, Object... args) { | ||||
|         getLog().info(StrUtil.format(context, args)); | ||||
|     } | ||||
| 
 | ||||
|     public String getTargetPath() { | ||||
|         return targetPath; | ||||
|     } | ||||
| 
 | ||||
|     public void setTargetPath(String targetPath) { | ||||
|         this.targetPath = targetPath; | ||||
|     } | ||||
| 
 | ||||
|     public String getTargetFile() { | ||||
|         return targetFile; | ||||
|     } | ||||
| 
 | ||||
|     public void setTargetFile(String targetFile) { | ||||
|         this.targetFile = targetFile; | ||||
|     } | ||||
| 
 | ||||
|     public Server[] getServers() { | ||||
|         return servers; | ||||
|     } | ||||
| 
 | ||||
|     public void setServers(Server[] servers) { | ||||
|         this.servers = servers; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,108 @@ | ||||
| package xyz.wbsite.deployee.config; | ||||
| 
 | ||||
| public class Server { | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="server" | ||||
|      */ | ||||
|     private String name; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="127.0.0.1" | ||||
|      */ | ||||
|     private String ip; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="22" | ||||
|      */ | ||||
|     private String port; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="root" | ||||
|      */ | ||||
|     private String username; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter | ||||
|      */ | ||||
|     private String password; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="test" | ||||
|      */ | ||||
|     private String deployActive; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="/www" | ||||
|      */ | ||||
|     private String deployPath; | ||||
| 
 | ||||
|     /** | ||||
|      * @parameter defaultValue="8080" | ||||
|      */ | ||||
|     private String deployPort; | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public void setName(String name) { | ||||
|         this.name = name; | ||||
|     } | ||||
| 
 | ||||
|     public String getIp() { | ||||
|         return ip; | ||||
|     } | ||||
| 
 | ||||
|     public void setIp(String ip) { | ||||
|         this.ip = ip; | ||||
|     } | ||||
| 
 | ||||
|     public String getPort() { | ||||
|         return port; | ||||
|     } | ||||
| 
 | ||||
|     public void setPort(String port) { | ||||
|         this.port = port; | ||||
|     } | ||||
| 
 | ||||
|     public String getUsername() { | ||||
|         return username; | ||||
|     } | ||||
| 
 | ||||
|     public void setUsername(String username) { | ||||
|         this.username = username; | ||||
|     } | ||||
| 
 | ||||
|     public String getPassword() { | ||||
|         return password; | ||||
|     } | ||||
| 
 | ||||
|     public void setPassword(String password) { | ||||
|         this.password = password; | ||||
|     } | ||||
| 
 | ||||
|     public String getDeployActive() { | ||||
|         return deployActive; | ||||
|     } | ||||
| 
 | ||||
|     public void setDeployActive(String deployActive) { | ||||
|         this.deployActive = deployActive; | ||||
|     } | ||||
| 
 | ||||
|     public String getDeployPath() { | ||||
|         return deployPath; | ||||
|     } | ||||
| 
 | ||||
|     public void setDeployPath(String deployPath) { | ||||
|         this.deployPath = deployPath; | ||||
|     } | ||||
| 
 | ||||
|     public String getDeployPort() { | ||||
|         return deployPort; | ||||
|     } | ||||
| 
 | ||||
|     public void setDeployPort(String deployPort) { | ||||
|         this.deployPort = deployPort; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,38 @@ | ||||
| package xyz.wbsite.deployee.util; | ||||
| 
 | ||||
| import java.io.ByteArrayOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.nio.charset.Charset; | ||||
| 
 | ||||
| /** | ||||
|  * IO流相关操作工具 | ||||
|  * | ||||
|  * @author wangbing | ||||
|  * @version 0.0.1 | ||||
|  * @since 1.8 | ||||
|  */ | ||||
| public class IOUtil extends cn.hutool.core.io.IoUtil { | ||||
| 
 | ||||
|     public static String toString(InputStream input, String encoding) throws IOException { | ||||
|         return toString(input, Charset.forName(encoding)); | ||||
|     } | ||||
| 
 | ||||
|     public static String toString(InputStream input, Charset encoding) throws IOException { | ||||
|         byte[] bytes = toByteArray(input); | ||||
|         return new String(bytes, encoding); | ||||
|     } | ||||
| 
 | ||||
|     public static byte[] toByteArray(InputStream input) throws IOException { | ||||
|         ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||||
| 
 | ||||
|         try { | ||||
|             copy(input, output); | ||||
|             return output.toByteArray(); | ||||
|         } catch (Throwable e) { | ||||
|             throw e; | ||||
|         } finally { | ||||
|             output.close(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| package xyz.wbsite.deployee.util.ssh; | ||||
| 
 | ||||
| public interface SshSimpleProgress { | ||||
| 
 | ||||
|     /** | ||||
|      * 进度更新(0~100) | ||||
|      * | ||||
|      * @param process 进度 | ||||
|      */ | ||||
|     void update(int process); | ||||
| } | ||||
| @ -0,0 +1,33 @@ | ||||
| package xyz.wbsite.deployee.util.ssh; | ||||
| 
 | ||||
| import cn.hutool.core.io.StreamProgress; | ||||
| 
 | ||||
| /** | ||||
|  * SimpleProgress包装类 | ||||
|  */ | ||||
| public class SshStreamProgressWrapper implements StreamProgress { | ||||
|     private int val; | ||||
|     private SshSimpleProgress progress; | ||||
| 
 | ||||
|     public SshStreamProgressWrapper(SshSimpleProgress progress) { | ||||
|         this.progress = progress; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void start() { | ||||
|         progress.update(val); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void progress(long total, long progressSize) { | ||||
|         int newVal = (int) (progressSize * 100.0 / total); | ||||
|         if (newVal > val) { | ||||
|             progress.update(val = newVal); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void finish() { | ||||
| 
 | ||||
|     } | ||||
| } | ||||
											
												Binary file not shown.
											
										
									
								| @ -0,0 +1,13 @@ | ||||
| [Unit]  | ||||
| Description={0} Service | ||||
| After=network.target remote-fs.target nss-lookup.target  | ||||
| 
 | ||||
| [Service]  | ||||
| Type=forking  | ||||
| ExecStart={0}/service{2}.sh start | ||||
| ExecReload={0}/service{2}.sh restart | ||||
| ExecStop={0}/service{2}.sh stop | ||||
| PrivateTmp=true | ||||
| 
 | ||||
| [Install]  | ||||
| WantedBy=multi-user.target | ||||
| @ -0,0 +1,77 @@ | ||||
| #!/bin/bash | ||||
| # work path | ||||
| cd $(dirname $0) | ||||
| #java home | ||||
| JAVA_HOME=jre | ||||
| #jar file | ||||
| APP_NAME=jar/{1} | ||||
| #active | ||||
| ACTIVE={2} | ||||
| #port | ||||
| PORT={3} | ||||
| #more arg | ||||
| APP_ARG= | ||||
| 
 | ||||
| usage() { | ||||
|     echo "Usage: sh service-*.sh [start|stop|restart|status]" | ||||
|     exit 1 | ||||
| } | ||||
| 
 | ||||
| is_exist(){ | ||||
|   pid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v /bin/bash|awk '{print $2}' ` | ||||
|   if [ -z "${pid}" ]; then | ||||
|    return 1 | ||||
|   else | ||||
|     return 0 | ||||
|   fi | ||||
| } | ||||
| 
 | ||||
| start(){ | ||||
|   is_exist | ||||
|   if [ $? -eq "0" ]; then | ||||
|     echo "${APP_NAME} is already running. pid=${pid} ." | ||||
|   else | ||||
|     nohup ${JAVA_HOME}/bin/java -Dfile.encoding=UTF-8 -jar $APP_NAME --spring.profiles.active=$ACTIVE --server.port=${PORT} $APP_ARG > /dev/null 2>&1 & | ||||
|   fi | ||||
| } | ||||
| 
 | ||||
| stop(){ | ||||
|   is_exist | ||||
|   if [ $? -eq "0" ]; then | ||||
|     kill -9 $pid | ||||
|   else | ||||
|     echo "${APP_NAME} is not running" | ||||
|   fi | ||||
| } | ||||
| 
 | ||||
| status(){ | ||||
|   is_exist | ||||
|   if [ $? -eq "0" ]; then | ||||
|     echo "${APP_NAME} is running. Pid is ${pid}" | ||||
|   else | ||||
|     echo "${APP_NAME} is NOT running." | ||||
|   fi | ||||
| } | ||||
| 
 | ||||
| restart(){ | ||||
|   stop | ||||
|   start | ||||
| } | ||||
| 
 | ||||
| case "$1" in | ||||
|   "start") | ||||
|     start | ||||
|     ;; | ||||
|   "stop") | ||||
|     stop | ||||
|     ;; | ||||
|   "status") | ||||
|     status | ||||
|     ;; | ||||
|   "restart") | ||||
|     restart | ||||
|     ;; | ||||
|   *) | ||||
|     usage | ||||
|     ;; | ||||
| esac | ||||
					Loading…
					
					
				
		Reference in new issue