Sparrowdo automation. Part 6. Sparrowdo modules - getting a bigger things using light primi...

Datetime:2016-08-22 23:00:33          Topic: Nginx  Perl           Share

This is what have been seen before:

Well, while keep writing a sparrowdo tutorial the tool keep growing too. Let me introduce something new and excited about sparrowdo automation - how one can easily create a higher level entities using so called sparrowdo modules .

Sparrowdo modules ...

So far we have talked about some sparrowdo primitives . They are light, they are small and they relate to a small specific tasks, under the hood they are just sparrow plugins with parameters - sparrowdo tasks.

Well, here is the list to recall a few:

  • System packages - package-generic plugin
  • CPAN packages - cpan-package plugin
  • Users and groups - are user and group plugins
  • Linux Services are represented by service plugin
  • And more and more and more ...

And so on, you can see all of them here, on sparrowhub site - https://sparrowhub.org/search . Most of sparrow plugins are just black boxes to solve a specific task. More or less plugins are just primitives .

Take a look at chef resources or ansible modules - they are probably of the same nature.

Now let's me introduce a sparrowdo module - a container for sparrow plugins.

Consider a quite common task. Installing Nginx web server. Having looked at sparrow toolkit we have all necessary bricks to "build" a running Nginx server:

  • Package-generic plugin to install nginx package
  • Service plugin to enable and run nginx service

Let's write code then:

use v6;

unit module Sparrowdo::Nginx;

use Sparrowdo;

our sub tasks (%args) {

  task_run  %(
    task => 'install nginx',
    plugin => 'package-generic',
    parameters => %( list => 'nginx' )
  );

  task_run  %(
    task => 'enable nginx',
    plugin => 'service',
    parameters => %( service => 'nginx', action => 'enable' )
  );

  task_run  %(
    task => 'start nginx',
    plugin => 'service',
    parameters => %( service => 'nginx', action => 'start' )
  );


}

Ok. The code is quite simple, I just want to add some vital comments here.

  • Sparrowdo modules are plain Perl6 modules.

  • You need to load Sparrowdo module to export some sparrowdo API function, like task_run or others.

  • You have to define at least a tasks(%args) function gets called when someone else use your modules, see how later

  • You optionally may handle some arguments get passed into your module, see tasks function signature.

  • And finally sparrowdo module is just a container for some sparrowdo tasks get called sequentially .

Ok. Now lets use our new sparrowdo module.

First step we need to ensure that module installed at the server where from we are going to run a sparrowdo tasks:

$ panda install Sparrowdo::Nginx

Ok when we are ready with module install we have two ways here.

  1. running module as is

  2. using module inside sparrowdo scenario.

Running module as is

This is the simplest way. This is very similar to running ansible modules:

sparrowdo --host=127.0.0.1 --module_run=Nginx

Running module via sparrowfile

Of course one can just use sparrowdo module using sparrowdo API

$ cat sparrowfile

run_module 'Nginx';

Sparrowdo uses a convention about modules names, it cut a Sparrowdo:: prefix from module name when run it via run_module function. So the rule is simple:

| Module Perl6 Name | Run_module $name parameter |
+-------------------+----------------------------+
| Sparrow::Foo::Bar | 'Foo::Bar'                 |

A current version of Sparrowdo::Nginx ignore an arguments, one day it would be possible to call 'Nginx' module with parameters:

run_module 'Nginx', %( port => 81 );

Little helpers for developers life

Sparrowdo provides some essentials helpers to simplify some developers tasks.

Guessing a target OS

It's very usual when we need to know a target server OS name to make a right decision about server configuration. Let me show you. Recall Nginx module, for centos we need install a repository so nginx package is not here by default:

our sub tasks (%args) {

  if target_os() ~~ m/centos/ {

    task_run  %(
      task => 'install epel-release',
      plugin => 'package-generic',
      parameters => %( list => 'epel-release' )
    );

  }

Passing a sparrowdo command line parameters

Remember apost on installing CPAN packages on the servers with http proxy restrictions?

Consider this sparrwodo module to install CPAN modules:

use v6;

unit module Sparrowdo::CpanInstall;

use Sparrowdo;

our sub tasks (%args) {

  task_run  %(
    task => 'install cpan modules',
    plugin => 'cpan-package',
    parameters => %( 
      list => %args<list>,
      http_proxy => input_params('HttpProxy'), 
      https_proxy => input_params('HttpsProxy'), 
    )
  );

}

And sparrowfile:

module_run 'CpanInstall' %(
  list => 'DBI Moose Mojolicious'
);

And finally sparrowdo scenario run:

$ sparrwodo --host=<$some-host> --http_proxy=<$http_proxy>  --https_proxy=<$https_proxy>

An input_params($param_name) function will pass all the sparrowdo client parameters back to Sparrowdo::CpanInstall module.

Conclusion

Sparrowdo modules are high level members of sparrowdo echo system. What is good they are just Perl6 modules. Anybody who codes at Perl6 could easily start a new ones ( a basic knowledge of existed sparrow plugins is required though ).

Here is a short list of my ones:

https://modules.perl6.org/#q=Sparrowdo%3A%3A

Will be happy to see a new members of Sparrowdo::* family at Perl6 modules repository.

Regards and have a good weekend.

Alexey Melezhik





About List