-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathpacker
executable file
·149 lines (109 loc) · 4.39 KB
/
packer
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
#!/usr/local/bin/python
# The MIT License (MIT)
#
# Copyright (c) 2014 Austin Hyde
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
def packer_in_path(module):
rc, stdout, stderr = module.run_command('which packer', check_rc=False)
return rc == 0
def pacman_in_path(module):
rc, stdout, stderr = module.run_command('which pacman', check_rc=False)
return rc == 0
def package_installed(module, pkg):
rc, stdout, stderr = module.run_command('pacman -Q %s' % pkg, check_rc=False)
return rc == 0
def get_sudo_user(module):
# ansible sets the SUDO_USER environment variable. Default to using this,
# calling `logname` if SUDO_USER is by some chance empty.
sudo_user = os.environ.get('SUDO_USER')
if not sudo_user:
rc, stdout, stderr = module.run_command('logname', check_rc=True)
sudo_user = stdout
return sudo_user
def check_packages(module, pkgs, state):
would_be_changed = []
for pkg in packages:
installed = package_installed(module, pkg)
if (state == 'present' and not installed) or (state == 'absent' and installed):
would_be_changed.append(pkg)
word = 'installed'
if state == 'absent':
word = 'removed'
if would_be_changed:
module.exit_json(changed=True, msg='%s package(s) would be %s' % (len(would_be_changed), word))
else:
module.exit_json(changed=False, msg='all packages are already %s' % word)
def install_packages(module, pkgs):
num_installed = 0
sudo_user = get_sudo_user(module)
cmd = 'sudo -u %s packer --noconfirm --noedit -S %s'
for pkg in pkgs:
if package_installed(module, pkg):
continue
rc, stdout, stderr = module.run_command(cmd % (sudo_user, pkg), check_rc=False)
if rc != 0:
module.fail_json(msg='failed to install package %s, because: %s' % (pkg,stderr))
num_installed += 1
if num_installed > 0:
module.exit_json(changed=True, msg='installed %s package(s)' % num_installed)
else:
module.exit_json(changed=False, msg='all packages were already installed')
def remove_packages(module, pkgs, recurse):
num_removed = 0
arg = 'R'
word = 'remove'
if recurse:
arg = 'Rs'
word = 'recursively remove'
cmd = 'pacman -%s --noconfirm %s'
for pkg in pkgs:
if not package_installed(module, pkg):
continue
rc, stdout, stderr = module.run_command(cmd % (arg, pkg), check_rc=False)
if rc != 0:
module.fail_json(msg='failed to %s package %s because: %s' % (word, pkg, stderr))
num_removed += 1
if num_removed > 0:
module.exit_json(changed=True, msg='removed %s package(s)' % num_removed)
else:
module.exit_json(changed=False, msg='all packages were already removed')
def main():
module = AnsibleModule(
argument_spec = dict(
name = dict(required=True),
state = dict(default='present', choices=['present','absent']),
recurse = dict(default='no', choices=BOOLEANS, type='bool')
),
supports_check_mode = True
)
if not packer_in_path(module):
module.fail_json(msg="could not locate packer executable")
if not pacman_in_path(module):
module.fail_json(msg="could not locate pacman executable")
p = module.params
pkgs = p['name'].split(',')
if module.check_mode:
check_packages(module, pkgs, p['state'])
if p['state'] == 'present':
install_packages(module, pkgs)
elif p['state'] == 'absent':
remove_packages(module, pkgs, p['recurse'])
from ansible.module_utils.basic import *
main()