Compare commits

..

4 Commits

Author SHA1 Message Date
f76f0bcdf2 remove non standard dashes 2026-04-26 14:58:35 -07:00
44c492313c client api updated 2026-04-26 14:36:25 -07:00
b4839fde66 add purge_venv to jenkinsfile for disk api 2026-04-25 21:39:45 -07:00
67f3f8440b add refresh_api to jenkinsfile 2026-04-18 16:14:22 -07:00
16 changed files with 347 additions and 73 deletions

View File

@ -3,3 +3,8 @@
roles_path = /var/jenkins_home/ansible-windows/roles
ansible_root = /var/jenkins_home/ansible-windows
# show verbose time details in log
stdout_callback = default
callback_whitelist = profile_roles, profile_tasks
callbacks_enabled = profile_roles, profile_tasks

View File

@ -0,0 +1 @@
powershell -executionpolicy bypass -Command \\home.cosmos\netlogon\cosmosrm.ps1

View File

@ -0,0 +1,17 @@
# script for setting ansible service account to registry key
$username = "cosmos-ansible"
$ansible_registry = "HKLM:\SOFTWARE\Cosmos\Ansible"
$password_key = "Password"
$password = (Get-ItemProperty $ansible_registry).$password_key
# This is what the thing needs to set the password
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
# Set password
$UserAccount = Get-LocalUser -Name $username
$UserAccount | Set-LocalUser -Password $securePassword
# Make it a local admin
Add-LocalGroupMember -Group "Administrators" -Member $username
# Various Ansible Settings
Set-Item -Path WSMan:\localhost\Service\Auth\Basic -Value $true
Enable-WSManCredSSP -Role Server -Force

View File

@ -5,6 +5,9 @@ pipeline {
parameters {
string(name: 'host_ip', description: 'Target System Address')
string(name: 'api_service_port', defaultValue: "5000", description: 'API Service Port, probably don\'t change this')
booleanParam(name: 'refresh_api', defaultValue: false, description: 'When checked this will just update the API')
booleanParam(name: 'purge_venv', defaultValue: false, description: 'When checked this will remove the venv folder')
// reference for later
// choice(name: 'DEPLOY_ENV', choices: ['dev', 'staging', 'prod'], description: 'Environment to deploy to')
// booleanParam(name: 'rename_host', defaultValue: true, description: 'When checked hostname will be renamed')
@ -54,7 +57,7 @@ pipeline {
ansible-playbook -i \$inventory_file \$playbook_file \
--ssh-common-args='-o StrictHostKeyChecking=no' \
--extra-vars "api_service_port=${params.api_service_port}"
--extra-vars "api_service_port=${params.api_service_port} refresh_api=${params.refresh_api} purge_venv=${params.purge_venv}"
"""
}
}

View File

@ -6,9 +6,9 @@
roles:
- show_user_vars
#- show_user_vars
- cosmos_init
#- cosmos_init
- storage_api

View File

@ -7,6 +7,8 @@ windows_base_packages:
- windirstat
- putty
windows_desktop_packages:
windows_features:
- NET-Framework-Features
- Telnet-Client

View File

@ -0,0 +1,16 @@
---
- name: Install base packages
when: false
win_chocolatey:
name:
- "{{ windows_desktop_packages_item }}"
state: present
loop: "{{ windows_desktop_packages }}"
loop_control:
loop_var: windows_desktop_packages_item
...

View File

@ -1,16 +1,27 @@
---
- name: skip if refresh
when: not refresh_api | bool
block:
- name: Get Windows version
win_shell: "systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List"
register: windows_version
- name: Print Windows host information
debug:
msg: "{{ windows_version }}"
- name: Install base packages
- name: Install base packages
win_chocolatey:
name:
- "{{ windows_base_packages_item }}"
state: present
ignore_checksums: true
loop: "{{ windows_base_packages }}"
loop_control:
loop_var: windows_base_packages_item
- name: Install Server Services
- name: Install Server Services
when: "'server' in windows_version"
ansible.windows.win_feature:
name:
- "{{ windows_features_item }}"
@ -19,30 +30,33 @@
loop_control:
loop_var: windows_features_item
- name: Disable ms_tcpip6 of all the Interface
- name: Disable ms_tcpip6 of all the Interfaces
community.windows.win_net_adapter_feature:
interface: '*'
state: disabled
component_id:
- ms_tcpip6
- name: disable IE Enhanced Security
- name: windows server items
when: "'server' in windows_version"
block:
- name: disable for user
- name: disable IE-ESC for user
win_shell: 'Set-ItemProperty -Path "HKLM:\\SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -Name "IsInstalled" -Value 0'
- name: disable for admin
- name: disable IE-ESC for admin
win_shell: 'Set-ItemProperty -Path "HKLM:\\SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}" -Name "IsInstalled" -Value 0'
- name: disable automatic updates
- name: disable automatic updates
ansible.windows.win_regedit:
path: HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU
name: NoAutoUpdate
data: 1
- name: disable Firewall
- name: disable Firewall
win_shell: "netsh advfirewall set allprofiles state off"
#- name: desktop items
# when: "'server' not in windows_version"
# include_tasks: desktop.yaml
...

View File

@ -10,13 +10,13 @@
msg: "Hostname: {{ hostname_output.stdout_lines[0] }}"
- name: Test API
win_shell: "C:\\Windows\\system32\\curl --silent http://{{ ansible_ssh_host }}:5000/disk"
register: api_test_output
- name: Show Test Results
debug:
msg: "{{ api_test_output.stdout_lines }}"
#- name: Test API
# win_shell: "C:\\Windows\\system32\\curl --silent http://{{ ansible_ssh_host }}:5000/disk"
# register: api_test_output
#
#- name: Show Test Results
# debug:
# msg: "{{ api_test_output.stdout_lines }}"
...

View File

@ -16,6 +16,8 @@ nssm_folder: "{{ cosmos_root_folder }}\\nssm"
disk_service_name: "disk_api"
purge_venv: false
api_service_port: "5000"
...

View File

@ -1,2 +1,6 @@
Flask
flask_apscheduler
psutil
requests
pywin32
wmi

View File

@ -5,6 +5,7 @@
###############################################
- name: set up python venv
when: not refresh_api | bool
include_tasks: python_venv.yaml
- name: build python exe
@ -14,6 +15,7 @@
include_tasks: nssm.yaml
- name: set up scheduled task
when: false
include_tasks: update_task.yaml
...

View File

@ -1,16 +1,18 @@
---
- name: Copy CrystalDiskInfo archive
- name: skip when refresh
when: not refresh_api | bool
block:
- name: Copy CrystalDiskInfo archive
ansible.windows.win_copy:
src: /var/jenkins_home/ansible-files/programs/CrystalDiskInfo.zip
dest: "{{ storage_api_root }}\\CrystalDiskInfo.zip"
- name: Extract CrystalDiskInfo archive
- name: Extract CrystalDiskInfo archive
community.windows.win_unzip:
src: "{{ storage_api_root }}\\CrystalDiskInfo.zip"
dest: "{{ storage_api_root }}\\dist\\"
- name: Install nssm
- name: Install nssm
win_chocolatey:
name: nssm
state: present
@ -39,7 +41,7 @@
- "failed...... {{ disk_service_status.failed }}"
- name: Test API
win_shell: "C:\\Windows\\system32\\curl http://{{ ansible_ssh_host }}:5000/disk"
win_shell: "C:\\Windows\\system32\\curl http://{{ ansible_ssh_host }}:5000/test"
register: api_test_output
- name: Show Test Results

View File

@ -1,6 +1,7 @@
---
- name: Create service working folder
when: not refresh_api | bool
ansible.windows.win_file:
path: "{{ storage_api_root }}"
state: directory
@ -25,6 +26,7 @@
dest: "{{ storage_api_root }}\\disk_service.py"
- name: install pyinstaller
when: not refresh_api | bool
win_shell: "{{ python_venv_bin }} -m pip install pyinstaller"
- name: compile binary
@ -34,6 +36,7 @@
chdir: "{{ storage_api_root }}"
- name: Open up port 5000
when: not refresh_api | bool
community.windows.win_firewall_rule:
name: _ansible_python_disk_service
description: "Firewall rule to allow traffic for Disk info API"

View File

@ -5,6 +5,20 @@
name: python
state: present
- name: Purge venv if asked
when: purge_venv | bool and not refresh_api | bool
block:
- name: Remove service
ansible.windows.win_service:
name: "{{ disk_service_name }}"
state: absent
- name: purge folder
ansible.windows.win_file:
path: "{{ python_venv }}"
state: absent
- name: Create venv folder
ansible.windows.win_file:
path: "{{ python_venv }}"

View File

@ -1,11 +1,26 @@
# this got big...
from flask import Flask, jsonify
from flask_apscheduler import APScheduler
import psutil
import os
import requests
from requests import RequestException, Response
import json
from subprocess import check_output
import win32api
from datetime import datetime, timedelta
import sys
from pathlib import Path
import wmi
import pythoncom
import ctypes
from ctypes import wintypes
app = Flask(__name__)
scheduler = APScheduler()
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
# Bits to Bytes etc
# human readable bytes
def bytes_to_human_readable(bytes):
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if bytes < 1024.0:
@ -24,7 +39,9 @@ def get_crystal_disk_info():
# Split the file content into sections for each drive
drive_sections = output.split('----------------------------------------------------------------------------')
disk_id = 0
for section in drive_sections:
lines = section.strip().splitlines()
data = {
"Hostname": None,
@ -38,7 +55,9 @@ def get_crystal_disk_info():
"Power On Count": None,
"Host Writes": None,
"Wear Level Count": None,
"Drive Letter": None
"Drive Letter": None,
"Interface": None,
"Transfer Mode": None
}
for line in lines:
if "Model" in line:
@ -73,11 +92,20 @@ def get_crystal_disk_info():
data["Drive Letter"] = line.split(":", 1)[1].strip()
elif "Disk Size" in line:
if ":" in line:
data["Disk Size"] = line.split(":", 1)[1].strip()
raw = line.split(":", 1)[1].strip()
data["Disk Size"] = raw.split('GB')[0].strip() + ' GB'
elif "Interface" in line:
if ":" in line:
data["Interface"] = line.split(":", 1)[1].strip()
elif "Transfer Mode" in line:
if ":" in line:
data["Transfer Mode"] = line.split(":", 1)[1].strip()
# This makes sure something was changed,
if any(value is not None for value in data.values()):
data["Disk ID"] = disk_id
drives.append(data)
data["Hostname"] = "{{ hostname_output.stdout_lines[0] }}"
disk_id = disk_id + 1
#data["Hostname"] = "{{ hostname_output.stdout_lines[0] }}"
if not drives:
raise ValueError("No drive data found")
@ -92,10 +120,12 @@ def get_disk_info():
partitions = psutil.disk_partitions()
for partition in partitions:
usage = psutil.disk_usage(partition.mountpoint)
drive_letter = partition.device.replace('\\\\', '\\').rstrip('\\')
disk_info.append({
'device': partition.device.replace('\\\\', '\\').rstrip('\\'),
'device': drive_letter,
'label': get_drive_label(drive_letter),
#'mountpoint': partition.mountpoint,
#'fstype': partition.fstype,
'fstype': partition.fstype,
'total': bytes_to_human_readable(usage.total),
'used': bytes_to_human_readable(usage.used),
'free': bytes_to_human_readable(usage.free),
@ -103,6 +133,101 @@ def get_disk_info():
})
return disk_info
# drive label function
def get_drive_label(drive_letter: str) -> str:
result = "none"
root = drive_letter.strip()
if not root.endswith(':'):
root += ':'
if not root.endswith('\\'):
root += '\\'
# Make sure the drive actually exists
if not os.path.exists(root):
# Not a valid drive letter, return None so the caller can decide what to do.
print(f"[DEBUG] Drive '{root}' does not exist.")
result = "drive does not exist 0_o"
# Prepare buffers for the Win32 API call
volume_name_buf = ctypes.create_unicode_buffer(260) # MAX_PATH
fs_name_buf = ctypes.create_unicode_buffer(260)
serial_number = wintypes.DWORD()
max_component_len = wintypes.DWORD()
file_system_flags = wintypes.DWORD()
# Call GetVolumeInformationW
res = ctypes.windll.kernel32.GetVolumeInformationW(
ctypes.c_wchar_p(root), # lpRootPathName
volume_name_buf, # lpVolumeNameBuffer
ctypes.sizeof(volume_name_buf), # nVolumeNameSize
ctypes.byref(serial_number), # lpVolumeSerialNumber
ctypes.byref(max_component_len), # lpMaximumComponentLength
ctypes.byref(file_system_flags), # lpFileSystemFlags
fs_name_buf, # lpFileSystemNameBuffer
ctypes.sizeof(fs_name_buf) # nFileSystemNameSize
)
if res == 0: # The call failed
err = ctypes.get_last_error()
print(f"[ERROR] GetVolumeInformationW failed for '{root}'. "
f"Win32 error code: {err}")
result = "label error"
else:
result = volume_name_buf.value
if volume_name_buf.value == '':
result = "no_label"
return result
# os info function
def get_os_info() -> str:
result = "windows"
pythoncom.CoInitialize()
#try:
wmi_data = wmi.WMI()
os_info = wmi_data.Win32_OperatingSystem()[0]
#return {
# "Name" : os_info.Name,
# "Version" : os_info.Version,
# "BuildNumber": os_info.BuildNumber,
# "InstallDate": os_info.InstallDate,
# "ProductType": int(os_info.ProductType)
#}
# 1. Major version + edition (e.g. "10 Pro")
# os_info.Caption → "Microsoft Windows 10 Pro"
parts = os_info.Caption.split()
major = parts[2] # "10"
edition = parts[3] # "Pro" (for server: "2019", etc.)
major_edition = f"{major} {edition}"
# 2. Build number
build = os_info.BuildNumber
# 3. Install date (WMI gives an ISO-8601 string)
# e.g. "20210930142300.000000+000"
install_ts = os_info.InstallDate[:14] # "20210930142300"
dt = datetime.strptime(install_ts, "%Y%m%d%H%M%S")
install_date = f"{dt.month}-{dt.day}-{dt.year}"
result = f"Windows {major_edition} - Build {build} - Installed {install_date}"
#except Exception as e:
# print(e)
# result = "wmi_error"
return result
# server reporter info
def get_server_info() -> dict:
result = {}
drives_dict = get_crystal_disk_info()
data_dict = {
"hostname": "{{ hostname_output.stdout_lines[0] }}",
"os_string": get_os_info(),
"drives": drives_dict["drives"],
"API_KEY": "deadbeef",
"storage_summary": get_disk_info()
}
result = data_dict
return result
# Flask endpoints
@app.route('/disk', methods=['GET'])
@ -113,5 +238,69 @@ def disk():
def drive_health():
return jsonify(get_crystal_disk_info())
@app.route('/full', methods=['GET'])
def full_summary():
return jsonify(get_server_info())
@app.route('/test', methods=['GET'])
def test_flask():
return jsonify({
"message": "hello world",
"os_string": get_os_info()
})
def server_reporter():
#base_url="https://cosmostat.matt-cloud.com"
base_url="http://10.200.27.20:5001"
url = f"{base_url}/storage_client_update"
data_dict = get_server_info()
result = []
try:
response = requests.post(url, json=data_dict)
# Raise an exception for non-2xx status codes
response.raise_for_status()
result = response.json()
except:
result = {
"message": "error"
}
# Return the JSON payload
return result
if __name__ == '__main__':
# disk info Loop Function
def update_disk_info():
diskinfo_command = f"{{ storage_api_root }}\\dist\\DiskInfo64.exe /CopyExit"
result = check_output(diskinfo_command, shell=True)
print(result)
server_reporter()
return result
# gonna try something wild
run_date = datetime.now() + timedelta(seconds=15)
scheduler.add_job(id='init_disk_info',
func=update_disk_info,
trigger='date',
run_date=run_date)
scheduler.add_job(id='update_disk_info',
func=update_disk_info,
trigger='interval',
seconds=10000)
scheduler.add_job(id='server_reporter',
func=server_reporter,
trigger='interval',
seconds=15)
scheduler.init_app(app)
scheduler.start()
#update_disk_info()
app.run(host='0.0.0.0', port={{ api_service_port }})