Completed
Pull Request — master (#474)
by
unknown
02:57
created

GetJobStatus.run()   F

Complexity

Conditions 9

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
dl 0
loc 36
rs 3
c 3
b 0
f 0
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from lib.icsp import ICSPBaseActions
17
import eventlet
18
19
20
class GetJobStatus(ICSPBaseActions):
21
    def run(self, job_id, monitor, monitor_interval=120, connection_details=None):
22
        self.set_connection(connection_details)
23
        self.get_sessionid()
24
25
        output = {}
26
        endpoint = "/rest/os-deployment-jobs"
27
        if monitor and not job_id:
28
            raise ValueError("Unable to proceed. Monitor \
29
                             feature requires a single Job ID")
30
31
        if job_id:
32
            endpoint = endpoint + "/%s" % (job_id)
33
34
        jobs = self.icsp_get(endpoint)
35
        # Single Job ID doesn't have Members element
36
        if not job_id:
37
            for job in jobs['members']:
38
                jobid = self.extract_id(job["uri"])
39
                output[jobid] = job['state']
40
        else:
41
            status = jobs['state']
42
            if monitor:
43
                jobid = self.extract_id(jobs["uri"])
44
                while status == "STATUS_ACTIVE":
45
                    eventlet.sleep(monitor_interval)
46
                    jobs = self.icsp_get(endpoint)
47
                    status = jobs['state']
48
                if status == 'STATUS_SUCCESS':
49
                    output[jobid] = jobs['state']
50
                else:
51
                    raise Exception("%s: %s" % (jobid, status))
52
            else:
53
                jobid = self.extract_id(jobs["uri"])
54
                output[jobid] = jobs['state']
55
56
        return {"jobs": output}
57
58