/
composer.py
301 lines (241 loc) · 8.66 KB
/
composer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# -*- coding: utf-8 -*-
'''
Installation of Composer Packages
=================================
These states manage the installed packages for composer for PHP. Note that
either composer is installed and accessible via a bin directory or you can pass
the location of composer in the state.
.. code-block:: yaml
get-composer:
cmd.run:
- name: 'CURL=`which curl`; $CURL -sS https://getcomposer.org/installer | php'
- unless: test -f /usr/local/bin/composer
- cwd: /root/
install-composer:
cmd.wait:
- name: mv /root/composer.phar /usr/local/bin/composer
- cwd: /root/
- watch:
- cmd: get-composer
/path/to/project:
composer.installed:
- no_dev: true
- require:
- cmd: install-composer
# Without composer installed in your PATH
# Note: composer.phar must be executable for state to work properly
/path/to/project:
composer.installed:
- composer: /path/to/composer.phar
- php: /usr/local/bin/php
- no_dev: true
'''
from __future__ import absolute_import, print_function, unicode_literals
# Import salt libs
from salt.exceptions import SaltException
def __virtual__():
'''
Only load if the composer module is available in __salt__
'''
return 'composer.install' in __salt__
def installed(name,
composer=None,
php=None,
user=None,
prefer_source=None,
prefer_dist=None,
no_scripts=None,
no_plugins=None,
optimize=None,
no_dev=None,
quiet=False,
composer_home='/root',
always_check=True,
env=None):
'''
Verify that the correct versions of composer dependencies are present.
name
Directory location of the ``composer.json`` file.
composer
Location of the ``composer.phar`` file. If not set composer will
just execute ``composer`` as if it is installed globally.
(i.e. ``/path/to/composer.phar``)
php
Location of the php executable to use with composer.
(i.e. ``/usr/bin/php``)
user
Which system user to run composer as.
.. versionadded:: 2014.1.4
prefer_source
``--prefer-source`` option of composer.
prefer_dist
``--prefer-dist`` option of composer.
no_scripts
``--no-scripts`` option of composer.
no_plugins
``--no-plugins`` option of composer.
optimize
``--optimize-autoloader`` option of composer. Recommended for production.
no_dev
``--no-dev`` option for composer. Recommended for production.
quiet
``--quiet`` option for composer. Whether or not to return output from composer.
composer_home
``$COMPOSER_HOME`` environment variable
always_check
If ``True``, *always* run ``composer install`` in the directory. This is the
default behavior. If ``False``, only run ``composer install`` if there is no
vendor directory present.
env
A list of environment variables to be set prior to execution.
'''
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
did_install = __salt__['composer.did_composer_install'](name)
# Check if composer.lock exists, if so we already ran `composer install`
# and we don't need to do it again
if always_check is False and did_install:
ret['result'] = True
ret['comment'] = 'Composer already installed this directory'
return ret
# The state of the system does need to be changed. Check if we're running
# in ``test=true`` mode.
if __opts__['test'] is True:
if did_install is True:
install_status = ""
else:
install_status = "not "
ret['comment'] = 'The state of "{0}" will be changed.'.format(name)
ret['changes'] = {
'old': 'composer install has {0}been run in {1}'.format(install_status, name),
'new': 'composer install will be run in {0}'.format(name)
}
ret['result'] = None
return ret
try:
call = __salt__['composer.install'](
name,
composer=composer,
php=php,
runas=user,
prefer_source=prefer_source,
prefer_dist=prefer_dist,
no_scripts=no_scripts,
no_plugins=no_plugins,
optimize=optimize,
no_dev=no_dev,
quiet=quiet,
composer_home=composer_home,
env=env
)
except (SaltException) as err:
ret['result'] = False
ret['comment'] = 'Error executing composer in \'{0}\': {1}'.format(name, err)
return ret
# If composer retcode != 0 then an exception was thrown and we dealt with it.
# Any other case is success, regardless of what composer decides to output.
ret['result'] = True
if quiet is True:
ret['comment'] = 'Composer install completed successfully, output silenced by quiet flag'
else:
ret['comment'] = 'Composer install completed successfully'
ret['changes'] = {
'stderr': call['stderr'],
'stdout': call['stdout']
}
return ret
def update(name,
composer=None,
php=None,
user=None,
prefer_source=None,
prefer_dist=None,
no_scripts=None,
no_plugins=None,
optimize=None,
no_dev=None,
quiet=False,
composer_home='/root',
env=None):
'''
Composer update the directory to ensure we have the latest versions
of all project dependencies.
name
Directory location of the ``composer.json`` file.
composer
Location of the ``composer.phar`` file. If not set composer will
just execute ``composer`` as if it is installed globally.
(i.e. /path/to/composer.phar)
php
Location of the php executable to use with composer.
(i.e. ``/usr/bin/php``)
user
Which system user to run composer as.
.. versionadded:: 2014.1.4
prefer_source
``--prefer-source`` option of composer.
prefer_dist
``--prefer-dist`` option of composer.
no_scripts
``--no-scripts`` option of composer.
no_plugins
``--no-plugins`` option of composer.
optimize
``--optimize-autoloader`` option of composer. Recommended for production.
no_dev
``--no-dev`` option for composer. Recommended for production.
quiet
``--quiet`` option for composer. Whether or not to return output from composer.
composer_home
``$COMPOSER_HOME`` environment variable
env
A list of environment variables to be set prior to execution.
'''
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
# Check if composer.lock exists, if so we already ran `composer install`
is_installed = __salt__['composer.did_composer_install'](name)
if is_installed:
old_status = "composer install has not yet been run in {0}".format(name)
else:
old_status = "composer install has been run in {0}".format(name)
# The state of the system does need to be changed. Check if we're running
# in ``test=true`` mode.
if __opts__['test'] is True:
ret['comment'] = 'The state of "{0}" will be changed.'.format(name)
ret['changes'] = {
'old': old_status,
'new': 'composer install/update will be run in {0}'.format(name)
}
ret['result'] = None
return ret
try:
call = __salt__['composer.update'](
name,
composer=composer,
php=php,
runas=user,
prefer_source=prefer_source,
prefer_dist=prefer_dist,
no_scripts=no_scripts,
no_plugins=no_plugins,
optimize=optimize,
no_dev=no_dev,
quiet=quiet,
composer_home=composer_home,
env=env
)
except (SaltException) as err:
ret['result'] = False
ret['comment'] = 'Error executing composer in \'{0}\': {1}'.format(name, err)
return ret
# If composer retcode != 0 then an exception was thrown and we dealt with it.
# Any other case is success, regardless of what composer decides to output.
ret['result'] = True
if quiet is True:
ret['comment'] = 'Composer update completed successfully, output silenced by quiet flag'
else:
ret['comment'] = 'Composer update completed successfully'
ret['changes'] = {
'stderr': call['stderr'],
'stdout': call['stdout']
}
return ret