| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_lx_package |
| 13 |
short_description: Manages Javascript LX packages on a BIG-IP |
| 14 |
description: |
| 15 |
- Manages Javascript LX packages on a BIG-IP. This module allows |
| 16 |
you to deploy LX packages to the BIG-IP and manage their lifecycle. |
| 17 |
version_added: "1.0.0" |
| 18 |
options: |
| 19 |
package: |
| 20 |
description: |
| 21 |
- The LX package that you want to upload or remove. When C(state) is C(present), |
| 22 |
and you intend to use this module in a C(role), we recommend you use |
| 23 |
the C({{ role_path }}) variable. An example is provided in the C(EXAMPLES) section. |
| 24 |
- When C(state) is C(absent), it is not necessary for the package to exist on the |
| 25 |
Ansible controller. If the full path to the package is provided, the fileame will |
| 26 |
specifically be cherry-picked from it to properly remove the package. |
| 27 |
type: path |
| 28 |
state: |
| 29 |
description: |
| 30 |
- Whether the LX package should exist or not. |
| 31 |
type: str |
| 32 |
default: present |
| 33 |
choices: |
| 34 |
- present |
| 35 |
- absent |
| 36 |
retain_package_file: |
| 37 |
description: |
| 38 |
- Specifies whether the install file should be deleted on successful installation of the package. |
| 39 |
type: bool |
| 40 |
default: false |
| 41 |
version_added: "1.4.0" |
| 42 |
notes: |
| 43 |
- Requires the RPM tool be installed on the host. This can be accomplished in |
| 44 |
different ways on each platform. On Debian based systems with C(apt); |
| 45 |
C(apt-get install rpm). On Mac with C(brew); C(brew install rpm). |
| 46 |
This command is already present on RedHat based systems. |
| 47 |
- Requires BIG-IP >= 12.1.0, because the required functionality is missing |
| 48 |
on prior versions. |
| 49 |
- The module name C(bigip_iapplx_package) has been deprecated in favor of C(bigip_lx_package). |
| 50 |
requirements: |
| 51 |
- Requires BIG-IP >= 12.1.0 |
| 52 |
- The 'rpm' tool installed on the Ansible controller |
| 53 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 54 |
author: |
| 55 |
- Tim Rupp (@caphrim007) |
| 56 |
- Wojciech Wypior (@wojtek0806) |
| 57 |
''' |
| 58 |
|
| 59 |
EXAMPLES = r''' |
| 60 |
- name: Install AS3 |
| 61 |
bigip_lx_package: |
| 62 |
package: f5-appsvcs-3.5.0-3.noarch.rpm |
| 63 |
provider: |
| 64 |
password: secret |
| 65 |
server: lb.mydomain.com |
| 66 |
user: admin |
| 67 |
delegate_to: localhost |
| 68 |
|
| 69 |
- name: Add an LX package stored in a role |
| 70 |
bigip_lx_package: |
| 71 |
package: "{{ roles_path }}/files/MyApp-0.1.0-0001.noarch.rpm'" |
| 72 |
provider: |
| 73 |
password: secret |
| 74 |
server: lb.mydomain.com |
| 75 |
user: admin |
| 76 |
delegate_to: localhost |
| 77 |
|
| 78 |
- name: Remove an LX package |
| 79 |
bigip_lx_package: |
| 80 |
package: MyApp-0.1.0-0001.noarch.rpm |
| 81 |
state: absent |
| 82 |
provider: |
| 83 |
password: secret |
| 84 |
server: lb.mydomain.com |
| 85 |
user: admin |
| 86 |
delegate_to: localhost |
| 87 |
|
| 88 |
- name: Install AS3 and don't delete package file |
| 89 |
bigip_lx_package: |
| 90 |
package: f5-appsvcs-3.5.0-3.noarch.rpm |
| 91 |
retain_package_file: true |
| 92 |
provider: |
| 93 |
password: secret |
| 94 |
server: lb.mydomain.com |
| 95 |
user: admin |
| 96 |
delegate_to: localhost |
| 97 |
''' |
| 98 |
|
| 99 |
RETURN = r''' |
| 100 |
# only common fields returned |
| 101 |
''' |
| 102 |
|
| 103 |
import os |
| 104 |
import time |
| 105 |
import traceback |
| 106 |
from datetime import datetime |
| 107 |
|
| 108 |
try: |
| 109 |
from packaging.version import Version |
| 110 |
except ImportError: |
| 111 |
HAS_PACKAGING = False |
| 112 |
Version = None |
| 113 |
PACKAGING_IMPORT_ERROR = traceback.format_exc() |
| 114 |
else: |
| 115 |
HAS_PACKAGING = True |
| 116 |
PACKAGING_IMPORT_ERROR = None |
| 117 |
|
| 118 |
from ansible.module_utils.basic import ( |
| 119 |
AnsibleModule, missing_required_lib |
| 120 |
) |
| 121 |
from ansible.module_utils.urls import urlparse |
| 122 |
|
| 123 |
from ..module_utils.bigip import F5RestClient |
| 124 |
from ..module_utils.common import ( |
| 125 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec, flatten_boolean |
| 126 |
) |
| 127 |
from ..module_utils.icontrol import ( |
| 128 |
tmos_version, upload_file |
| 129 |
) |
| 130 |
from ..module_utils.teem import send_teem |
| 131 |
|
| 132 |
|
| 133 |
class Parameters(AnsibleF5Parameters): |
| 134 |
api_attributes = [] |
| 135 |
returnables = [] |
| 136 |
|
| 137 |
@property |
| 138 |
def package(self): |
| 139 |
if self._values['package'] is None: |
| 140 |
return None |
| 141 |
return self._values['package'] |
| 142 |
|
| 143 |
@property |
| 144 |
def package_file(self): |
| 145 |
if self._values['package'] is None: |
| 146 |
return None |
| 147 |
return os.path.basename(self._values['package']) |
| 148 |
|
| 149 |
@property |
| 150 |
def package_name(self): |
| 151 |
"""Return a valid name for the package |
| 152 |
|
| 153 |
BIG-IP determines the package name by the content of the RPM info. |
| 154 |
It does not use the filename. Therefore, we do the same. This method |
| 155 |
is only used though when the file actually exists on your Ansible |
| 156 |
controller. |
| 157 |
|
| 158 |
If the package does not exist, then we instead use the filename |
| 159 |
portion of the 'package' argument that is provided. |
| 160 |
|
| 161 |
Non-existence typically occurs when using 'state' = 'absent' |
| 162 |
|
| 163 |
:return: |
| 164 |
""" |
| 165 |
cmd = ['rpm', '-qp', '--queryformat', '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}', self.package] |
| 166 |
rc, out, err = self._module.run_command(cmd) |
| 167 |
if not out: |
| 168 |
return str(self.package_file) |
| 169 |
return out |
| 170 |
|
| 171 |
@property |
| 172 |
def package_root(self): |
| 173 |
if self._values['package'] is None: |
| 174 |
return None |
| 175 |
base = os.path.basename(self._values['package']) |
| 176 |
result = os.path.splitext(base) |
| 177 |
return result[0] |
| 178 |
|
| 179 |
@property |
| 180 |
def retain_package_file(self): |
| 181 |
return flatten_boolean(self._values['retain_package_file']) |
| 182 |
|
| 183 |
|
| 184 |
class ApiParameters(Parameters): |
| 185 |
pass |
| 186 |
|
| 187 |
|
| 188 |
class ModuleParameters(Parameters): |
| 189 |
pass |
| 190 |
|
| 191 |
|
| 192 |
class Changes(Parameters): |
| 193 |
def to_return(self): |
| 194 |
result = {} |
| 195 |
try: |
| 196 |
for returnable in self.returnables: |
| 197 |
result[returnable] = getattr(self, returnable) |
| 198 |
result = self._filter_params(result) |
| 199 |
except Exception: |
| 200 |
raise |
| 201 |
return result |
| 202 |
|
| 203 |
|
| 204 |
class UsableChanges(Changes): |
| 205 |
pass |
| 206 |
|
| 207 |
|
| 208 |
class ReportableChanges(Changes): |
| 209 |
pass |
| 210 |
|
| 211 |
|
| 212 |
class ModuleManager(object): |
| 213 |
def __init__(self, *args, **kwargs): |
| 214 |
self.module = kwargs.get('module', None) |
| 215 |
self.client = F5RestClient(**self.module.params) |
| 216 |
self.want = ModuleParameters(module=self.module, params=self.module.params) |
| 217 |
self.changes = UsableChanges() |
| 218 |
|
| 219 |
def exec_module(self): |
| 220 |
result = dict() |
| 221 |
start = datetime.now().isoformat() |
| 222 |
version = tmos_version(self.client) |
| 223 |
changed = False |
| 224 |
state = self.want.state |
| 225 |
if Version(version) <= Version('12.0.0'): |
| 226 |
raise F5ModuleError( |
| 227 |
"This version of BIG-IP is not supported." |
| 228 |
) |
| 229 |
|
| 230 |
if state == "present": |
| 231 |
changed = self.present() |
| 232 |
elif state == "absent": |
| 233 |
changed = self.absent() |
| 234 |
|
| 235 |
changes = self.changes.to_return() |
| 236 |
result.update(**changes) |
| 237 |
result.update(dict(changed=changed)) |
| 238 |
send_teem(start, self.client, self.module, version) |
| 239 |
return result |
| 240 |
|
| 241 |
def present(self): |
| 242 |
if self.exists(): |
| 243 |
return False |
| 244 |
else: |
| 245 |
return self.create() |
| 246 |
|
| 247 |
def absent(self): |
| 248 |
changed = False |
| 249 |
if self.exists(): |
| 250 |
changed = self.remove() |
| 251 |
return changed |
| 252 |
|
| 253 |
def remove(self): |
| 254 |
if self.module.check_mode: |
| 255 |
return True |
| 256 |
self.remove_from_device() |
| 257 |
if self.exists(): |
| 258 |
raise F5ModuleError("Failed to delete the LX package.") |
| 259 |
return True |
| 260 |
|
| 261 |
def create(self): |
| 262 |
if self.module.check_mode: |
| 263 |
return True |
| 264 |
if not os.path.exists(self.want.package): |
| 265 |
if self.want.package.startswith('/'): |
| 266 |
raise F5ModuleError( |
| 267 |
"The specified LX package was not found at {0}.".format(self.want.package) |
| 268 |
) |
| 269 |
else: |
| 270 |
raise F5ModuleError( |
| 271 |
"The specified LX package was not found in {0}.".format(os.getcwd()) |
| 272 |
) |
| 273 |
if not self.check_file_exists_on_device(): |
| 274 |
self.upload_to_device() |
| 275 |
self.create_on_device() |
| 276 |
self.enable_iapplx_on_device() |
| 277 |
if self.want.retain_package_file == 'no': |
| 278 |
self.remove_package_file_from_device() |
| 279 |
if self.exists(): |
| 280 |
return True |
| 281 |
else: |
| 282 |
raise F5ModuleError("Failed to install LX package.") |
| 283 |
|
| 284 |
def exists(self): |
| 285 |
exists = False |
| 286 |
packages = self.get_installed_packages_on_device() |
| 287 |
if os.path.exists(self.want.package): |
| 288 |
exists = True |
| 289 |
for package in packages: |
| 290 |
if exists: |
| 291 |
if self.want.package_name == package['packageName']: |
| 292 |
return True |
| 293 |
else: |
| 294 |
if self.want.package_root == package['packageName']: |
| 295 |
return True |
| 296 |
return False |
| 297 |
|
| 298 |
def get_installed_packages_on_device(self): |
| 299 |
uri = "https://{0}:{1}/mgmt/shared/iapp/package-management-tasks".format( |
| 300 |
self.client.provider['server'], |
| 301 |
self.client.provider['server_port'] |
| 302 |
) |
| 303 |
params = dict(operation='QUERY') |
| 304 |
resp = self.client.api.post(uri, json=params) |
| 305 |
|
| 306 |
try: |
| 307 |
response = resp.json() |
| 308 |
except ValueError as ex: |
| 309 |
raise F5ModuleError(str(ex)) |
| 310 |
|
| 311 |
if resp.status not in [200, 201, 202] or 'code' in response and response['code'] not in [200, 201, 202]: |
| 312 |
raise F5ModuleError(resp.content) |
| 313 |
|
| 314 |
path = urlparse(response["selfLink"]).path |
| 315 |
task = self._wait_for_task(path) |
| 316 |
|
| 317 |
if task['status'] == 'FINISHED': |
| 318 |
return task['queryResponse'] |
| 319 |
raise F5ModuleError( |
| 320 |
"Failed to find the installed packages on the device." |
| 321 |
) |
| 322 |
|
| 323 |
def _wait_for_task(self, path): |
| 324 |
task = None |
| 325 |
for x in range(0, 60): |
| 326 |
task = self.check_task_on_device(path) |
| 327 |
if task['status'] in ['FINISHED', 'FAILED']: |
| 328 |
return task |
| 329 |
time.sleep(1) |
| 330 |
return task |
| 331 |
|
| 332 |
def check_task_on_device(self, path): |
| 333 |
uri = "https://{0}:{1}{2}".format( |
| 334 |
self.client.provider['server'], |
| 335 |
self.client.provider['server_port'], |
| 336 |
path |
| 337 |
) |
| 338 |
resp = self.client.api.get(uri) |
| 339 |
|
| 340 |
try: |
| 341 |
response = resp.json() |
| 342 |
except ValueError as ex: |
| 343 |
raise F5ModuleError(str(ex)) |
| 344 |
|
| 345 |
if resp.status in [200, 201, 202] or 'code' in response and response['code'] in [200, 201, 202]: |
| 346 |
return response |
| 347 |
raise F5ModuleError(resp.content) |
| 348 |
|
| 349 |
def upload_to_device(self): |
| 350 |
url = 'https://{0}:{1}/mgmt/shared/file-transfer/uploads'.format( |
| 351 |
self.client.provider['server'], |
| 352 |
self.client.provider['server_port'] |
| 353 |
) |
| 354 |
try: |
| 355 |
upload_file(self.client, url, self.want.package) |
| 356 |
except F5ModuleError: |
| 357 |
raise F5ModuleError( |
| 358 |
"Failed to upload the file." |
| 359 |
) |
| 360 |
|
| 361 |
def check_file_exists_on_device(self): |
| 362 |
params = dict( |
| 363 |
command="run", |
| 364 |
utilCmdArgs="/var/config/rest/downloads/{0}".format(self.want.package_file) |
| 365 |
) |
| 366 |
uri = "https://{0}:{1}/mgmt/tm/util/unix-ls".format( |
| 367 |
self.client.provider['server'], |
| 368 |
self.client.provider['server_port'] |
| 369 |
) |
| 370 |
resp = self.client.api.post(uri, json=params) |
| 371 |
try: |
| 372 |
response = resp.json() |
| 373 |
except ValueError as ex: |
| 374 |
raise F5ModuleError(str(ex)) |
| 375 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 376 |
if 'commandResult' in response: |
| 377 |
if 'No such file or directory' in response['commandResult']: |
| 378 |
return False |
| 379 |
elif self.want.package_file in response['commandResult']: |
| 380 |
return True |
| 381 |
raise F5ModuleError(resp.content) |
| 382 |
|
| 383 |
def remove_package_file_from_device(self): |
| 384 |
params = dict( |
| 385 |
command="run", |
| 386 |
utilCmdArgs="/var/config/rest/downloads/{0}".format(self.want.package_file) |
| 387 |
) |
| 388 |
uri = "https://{0}:{1}/mgmt/tm/util/unix-rm".format( |
| 389 |
self.client.provider['server'], |
| 390 |
self.client.provider['server_port'] |
| 391 |
) |
| 392 |
resp = self.client.api.post(uri, json=params) |
| 393 |
try: |
| 394 |
response = resp.json() |
| 395 |
except ValueError as ex: |
| 396 |
raise F5ModuleError(str(ex)) |
| 397 |
|
| 398 |
if resp.status in [200, 201, 202] or 'code' in response and response['code'] in [200, 201, 202]: |
| 399 |
return True |
| 400 |
raise F5ModuleError(resp.content) |
| 401 |
|
| 402 |
def create_on_device(self): |
| 403 |
remote_path = "/var/config/rest/downloads/{0}".format(self.want.package_file) |
| 404 |
params = dict( |
| 405 |
operation='INSTALL', packageFilePath=remote_path |
| 406 |
) |
| 407 |
uri = "https://{0}:{1}/mgmt/shared/iapp/package-management-tasks".format( |
| 408 |
self.client.provider['server'], |
| 409 |
self.client.provider['server_port'] |
| 410 |
) |
| 411 |
|
| 412 |
resp = self.client.api.post(uri, json=params) |
| 413 |
|
| 414 |
try: |
| 415 |
response = resp.json() |
| 416 |
except ValueError as ex: |
| 417 |
raise F5ModuleError(str(ex)) |
| 418 |
|
| 419 |
if resp.status not in [200, 201, 202] or 'code' in response and response['code'] not in [200, 201, 202]: |
| 420 |
raise F5ModuleError(resp.content) |
| 421 |
|
| 422 |
path = urlparse(response["selfLink"]).path |
| 423 |
task = self._wait_for_task(path) |
| 424 |
|
| 425 |
if task['status'] == 'FINISHED': |
| 426 |
return True |
| 427 |
else: |
| 428 |
raise F5ModuleError(task['errorMessage']) |
| 429 |
|
| 430 |
def remove_from_device(self): |
| 431 |
params = dict( |
| 432 |
operation='UNINSTALL', |
| 433 |
packageName=self.want.package_root |
| 434 |
) |
| 435 |
uri = "https://{0}:{1}/mgmt/shared/iapp/package-management-tasks".format( |
| 436 |
self.client.provider['server'], |
| 437 |
self.client.provider['server_port'] |
| 438 |
) |
| 439 |
|
| 440 |
resp = self.client.api.post(uri, json=params) |
| 441 |
|
| 442 |
try: |
| 443 |
response = resp.json() |
| 444 |
except ValueError as ex: |
| 445 |
raise F5ModuleError(str(ex)) |
| 446 |
|
| 447 |
if resp.status not in [200, 201, 202] or 'code' in response and response['code'] not in [200, 201, 202]: |
| 448 |
raise F5ModuleError(resp.content) |
| 449 |
|
| 450 |
path = urlparse(response["selfLink"]).path |
| 451 |
task = self._wait_for_task(path) |
| 452 |
|
| 453 |
if task['status'] == 'FINISHED': |
| 454 |
return True |
| 455 |
return False |
| 456 |
|
| 457 |
def enable_iapplx_on_device(self): |
| 458 |
params = dict( |
| 459 |
command="run", |
| 460 |
utilCmdArgs='-c "touch /var/config/rest/iapps/enable"' |
| 461 |
) |
| 462 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 463 |
self.client.provider['server'], |
| 464 |
self.client.provider['server_port'] |
| 465 |
) |
| 466 |
resp = self.client.api.post(uri, json=params) |
| 467 |
try: |
| 468 |
response = resp.json() |
| 469 |
except ValueError as ex: |
| 470 |
raise F5ModuleError(str(ex)) |
| 471 |
|
| 472 |
if resp.status in [200, 201, 202] or 'code' in response and response['code'] in [200, 201, 202]: |
| 473 |
return True |
| 474 |
raise F5ModuleError(resp.content) |
| 475 |
|
| 476 |
|
| 477 |
class ArgumentSpec(object): |
| 478 |
def __init__(self): |
| 479 |
self.supports_check_mode = True |
| 480 |
argument_spec = dict( |
| 481 |
state=dict( |
| 482 |
default='present', |
| 483 |
choices=['present', 'absent'] |
| 484 |
), |
| 485 |
package=dict(type='path'), |
| 486 |
retain_package_file=dict( |
| 487 |
default='no', |
| 488 |
type='bool' |
| 489 |
), |
| 490 |
) |
| 491 |
self.argument_spec = {} |
| 492 |
self.argument_spec.update(f5_argument_spec) |
| 493 |
self.argument_spec.update(argument_spec) |
| 494 |
self.required_if = [ |
| 495 |
['state', 'present', ['package']] |
| 496 |
] |
| 497 |
|
| 498 |
|
| 499 |
def main(): |
| 500 |
spec = ArgumentSpec() |
| 501 |
|
| 502 |
module = AnsibleModule( |
| 503 |
argument_spec=spec.argument_spec, |
| 504 |
supports_check_mode=spec.supports_check_mode, |
| 505 |
required_if=spec.required_if |
| 506 |
) |
| 507 |
|
| 508 |
if not HAS_PACKAGING: |
| 509 |
module.fail_json( |
| 510 |
msg=missing_required_lib('packaging'), |
| 511 |
exception=PACKAGING_IMPORT_ERROR |
| 512 |
) |
| 513 |
|
| 514 |
try: |
| 515 |
mm = ModuleManager(module=module) |
| 516 |
results = mm.exec_module() |
| 517 |
module.exit_json(**results) |
| 518 |
except F5ModuleError as ex: |
| 519 |
module.fail_json(msg=str(ex)) |
| 520 |
|
| 521 |
|
| 522 |
if __name__ == '__main__': |
| 523 |
main() |