Merge "Add common openstack opertation scenarios: network"
[yardstick.git] / ansible / library / my_make.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2015, Linus Unnebäck <linus@folkdatorn.se>
5 #
6 # This file is part of Ansible
7 #
8 # This module is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This software is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this software.  If not, see <http://www.gnu.org/licenses/>.
20
21 from __future__ import absolute_import
22 DOCUMENTATION = '''
23 ---
24 module: my_make
25 short_description: Run targets in a Makefile
26 requirements: [ make ]
27 version_added: "2.1"
28 author: Linus Unnebäck (@LinusU) <linus@folkdatorn.se>
29 description:
30   - Run targets in a Makefile.
31 options:
32   target:
33     description:
34       - The target to run
35     required: false
36     default: none
37   params:
38     description:
39       - Any extra parameters to pass to make
40     required: false
41     default: none
42   extra_args:
43     description:
44       - Any extra options to pass to make
45     required: false
46     default: none
47   chdir:
48     description:
49       - cd into this directory before running make
50     required: true
51 '''
52
53 EXAMPLES = '''
54 # Build the default target
55 - make: chdir=/home/ubuntu/cool-project
56
57 # Run `install` target as root
58 - make: chdir=/home/ubuntu/cool-project target=install
59   become: yes
60
61 # Pass in extra arguments to build
62 - make:
63     chdir: /home/ubuntu/cool-project
64     target: all
65     params:
66       NUM_THREADS: 4
67       BACKEND: lapack
68 '''
69
70 # TODO: Disabled the RETURN as it was breaking docs building. Someone needs to
71 # fix this
72 RETURN = '''# '''
73
74
75 def format_params(params):
76     return [k + '=' + str(v) for k, v in params.items()]
77
78
79 def push_arguments(cmd, args):
80     if args['extra_args'] is not None:
81         cmd.extend(shlex.split(args['extra_args']))
82     if args['target'] is not None:
83         cmd.append(args['target'])
84     if args['params'] is not None:
85         cmd.extend(format_params(args['params']))
86     return cmd
87
88
89 def check_changed(make_path, module, args):
90     cmd = push_arguments([make_path, '--question'], args)
91     rc, _, __ = module.run_command(cmd, check_rc=False, cwd=args['chdir'])
92     return rc != 0
93
94
95 def run_make(make_path, module, args):
96     cmd = push_arguments([make_path], args)
97     module.run_command(cmd, check_rc=True, cwd=args['chdir'])
98
99
100 def main():
101     module = AnsibleModule(
102         supports_check_mode=True,
103         argument_spec=dict(
104             target=dict(required=False, default=None, type='str'),
105             params=dict(required=False, default=None, type='dict'),
106             extra_args=dict(required=False, default=None, type='str'),
107             chdir=dict(required=True, default=None, type='str'),
108         ),
109     )
110     args = dict(
111         changed=False,
112         failed=False,
113         target=module.params['target'],
114         params=module.params['params'],
115         extra_args=module.params['extra_args'],
116         chdir=module.params['chdir'],
117     )
118     make_path = module.get_bin_path('make', True)
119
120     # Check if target is up to date
121     args['changed'] = check_changed(make_path, module, args)
122
123     # Check only; don't modify
124     if module.check_mode:
125         module.exit_json(changed=args['changed'])
126
127     # Target is already up to date
128     if not args['changed']:
129         module.exit_json(**args)
130
131     run_make(make_path, module, args)
132     module.exit_json(**args)
133
134 from ansible.module_utils.basic import *
135
136 if __name__ == '__main__':
137     main()