OpenShift 走一遭



    想要把範例程式公開到網路上,有用到資料庫,想說Google的AppEngine有點麻煩,找了一下,於是決定放到OpenShift,並記錄一下過程。

    既然是範例程式,當然有必要準備一些先期工作 ,例如:安裝好Java環境、GitMaven

    首先,當然是到OpenShift先註冊一個帳號

好了,現在去收信並啟用帳號

完成後直接登錄 App Console後,然後我們要建立第一個專案。

選擇建立Tomcat 專案

填入資料,下圖中的Source Code不用填,除非已經有了源碼,要從外部匯入專案
專案建立完成,然後選擇我們要用的資料庫(在此我選擇了PostgreSQL 9.2)
Cartridge:卡匣,想成模版就可以了
 完成後
現在開始要在電腦端安裝rhc工具
以fedora來說,執行
$sudo yum install rubygem-rhc
剩下的步驟造官方操作這裡作即可


$ rhc setup
OpenShift Client Tools (RHC) Setup Wizard

This wizard will help you upload your SSH keys, set your application namespace, and check that other programs like Git are
properly installed.

If you have your own OpenShift server, you can specify it now. Just hit enter to use the server for OpenShift Online:
openshift.redhat.com.
Enter the server hostname: |openshift.redhat.com|

You can add more servers later using 'rhc server'.

Login to openshift.redhat.com: kent@somewhere.com.tw
Password: ********

OpenShift can create and store a token on disk which allows to you to access the server without using your password. The key
is stored in your home directory and should be kept secret.  You can delete the key at any time by running 'rhc logout'.
Generate a token now? (yes|no) yes
Generating an authorization token for this client ... lasts about 1 month

Saving configuration to /root/.openshift/express.conf ... done

No SSH keys were found. We will generate a pair of keys for you.

    Created: /root/.ssh/id_rsa.pub

Your public SSH key must be uploaded to the OpenShift server to access code.  Upload now? (yes|no) yes

Since you do not have any keys associated with your OpenShift account, your new key will be uploaded as the 'default' key.

Uploading key 'default' ... done

Checking for git ... found git version 2.1.0

Checking common problems .. done

Checking for a domain ... yes2000

Checking for applications ... found 1

 helloworld http://helloworld-yes2000.rhcloud.com/

 You are using 1 of 3 total gears
 The following gear sizes are available to you: small

Your client tools are now configured.
註:$rhc setup,完成後即記住用戶資訊,若是太久沒有動作(或是下次開機),則會視為離線,則只要再執行一次rhc setup,即可回覆連線,若要換人,則執行 $rhc logout再以$rhc setup -l kent@anotherplace.com.tw進行設定即可。
現在我們可以看一下現有應用程式(專案)資訊
$ rhc apps
helloworld @ http://helloworld-yes2000.rhcloud.com/ (uuid: 55979949e0b8cd2068000069)
------------------------------------------------------------------------------------
 Domain:     yes2000
 Created:    4:28 PM
 Gears:      1 (defaults to small)
 Git URL:  ssh://55979949e0b8cd2068000069@helloworld-yes2000.rhcloud.com/~/git/helloworld.git/
 SSH:        55979949e0b8cd2068000069@helloworld-yes2000.rhcloud.com
 Deployment: auto (on git push)

 jbossews-2.0 (Tomcat 7 (JBoss EWS 2.0))
 ---------------------------------------
    Gears: Located with postgresql-9.2

 postgresql-9.2 (PostgreSQL 9.2)
 -------------------------------
    Gears:          Located with jbossews-2.0
    Connection URL: postgresql://$OPENSHIFT_POSTGRESQL_DB_HOST:$OPENSHIFT_POSTGRESQL_DB_PORT
    Database Name:  helloworld
    Password:       NwSiUqHDxxxj
    Username:       adminxcxxxxg
接下來我們把整個專案抓到我們的電腦
$git clone ssh://55979949e0b8cd2068000069@helloworld-yes2000.rhcloud.com/~/git/helloworld.git/
$cd helloworld
專案目錄結構如下:
跟一般maven web application相仿,
index.html與snoop.jsp可視需要決定是否保留,
其中snoop.jsp可顯示系統狀態資訊,
若是專案要以war發佈,
直接將war檔放到webapps目錄下即可。
其中pom.xml有一段特別重要
...
 <profile>
       <!-- 當在OpenShift主機端建構時,會使用'openshift' profile 做為mvn的設定. -->
       <!-- 添加必要的設定到該profile,以便主機端可照您的需求建構程式 -->
       <!-- 預設的建構輸出路徑為'webapps' 目錄. -->
       <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
       <id>openshift</id>
       <properties><!-- 主機端建構時不想引發測試程式. -->
               <maven.test.skip>true</maven.test.skip>
       </properties>
       <build>
           <finalName>${project.artifactId}</finalName>
           <plugins>
               <plugin>
                   <artifactId>maven-war-plugin</artifactId>
                   <version>2.1.1</version>
                   <configuration>
                       <outputDirectory>webapps</outputDirectory>
                       <warName>ROOT</warName>
                   </configuration>
               </plugin>
           </plugins>
       </build>
   </profile>
...

連結資料庫
我們沒有辦法直接連接openshift的資料庫,必須透過port-forward的方式來連接
$ rhc port-forward -a helloworld
Checking available ports ... done
Forwarding ports ...
Address already in use(位址已被佔用) - bind(2) while forwarding port 5432. Trying local port 5433

To connect to a service running on OpenShift, use the Local address

Service    Local               OpenShift
---------- -------------- ---- ------------------
java       127.0.0.1:8080  =>  127.7.185.129:8080
postgresql 127.0.0.1:5433  =>  127.7.185.130:5432

Press CTRL-C to terminate port forwarding
是的,此時只要以資料庫連結本機的 5433 port,就可直接操縱資料庫了(速度不快),同時也可以用http://localhost:8080/來開始遠端Web page。

Tomcat的JNDI設定
以下以設定一個 jdbc/hw JNDI 為例
#切換到專案目錄
$cd helloworld
#編輯tomcat 設定
$vi .openshift/config/server.xml
...
<GlobalNamingResources>
 <Resource name="jdbc/hw" auth="Container" type="javax.sql.DataSource"
     username="${env.OPENSHIFT_POSTGRESQL_DB_USERNAME}"
     password="${env.OPENSHIFT_POSTGRESQL_DB_PASSWORD}"
     url="jdbc:postgresql://${env.OPENSHIFT_POSTGRESQL_DB_HOST}:${env.OPENSHIFT_POSTGRESQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}"
     driverClassName="org.postgresql.Driver" initialSize="5" maxWait="5000"
     maxActive="120" maxIdle="5" validationQuery="select 1" poolPreparedStatements="true">
 </Resource>
:x
#存檔後再開啟變更以下檔案
$vi .openshift/config/catalina.properties
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.home}/../app-root/data/*.jar
:x
#存檔後再設定程式resource link
$mkdir -p ./src/main/webapp/META-INF
$vi ./src/main/webapp/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true">
   <ResourceLink name="jdbc/hw" type="javax.sql.DataSource" global="jdbc/hw"/>
</Context>
:x
#存檔後再以ssh連線到openshift 主機的專案目錄
$rhc ssh -a helloworld
#切換到資料存放目錄,我們要把jdbc driver放這裡
[helloworld-yes2000.rhcloud.com 55979949e0b8cd2068000069]\>cd $OPENSHIFT_DATA_DIR
#下載jdbc driver函式庫
[hellow-yes2000…. data]\>wget https://jdbc.postgresql.org/download/postgresql-9.4-1201.jdbc41.jar
#離開
[helloworld-yes2000….]\>exit
剩下的事,就是照一般Maven開發Webapp的方式進行程式撰寫,當程式完成後,請進行
$git commit 並將程式 $git push
當程式上傳時,主機端會建構程式並啟用。

之前有寫過一個郵遞區號查詢的程式,這次把放到OpenShift 上面(OpenShift的首頁好像Cache的有點嚴重,若您的程式沒有錯,也沒出現,請用力刷Ctrl+F5,這個問題卡了我好久),OpenShift 完成後拉下的程式放在這裡,您撰寫的時候可以參考看看。

留言

這個網誌中的熱門文章

企業人員的統一管理-FreeIPA學習筆記

證交所最佳五檔的程式解析

Postgresql HA