So, this post serves as a wiki for how to deploy a wildfly application into openshift and connect it to a MySql database.
In my scenario, i had a maven web application created locally, as well as a local database. As a newbie to Openshift, i spent sometime learning commands and performing my deployment. As such, i’ll go step-by-step, from the domain configuration to the actual application testing for anyone doing the same.
1. Setting the domain
The first thing you have to do is to get an openshift account. To do so, just access openshift and follow their steps… (i’ll skip this part). After doing so, access applications and add a new application.
In this page, you’ll find a list of cartridges (kind of like bundles) which can be configured for the application. Select a wildfly instance (8, 9 or 10, it’s up to you). If at this time, you have not yet configured your domain, you’ll be asked to do so, as well as provide your application name. For instances, if you write your application name as ‘app1’, and your domain as ‘mydomainxpto’, your application address will follow a configuration similar to http://app1-mydomainxpto.rhcloud.com. At the end of the page, confirm your cartidge selection and wait (can take a while).
After completing this step, you’ll receive admin/password for your wildlfy instance. Save them. Perform similar steps for the mysql database by adding it from the application detail page. I recommend adding phpmyadmin as well. Save the credentials.
At this stage, your ‘server’ is ready to receive the code.
2. Setting the access tools
When shifting to code, you should verify that you possess the rhc tools and git installed. If not, install them. You’ll need ruby (add executables to path when installing) and git, and then install rhc tools by doing ‘gem install rhc‘. These steps are all very well described on the openshift getting started page.
3. Setting the application git repository
To start building your app, start by selecting a folder where you want your git repository to be in. After that, open a git cmd window on it.
At this stage, we already built our application on the server, so we’ll use clone to get the app into our local repository. To do that, access the openshift page and obtain the ssh key for your app. It is right there on your application details page. Do ‘git clone <sshkey>‘. Ok. You now should have your app in your selected folder.
4. Editing the standalone.xml
As we’ll be using MySQL, we have to obtain the datasource to place it in our persistence.xml. There is no need to search for username, password, hosts or ports. Openshift already creates this for you in the standalone.xml file through environment variables that are set at deployment. The MySQLDS datasource however comes disabled by default so access the file, and edit the parameter ‘enabled’ to true (similar to what is in the ExampleDS). This file can be found inside .openshift/config in your app folder.
5. Editing your pom.xml
Your created app folder already has a pom.xml. Do not delete it. Instead, access your local application pom.xml and copy all dependencies into the app’s pom.xml. Leave the profile as it is. Generally, you’ll not need to change it.
6. Copy all of your code
Access your local application maven folder and copy the src folder into your new app folder.
7. Edit persistence.xml
Access the persistence.xml on the new app and edit its datasources as it is declared on the standalone.xml file. For MySQL, it normally looks like ‘java:jboss/datasources/MySQLDS’.
8. Create your database
By rule, your database name will be the same as the application name (in our example ‘app1’). This is changeable. Note however that you’ll need to edit the standalone.xml datasource.
Some applications create their own tables, but if yours does not, simply access use phpmyadmin to create them using a SQL script. To access phpmyadmin just add ‘/phpmyadmin‘ to your application url.
9. Deploy the application
To deploy the application, start by accessing your main app folder via cmd. Then use ‘git add –all’ followed by ‘git commit -m “Some message”‘. After doing this, do ‘git push’. The server will restart, compile your application and deploy it. It all goes well, it should be up and running at your defined url.
10. Troubleshooting
As we know, in programming, things rather do not go well in their first try. As such, here’s some tips to help:
A) The git push cmd does not provide a lot of information regarding the deployment of the app into the wildfly server. Rhc tools has a command to help you check the log files in an updated way, so you can actually see what’s happening during the deploying. Open a new cmd window, and do ‘rhc tail -a <yourappname>‘.
B) The wildfly administration panel can also assist in editing operations or verifying errors. For instance, rather than changing the standalone.xml file regarding the datasource, you can do it here. To access it, you have to call ‘rhc port-forward app1‘. This will allow you to access server the admin panel via a localhost address.
C) Persistence errors. These are normally the biggest problem. Verify that entity table names are according to what’s on the mysql instance (uppercase problems are common!). Entering hibernate in pom.xml with provided scope can be a solution.
And this is it! Hope it helps any one. 🙂