Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Extract uname fact gathering to raw tasks. Closes #94. (#95)
Browse files Browse the repository at this point in the history
* Extract uname fact gathering to raw tasks. Closes #94.

* remove the commented out code.
  • Loading branch information
chambridge committed Jul 27, 2017
1 parent 791aa5c commit 373e18c
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -19,6 +19,6 @@ test/coverage
data
doc/source/modules.rst
doc/source/rho.rst

.coverage
.vagrant
*.retry
35 changes: 9 additions & 26 deletions library/run_cmds.py
Expand Up @@ -51,8 +51,7 @@
# passed in through the playbook (attached
# to the field requested using an '_').

DEFAULT_CMD_DICT = {"Username": uname_command.UnameRhoCmd,
"RedhatRelease":
DEFAULT_CMD_DICT = {"RedhatRelease":
redhat_release_command.RedhatReleaseRhoCmd,
"Instnum": file_commands.InstnumRhoCmd,
"SysId": file_commands.SystemIdRhoCmd,
Expand Down Expand Up @@ -102,13 +101,14 @@ def __init__(self, module):
if isinstance(self.fact_names, list):
for f_name in self.fact_names:
f_name_list = f_name.strip().split('_', 1)
command = DEFAULT_CMD_DICT[f_name_list[0]]
fact = f_name_list[1]
if command in DEFAULT_CMDS:
if command in self.facts_requested.keys():
self.facts_requested[command].append(fact)
else:
self.facts_requested[command] = [fact]
if f_name_list[0] in DEFAULT_CMD_DICT:
command = DEFAULT_CMD_DICT[f_name_list[0]]
fact = f_name_list[1]
if command in DEFAULT_CMDS:
if command in self.facts_requested.keys():
self.facts_requested[command].append(fact)
else:
self.facts_requested[command] = [fact]

# If type of input is a string then the only allowed,
# string is 'default' which means the user has requested
Expand Down Expand Up @@ -140,23 +140,6 @@ def execute_commands(self):
rcmd.run_cmd(self.facts_requested[command])
info_dict.update(rcmd.data)

if 'all' not in self.facts_requested.values():

# list that stores out the flattened out
# values list of the facts_requested dictionary
# to keep track of all fields requested so
# that only those can be reported.

facts_requested_list = [item
for sublist
in self.facts_requested.values()
for item in sublist]

if 'all' not in facts_requested_list:
for k in info_dict:
if k not in facts_requested_list:
info_dict.pop(k, None)

info_dict['connection.host'] = self.host
info_dict['connection.port'] = self.port
info_dict['connection.uuid'] = str(uuid.uuid4())
Expand Down
26 changes: 14 additions & 12 deletions module_utils/cpu_command.py
Expand Up @@ -73,25 +73,27 @@ def parse_data_cpu(self, results):
stdout=sp.PIPE,
stderr=sp.PIPE)
dmidecode = process.communicate()
for line in results["get_cpu_info"][0].splitlines():
if line.find("processor") == 0:
cpu_count += 1
if "get_cpu_info" in results:
for line in results["get_cpu_info"][0].splitlines():
if line.find("processor") == 0:
cpu_count += 1
data["cpu.count"] = cpu_count
data['cpu.socket_count'] = dmidecode[1].\
replace('\n', '').replace('\r', '') \
if dmidecode[1] else \
len(re.findall('Socket Designation', dmidecode[0]))

cpu_dict = {}
for line in results["get_cpu_info"][0].splitlines():
# first blank line should be end of first cpu
# for this case, we are only grabbing the fields from the first
# cpu and the total count. Should be close enough.
if line == "":
break
parts = line.split(':')
# should'nt be ':', but just in case, join all parts and strip
cpu_dict[parts[0].strip()] = ("".join(parts[1:])).strip()
if "get_cpu_info" in results:
for line in results["get_cpu_info"][0].splitlines():
# first blank line should be end of first cpu
# for this case, we are only grabbing the fields from the first
# cpu and the total count. Should be close enough.
if line == "":
break
parts = line.split(':')
# should'nt be ':', but just in case, join all parts and strip
cpu_dict[parts[0].strip()] = ("".join(parts[1:])).strip()

# we don't need everything, just parse out the interesting bits

Expand Down
2 changes: 1 addition & 1 deletion rho/scancommand.py
Expand Up @@ -351,7 +351,7 @@ def _do_command(self):
sys.exit(1)

if facts == ['default']:
facts_to_collect = 'default'
facts_to_collect = list(utilities.DEFAULT_FACTS_TUPLE)
elif os.path.isfile(facts[0]):
facts_to_collect = _read_in_file(facts[0])
else:
Expand Down
68 changes: 68 additions & 0 deletions rho/utilities.py
Expand Up @@ -19,6 +19,74 @@
CREDENTIALS_PATH = 'data/credentials'
PROFILES_PATH = 'data/profiles'

UNAME_FACTS_TUPLE = ('Username_uname.os',
'Username_uname.hostname',
'Username_uname.processor',
'Username_uname.kernel',
'Username_uname.all',
'Username_uname.hardware_platform')

REDHAT_RELEASE_FACTS_TUPLE = ('RedhatRelease_redhat-release.name',
'RedhatRelease_redhat-release.version',
'RedhatRelease_redhat-release.release')

INSTNUM_FACTS_TUPLE = ('Instnum_instnum.instnum',)

SYSID_FACTS_TUPLE = ('SysId_systemid.system_id',
'SysId_systemid.username')

CPU_FACTS_TUPLE = ('Cpu_cpu.count',
'Cpu_cpu.socket_count',
'Cpu_cpu.vendor_id',
'Cpu_cpu.bogomips',
'Cpu_cpu.cpu_family',
'Cpu_cpu.model_name',
'Cpu_cpu.model_ver')

ETC_RELEASE_FACTS_TUPLE = ('EtcRelease_etc_release.name',
'EtcRelease_etc_release.version',
'EtcRelease_etc_release.release')

ETC_ISSUE_FACTS_TUPLE = ('EtcIssue_etc-issue.etc-issue',)

DMI_FACTS_TUPLE = ('Dmi_dmi.bios-vendor',
'Dmi_dmi.bios-version',
'Dmi_dmi.system-manufacturer',
'Dmi_dmi.processor-family')

VIRT_FACTS_TUPLE = ('Virt_virt.virt',
'Virt_virt.type',
'Virt_virt.num_guests',
'Virt_virt.num_running_guests')

RH_PKG_FACTS_TUPLE = ('RedhatPackages_redhat-packages.is_redhat',
'RedhatPackages_redhat-packages.num_rh_packages',
'RedhatPackages_redhat-packages.num_installed_packages',
'RedhatPackages_redhat-packages.last_installed',
'RedhatPackages_redhat-packages.last_built')

VIRT_WHAT_FACTS_TUPLE = ('VirtWhat_virt-what.type',)

DATE_FACTS_TUPLE = ('Date_date.date',
'Date_date.anaconda_log',
'Date_date.machine_id',
'Date_date.filesystem_create',
'Date_date.yum_history')

SUBMAN_FACTS_TUPLE = ('SubmanFacts_subman.cpu.core(s)_per_socket',
'SubmanFacts_subman.cpu.cpu(s)',
'SubmanFacts_subman.cpu.cpu_socket(s)',
'SubmanFacts_subman.virt.host_type',
'SubmanFacts_subman.virt.is_guest',
'SubmanFacts_subman.virt.uuid',
'SubmanFacts_subman.has_facts_file')

DEFAULT_FACTS_TUPLE = SUBMAN_FACTS_TUPLE + DATE_FACTS_TUPLE \
+ VIRT_WHAT_FACTS_TUPLE + RH_PKG_FACTS_TUPLE + VIRT_FACTS_TUPLE \
+ DMI_FACTS_TUPLE + ETC_ISSUE_FACTS_TUPLE + ETC_RELEASE_FACTS_TUPLE \
+ CPU_FACTS_TUPLE + SYSID_FACTS_TUPLE + INSTNUM_FACTS_TUPLE \
+ REDHAT_RELEASE_FACTS_TUPLE + UNAME_FACTS_TUPLE


def ensure_config_dir_exists():
"""Ensure the Rho configuration directory exists."""
Expand Down
1 change: 1 addition & 0 deletions rho_playbook.yml
Expand Up @@ -8,6 +8,7 @@
hosts: all
roles:
- collect
- uname


- name: write facts first to a variable and then to csv locally
Expand Down
73 changes: 73 additions & 0 deletions roles/uname/tasks/main.yml
@@ -0,0 +1,73 @@
---

- name: gather uname.os fact
raw: uname -s
register: uname_os
ignore_errors: yes
when: '"Username_uname.os" in facts_to_collect'

- name: add uname.os to dictionary
set_fact:
uname: "{{ uname|default({}) | combine({ item: uname_os['stdout_lines'][0] }) }}"
with_items:
- 'uname.os'

- name: gather uname.hostname fact
raw: uname -n
register: uname_hostname
ignore_errors: yes
when: '"Username_uname.hostname" in facts_to_collect'

- name: add uname.hostname to dictionary
set_fact:
uname: "{{ uname|default({}) | combine({ item: uname_hostname['stdout_lines'][0] }) }}"
with_items:
- 'uname.hostname'

- name: gather uname.processor fact
raw: uname -p
register: uname_processor
ignore_errors: yes
when: '"Username_uname.processor" in facts_to_collect'

- name: add uname.processor to dictionary
set_fact:
uname: "{{ uname|default({}) | combine({ item: uname_processor['stdout_lines'][0] }) }}"
with_items:
- 'uname.processor'

- name: gather uname.kernel fact
raw: uname -r
register: uname_kernel
ignore_errors: yes
when: '"Username_uname.kernel" in facts_to_collect'

- name: add uname.kernel to dictionary
set_fact:
uname: "{{ uname|default({}) | combine({ item: uname_kernel['stdout_lines'][0] }) }}"
with_items:
- 'uname.kernel'

- name: gather uname.all fact
raw: uname -a
register: uname_all
ignore_errors: yes
when: '"Username_uname.all" in facts_to_collect'

- name: add uname.all to dictionary
set_fact:
uname: "{{ uname|default({}) | combine({ item: uname_all['stdout_lines'][0] }) }}"
with_items:
- 'uname.all'

- name: gather uname.hardware_platform fact
raw: uname -i
register: uname_hardware_platform
ignore_errors: yes
when: '"Username_uname.hardware_platform" in facts_to_collect'

- name: add uname.hardware_platform to dictionary
set_fact:
uname: "{{ uname|default({}) | combine({ item: uname_hardware_platform['stdout_lines'][0] }) }}"
with_items:
- 'uname.hardware_platform'
2 changes: 1 addition & 1 deletion roles/write/tasks/main.yml
@@ -1,7 +1,7 @@
---

- name: store facts from all hosts in a variable
set_fact: host_fact={{hostvars[item]["res"]}}
set_fact: host_fact={{hostvars[item]["res"] | combine(hostvars[item]["uname"]) }}
with_items: "{{groups.alpha}}"
register: host_facts

Expand Down

0 comments on commit 373e18c

Please sign in to comment.