What Is A Hook?
‘Hook’ is a term that denotes that some code is inserted into some standard operation in order to enable customization. MilesWeb has teamed up with Jelastic where the users are allowed to execute their custom script before or after an application builds or deploys the operations. This implementation enables complete automation of your solution delivery by performing all the application customizations through a single service panel for applying all of them at once in the process of a single deploy action.
As hooks are a part of the application deployment process, users can directly set them through the application deployment dialogue. An appropriate form will be displayed while referring to any of the options given below:
Deployment Manager : ‘Deploy to…’ icon (this option is visible in the bottom of the dashboard)
‘Deployments’ button for the application server
- Within the opened frame, hover or Hooks and expand the section. Click either on the ‘Pre’ or ‘Post’ button in order to provide your code. This code will be executed just before the deployment or right after the deployment.
- In the code editor window, you can utilize any suitable programming language – the only necessity here is that the appropriate code interpreter should be pre-installed in a container.
On the basis of the engine that your container is operating on, you can execute the required programming language scripts within the body of your hook. For instance, in order to fetch the exact container engine version and to store it in the server logs, the scripts mentioned below can be used as a hook:
Quick Tip : For working with your scripts code more easily and conveniently some supplementary options are offered in the top editor pane. They are mentioned below:
Wrap lines : This option breaks the line in case it reaches the frame border.
Search : This option enables you to find the required information easily with search options like ‘Match case’ and ‘Regex’.
Help : This option redirects you to the current document to give you the hook usage details.
Click on the ‘Apply’ option when you are ready to proceed with the application deployment.
- When the application deployment is completed, you can click on the ‘Show logs’ option present in the appeared dashboard notification in order to view the information on the handled process and their results.
Quick Tip : You can find the dedicated hooks.log file in the Log section of the application server.
For analyzing the logged data on ‘Build’ hooks implementation, you must check the same file at the appropriate ‘Maven’ build node.
Hook Use Cases
Hooks offer a wide range of opportunities for the developers through which the developers can automate many processes and get an application that is ready to work without any manual intervention. For instance, below mentioned are the most common tasks that can be configured to be automatically implemented by hooks.
- Pre-build/deploy operations (operations that are executed before building and/or deploying the application)
- For checking if all the requirements are met
- For pre-installing the necessary software
- For clearing or preparing a dedicated folder for an application
- For the log data
- Post build/deploy actions (actions that are executed after the application is created and/or the deployment of the application is already completed)
- For restarting your application sever after the deployment
- For applying any other customization
- For the log data
Actions Logging With Hooks
Below mentioned is a simple example of your own log file creation though hooks.
- Start the deployment of your project in any desired way. We will consider the example of ‘HelloWorld.zip’ archieve that is available for every new account in the Deployment manager.
- Expand the ‘Hooks’ option, click on ‘Pre’ hook and paste the code mentioned below in the opened editor frame:
echo “$(date) – deployment start” >> ~/mylog
After the execution of this command, a strong will be added to the ‘mylog’ file (this file will be automatically created in the server directory in case it does not exist) for indicating the deployment start time.
- For ‘Post’ hook, add the code mentioned below:
echo “$(date) – deployment end” >> ~/mylog
Quick Tip : If required you can make use of the ‘exit’ command for breaking your hook and thereby breaking the implementation of the appropriate deployment / build operation at any point. Here the 0 value (i.e. the exit 0) is used for indicating success and if any other value is shown, it denotes an error ( example – exit 1).
Here, we have logged the end of the deployment.
- Deploy your application and check the ‘mylog’ file for checking if the hooks that we started were successfully executed.
In this case, with the custom ‘mylog’ file along with the application deployment start/end timestamps state that the scripts have successfully worked required.
Application Restart With Hooks
Let’s have a look at another example of hooks usage – With restarting your Tomcat application server immediately after the project deployment.
- Start the deployment of any application (let’s use the default unified HelloWorld package present in the Deployment manager and configure the below mentioned ‘Post’ deploy hook:
sudo service tomcat restart
After this step your Tomcat application server will be restarted automatically after deployment.
- Go to the ‘Log’ section and see ‘hooks.log’ for getting a complete response for your hook execution.
In this way you can start working with your application right after its deployment without the need of executing any additional steps.