Class: Sxn::Rules::SetupCommandsRule

Inherits:
BaseRule
  • Object
show all
Defined in:
lib/sxn/rules/setup_commands_rule.rb

Overview

SetupCommandsRule executes project setup commands safely using the SecureCommandExecutor. It supports environment variables, conditional execution, and proper error handling.

Configuration format: {

"commands" => [
  {
    "command" => ["bundle", "install"],
    "env" => { "RAILS_ENV" => "development" },
    "timeout" => 120,
    "condition" => "file_missing:Gemfile.lock",
    "description" => "Install Ruby dependencies",
    "required" => true,
    "working_directory" => "."
  }
],
"continue_on_failure" => false

}

Examples:

Basic usage

rule = SetupCommandsRule.new(
  "rails_setup",
  {
    "commands" => [
      { "command" => ["bundle", "install"] },
      { "command" => ["bin/rails", "db:create"] },
      { "command" => ["bin/rails", "db:migrate"] }
    ]
  },
  "/path/to/project",
  "/path/to/session"
)
rule.validate
rule.apply

Constant Summary collapse

CONDITION_TYPES =

Supported condition types for conditional execution

{
  "file_exists" => :file_exists?,
  "file_missing" => :file_missing?,
  "directory_exists" => :directory_exists?,
  "directory_missing" => :directory_missing?,
  "command_available" => :command_available?,
  "env_var_set" => :env_var_set?,
  "always" => :always_true
}.freeze
DEFAULT_TIMEOUT =

Default command timeout in seconds

60
MAX_TIMEOUT =

Maximum allowed timeout in seconds

1800

Constants included from BaseRule::States

BaseRule::States::APPLIED, BaseRule::States::APPLYING, BaseRule::States::FAILED, BaseRule::States::PENDING, BaseRule::States::ROLLED_BACK, BaseRule::States::ROLLING_BACK, BaseRule::States::VALIDATED, BaseRule::States::VALIDATING

Instance Attribute Summary

Attributes inherited from BaseRule

#changes, #config, #dependencies, #errors, #name, #project_path, #session_path, #state

Instance Method Summary collapse

Methods inherited from BaseRule

#applied?, #can_execute?, #description, #duration, #failed?, #required?, #rollback, #rollbackable?, #to_h, #type, #validate, #validate_config_hash

Constructor Details

#initialize(arg1 = nil, arg2 = nil, arg3 = nil, arg4 = nil, dependencies: []) ⇒ SetupCommandsRule

Initialize the setup commands rule



63
64
65
66
67
# File 'lib/sxn/rules/setup_commands_rule.rb', line 63

def initialize(arg1 = nil, arg2 = nil, arg3 = nil, arg4 = nil, dependencies: [])
  super
  @command_executor = Security::SecureCommandExecutor.new(@session_path, logger: logger)
  @executed_commands = []
end

Instance Method Details

#apply(_context = {}) ⇒ Object

Apply the command execution operations



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sxn/rules/setup_commands_rule.rb', line 72

def apply(_context = {})
  change_state!(APPLYING)
  continue_on_failure = @config.fetch("continue_on_failure", false)

  begin
    @config["commands"].each_with_index do |command_config, index|
      apply_command(command_config, index, continue_on_failure)
    end

    change_state!(APPLIED)
    log(:info, "Successfully executed #{@executed_commands.size} commands")
    true
  rescue StandardError => e
    @errors << e
    change_state!(FAILED)
    raise ApplicationError, "Failed to execute setup commands: #{e.message}"
  end
end

#execution_summaryObject

Get summary of executed commands



92
93
94
95
96
97
98
99
100
101
# File 'lib/sxn/rules/setup_commands_rule.rb', line 92

def execution_summary
  @executed_commands.map do |cmd_result|
    {
      command: cmd_result[:command],
      success: cmd_result[:result].success?,
      duration: cmd_result[:result].duration,
      exit_status: cmd_result[:result].exit_status
    }
  end
end