Fix pylint warnings in fetch_url_and_verify
[yardstick.git] / ansible / library / fetch_url_and_verify.py
1 #!/usr/bin/env python
2 # Copyright (c) 2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # 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 # pylint: disable-all
17
18 DOCUMENTATION = '''
19 ---
20 module: fetch_url_and_verify
21 short_description: Fetch image and verify against a SHA256SUM URL
22 description:
23     - Download a URL and check it against a remote SHA256SUMS file
24 options:
25   url: Image URL
26   image_dest: Image filename
27   sha256_url: SHA256SUMS URL
28   dest: python file mode (w, wb, a, ab)
29   retries: fetch retries
30 '''
31
32
33 def main():
34     module = AnsibleModule(
35         argument_spec={
36             'url': {'required': True, 'type': 'str'},
37             'sha256url': {'required': True, 'type': 'str'},
38             'dest': {'required': True, 'type': 'path'},
39             'retries': {'required': False, 'type': 'int', 'default': 3},
40         }
41     )
42     params = module.params
43     url = params['url']
44     dest = params['dest']
45     sha256url = params['sha256url']
46     retries = params['retries']
47
48     image_dir, image_filename = os.path.split(dest)
49     rc, stdout, stderr = module.run_command(['curl', '-sS', sha256url])
50     if rc == 0 and stdout:
51         sha256line = next(
52             (l for l in stdout.splitlines() if image_filename in l), "")
53     if not sha256line:
54         module.fail_json(
55             msg="Unable to find SHA256SUMS line for file {}".format(
56                 image_filename))
57     rc = \
58     module.run_command(['sha256sum', '-c'], data=sha256line, cwd=image_dir)[0]
59     if rc == 0:
60         sha256sum = sha256line.split()[0]
61         module.exit_json(changed=False, dest=dest, url=url,
62                          sha256sum=sha256sum)
63
64     for _ in range(retries):
65         curl_rc, stdout, stderr = module.run_command(
66             ['curl', '-sS', '-o', dest, url], cwd=image_dir)
67         if curl_rc == 0:
68             sha256_rc, stdout, stderr = module.run_command(['sha256sum', '-c'],
69                                                            data=sha256line,
70                                                            cwd=image_dir)
71             if sha256_rc == 0:
72                 module.exit_json(changed=True)
73
74     module.fail_json(msg="Unable to download {}".format(url), stdout=stdout,
75                      stderr=stderr)
76
77
78 # <<INCLUDE_ANSIBLE_MODULE_COMMON>>
79 from ansible.module_utils.basic import *
80
81 if __name__ == '__main__':
82     main()