During this tutorial we will start from a clean installation of csm-orc and work our way to an
orchestrated solution.
Set up our project
# First we will create a new folder for our project
mkdirMyFirstOrchestrationProject
cdMyFirstOrchestrationProject
# Now that we are in our project folder we will set up the orchestrator using a python venv# We create the venv in the folder `.venv`
python-mvenv.venv
# We activate the venv
..venv/bin/activate
# Now we can install the orchestrator using pip
pipinstallcosmotech-run-orchestrator
# We can check that our installation worked by running the orchestrator help
csm-orc--help
After all that our project is ready to start
Creating our first scripts
In this part we will create 2 simple python script that will interact by using a common file,
the first script should write the first N members of the Fibonacci sequence on the file,
and the second one will display them.
fibonacci.py
1 2 3 4 5 6 7 8 91011121314151617181920212223
importargparseparser=argparse.ArgumentParser(description="Fibonacci printer")parser.add_argument("n",type=int,help="The max rank of the fibonacci sequence to write")parser.add_argument("filename",type=argparse.FileType('w'),help="The file to write to")deffibonacci_sequence(n:int=0):a,b=0,1for_inrange(n):yieldaa,b=b,a+bif__name__=="__main__":args=parser.parse_args()withargs.filenameasf:forvinfibonacci_sequence(args.n):f.write(f"{v}\n")
display_file.py
1 2 3 4 5 6 7 8 910111213
importargparseparser=argparse.ArgumentParser(description="File printer")parser.add_argument("filename",type=argparse.FileType('r'),help="The file to print")if__name__=="__main__":args=parser.parse_args()withargs.filenameasf:forvinf.readlines():print(v.strip())
{"$schema":"https://json-schema.org/draft/2020-12/schema","$id":"https://cosmotech.com/run_template.schema.json","title":"Run Template","description":"A run template description","type":"object","properties":{"commandTemplates":{"description":"A list of Commands Templates","type":"array","items":{"description":"A Command Template describe a single executable with default properties","type":"object","properties":{"id":{"type":"string","description":"The Id of the Command Template","pattern":"^\\S*$"},"description":{"type":"string","description":"A description of the command template"},"command":{"type":"string","description":"The root bash command necessary to execute the template"},"arguments":{"type":"array","description":"The list of default arguments passed to the command","items":{"type":"string"}},"useSystemEnvironment":{"type":"boolean","description":"Should the system environment be fully passed to the command ?"},"environment":{"type":"object","description":"The default list of Environment Variables required for the command","patternProperties":{".+":{"$ref":"#/$defs/environmentVariable"}},"minProperties":1}},"additionalProperties":false,"required":["id","command"]}},"steps":{"description":"A list of Steps descriptors","type":"array","items":{"description":"A Step is a single instance of a Command that is scheduled to be run, can use an existing command or define its own","type":"object","properties":{"id":{"type":"string","description":"The Id of the Step","pattern":"^\\S*$"},"description":{"type":"string","description":"A description of the step (override command template description)"},"commandId":{"type":"string","description":"An Id for an existing command"},"command":{"type":"string","description":"The root bash command necessary to execute the command"},"arguments":{"type":"array","description":"The list of arguments passed to the command (replace the default one)","items":{"type":"string"}},"useSystemEnvironment":{"type":"boolean","description":"Should the system environment be fully passed to the command ?"},"environment":{"type":"object","description":"The list of Environment Variables defined for the command (replace the default one)","patternProperties":{".+":{"$ref":"#/$defs/environmentVariable"}},"minProperties":1},"precedents":{"type":"array","description":"A list of steps that have to be run before this one","items":{"type":"string"}}},"additionalProperties":false,"oneOf":[{"required":["id","command"]},{"required":["id","commandId"]}]}}},"required":["steps"],"$defs":{"environmentVariable":{"type":"object","description":"A environment variable descriptor","properties":{"defaultValue":{"type":"string","description":"The default value of the required variable, if not set, the variable has to be set in the system"},"value":{"type":"string","description":"The effective value of the required variable, will override any system value"},"description":{"type":"string","description":"A description of the required Environment Variable for documentation reasons"}},"additionalProperties":false}}}
The first step
In the schema we can see that it is divided in two parts :
- the CommandTemplate
- the Step
For this first orchestration file we will only use steps.
In the example run.json you can see on line 12 the apparition of the key-words precedents,
it is used to order our operations.
Here by setting the step run-fibo as a precedent to the step run-display
we ensure that the first script will run before the second.
And now we created a simple example of orchestration file to run some of our scripts.
In the next tutorial we will look at how to use CommandTemplates to re-use possibly complex commands, and Environment Variables to change the effect of our commands.