Add ansible scripts to deploy Kubernetes
[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     sha256line = ''
48
49     image_dir, image_filename = os.path.split(dest)
50     rc, stdout, stderr = module.run_command(['curl', '-sS', sha256url])
51     if rc == 0 and stdout:
52         sha256line = next(
53             (l for l in stdout.splitlines() if image_filename in l), "")
54     if not sha256line:
55         module.fail_json(
56             msg="Unable to find SHA256SUMS line for file {}".format(
57                 image_filename))
58     rc = \
59     module.run_command(['sha256sum', '-c'], data=sha256line, cwd=image_dir)[0]
60     if rc == 0:
61         sha256sum = sha256line.split()[0]
62         module.exit_json(changed=False, dest=dest, url=url,
63                          sha256sum=sha256sum)
64
65     for _ in range(retries):
66         curl_rc, stdout, stderr = module.run_command(
67             ['curl', '-sS', '-o', dest, url], cwd=image_dir)
68         if curl_rc == 0:
69             sha256_rc, stdout, stderr = module.run_command(['sha256sum', '-c'],
70                                                            data=sha256line,
71                                                            cwd=image_dir)
72             if sha256_rc == 0:
73                 module.exit_json(changed=True)
74
75     module.fail_json(msg="Unable to download {}".format(url), stdout=stdout,
76                      stderr=stderr)
77
78
79 # <<INCLUDE_ANSIBLE_MODULE_COMMON>>
80 from ansible.module_utils.basic import *
81
82 if __name__ == '__main__':
83     main()