Sparrow - Simple Deploy of Ruby scripts

Alexey Melezhik - Sep 20 '18 - - Dev Community

Sparrow is a script development and management platform. Sparrow supports scripts written on 4 languages - Perl, Bash, Python and Ruby.

Specially for the Ruby developers in this post I am going to show how to deploy Ruby scripts using Sparrow.

This is post is the second one in my Sparrow post series. The previous one was
about Using Sparrow to deploy Python scripts, so if you're Python developer - read that post (:

But before going into tech details lets brief why you might need this:

Motivation

You want to share your scripts with others. For some reasons you are not ready to distribute your script as RubyGem module and they are:

  • Your script is just a script, there is no modules here, so RubyGem distribution might be overkill.

  • Your script is a private tool, you don't want to distribute it through the public pip repo, and maintaining private RubyGem repo is an effort when you just want to distribute a script.

So, go ahead and see how you can distribute scripts with Sparrow ...

Script

Say you have script script.rb which just takes a couple of parameters from command line and print them ( we omit the code that parses command line parameters, you will see why later):

$ cat script.rb

puts "main.foo is #{foo}"
puts "main.bar is #{bar}"

Enter fullscreen mode Exit fullscreen mode
script.rb --main.foo=1 --main.bar=2

Enter fullscreen mode Exit fullscreen mode

Here are few simple steps to make a Sparrow distribution for this script:

Install Sparrow

We will need a Sparrow cli to upload script to SparrowHub - script repository.
We might also need a tool to test the script before we upload it, it is called strun, we will see later how we are going use it. strun is installed as apart of Sparrow cli.

$ cpanm Sparrow

Enter fullscreen mode Exit fullscreen mode

Create SparrowHub account

Go to https://sparrowhub.org/sign_up

Generate Sparrowhub token:

Sparrowhub token is special UID assigned to your Sparrowhub account and it is required when you upload distribution to SparrowHub - scripts repository.
Token is authentication mechanism used by SparrowHub when your upload data.

$ cat ~/sparrowhub.json

{
    "user"  : "melezhik",
    "token" : "ADB4F4DC-9F3B-11E5-B394-D4E152C9AB83"
}
Enter fullscreen mode Exit fullscreen mode

"Convert" your script into Sparrow plugin:

Name your script story.rb

As Sparrow takes some conventions on running script, we should follow them, that's not difficult though:

$ mv script.rb story.rb
Enter fullscreen mode Exit fullscreen mode

Handle input parameters

Sparrow comes with out of the box mechanism to handle input parameters for scripts, just add the lines to the beginning of yours one:

$ nano story.rb


foo = config['main']['foo']
bar = config['main']['bar']
Enter fullscreen mode Exit fullscreen mode

If you want to set default setting for input parameters, you can easy do so:

$ nano suite.yaml

main:
  foo: 1
  bar: 2
Enter fullscreen mode Exit fullscreen mode

Declare dependecies

Sparrow uses bundler to resolve Ruby modules dependencies comes with your script. Just create Gemfile in gemfile format to declare all the dependencies if you have any:

$ nano Gemfile
Enter fullscreen mode Exit fullscreen mode

Create distribution file

Sparrow metafile is simple file in JSON format describing your distribution, the minimum configuration could look like:

$ nano sparrow.json
{
    "name": "ruby-echo-script",
    "version": "0.1.0",
    "description" : "this is my simple echo script"
}

Enter fullscreen mode Exit fullscreen mode

Ok, in just creating 3 files ( besides initial script ) and making slight modification of initial Ruby script we are ready for distribution.

But before upload let's test our script. We are going to use strun - internal Sparrow script runner, which factually run scripts:

$ strun --param main.foo=10 --param main.bar=20

2018-09-13 18:50:07 :  [path] /
main.foo is 1
main.bar is 2
ok      scenario succeeded
STATUS  SUCCEED
Enter fullscreen mode Exit fullscreen mode

Now we are safe to make upload:

$ sparrow plg upload
sparrow.json file validated ...
plugin ruby-echo-script version 0.001000 upload OK
Enter fullscreen mode Exit fullscreen mode

As an immediate benefit, the plugin you've just uploaded, is available through SparrowHub web site, just click here and you will get a nice page with your distribution details:

https://raw.githubusercontent.com/melezhik/ruby-echo-script/master/sparrow-ruby-echo-script-plugin.png

I have not told you, but that markdown documentation you could have added is also supported (: , just place README.md to your distribution!

Ok let's go to installation phase, when someone want to run your script.

Installing distribution

Now your colleagues mates have been looking forward for your neat script to run. Well with Sparrow it's just a piece of cake. These are free simple steps:

  • Install Sparrow client
  • Install your distribtion
  • And ... yeah ... run your scripts

It's just three simple steps:

$ cpanm Sparrow

Enter fullscreen mode Exit fullscreen mode
$ sparrow index update && sparrow plg install ruby-echo-script
get index updates from SparrowHub ... OK
installing public@ruby-echo-script version 0.001000 ...

Enter fullscreen mode Exit fullscreen mode
$ sparrow plg run ruby-echo-script --param main.foo=1000 --param main.bar=2000

2018-09-13 18:57:57 : [plg] ruby-echo-script [path] /
main.foo is 1000
main.bar is 2000
ok      scenario succeeded
STATUS  SUCCEED

Enter fullscreen mode Exit fullscreen mode

Distributing private scripts

And finally last and not the least. In case you want to keep you scripts private and aren't willing to use public SparrowHub repository, happily Sparrow supports remote git distrubtion. Just place those 4 files into Git repo and setup Sparrow client on target host using those one:

$ nano ~/sparrow.list
ruby-echo-script   https://github.com/melezhik/ruby-echo-script.git
Enter fullscreen mode Exit fullscreen mode

Comments, questions, ideas

As always are welcome.


SparrowHub - small scripts to make bigger things

Alexey

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player