Completed
Pull Request — master (#544)
by
unknown
09:52 queued 03:16
created

GetJobStatus.run()   F

Complexity

Conditions 9

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
dl 0
loc 37
rs 3
c 0
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,
22
            connection_details=None):
23
        self.set_connection(connection_details)
24
        self.get_sessionid()
25
26
        output = {}
27
        endpoint = "/rest/os-deployment-jobs"
28
        if monitor and not job_id:
29
            raise ValueError("Unable to proceed. Monitor \
30
                             feature requires a single Job ID")
31
32
        if job_id:
33
            endpoint = endpoint + "/%s" % (job_id)
34
35
        jobs = self.icsp_get(endpoint)
36
        # Single Job ID doesn't have Members element
37
        if not job_id:
38
            for job in jobs['members']:
39
                jobid = self.extract_id(job["uri"])
40
                output[jobid] = job['state']
41
        else:
42
            status = jobs['state']
43
            if monitor:
44
                jobid = self.extract_id(jobs["uri"])
45
                while status == "STATUS_ACTIVE":
46
                    eventlet.sleep(monitor_interval)
47
                    jobs = self.icsp_get(endpoint)
48
                    status = jobs['state']
49
                if status == 'STATUS_SUCCESS':
50
                    output[jobid] = jobs['state']
51
                else:
52
                    raise Exception("%s: %s" % (jobid, status))
53
            else:
54
                jobid = self.extract_id(jobs["uri"])
55
                output[jobid] = jobs['state']
56
57
        return {"jobs": output}
58