差異處
這裏顯示兩個版本的差異處。
java:ant:execute_job_with_scp [2017/08/19 23:33] 127.0.0.1 外部編輯 |
java:ant:execute_job_with_scp [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>jenkins ant cygwin signtool}} | ||
- | ====== 透過SCP執行某些跨平台工作 ====== | ||
- | ===== Problem ===== | ||
- | 某天我們有了要在exe檔上sign certificate的需求,而我們目前狀況如下: | ||
- | * Signtool Server: Windows Server 2003,32位元。 | ||
- | * Build Server: Jenkins,CentOS6,64位元。 | ||
- | * 簡化後的Build Process: 從版控系統checkout code後,開始編譯,產生各專案artifacts;透過installanywhre產生出installer;計算CRC code,並包成一個壓縮檔。 | ||
- | ===== Thinking ===== | ||
- | 基於以上需求,我開始思考有哪些做法: | ||
- | - 拆解build job: 將步驟拆成多個build job。signtool server為jenkins slave,其中一個job可以將產生的installer丟過去執行。 | ||
- | - 不拆解build job: 讓Signtool Server上有類似jenkins slave的程式,可以接收我們的指令,而動作可以在一個build流成內完成。 | ||
- | 使用第一種做法,我需要refactor build script,並且要考慮installer傳輸或放置在可抓取位置問題。而第二個做法呢? 考慮容易且可以傳輸檔案、執行指令的protocol: | ||
- | - 透過ssh server: 可使用cygwin或其它現有的ssh server。 | ||
- | - 透過rest api: 可基於[[https://spring.io/guides/gs/rest-service/|sprint restapi]]去實作,有sprint boot後,實作或發布app簡單很多。 | ||
- | 為了簡單也不需要額外實作,我選擇了ssh server的方式。 | ||
- | ===== How to? ===== | ||
- | ==== select ssh server - apache sshd ==== | ||
- | 在ssh server的選擇上,首先我使用了cygwin。在Signtool Server上安裝cygwin後,首先遇到類似以下的訊息 | ||
- | <code> | ||
- | could not be located in the dynamic link library KERNEL32.dll | ||
- | </code> | ||
- | 嘗試過不同版本的cygwin依然無法解決,接著改用[[https://mina.apache.org/sshd-project/|apache sshd]]。我使用的是1.2.0的版本,我遇到以下問題: | ||
- | - sshd.bat找不到main class: 從source code得知,bat launch的class名稱需改為org.apache.sshd.server.SshServer。 | ||
- | - 連線後該如何登入: 從source code得知,只要給一個帳號與密碼是相同即可登入。如果有疑慮就需要自己override PasswordAuthenticator,可以參考[[https://mina.apache.org/sshd-project/configuring_security.html|link]]。 | ||
- | - 執行發生找不到jvm.dll的問題: 由於apache sshd必須使用java7以上,且windows 2003不支援java8,因此我使用java 7。而這個問題只要將jre7/bin/client複製到jre7/bin/server後,即可解決。 | ||
- | 在成功啟動apache sshd後,可以透過putty連線測試一下。 | ||
- | ==== register the sshd as a service ==== | ||
- | (此部分需參考後記)\\ | ||
- | 將程式註冊為service有不少方法,最簡單的方法是透過mssm。首先透過mssm執行: | ||
- | <code bash> | ||
- | mssm.exe install apache-sshd | ||
- | </code> | ||
- | 接著會出現如下圖畫面,去選擇你sshd.bat:\\ | ||
- | (後記: 預設Logon帳號為System Account,但在sign certificate上會有問題,需特別指定帳號。可參考下個章節。)\\ | ||
- | {{:java:ant:mssm_register_sshd.png|}}\\ | ||
- | 安裝完成後,可以透過sc指令確認並啟動: | ||
- | <code bash> | ||
- | sc query apache-sshd | ||
- | sc start apache-sshd | ||
- | </code> | ||
- | {{:java:ant:sc_query_apache_sshd.png|}}\\ | ||
- | 請記得再次確認: | ||
- | - 重新啟動後會自動啟動。 | ||
- | - 可透過putty連線。 | ||
- | ==== run scp with ant ==== | ||
- | ant scp的task會基於[[https://sourceforge.net/projects/jsch/|jsch]],請記得先下載並放置於ANT_HOME/lib下。以下為我client side的ant script: | ||
- | <code xml> | ||
- | <project name="test-sign"> | ||
- | <target name="test-sign"> | ||
- | <property name="exe.file.name" value="test.exe"></property> | ||
- | <scp file="build/${exe.file.name}" todir="root:root@10.146.127.213:/apache-sshd-1.2.0/build" port="8000" trust="true"/> | ||
- | <sshexec command="sign.bat ${exe.file.name}" host="10.146.127.213" port="8000" username="root" password="root" trust="true"/> | ||
- | <scp file="root:root@10.146.127.213:/apache-sshd-1.2.0/build/${exe.file.name}" todir="dist" port="8000" trust="true"/> | ||
- | </target> | ||
- | </project> | ||
- | </code> | ||
- | 簡單說明一下我的ant script, | ||
- | - 要sign的執行檔為test.exe,sign之前放至於build資料夾,sign之後放於dist。 | ||
- | - signtool server為10.146.127.213,apache-sshd-1.2.0為預設工作目錄。 | ||
- | - 首先會將test.exe丟到signtool server,接著透過sign.bat執行sign的動作,最後再將結果抓回放置到dist中。 | ||
- | sign.bat首先會確認檔案是否存在,接著就是執行signtool: | ||
- | <code bash> | ||
- | @echo off | ||
- | cd /d "D:\\apache-sshd-1.2.0" | ||
- | |||
- | if "%1" == "" ( | ||
- | echo Please enter the file name | ||
- | exit /b 1 | ||
- | ) | ||
- | |||
- | set sign_file=D:\\apache-sshd-1.2.0\build\%1 | ||
- | |||
- | if not exist "%sign_file%" ( | ||
- | echo Can't find %sign_file% | ||
- | exit /b 1 | ||
- | ) | ||
- | |||
- | C:\WinDDK\6000\bin\setenv.bat C:\WinDDK\6000\ fre AMD64 WLH & signtool sign /v /ac D:\my.cer /s My /n "IssueToName" /t http://timestamp.verisign.com/scripts/timestamp.dll "%sign_file%" | ||
- | </code> | ||
- | 然而,事情並不如想像中順利,執行後發生以下錯誤:\\ | ||
- | {{:java:ant:no_certificate_error_when_run_signtool_with_ssh.png|}}\\ | ||
- | 這個問題是sshd執行環境找不到我所要求的certificate,我嘗試過修改/s與certificte放置路徑皆無法成功。我在想,這應該是執行apache-sshd的使用者所造成的吧? 後來,我先透過了psexe.exe遠端連線執行的方式解決了這個問題: | ||
- | - 在D:\\apache-sshd-1.2.0中放置[[https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx|psexe.exe]]。 | ||
- | - 將client ant的sshexec改為以下內容,目的是為了透過administrator登入去執行sign.bat: | ||
- | <code xml> | ||
- | <sshexec command="psexec -accepteula -u administrator -p 123456 \\localhost -w D:\apache-sshd-1.2.0 cmd.exe /c call sign.bat ${exe.file.name}" host="10.146.127.213" port="8000" username="root" password="root" trust="true" /> | ||
- | </code> | ||
- | 果然成功了:\\ | ||
- | {{:java:ant:run_signtool_with_psexe.png|}}\\ | ||
- | \\ | ||
- | 後來睡覺前,一直在思考,為什麼會有使用者區別? 應該是和註冊service的login帳戶有關吧? 於是我首先嘗試改用batch啟動apache-sshd,發現可以正常sign certificate;接著重新註冊service並將Logon帳戶從system改為特定的:\\ | ||
- | {{:java:ant:mssm_setting_login_account.png|}}\\ | ||
- | 然後改用原本的sshexec,果不其然,成功了:\\ | ||
- | {{:java:ant:run_signtool_without_psexe.png|}} | ||
- | ===== 後記 ===== | ||
- | 某天正式開始啟用此功能後,發現傳大檔案都會發生問題。經過trace,發現是由於nssm註冊為service後,會一分鐘重新啟動一次:\\ | ||
- | {{:java:ant:nssm_with_sshd_bat_crashed_period.png|}}\\ | ||
- | 詳細原因我沒有深究,但我把啟動的prcoess由bat改為exe就正常了:\\ | ||
- | {{:java:ant:nssm_install_sshd_with_java_exe.png|}}\\ | ||
- | 使用的Arguments如下,請改成你自己對應路徑,我是echo bat內容複製貼上的: | ||
- | <code bash> | ||
- | -server -Xmx512M -Dcom.sun.management.jmxremote -classpath ";D:\apache-sshd-1.2.0\scripts\..\lib\bcpg-jdk15on-1.54.jar;D:\apache-sshd-1.2.0\scripts\..\lib\bcpkix-jdk15on-1.54.jar;D:\apache-sshd-1.2.0\scripts\..\lib\bcprov-jdk15on-1.54.jar;D:\apache-sshd-1.2.0\scripts\..\lib\slf4j-api-1.7.16.jar;D:\apache-sshd-1.2.0\scripts\..\lib\slf4j-jdk14-1.7.16.jar;D:\apache-sshd-1.2.0\scripts\..\lib\sshd-core-1.2.0.jar;D:\apache-sshd-1.2.0\scripts\..\lib\tomcat-apr-5.5.23.jar" -Dsshd.home="D:\apache-sshd-1.2.0\scripts\.." org.apache.sshd.server.SshServer | ||
- | </code> | ||
- | ===== Reference ===== | ||
- | * [[https://mina.apache.org/sshd-project/|apache sshd]] | ||
- | * [[http://stackoverflow.com/questions/3582108/create-windows-service-from-executable|NSSM - the Non-Sucking Service Manager]] | ||
- | * [[http://stackoverflow.com/questions/18123144/missing-server-jvm-java-jre7-bin-server-jvm-dll|missing-server-jvm-java-jre7-bin-server-jvm-dll]] | ||
- | * [[https://ant.apache.org/manual/Tasks/scp.html|ant scp task]] | ||
- | * [[https://sourceforge.net/projects/jsch/|jsch]] | ||
- | * [[https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx|psexe]] | ||
- | * [[https://dotblogs.com.tw/kevinya/2016/02/16/153912|遠端執行程式(psexec)]] | ||
- | |||
- | ===== ===== | ||
- | ---- | ||
- | \\ | ||
- | ~~DISQUS~~ |