Mar 10, 2014

QNAP NAS - My First App HelloWorld!

以QDK打包顯示 "Hello World!" 的網頁,成為可安裝QPKG,並在系統啟動時,就可以服務

還記得QDK都在 /share/Public/ 打包QPKG,因此到此路徑,並確認 qbuild 路徑已經設定到PATH。
餐可 Run .sh 到底是在那個shell內? 設定QDK的qbuild路徑
[~] # cd /share/Public[/share/Public] # . pQDK.sh /bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/local/sbin:/opt/bin:/opt/sbin:/share/MD0_DATA/.qpkg/QDK
[/share/Public] # qbuild --create-env helloworld[/share/Public] # cd helloworld/[/share/Public/helloworld] # ls -ladrwxr-xr-x 9 admin administ 4096 Mar 10 09:44 ./drwxrwxrwx 4 admin administ 4096 Mar 10 09:44 ../drwxr-xr-x 2 admin administ 4096 Jul 17 2011 arm-x09/drwxr-xr-x 2 admin administ 4096 Jul 17 2011 arm-x19/drwxr-xr-x 2 admin administ 4096 Jul 17 2011 config/drwxr-xr-x 2 admin administ 4096 Jul 17 2011 icons/-rw-r--r-- 1 admin administ 4329 Jul 17 2011 package_routines-rw-r--r-- 1 admin administ 1952 Mar 10 09:44 qpkg.cfgdrwxr-xr-x 2 admin administ 4096 Mar 10 09:44 shared/drwxr-xr-x 2 admin administ 4096 Jul 17 2011 x86/drwxr-xr-x 2 admin administ 4096 Jul 17 2011 x86_64/[/share/Public/helloworld] # vi package_routines

在系統啟動時開啟服務 (SYS_QPKG_SERVICE_ENABLED="TRUE"),此設定不是每個安裝都一樣的部分,因此在「package_routines」設定。
## If the QPKG should be enabled or disabled after the installation/upgrade.SYS_QPKG_SERVICE_ENABLED="TRUE"## Name and location of system shares#SYS_PUBLIC_SHARE=""

要自行產生HTML檔,web檔案和程式 (run with PHP) 大多數屬於platform-independent,因此放在shared,為了方便管理,養成好習慣。

  • shared/web 放所有web相關程式,用web做IO介面,方便接收指令和資料,並做好視覺化呈現。
  • 在Ubuntu開發console app做資料處理,也就是可命令化的執行檔,並設計好參數,如同Linux常用指令 (e.g. grep, chmod, cat, ...),盡量用standard library。若可以和裝置無關,就可放置在sharedshared/exe。若最終開發的console還是用到與平台相關的libraries,就要放到對應平台目錄。
[/share/Public/helloworld] # cd shared/[/share/Public/helloworld/shared] # mkdir web[/share/Public/helloworld/shared] # cd web[/share/Public/helloworld/shared/web] # vi index.html[/share/Public/helloworld/shared/web] # 

index.html file:
<html><body><h1>Hello World!</h1></body></html>~~

接著開始修改安裝檔:
[/share/Public/helloworld/shared/web] # cd ..[/share/Public/helloworld/shared] # ls -la drwxr-xr-x 3 admin administ 4096 Mar 10 10:41 ./drwxr-xr-x 9 admin administ 4096 Mar 10 09:58 ../-rwxr-xr-x 1 admin administ 723 Mar 10 10:41 helloworld.sh*drwxr-xr-x 2 admin administ 4096 Mar 10 10:12 web/[/share/Public/helloworld/shared] # vi helloworld.sh

QDK的qbuild會為專案建立 share/<name>.sh 的安裝script (helloworld.sh)。修改.sh檔 (vi helloworld.sh),根據需求加入以下scripts指令,在此範例就是要:
  • 找到未來helloworld.qpkg的安裝路徑
  • QNAP系統Web的路徑
  • 並將 shared/web/ 以symbolic link連結到 <Web>/helloworld,就可以由 http://host/helloworld取得。
#!/bin/shCONF=/etc/config/qpkg.confQPKG_NAME="helloworld"QPKG_PATH=$(/sbin/getcfg $QPKG_NAME Install_Path -f $CONF)WEB_SHARE=$(/sbin/getcfg SHARE_DEF defWeb -d Qweb -f /etc/config/def_share.info)WEB_PATH=$(/sbin/getcfg $WEB_SHARE path -f /etc/config/smb.conf)case "$1" in start) ENABLED=$(/sbin/getcfg $QPKG_NAME Enable -u -d FALSE -f $CONF) if [ "$ENABLED" != "TRUE" ]; then echo "$QPKG_NAME is disabled." exit 1 fi : ADD START ACTIONS HERE ln -sf $QPKG_PATH/web $WEB_PATH/$QPKG_NAME ;; stop) : ADD STOP ACTIONS HERE rm -f $WEB_PATH/$QPKG_NAME ;; restart) $0 stop $0 start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1esacexit 0

  • QPKG_PATH=$(/sbin/getcfg $QPKG_NAME Install_Path -f $CONF)」在qpkg.conf設定檔取得QDK安裝路徑:QPKG_PATH = /share/MD0_DATA/.qpkg/QDK
[/share/Public/helloworld/shared] # cat /etc/config/qpkg.conf | grep "helloworld"[helloworld]Name = helloworldQPKG_File = helloworld.qpkgShell = /share/MD0_DATA/.qpkg/helloworld/helloworld.shInstall_Path = /share/MD0_DATA/.qpkg/helloworldWebUI = /helloworld




[/share/Public/helloworld/build] # getcfg helloworld Install_Path -f /etc/config/qpkg.conf /share/MD0_DATA/.qpkg/helloworld[/share/Public/helloworld/build] #


  • WEB_SHARE=$(/sbin/getcfg SHARE_DEF defWeb -d Qweb -f /etc/config/def_share.info):WEB_SHARE = Web
[/share/Public/helloworld/shared] # cat /etc/config/def_share.info [SHARE_DEF]defPublic = PublicdefDownload = DownloaddefMultimedia = MultimediadefRecordings = RecordingsdefUsb = UsbdefWeb = WebdefVolMP = /share/MD0_DATA

  • WEB_PATH=$(/sbin/getcfg $WEB_SHARE path -f /etc/config/smb.conf):WEB_PATH = /share/MD0_DATA/Web
[/share/Public/helloworld/shared] # cat /etc/config/smb.conf | grep "Web"[Web]path = /share/MD0_DATA/Web

目的就是要如同以下例子,建立symbilic link: helloworld -> ...
[/share/MD0_DATA/Web] # ls -ladrwxrwxrwx 3 admin administ 4096 Mar 10 11:06 ./drwxrwxrwx 22 admin administ 4096 Mar 2 03:00 ../lrwxrwxrwx 1 admin administ 33 Mar 9 21:24 aria2 -> /share/MD0_DATA/.qpkg/aria2/aria2/lrwxrwxrwx 1 admin administ 38 Feb 25 12:45 hello_qnap -> /share/MD0_DATA/.qpkg/aria2/hello_qnap@
-rw-r--r-- 1 admin administ 1139 Feb 25 00:15 index.phpdrwxr-xr-x 11 admin administ 4096 Aug 19 2010 phpMyAdmin/lrwxrwxrwx 1 admin administ 51 Mar 9 10:27 signagestation -> /share/MD0_DATA/.qpkg/SignageStation/signagestation/
lrwxrwxrwx 1 admin administ 38 Mar 9 21:24 videostation -> /share/MD0_DATA/.qpkg/VideoStation/web/
[/share/MD0_DATA/Web] #

因此,在開啟服務時:「ln -sf $QPKG_PATH/web $WEB_PATH/$QPKG_NAME」相當於
ln -sf /share/MD0_DATA/.qpkg/QDK/helloworld/web  /share/MD0_DATA/Web/helloworld  

在停止時:「rm -f $WEB_PATH/$QPKG_NAME」相當於刪除建立之symbolic link。

記得設定專案設定檔 (App Metadata Information):qpkg.cfg。以下僅設定web服務相關部分。
[/share/Public/helloworld] # vi qpkg.cfg

# Relative path to web interfaceQPKG_WEBUI="/helloworld" # web根目錄以下的hellowrold# Port number for the web interface.QPKG_WEB_PORT="80"

建立專案QPKG檔 (qbuild),並修改權限能執行,執行後就直接安裝。
[/share/Public/helloworld] # qbuildCreating archive with data files...Creating archive with control files...Creating QPKG package...[/share/Public/helloworld] # [/share/Public/helloworld] # cd build/[/share/Public/helloworld/build] # ls -ladrwxr-xr-x 2 admin administ 4096 Mar 10 11:16 ./drwxr-xr-x 10 admin administ 4096 Mar 10 11:16 ../-rw-r--r-- 1 admin administ 21813 Mar 10 11:16 helloworld_0.1.qpkg[/share/Public/helloworld/build] # chmod 755 helloworld_0.1.qpkg [/share/Public/helloworld/build] # ls -la drwxr-xr-x 2 admin administ 4096 Mar 10 11:16 ./drwxr-xr-x 10 admin administ 4096 Mar 10 11:16 ../-rwxr-xr-x 1 admin administ 21813 Mar 10 11:16 helloworld_0.1.qpkg*[/share/Public/helloworld/build] # ./helloworld_0.1.qpkg Install QNAP package on TS-NAS...././qpkg.cfg./qinstall.sh./package_routines0+1 records in0+1 records out0+1 records in0+1 records outLink service start/stop script: helloworld.shSet QPKG information in /etc/config/qpkg.confhelloworld is disabled.helloworld 0.1 has been installed in /share/MD0_DATA/.qpkg/helloworld.[/share/Public/helloworld/build] #
以上安裝時,顯而易見是出了問題,登入QNAP桌面檢查,原來出了 "insufficient ramdisk space" 的問題,TS-212只有512 MB RAM,跑起來偶爾會不穩定。

重新開機後,再次qbuild並安裝就好了。可以看得出來,QPKG內容檔案安裝順序為:
./qpkg.cfg  =>  ./qinstall.sh  =>  ./package_routines  =>  helloworld.sh
[/share/Public/helloworld] # qbuildCreating archive with data files...Creating archive with control files...Creating QPKG package...[/share/Public/helloworld] # cd build/[/share/Public/helloworld/build] # chmod 755 helloworld_0.1.qpkg [/share/Public/helloworld/build] # ./helloworld_0.1.qpkg Install QNAP package on TS-NAS...33+1 records in33+1 records out././qpkg.cfg./qinstall.sh./package_routines0+1 records in0+1 records out0+1 records in0+1 records outhelloworld is already installed. Setup will now perform package upgrading.Link service start/stop script: helloworld.shSet QPKG information in /etc/config/qpkg.confQPKG_PATH=/share/MD0_DATA/.qpkg/helloworldWEB_SHARE=WebWEB_PATH=/share/MD0_DATA/Webhelloworld 0.1 has been installed in /share/MD0_DATA/.qpkg/helloworld.[/share/Public/helloworld/build] #

以上在helloworld.sh加入echo顯示訊息,確定安裝路徑正確。此方式可以參考,以便能debug!
QPKG_PATH=$(/sbin/getcfg $QPKG_NAME Install_Path -f $CONF)echo QPKG_PATH=$QPKG_PATHWEB_SHARE=$(/sbin/getcfg SHARE_DEF defWeb -d Qweb -f /etc/config/def_share.info)echo WEB_SHARE=$WEB_SHAREWEB_PATH=$(/sbin/getcfg $WEB_SHARE path -f /etc/config/smb.conf)echo WEB_PATH=$WEB_PATH

確認安裝路徑正確,且symbolic link已經建立。
[/share/Public/helloworld/build] # cd /share/MD0_DATA/Web[/share/MD0_DATA/Web] # ls -ladrwxrwxrwx 3 admin administ 4096 Mar 10 11:42 ./drwxrwxrwx 22 admin administ 4096 Mar 2 03:00 ../lrwxrwxrwx 1 admin administ 33 Mar 10 11:40 aria2 -> /share/MD0_DATA/.qpkg/aria2/aria2/lrwxrwxrwx 1 admin administ 38 Feb 25 12:45 hello_qnap -> /share/MD0_DATA/.qpkg/aria2/hello_qnap@
lrwxrwxrwx 1 admin administ 36 Mar 10 11:42 helloworld -> /share/MD0_DATA/.qpkg/helloworld/web/-rw-r--r-- 1 admin administ 1139 Feb 25 00:15 index.phpdrwxr-xr-x 11 admin administ 4096 Aug 19 2010 phpMyAdmin/lrwxrwxrwx 1 admin administ 51 Mar 10 11:40 signagestation -> /share/MD0_DATA/.qpkg/SignageStation/signagestation/lrwxrwxrwx 1 admin administ 47 Mar 10 11:34 videostation -> /share/MD0_DATA/.qpkg/VideoStation/web.disabled/[/share/MD0_DATA/Web] # cd helloworld[/share/MD0_DATA/Web/helloworld] # ls -ladrwxr-xr-x 2 admin administ 4096 Mar 10 10:12 ./drwxr-xr-x 3 admin administ 4096 Mar 10 11:42 ../-rw-r--r-- 1 admin administ 48 Mar 10 10:00 index.html[/share/MD0_DATA/Web/helloworld] #

App Center可以看到安裝成功,並且已經啟動。
安裝後,預設是開機啟動

click 「開啟」:直接開new tab連線web,雖然只開port 80,https也可以

手動改為port 80 (http://), tested ok!
在App Center停止helloworld後,當然就看不到helloworld的連結。
[/share/MD0_DATA/Web] # ls -ladrwxrwxrwx 3 admin administ 4096 Mar 10 15:16 ./drwxrwxrwx 22 admin administ 4096 Mar 2 03:00 ../lrwxrwxrwx 1 admin administ 33 Mar 10 11:40 aria2 -> /share/MD0_DATA/.qpkg/aria2/aria2/lrwxrwxrwx 1 admin administ 38 Feb 25 12:45 hello_qnap -> /share/MD0_DATA/.qpkg/aria2/hello_qnap@
-rw-r--r-- 1 admin administ 1139 Feb 25 00:15 index.phpdrwxr-xr-x 11 admin administ 4096 Aug 19 2010 phpMyAdmin/lrwxrwxrwx 1 admin administ 51 Mar 10 11:40 signagestation -> /share/MD0_DATA/.qpkg/SignageStation/signagestation/lrwxrwxrwx 1 admin administ 47 Mar 10 11:34 videostation -> /share/MD0_DATA/.qpkg/VideoStation/web.disabled/[/share/MD0_DATA/Web] #

No comments :

Post a Comment