Merge branch 'master' into 1971-show-image-thumbnails
[arvados.git] / doc / user / tutorials / tutorial-firstscript.html.textile.liquid
1 ---
2 layout: default
3 navsection: userguide
4 navmenu: Tutorials
5 title: "Writing a pipeline"
6 ...
7
8 In this tutorial, we will write the "hash" script demonstrated in the first tutorial.
9
10 *This tutorial assumes that you are "logged into an Arvados VM instance":{{site.baseurl}}/user/getting_started/ssh-access.html#login, and have a "working environment.":{{site.baseurl}}/user/getting_started/check-environment.html*
11
12 This tutorial uses *@you@* to denote your username.  Replace *@you@* with your user name in all the following examples.
13
14 h2. Setting up Git
15
16 All Crunch scripts are managed through the @git@ revision control system.  Before you start using git, you should do some basic configuration (you only need to do this the first time):
17
18 <notextile>
19 <pre><code>~$ <span class="userinput">git config --global user.name "Your Name"</span>
20 ~$ <span class="userinput">git config --global user.email <b>you</b>@example.com</span></code></pre>
21 </notextile>
22
23 On the Arvados Workbench, navigate to "Compute %(rarr)&rarr;% Code repositories":https://{{site.arvados_workbench_host}}/repositories.  You should see a repository with your user name listed in the *name* column.  Next to *name* is the column *push_url*.  Copy the *push_url* value associated with your repository.  This should look like <notextile><code>git@git.{{ site.arvados_api_host }}:<b>you</b>.git</code></notextile>.
24
25 Next, on the Arvados virtual machine, clone your git repository:
26
27 <notextile>
28 <pre><code>~$ <span class="userinput">git clone git@git.{{ site.arvados_api_host }}:<b>you</b>.git</span>
29 Cloning into '<b>you</b>'...</code></pre>
30 </notextile>
31
32 This will create an git checkout in the directory called *@you@*.
33
34 {% include 'notebox_begin' %}
35 For more information about using @git@, try
36
37 notextile. <pre><code>$ <span class="userinput">man gittutorial</span></code></pre>
38
39 or *"search Google for git tutorials":http://google.com/#q=git+tutorial*.
40 {% include 'notebox_end' %}
41
42 h2. Creating a Crunch script
43
44 Start by entering the *@you@* directory created by @git clone@.  Next create a subdirectory called @crunch_scripts@ and change to that directory:
45
46 <notextile>
47 <pre><code>~$ <span class="userinput">cd <b>you</b></span>
48 ~/<b>you</b>$ <span class="userinput">mkdir crunch_scripts</span>
49 ~/<b>you</b>$ <span class="userinput">cd crunch_scripts</span></code></pre>
50 </notextile>
51
52 Next, using @nano@ or your favorite Unix text editor, create a new file called @hash.py@ in the @crunch_scripts@ directory.
53
54 notextile. <pre>~/<b>you</b>/crunch_scripts$ <code class="userinput">nano hash.py</code></pre>
55
56 Add the following code to compute the md5 hash of each file in a collection:
57
58 <notextile> {% code 'tutorial_hash_script_py' as python %} </notextile>
59
60 Make the file executable:
61
62 notextile. <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">chmod +x hash.py</span></code></pre>
63
64 {% include 'notebox_begin' %}
65 The steps below describe how to execute the script after committing changes to git. To run a script locally for testing, please see "debugging a crunch script":{{site.baseurl}}/user/topics/tutorial-job-debug.html.
66
67 {% include 'notebox_end' %}
68
69 Next, add the file to git staging.  This tells @git@ that the file should be included on the next commit.
70
71 notextile. <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">git add hash.py</span></code></pre>
72
73 Next, commit your changes to git.  All staged changes are recorded into the local git repository:
74
75 <notextile>
76 <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">git commit -m"my first script"</span>
77 [master (root-commit) 27fd88b] my first script
78  1 file changed, 33 insertions(+)
79  create mode 100755 crunch_scripts/hash.py</code></pre>
80 </notextile>
81
82 Finally, upload your changes to the Arvados server:
83
84 <notextile>
85 <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">git push origin master</span>
86 Counting objects: 4, done.
87 Compressing objects: 100% (2/2), done.
88 Writing objects: 100% (4/4), 682 bytes, done.
89 Total 4 (delta 0), reused 0 (delta 0)
90 To git@git.qr1hi.arvadosapi.com:you.git
91  * [new branch]      master -> master</code></pre>
92 </notextile>
93
94 h2. Create a pipeline template
95
96 Next, create a file that contains the pipeline definition:
97
98 <notextile>
99 <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">cd ~</span>
100 ~$ <span class="userinput">cat &gt;the_pipeline &lt;&lt;EOF
101 {
102   "name":"My first pipeline",
103   "components":{
104     "do_hash":{
105       "script":"hash.py",
106       "script_parameters":{
107         "input":{
108           "required": true,
109           "dataclass": "Collection"
110         }
111       },
112       "repository":"$USER",
113       "script_version":"master",
114       "output_is_persistent":true
115     }
116   }
117 }
118 EOF
119 </span></code></pre>
120 </notextile>
121
122 * @cat@ is a standard Unix utility that writes a sequence of input to standard output.
123 * @<<EOF@ tells the shell to direct the following lines into the standard input for @cat@ up until it sees the line @EOF@.
124 * @>the_pipeline@ redirects standard output to a file called @the_pipeline@.
125 * @"name"@ is a human-readable name for the pipeline.
126 * @"components"@ is a set of scripts that make up the pipeline.
127 * The component is listed with a human-readable name (@"do_hash"@ in this example).
128 * @"repository"@ is the name of a git repository to search for the script version.  You can access a list of available git repositories on the Arvados Workbench under "Compute %(rarr)&rarr;% Code repositories":https://{{site.arvados_workbench_host}}/repositories.  Your shell should automatically fill in @$USER@ with your login name, so that the final JSON has @"repository"@ pointed at your personal git repository.
129 * @"script_version"@ specifies the version of the script that you wish to run.  This can be in the form of an explicit git revision hash, a tag, or a branch (in which case it will use the HEAD of the specified branch).  Arvados logs the script version that was used in the run, enabling you to go back and re-run any past job with the guarantee that the exact same code will be used as was used in the previous run.
130 * @"script"@ specifies the filename of the script to run.  Crunch expects to find this in the @crunch_scripts/@ subdirectory of the git repository.
131 * @"script_parameters"@ describes the parameters for the script.  In this example, there is one parameter called @input@ which is @required@ and is a @Collection@.
132 * @"output_is_persistent"@ indicates whether the output of the job is considered valuable. If this value is false (or not given), the output will be treated as intermediate data and eventually deleted to reclaim disk space.
133
134 Now, use @arv pipeline_template create@ to register your pipeline template in Arvados:
135
136 <notextile>
137 <pre><code>~$ <span class="userinput">arv pipeline_template create --pipeline-template "$(cat the_pipeline)"</span>
138 </code></pre>
139 </notextile>
140
141 Your new pipeline template will appear on the Workbench "Compute %(rarr)&rarr;% Pipeline&nbsp;templates":https://{{ site.arvados_workbench_host }}/pipeline_instances page.  You can run the "pipeline using Workbench":tutorial-pipeline-workbench.html.