1## Jobs
2
3### List jobs
4
5This endpoint lists all known jobs in the system registered with Nomad.
6
7https://www.nomadproject.io/api/jobs.html#list-jobs
8
9Example:
10
11```
12import nomad
13
14my_nomad = nomad.Nomad(host='192.168.33.10')
15
16jobs = my_nomad.jobs.get_jobs()
17
18for job in jobs:
19  print (job)
20```
21
22### Create new job
23
24This endpoint creates (aka "registers") a new job in the system.
25
26https://www.nomadproject.io/api/jobs.html#create-job
27
28Example:
29
30```
31import nomad
32
33job = {'Job': {'AllAtOnce': None,
34  'Constraints': None,
35  'CreateIndex': None,
36  'Datacenters': ['dc1'],
37  'ID': 'example',
38  'JobModifyIndex': None,
39  'Meta': None,
40  'ModifyIndex': None,
41  'Name': 'example',
42  'Namespace': None,
43  'ParameterizedJob': None,
44  'ParentID': None,
45  'Payload': None,
46  'Periodic': None,
47  'Priority': None,
48  'Region': None,
49  'Stable': None,
50  'Status': None,
51  'StatusDescription': None,
52  'Stop': None,
53  'SubmitTime': None,
54  'TaskGroups': [{'Constraints': None,
55    'Count': 1,
56    'EphemeralDisk': {'Migrate': None, 'SizeMB': 300, 'Sticky': None},
57    'Meta': None,
58    'Name': 'cache',
59    'RestartPolicy': {'Attempts': 10,
60     'Delay': 25000000000,
61     'Interval': 300000000000,
62     'Mode': 'delay'},
63    'Tasks': [{'Artifacts': None,
64      'Config': {'image': 'redis:3.2', 'port_map': [{'db': 6379}]},
65      'Constraints': None,
66      'DispatchPayload': None,
67      'Driver': 'docker',
68      'Env': None,
69      'KillTimeout': None,
70      'Leader': False,
71      'LogConfig': None,
72      'Meta': None,
73      'Name': 'redis',
74      'Resources': {'CPU': 500,
75       'DiskMB': None,
76       'IOPS': None,
77       'MemoryMB': 256,
78       'Networks': [{'CIDR': '',
79         'Device': '',
80         'DynamicPorts': [{'Label': 'db', 'Value': 0}],
81         'IP': '',
82         'MBits': 10,
83         'ReservedPorts': None}]},
84      'Services': [{'AddressMode': '',
85        'CheckRestart': None,
86        'Checks': [{'Args': None,
87          'CheckRestart': None,
88          'Command': '',
89          'Header': None,
90          'Id': '',
91          'InitialStatus': '',
92          'Interval': 10000000000,
93          'Method': '',
94          'Name': 'alive',
95          'Path': '',
96          'PortLabel': '',
97          'Protocol': '',
98          'TLSSkipVerify': False,
99          'Timeout': 2000000000,
100          'Type': 'tcp'}],
101        'Id': '',
102        'Name': 'global-redis-check',
103        'PortLabel': 'db',
104        'Tags': ['global', 'cache']}],
105      'ShutdownDelay': 0,
106      'Templates': None,
107      'User': '',
108      'Vault': None}],
109    'Update': None}],
110  'Type': 'service',
111  'Update': {'AutoRevert': False,
112   'Canary': 0,
113   'HealthCheck': None,
114   'HealthyDeadline': 180000000000,
115   'MaxParallel': 1,
116   'MinHealthyTime': 10000000000,
117   'Stagger': None},
118  'VaultToken': None,
119  'Version': None}}
120
121my_nomad = nomad.Nomad(host='192.168.33.10')
122
123response = my_nomad.jobs.register_job(job)
124```
125
126
127### Parse Job
128To convert to python dict and verify for correctness a hcl/nomad file. The example will use "example.nomad" when running
129`nomad job init` and it will assume this file is in the current working directory. In practice this file should already
130be read and used as the parameter hcl.
131
132https://www.nomadproject.io/api/jobs.html#parse-job
133
134```python
135
136import nomad
137
138nomad_client = nomad.Nomad()
139
140with open("example.nomad", "r") as fh:
141    try:
142        job_raw_nomad = fh.read()
143        job_dict = nomad_client.jobs.parse(job_raw_nomad)
144    except nomad.api.exceptions.BadRequestNomadException as err:
145        print(err.nomad_resp.reason)
146        print(err.nomad_resp.text)
147```
148
149On success of example.nomad being successfully parsed job_dict will have:
150
151```python
152{'AllAtOnce': None,
153 'Constraints': None,
154 'CreateIndex': None,
155 'Datacenters': ['dc1'],
156 'Dispatched': False,
157 'ID': 'example',
158 'JobModifyIndex': None,
159 'Meta': None,
160 'Migrate': {'HealthCheck': 'checks',
161             'HealthyDeadline': 300000000000,
162             'MaxParallel': 1,
163             'MinHealthyTime': 10000000000},
164 'ModifyIndex': None,
165 'Name': 'example',
166 'Namespace': None,
167 'ParameterizedJob': None,
168 'ParentID': None,
169 'Payload': None,
170 'Periodic': None,
171 'Priority': None,
172 'Region': None,
173 'Reschedule': None,
174 'Stable': None,
175 'Status': None,
176 'StatusDescription': None,
177 'Stop': None,
178 'SubmitTime': None,
179 'TaskGroups': [{'Constraints': None,
180                 'Count': 1,
181                 'EphemeralDisk': {'Migrate': None,
182                                   'SizeMB': 300,
183                                   'Sticky': None},
184                 'Meta': None,
185                 'Migrate': None,
186                 'Name': 'cache',
187                 'ReschedulePolicy': None,
188                 'RestartPolicy': {'Attempts': 2,
189                                   'Delay': 15000000000,
190                                   'Interval': 1800000000000,
191                                   'Mode': 'fail'},
192                 'Tasks': [{'Artifacts': None,
193                            'Config': {'image': 'redis:3.2',
194                                       'port_map': [{'db': 6379}]},
195                            'Constraints': None,
196                            'DispatchPayload': None,
197                            'Driver': 'docker',
198                            'Env': None,
199                            'KillSignal': '',
200                            'KillTimeout': None,
201                            'Leader': False,
202                            'LogConfig': None,
203                            'Meta': None,
204                            'Name': 'redis',
205                            'Resources': {'CPU': 500,
206                                          'DiskMB': None,
207                                          'IOPS': None,
208                                          'MemoryMB': 256,
209                                          'Networks': [{'CIDR': '',
210                                                        'Device': '',
211                                                        'DynamicPorts': [{'Label': 'db',
212                                                                          'Value': 0}],
213                                                        'IP': '',
214                                                        'MBits': 10,
215                                                        'ReservedPorts': None}]},
216                            'Services': [{'AddressMode': '',
217                                          'CanaryTags': None,
218                                          'CheckRestart': None,
219                                          'Checks': [{'AddressMode': '',
220                                                      'Args': None,
221                                                      'CheckRestart': None,
222                                                      'Command': '',
223                                                      'GRPCService': '',
224                                                      'GRPCUseTLS': False,
225                                                      'Header': None,
226                                                      'Id': '',
227                                                      'InitialStatus': '',
228                                                      'Interval': 10000000000,
229                                                      'Method': '',
230                                                      'Name': 'alive',
231                                                      'Path': '',
232                                                      'PortLabel': '',
233                                                      'Protocol': '',
234                                                      'TLSSkipVerify': False,
235                                                      'Timeout': 2000000000,
236                                                      'Type': 'tcp'}],
237                                          'Id': '',
238                                          'Name': 'redis-cache',
239                                          'PortLabel': 'db',
240                                          'Tags': ['global', 'cache']}],
241                            'ShutdownDelay': 0,
242                            'Templates': None,
243                            'User': '',
244                            'Vault': None}],
245                 'Update': None}],
246 'Type': 'service',
247 'Update': {'AutoRevert': False,
248            'Canary': 0,
249            'HealthCheck': None,
250            'HealthyDeadline': 180000000000,
251            'MaxParallel': 1,
252            'MinHealthyTime': 10000000000,
253            'ProgressDeadline': None,
254            'Stagger': None},
255 'VaultToken': None,
256 'Version': None}
257```
258
259On failure it will raise `BadRequestNomadException` we can inspect the requests response:
260```
261>>> err.nomad_resp.reason
262'Bad Request'
263
264err.nomad_resp.text
265"error parsing 'job': 1 error(s) occurred:\n\n* job: invalid key: datacenter"
266
267```