Class: Gjallarhorn::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/gjallarhorn/cli.rb

Overview

Command-line interface for Gjallarhorn deployment operations

The CLI class provides a Thor-based command-line interface for all deployment operations including deploy, status checks, rollbacks, and configuration management. All methods include comprehensive error handling and user-friendly output.

Examples:

Deploy to production

gjallarhorn deploy production myapp:v1.2.3

Check status with custom config

gjallarhorn status staging --config staging-deploy.yml

Rollback to previous version

gjallarhorn rollback production v1.2.2

Since:

  • 0.1.0

Constant Summary collapse

DEFAULT_CONFIG_FILE =

Default configuration file path

Since:

  • 0.1.0

"config/deploy.yml"

Instance Method Summary collapse

Instance Method Details

#configObject

Display the current configuration in YAML format

Since:

  • 0.1.0



78
79
80
81
82
83
84
# File 'lib/gjallarhorn/cli.rb', line 78

def config
  configuration = Configuration.new(options[:config])
  puts configuration.to_yaml
rescue StandardError => e
  puts "Configuration error: #{e.message}"
  exit 1
end

#deploy(environment, image) ⇒ Object

Deploy a container image to the specified environment

Parameters:

  • environment (String)

    Target environment name

  • image (String)

    Container image tag to deploy

Since:

  • 0.1.0



35
36
37
38
39
40
41
# File 'lib/gjallarhorn/cli.rb', line 35

def deploy(environment, image)
  deployer = Deployer.new(options[:config])
  deployer.deploy(environment, image)
rescue StandardError => e
  puts "Deployment failed: #{e.message}"
  exit 1
end

#display_deployment_record(record) ⇒ void (private)

This method returns an undefined value.

Display a single deployment record with formatting

Parameters:

  • record (Hash)

    Deployment record

Since:

  • 0.1.0



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gjallarhorn/cli.rb', line 130

def display_deployment_record(record)
  timestamp = Time.parse(record["timestamp"]).strftime("%Y-%m-%d %H:%M:%S UTC")
  status_indicator = case record["status"]
                     when "success" then ""
                     when "failed" then ""
                     else "🔄"
                     end

  puts "#{status_indicator} #{timestamp} - #{record["image"]}"
  puts "   Status: #{record["status"]}"
  puts "   Strategy: #{record["strategy"]}" if record["strategy"]
  puts "   Error: #{record["error"]}" if record["error"]
  puts
end

#history(environment) ⇒ Object

Display deployment history for the specified environment

Parameters:

  • environment (String)

    Target environment name

Since:

  • 0.1.0



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gjallarhorn/cli.rb', line 92

def history(environment)
  history_manager = History.new
  records = history_manager.get_history(environment: environment, limit: options[:limit])

  if records.empty?
    puts "No deployment history found for #{environment}"
    return
  end

  puts "Deployment history for #{environment}:"
  puts "=" * 60

  records.each do |record|
    display_deployment_record(record)
  end

  # Show statistics
  stats = history_manager.statistics(environment: environment)
  puts "Statistics:"
  puts "  Total deployments: #{stats[:total_deployments]}"
  puts "  Success rate: #{stats[:success_rate]}% (#{stats[:successful_deployments]}/#{stats[:total_deployments]})"
rescue StandardError => e
  puts "Failed to retrieve history: #{e.message}"
  exit 1
end

#rollback(environment, version) ⇒ Object

Rollback services in an environment to a previous version

Parameters:

  • environment (String)

    Target environment name

  • version (String)

    Version to rollback to

Since:

  • 0.1.0



67
68
69
70
71
72
73
# File 'lib/gjallarhorn/cli.rb', line 67

def rollback(environment, version)
  deployer = Deployer.new(options[:config])
  deployer.rollback(environment, version)
rescue StandardError => e
  puts "Rollback failed: #{e.message}"
  exit 1
end

#status(environment) ⇒ Object

Check the deployment status for services in an environment

Parameters:

  • environment (String)

    Target environment name

Since:

  • 0.1.0



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gjallarhorn/cli.rb', line 48

def status(environment)
  deployer = Deployer.new(options[:config])
  result = deployer.status(environment)

  puts "Status for #{environment}:"
  result.each do |status_info|
    puts "  #{status_info}"
  end
rescue StandardError => e
  puts "Status check failed: #{e.message}"
  exit 1
end

#versionObject

Display the current Gjallarhorn version

Since:

  • 0.1.0



120
121
122
# File 'lib/gjallarhorn/cli.rb', line 120

def version
  puts Gjallarhorn::VERSION
end