Sparrow - Simple Deploy of Ruby scripts
Tags: ruby, sparrow, automation, perl
Published: 2018-09-20 17:16:49

---
title: Sparrow - Simple Deploy of Ruby scripts
published: true
description: How to deploy Riby scripts using Sparrow script management platform
tags: ruby, sparrow, automation, perl
---

[Sparrow](https://github.com/melezhik/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](https://dev.to/melezhik/sparrow---simple-deploy-of-python-scripts-3iel), 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}"

```


```
script.rb --main.foo=1 --main.bar=2

```

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

```


## 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"
}
```


## "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
```


## 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']
```


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


```
$ nano suite.yaml

main:
  foo: 1
  bar: 2
```

## Declare dependecies


Sparrow uses [bundler](https://bundler.io/) to resolve Ruby modules dependencies comes with your script.  Just create `Gemfile` in [gemfile](https://bundler.io/v1.16/gemfile_man.html) format to declare all the dependencies if you have any:

```
$ nano Gemfile
```


## 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"
}

```


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
```

Now we are safe to make upload:


```
$ sparrow plg upload
sparrow.json file validated ...
plugin ruby-echo-script version 0.001000 upload OK
```

As an immediate benefit, the plugin you've just uploaded, is available through SparrowHub web site, just [click here](https://sparrowhub.org/info/ruby-echo-script) 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](https://raw.githubusercontent.com/melezhik/ruby-echo-script/master/plugin-page.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

```

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

```

```
$ 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

```


# 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
```


# Comments, questions, ideas


As always are welcome.


---


***SparrowHub - small scripts to make bigger things***


Alexey