Skip to content

Commit

Permalink
Resolved issue that prevented operation with Python 2.6.
Browse files Browse the repository at this point in the history
Improved error handling throught.
  • Loading branch information
mstillings committed Nov 4, 2020
1 parent 273e6b7 commit fefe214
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 56 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ To install from [GitHub](https://github.com/OneIdentity/ansible-privilege-manage

Using `ansible-galaxy` command:
```bash
ansible-galaxy collection install https://github.com/OneIdentity/ansible-privilege-manager/releases/download/v0.0.1/oneidentity-privilege_manager-0.0.1.tar.gz
ansible-galaxy collection install https://github.com/OneIdentity/ansible-privilege-manager/releases/download/v0.0.2/oneidentity-privilege_manager-0.0.2.tar.gz
```

The collection can also be added to a project's `requirements.yml` file
```yaml
---
collections:
- name: https://github.com/OneIdentity/ansible-privilege-manager/releases/download/v0.0.1/oneidentity-privilege_manager-0.0.1.tar.gz
- name: https://github.com/OneIdentity/ansible-privilege-manager/releases/download/v0.0.2/oneidentity-privilege_manager-0.0.2.tar.gz
```

and installed using the `ansible-galaxy` command. This method allows all required collections for a project to be specified in one place and installed with one command.
Expand Down Expand Up @@ -100,7 +100,7 @@ For local build and installation, you can clone the Git repository, build the co
The build command will generate an Ansible Galaxy collection artifact with a `tar.gz` file extension, sample output will look like the following:

```
Created collection for oneidentity.privilege_manager at /home/user/ansible-privilege-manager/oneidentity-privilege_manager-0.0.1.tar.gz
Created collection for oneidentity.privilege_manager at /home/user/ansible-privilege-manager/oneidentity-privilege_manager-0.0.2.tar.gz
```

`Pleae note the path shown above is just an example, the path to your build artifact will be in the root directory of the cloned repository.`
Expand All @@ -110,14 +110,14 @@ For local build and installation, you can clone the Git repository, build the co
Using `ansible-galaxy` command:

```bash
ansible-galaxy collection install /home/user/ansible-privilege-manager/oneidentity-privilege_manager-0.0.1.tar.gz
ansible-galaxy collection install /home/user/ansible-privilege-manager/oneidentity-privilege_manager-0.0.2.tar.gz
```

The collection can also be added to a project's `requirements.yml` file
```yaml
---
collections:
- name: /home/user/ansible-privilege-manager/oneidentity-privilege_manager-0.0.1.tar.gz
- name: /home/user/ansible-privilege-manager/oneidentity-privilege_manager-0.0.2.tar.gz
```

and installed using the `ansible-galaxy` command. This method allows all required collections for a project to be specified in one place and installed with one command.
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace: oneidentity
name: privilege_manager

# Semantic versioning compliant version designation
version: "0.0.1"
version: "0.0.2"

# The path do the Markdown(.md) readme file
readme: README.md
Expand Down
5 changes: 4 additions & 1 deletion plugins/module_utils/check_file_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ def get_file_version(file_path, version_cmd):

# Exec file to get version
try:
rval_bytes = subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
p = subprocess.Popen(' '.join(cmd), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
rval_bytes, rval_err = p.communicate()
rval_bytes += rval_err
except subprocess.CalledProcessError as e:
rval_bytes = e.output
# Popen returns list of bytes so we have to decode to get a string
rval_str = rval_bytes.decode(sys.stdout.encoding)

# Compile regex
Expand Down
6 changes: 4 additions & 2 deletions plugins/module_utils/pmjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ def pmjoin_status():

# Call pm*info
try:
rval_bytes = subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
p = subprocess.Popen(' '.join(cmd), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
rval_bytes, rval_err = p.communicate()
rval_bytes += rval_err
# This exception happens when the process exits with a non-zero return code
except subprocess.CalledProcessError as e:
# Just grab output bytes likes a normal exit, we'll parse it for errors anyway
rval_bytes = e.output
# check_output returns list of bytes so we have to decode to get a string
# Popen returns list of bytes so we have to decode to get a string
rval_str = rval_bytes.decode(sys.stdout.encoding)

# Parse vastool return
Expand Down
41 changes: 26 additions & 15 deletions plugins/modules/pmjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
from ansible.module_utils.basic import AnsibleModule
import sys
import subprocess
import traceback
import re
import ansible_collections.oneidentity.privilege_manager.plugins.module_utils.pmjoin as pmj

Expand Down Expand Up @@ -246,17 +247,23 @@ def run_normal(params, result):
facts_verbose = params['facts_verbose']
facts_key = params['facts_key'] if params['facts_key'] else FACTS_KEY_DEFAULT

# Check pmjoin
err, pmjoin_path, pminfo_path, pmjoin_version = pmj.pmjoin_find()
try:

# Check pmjoin
err, pmjoin_path, pminfo_path, pmjoin_version = pmj.pmjoin_find()

# Run pmjoin
if err is None:
err, changed, output = run_pmjoin(
pmjoin_path,
state,
server,
password,
extra_args)

# Run pmjoin
if err is None:
err, changed, output = run_pmjoin(
pmjoin_path,
state,
server,
password,
extra_args)
except Exception:
tb = traceback.format_exc()
err = str(tb)

# Build result
result['changed'] = changed
Expand Down Expand Up @@ -351,18 +358,20 @@ def run_pmjoin_join(
cmd += [path]
cmd += ['-b']
cmd += ['-a']
# cmd += ['-q']
cmd += ['-q']
cmd += [server]
cmd += [extra_args] if extra_args else []

# Call vastool
try:
rval_bytes = subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
p = subprocess.Popen(' '.join(cmd), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
rval_bytes, rval_err = p.communicate(password)
rval_bytes += rval_err
# This exception happens when the process exits with a non-zero return code
except subprocess.CalledProcessError as e:
# Just grab output bytes likes a normal exit, we'll parse it for errors anyway
rval_bytes = e.output
# check_output returns list of bytes so we have to decode to get a string
# Popen returns list of bytes so we have to decode to get a string
rval_str = rval_bytes.decode(sys.stdout.encoding)

# Parse pmjoin return
Expand Down Expand Up @@ -392,12 +401,14 @@ def run_pmjoin_unjoin(

# Call vastool
try:
rval_bytes = subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
p = subprocess.Popen(' '.join(cmd), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
rval_bytes, rval_err = p.communicate()
rval_bytes += rval_err
# This exception happens when the process exits with a non-zero return code
except subprocess.CalledProcessError as e:
# Just grab output bytes likes a normal exit, we'll parse it for errors anyway
rval_bytes = e.output
# check_output returns list of bytes so we have to decode to get a string
# Popen returns list of bytes so we have to decode to get a string
rval_str = rval_bytes.decode(sys.stdout.encoding)

# Parse pmjoin return
Expand Down
33 changes: 21 additions & 12 deletions plugins/modules/preflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@

from ansible.module_utils.basic import AnsibleModule
import sys
import traceback
import subprocess
import ansible_collections.oneidentity.privilege_manager.plugins.module_utils.check_file_exec as cfe

Expand Down Expand Up @@ -258,17 +259,23 @@ def run_normal(params, result):
facts_key = params['facts_key'] if params['facts_key'] else FACTS_KEY_DEFAULT
path = params['path'] if params['path'] else PATH_DEFAULT

# Check preflight
err, version = cfe.check_file_exec(path, '-v')
try:

# Check preflight
err, version = cfe.check_file_exec(path, '-v')

# Run preflight
if err is None:
err, steps = run_preflight(
mode,
server,
verbose,
extra_args,
path)

# Run preflight
if err is None:
err, steps = run_preflight(
mode,
server,
verbose,
extra_args,
path)
except Exception:
tb = traceback.format_exc()
err = str(tb)

# Build result
result['changed'] = False # preflight never makes any changes to the host
Expand Down Expand Up @@ -314,12 +321,14 @@ def run_preflight(

# Call preflight
try:
rval_bytes = subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
p = subprocess.Popen(' '.join(cmd), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
rval_bytes, rval_err = p.communicate()
rval_bytes += rval_err
# This exception happens when the process exits with a non-zero return code
except subprocess.CalledProcessError as e:
# Just grab output bytes likes a normal exit, we'll parse it for errors anyway
rval_bytes = e.output
# check_output returns list of bytes so we have to decode to get a string
# Popen returns list of bytes so we have to decode to get a string
rval_str = rval_bytes.decode(sys.stdout.encoding)

# Parse preflight return
Expand Down
37 changes: 22 additions & 15 deletions plugins/modules/software_pkgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@

from ansible.module_utils.basic import AnsibleModule
import os
import traceback
import glob
import re

Expand Down Expand Up @@ -300,23 +301,29 @@ def run_normal(params, result):
facts = params['facts']
facts_key = params['facts_key'] if params['facts_key'] else FACTS_KEY_DEFAULT

# Check directory
err = check_dir(path)
try:

# Check mode
err, sub_dirs = check_mode(mode)
# Check directory
err = check_dir(path)

# Find packages
if err is None:
for sub_dir in sub_dirs:
err, p = find_packages(path, sub_dir, sys, dist, arch)
if err is None:
packages.update(p)
else:
break

if not err and not packages:
err = 'No packages found for sys=' + sys + ', dist=' + dist + ', arch=' + arch
# Check mode
err, sub_dirs = check_mode(mode)

# Find packages
if err is None:
for sub_dir in sub_dirs:
err, p = find_packages(path, sub_dir, sys, dist, arch)
if err is None:
packages.update(p)
else:
break

if not err and not packages:
err = 'No packages found for sys=' + sys + ', dist=' + dist + ', arch=' + arch

except Exception:
tb = traceback.format_exc()
err = str(tb)

# Build result
result['changed'] = False # Never makes any changes to the host
Expand Down
2 changes: 1 addition & 1 deletion roles/common/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---

collection_version: '0.0.1'
collection_version: '0.0.2'
2 changes: 1 addition & 1 deletion roles/join/tasks/pmjoin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
# Fail if there was a message returned
- fail:
msg: "{{ result.msg }}"
when: result.msg
when: result.msg is defined and result.msg
5 changes: 5 additions & 0 deletions roles/preflight/tasks/utils/package_copy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
mode: 'u+rwx'
ignore_errors: true
changed_when: false
register: rval

- fail:
msg: "{{ rval.msg }}"
when: rval.msg is defined and rval.msg
5 changes: 5 additions & 0 deletions roles/preflight/tasks/utils/temp_dir_delete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
state: absent
ignore_errors: true
changed_when: false
register: rval

- fail:
msg: "{{ rval.msg }}"
when: rval.msg is defined and rval.msg
2 changes: 1 addition & 1 deletion roles/software/tasks/check_package_directory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
- fail:
msg: "{{ software_pkgs.msg }}"
register: result
when: software_pkgs.msg
when: software_pkgs.msg is defined and software_pkgs.msg
2 changes: 1 addition & 1 deletion roles/software/tasks/temp_dir_create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

- fail:
msg: "{{ result.msg }}"
when: result.failed
when: result.failed is defined and result.failed
2 changes: 1 addition & 1 deletion roles/software/tasks/temp_dir_delete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

- fail:
msg: "{{ result.msg }}"
when: result.failed
when: result.failed is defined and result.failed
5 changes: 5 additions & 0 deletions roles/software/tasks/utils/package_copy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
dest: "{{ package_dest }}"
ignore_errors: true
changed_when: false
register: rval

- fail:
msg: "{{ rval.msg }}"
when: rval.msg is defined and rval.msg

0 comments on commit fefe214

Please sign in to comment.