|
| 1 | +require 'open3' |
| 2 | +require 'yaml' |
| 3 | +require 'tempfile' |
| 4 | + |
| 5 | +# ProcedureVerifier: Validates AsciiDoc procedures as "guided exercises" |
| 6 | +class ProcedureVerifier |
| 7 | + def initialize(file_path) |
| 8 | + @file_path = file_path |
| 9 | + @content = File.read(file_path) |
| 10 | + @results = [] |
| 11 | + end |
| 12 | + |
| 13 | + def run_verification |
| 14 | + puts "--- Starting Procedure Validation: #{@file_path} ---" |
| 15 | + |
| 16 | + # 1. Check for Best Practices (Instructional Design) |
| 17 | + check_best_practices |
| 18 | + |
| 19 | + # 2. Extract and Process Blocks |
| 20 | + blocks = extract_code_blocks |
| 21 | + |
| 22 | + if blocks.empty? |
| 23 | + puts "[ERROR] No executable steps or source blocks found." |
| 24 | + return |
| 25 | + end |
| 26 | + |
| 27 | + blocks.each_with_index do |block, index| |
| 28 | + process_step(index + 1, block[:instruction], block[:type], block[:content], block[:is_example]) |
| 29 | + end |
| 30 | + |
| 31 | + summarize |
| 32 | + end |
| 33 | + |
| 34 | + private |
| 35 | + |
| 36 | + def extract_code_blocks |
| 37 | + blocks = [] |
| 38 | + lines = @content.lines |
| 39 | + i = 0 |
| 40 | + current_step = nil |
| 41 | + |
| 42 | + while i < lines.length |
| 43 | + line = lines[i] |
| 44 | + |
| 45 | + # Match numbered steps (., .., ..., etc.) to track context |
| 46 | + if line =~ /^(\.+)\s+(.+)$/ |
| 47 | + current_step = $2.strip |
| 48 | + end |
| 49 | + |
| 50 | + # Match source blocks with various formats: |
| 51 | + # [source,terminal], [source,bash], [source,yaml] |
| 52 | + # [source,terminal,subs="attributes+"], etc. |
| 53 | + if line =~ /^\[source,(terminal|bash|yaml|shell)(?:,.*?)?\]\s*$/ |
| 54 | + source_type = $1 |
| 55 | + |
| 56 | + # Check if this is an example output block (preceded by "Example output" or similar) |
| 57 | + is_example = false |
| 58 | + lookback = [i - 1, 0].max |
| 59 | + 5.times do |
| 60 | + break if lookback < 0 |
| 61 | + prev_line = lines[lookback].to_s.downcase |
| 62 | + if prev_line.include?("example output") || prev_line.include?("output is shown") |
| 63 | + is_example = true |
| 64 | + break |
| 65 | + end |
| 66 | + break if prev_line =~ /^(\.+)\s+/ # Stop at previous step |
| 67 | + lookback -= 1 |
| 68 | + end |
| 69 | + |
| 70 | + # Find the opening ---- delimiter |
| 71 | + i += 1 |
| 72 | + while i < lines.length && lines[i] !~ /^----\s*$/ |
| 73 | + i += 1 |
| 74 | + end |
| 75 | + |
| 76 | + # Extract content between ---- delimiters |
| 77 | + i += 1 |
| 78 | + content_lines = [] |
| 79 | + while i < lines.length && lines[i] !~ /^----\s*$/ |
| 80 | + content_lines << lines[i] |
| 81 | + i += 1 |
| 82 | + end |
| 83 | + |
| 84 | + content = content_lines.join.strip |
| 85 | + |
| 86 | + # Normalize terminal/shell to bash for execution |
| 87 | + type = (source_type == 'terminal' || source_type == 'shell') ? 'bash' : source_type |
| 88 | + |
| 89 | + # Only add non-empty blocks |
| 90 | + unless content.empty? |
| 91 | + blocks << { |
| 92 | + instruction: current_step || "Unknown step", |
| 93 | + type: type, |
| 94 | + content: content, |
| 95 | + is_example: is_example |
| 96 | + } |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + i += 1 |
| 101 | + end |
| 102 | + |
| 103 | + blocks |
| 104 | + end |
| 105 | + |
| 106 | + def check_best_practices |
| 107 | + # Ensure no "Magic Steps" - Identify assumed knowledge |
| 108 | + # Detect common CLI login/auth patterns across products |
| 109 | + login_patterns = [ |
| 110 | + 'oc login', # OpenShift |
| 111 | + 'ssh ', # Remote access |
| 112 | + 'sudo ', # Privilege escalation |
| 113 | + 'ansible-navigator', # Ansible |
| 114 | + 'ansible-playbook', # Ansible |
| 115 | + 'subscription-manager', # RHEL |
| 116 | + 'dnf install', # RHEL/Fedora |
| 117 | + 'yum install', # RHEL/CentOS |
| 118 | + 'kubectl', # Kubernetes |
| 119 | + 'export ', # Environment setup |
| 120 | + 'source ', # Environment setup |
| 121 | + ] |
| 122 | + |
| 123 | + has_setup = login_patterns.any? { |p| @content.include?(p) } |
| 124 | + |
| 125 | + if @content.length > 500 && !has_setup |
| 126 | + puts "[ADVICE] Warning: No login, environment setup, or tool invocation found. Check for 'magic steps'." |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + def process_step(step_num, instruction, type, content, is_example) |
| 131 | + puts "\n[Step #{step_num}] #{instruction}" |
| 132 | + |
| 133 | + if is_example |
| 134 | + puts "[SKIP] Example output - not executed" |
| 135 | + return |
| 136 | + end |
| 137 | + |
| 138 | + # Check for placeholders that need user input |
| 139 | + if has_placeholders?(content) |
| 140 | + puts "[SKIP] Contains placeholders (e.g., <path_to_must_gather>) - requires user input" |
| 141 | + puts "Content preview: #{content[0..100]}..." |
| 142 | + return |
| 143 | + end |
| 144 | + |
| 145 | + case type |
| 146 | + when 'yaml' |
| 147 | + validate_yaml(content, step_num) |
| 148 | + when 'bash' |
| 149 | + execute_bash(content, step_num, instruction) |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + def has_placeholders?(content) |
| 154 | + # Check for common placeholder patterns |
| 155 | + content.match?(/<[^>]+>/) || # Angle bracket placeholders like <path> |
| 156 | + content.match?(/\$\{[^}]+\}/) || # Variable placeholders like ${VAR} |
| 157 | + content.include?('CHANGEME') || |
| 158 | + content.include?('REPLACE') |
| 159 | + end |
| 160 | + |
| 161 | + def validate_yaml(content, step_num) |
| 162 | + begin |
| 163 | + # Lint the YAML for syntax errors |
| 164 | + YAML.safe_load(content) |
| 165 | + puts "[VALID] YAML syntax for Step #{step_num} is correct." |
| 166 | + |
| 167 | + # If it looks like a Kubernetes/OpenShift resource, try dry-run validation |
| 168 | + if content.include?("apiVersion:") |
| 169 | + # Detect available CLI tool |
| 170 | + cli_tool = if system('which oc > /dev/null 2>&1') |
| 171 | + 'oc' |
| 172 | + elsif system('which kubectl > /dev/null 2>&1') |
| 173 | + 'kubectl' |
| 174 | + else |
| 175 | + nil |
| 176 | + end |
| 177 | + |
| 178 | + if cli_tool |
| 179 | + Tempfile.open(['resource', '.yaml']) do |f| |
| 180 | + f.write(content) |
| 181 | + f.close |
| 182 | + stdout, stderr, status = Open3.capture3("#{cli_tool} apply -f #{f.path} --dry-run=client") |
| 183 | + if status.success? |
| 184 | + puts "[VALID] Resource logic (dry-run via #{cli_tool}) passed for Step #{step_num}." |
| 185 | + else |
| 186 | + puts "[FAILURE] Resource validation failed: #{stderr}" |
| 187 | + end |
| 188 | + end |
| 189 | + else |
| 190 | + puts "[SKIP] No oc or kubectl CLI found — skipping resource dry-run for Step #{step_num}." |
| 191 | + end |
| 192 | + end |
| 193 | + rescue Psych::SyntaxError => e |
| 194 | + puts "[FAILURE] YAML Syntax error in Step #{step_num}: #{e.message}" |
| 195 | + end |
| 196 | + end |
| 197 | + |
| 198 | + def execute_bash(command, step_num, instruction) |
| 199 | + # Clean up command: remove $ prompt symbols from each line |
| 200 | + clean_command = command.lines.map { |line| line.sub(/^\$ /, '') }.join |
| 201 | + |
| 202 | + # Handle multi-line commands with backslash continuations |
| 203 | + if clean_command.include?('\\') |
| 204 | + # Join lines that end with backslash |
| 205 | + clean_command = clean_command.gsub(/\\\n\s*/, ' ').strip |
| 206 | + else |
| 207 | + clean_command = clean_command.strip |
| 208 | + end |
| 209 | + |
| 210 | + puts "Executing: #{clean_command[0..150]}#{clean_command.length > 150 ? '...' : ''}" |
| 211 | + |
| 212 | + stdout, stderr, status = Open3.capture3(clean_command) |
| 213 | + |
| 214 | + if status.success? |
| 215 | + puts "[SUCCESS] Step #{step_num} executed." |
| 216 | + |
| 217 | + # Show output if it's a verification/check command |
| 218 | + if instruction.downcase.match?(/verify|check|confirm|retrieve|identify/) |
| 219 | + puts "Output: #{stdout.strip[0..200]}" unless stdout.strip.empty? |
| 220 | + puts "-> Verification successfully performed." |
| 221 | + end |
| 222 | + |
| 223 | + @results << { step: step_num, status: :passed, output: stdout.strip } |
| 224 | + else |
| 225 | + puts "[FAILURE] Step #{step_num} failed." |
| 226 | + puts "STDERR: #{stderr.strip}" unless stderr.strip.empty? |
| 227 | + puts "STDOUT: #{stdout.strip}" unless stdout.strip.empty? |
| 228 | + @results << { step: step_num, status: :failed, error: stderr.strip } |
| 229 | + |
| 230 | + # Don't exit immediately - continue with warnings |
| 231 | + puts "[WARNING] Continuing with remaining steps despite failure..." |
| 232 | + end |
| 233 | + end |
| 234 | + |
| 235 | + def summarize |
| 236 | + puts "\n" + "="*60 |
| 237 | + puts "FINAL SUMMARY" |
| 238 | + puts "="*60 |
| 239 | + |
| 240 | + passed = @results.count { |r| r[:status] == :passed } |
| 241 | + failed = @results.count { |r| r[:status] == :failed } |
| 242 | + |
| 243 | + puts "Total executable steps: #{@results.size}" |
| 244 | + puts "Passed: #{passed}" |
| 245 | + puts "Failed: #{failed}" |
| 246 | + |
| 247 | + if failed > 0 |
| 248 | + puts "\nFailed steps:" |
| 249 | + @results.select { |r| r[:status] == :failed }.each do |result| |
| 250 | + puts " - Step #{result[:step]}: #{result[:error]&.split("\n")&.first}" |
| 251 | + end |
| 252 | + end |
| 253 | + |
| 254 | + # Check if a global verification example was included |
| 255 | + unless @content.downcase.include?("verify") || @content.downcase.include?(".verification") |
| 256 | + puts "\n[ADVICE] Consider adding an end-to-end verification step to this procedure." |
| 257 | + end |
| 258 | + |
| 259 | + puts "="*60 |
| 260 | + puts passed == @results.size ? "✓ All steps PASSED" : "✗ Some steps FAILED" |
| 261 | + puts "="*60 |
| 262 | + end |
| 263 | +end |
| 264 | + |
| 265 | +# Execution |
| 266 | +if ARGV.empty? |
| 267 | + puts "Usage: ruby verify_procedure.rb <file.adoc>" |
| 268 | +else |
| 269 | + ProcedureVerifier.new(ARGV[0]).run_verification |
| 270 | +end |
0 commit comments