📜 ⬆️ ⬇️

Developing wrapper scripts using the Sparrow tool

Good time of day! In this post I want to tell you how to use the Sparrow tool to easily and simply write your own wrappers to existing scripts and utilities, as well as why you may need it.


Very often we deal with various scripts that we have to run for different tasks and on different servers. These scripts can be written by us or installed as part of software packages. Anyway, very often the task comes down to the fact that you just need to run a certain script with a set of parameters:


script <arguments> 

The main difficulty here can be that the script arguments can be quite spreading and complex, and at the same time, depending on the context of the task, also different. This gives rise to a number of inconveniences, of course, not so critical, but nevertheless which I would like to mention:



So, here we can get out the wrapper scripts that will encapsulate everything related to the logic of preparing input parameters for a given script, and then run this script with these parameters.


Instead of writing your own wrapper scripts, you can use the Sparrow tool, and I'll show you now how easy and simple this can be.


Sparrow installation


Sparrow is a CPAN module, so we set it accordingly:


 $ cpanm Sparrow 

Select the script for which we will write the wrapper


Since this is a tutorial article, I’ll choose any script, for purely informational purposes, in real life, it will be a script or scripts that you use in your work. Let it be the speedtest-cli utility designed to test the speed of the Internet on your local host. Judging by the documentation, the script has a sufficiently large number of settings that are specified through command line arguments - just the very case when a wrapper may be appropriate. Imagine that we want to run this script on crown and send reports to analyze the availability of the Internet for a certain period of time. Let's say, on. interested in two options for calling the script:


 speedtest-cli --no-download #     

and


 speedtest-cli --no-upload #     

And in both cases, we want to always add an option.


 --bytes #      ,     

Also, suppose that in the first case we want to specify a wait timeout for http requests:


 --timeout 10 # http - 

Ok, so we have two separate speedtest-cli runs with different parameters.


Writing a wrapper script in the form of a Sparrow plugin


Create a script story:


 $ nano story.bash speedtest-cli $(args_cli) 

In this case, all the work is done by the predetermined Sparrow bash function args_cli , which transparently passes all input parameters to the speedtest-cli script.


Determine the boot loader speedtest-cli. Sparrow is able to set dependencies for scripts, supporting a number of package managers defined for various programming languages, including Python. The speedtest-cli utility is installed as a pip module, so we simply define the dependency file in the style of the pip installer:


 $ nano requirements.txt speedtest-cli==1.0.6 

Well, let's go further, it remains to determine the file with the plugin meta data and actually upload it to the Sparrow plugin repository :


 $ nano sparrow.json { "name" : "speedtest-cli", "description" : "Simple wrapper for speedtest-cli from https://github.com/sivel/speedtest-cli", "version" : "0.0.1", "url" : "https://github.com/melezhik/sparrow-plugins/tree/master/speedtest-cli", "category": "utilities", "python_version" : 2, "sparrow_version": "0.2.45" } 

The file format of the meta data for the Sparrow plugin is described in detail in the documentation for Sparrow, we will not analyze it here, we only care that the plugin is called like a script for which it provides a wrapper.


Now we are ready to load the plugin into the repository:


  $ sparrow plg upload #        

Configuring Sparrow Tasks to Run a Wrapper Script


This is where the fun begins. This is how we will use the plugin we created to run the speedtest-cli utility described earlier.


Suppose we want to run this utility on another server. It is not difficult to guess that first we need to install the appropriate Sparrow plugin:


 $ sparrow plg install speedtest-cli 

If everything goes well, we will get the Sparrow plugin installed and the speedtest-cli utility itself, along with all the Python dependencies.


Let's make a simple verification that the plugin works:


  $ sparrow plg run speedtest-cli -- --help 

If all is well, then we will get help from the speedtest-cli utility.


In order to associate the launched plugin with certain parameters, we create tasks. We recall that we need to run speedtest-cli with different arguments.


  $ sparrow project create monitoring #   -     $ sparrow task add monitoring nettest-download speedtest-cli $ sparrow task add monitoring nettest-upload speedtest-cli 

With the last two commands, we have created tasks for different launches of the speedtest-cli utility, now we will configure them:


 $ sparrow task ini monitoring/nettest-download --- args: - timeout: 10 - - bytes - no-upload 

$ sparrow task ini monitoring / nettest-upload


  --- args: - - bytes - no-download 

Now we just need to run our tasks:


 $ sparrow task run monitoring/nettest-upload $ sparrow task run monitoring/nettest-download 

Our wrappers are ready and working properly.


Conclusion


Sparrow makes it easy and simple to write wrappers for almost any console utility. This eliminates the need to write separate scripts to run the same utility with different parameters, for this there is a simple and powerful tool Sparrow tasks. Sparrow plugins are portable to almost any Linux server where Perl is installed. Also setting up Sparrow-style scripts involves generating input data in the YAML and JSON formats, which greatly simplifies running such scripts from any modern programming languages ​​and makes their automation more simple. For example, you can see the Sparrowdo project, which allows you to configure and run Sparrow tasks remotely via ssh.


Related Links



Sincerely, Alexey Melezhik, author Sparrow


')

Source: https://habr.com/ru/post/333410/


All Articles