Sparrow is a script development and management platform. Sparrow supports scripts written on 4 languages - Perl, Bash, Python and Ruby.
Specially for the Python developers in this post I am going to show how to deploy Python scripts using Sparrow.
The idea for this post has been raised after reading this stackoverflow question.
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 pip module and they are:
Your script is just a script, there is no modules here, so pip 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 pip repo is an effort when you just want to distribute a script
So, go ahead and see how you can distribute a script with Sparrow ...
Script
Say you have script script.py
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.py
print "main.foo is", foo
print "main.bar is", bar
script.py --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.py
As Sparrow takes some conventions on running script, we should follow them, that's not difficult though:
$ mv script.py story.py
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 your script:
$ nano story.py
#!python
from outthentic import *
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 pip installer to resolve Python modules dependencies comes with your script. Just create requirements.txt
in pip requirements format to declare all the dependencies if you have any:
$ nano requirements.txt
Create distribution file
Sparrow metafile is simple file in JSON format describing your distribution, the minimum configuration could look like:
$ nano sparrow.json
{
"name": "python-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 python 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 python-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 and you will get a nice page with your distribution details:
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 python-echo-script
get index updates from SparrowHub ... OK
installing public@python-echo-script version 0.001000 ...
$ sparrow plg run python-echo-script --param main.foo=1000 --param main.bar=2000
2018-09-13 18:57:57 : [plg] python-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
python-echo-script https://github.com/melezhik/python-echo-script.git
Comments, questions, ideas
As always are welcome.
SparrowHub - small scripts to make bigger things
Alexey