Showing posts with label Shell Script. Show all posts
Showing posts with label Shell Script. Show all posts

Tuesday, February 21, 2017

A Caveman's Bash Editor

WSO2 is in the process of improving its CI/CD pipeline in preparation for a bunch of new products(btw did you hear about our exciting new integration language? find more information here: http://ballerinalang.org/) and yours truly was given the awesome responsibility of creating a jenkins post build shell script inline with this goal. During this process I noticed a serious lack of tools for shell/bash script writers and so i wrote this simple helper script.

bash script
vim script


How to get it working,

  1. Install vim if you don’t have it in your box.
  2. Copy the vimrc file to $HOME/.vim/
  3. Move CavemansBashEditor.sh to a location of your liking and run it providing the bash script that needs to be syntax checked as a script parameter e.g. ./CavemansBashEditor.sh /home/dumiduh/myscript.sh
  4. Leave the script running and refer to it while editing your script(Refer screenshot below).




Notes,

  • The script uses ‘bash -n’ linux command and will only pick up syntax errors it picks up.
  • The vimrc file changes the vim editors behavior so that it automatically saves changes, this behavior can be a pain at times so remove the file from $HOME/.vim/ when the script is not used.
  • The script was tested on Ubuntu 14.04 with vim 2:7.4.052-1ubuntu3


Saturday, August 29, 2015

How to Invoke a Shell Script using WSO2 ESB


whats covered: creating a custom mediator to invoke shell scripts for ESB 4.8.1.


1) Create a Mediator Project


Generate a mediator project using WSO2 Developer Studio. Developer Studio Dashboard > Mediator Project.


2) Put in the logic to execute shell scripts


exec() method of the of the current runtime object can be used for this purpose. put the logic inside the mediate method(this method should return true to continue the mediation flow). Find the complete code here[1]


    ...
    ...   
    public String execute()
    {
        StringBuilder output = new StringBuilder();
        Process p;
        try
        {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }

        return output.toString();
    }
    ...
    ...


3) Build and Copy the jar

build the project. Copy the created jar file to <ESB_HOME>/repository/components/lib .


4) Create a proxy service

create a proxy service with the class mediator in the mediation path.

...
...
<inSequence>
<class name="org.wso2.demo.ShellScriptMediator">
      <property name="scriptname"
                value="/home/dumiduh/backup_script.sh"/>
      <property name="scriptparam"
                value="/home/dumiduh/BACKUPS"/>
</class>
<drop/>
</inSequence>
...
...


[1] - https://github.com/handakumbura/ShellScriptMediatorDemo/tree/master

Find more info on the class mediator here,
https://docs.wso2.com/display/ESB481/Class+Mediator


What's in my Bag? EDC of a Tester