Class: Gjallarhorn::Deployer

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

Overview

Main deployment orchestrator class

Since:

  • 0.1.0

Constant Summary collapse

DEFAULT_CONFIG_FILE =

Default configuration file path

Since:

  • 0.1.0

"config/deploy.yml"
ADAPTERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Mapping of provider names to their corresponding adapter classes

Since:

  • 0.1.0

{
  "aws" => Adapter::AWSAdapter,
  "gcp" => nil, # TODO: Implement in Phase 2
  "azure" => nil, # TODO: Implement in Phase 2
  "docker" => nil, # TODO: Implement in Phase 2
  "kubernetes" => nil # TODO: Implement in Phase 3
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = DEFAULT_CONFIG_FILE) ⇒ Deployer

Initialize a new Deployer instance

Parameters:

  • config_file (String) (defaults to: DEFAULT_CONFIG_FILE)

    Path to the YAML configuration file

Raises:

Since:

  • 0.1.0



61
62
63
64
65
# File 'lib/gjallarhorn/deployer.rb', line 61

def initialize(config_file = DEFAULT_CONFIG_FILE)
  @configuration = Configuration.new(config_file)
  @logger = Logger.new($stdout)
  @history = History.new
end

Instance Attribute Details

#configurationConfiguration (readonly)

Returns The loaded deployment configuration.

Returns:

Since:

  • 0.1.0



52
53
54
# File 'lib/gjallarhorn/deployer.rb', line 52

def configuration
  @configuration
end

#loggerLogger (readonly)

Returns Logger instance for deployment operations.

Returns:

  • (Logger)

    Logger instance for deployment operations

Since:

  • 0.1.0



55
56
57
# File 'lib/gjallarhorn/deployer.rb', line 55

def logger
  @logger
end

Instance Method Details

#create_adapter(environment) ⇒ Adapter::Base (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create an adapter instance for the specified environment

Parameters:

  • environment (String)

    Target environment name

Returns:

Raises:

Since:

  • 0.1.0



180
181
182
183
184
185
186
187
188
# File 'lib/gjallarhorn/deployer.rb', line 180

def create_adapter(environment)
  env_config = @configuration.environment(environment)
  provider = env_config["provider"]
  adapter_class = ADAPTERS[provider]

  raise DeploymentError, "Provider '#{provider}' not yet implemented" unless adapter_class

  adapter_class.new(env_config)
end

#create_deployment_strategy(strategy_name, adapter, environment) ⇒ Deployment::Strategy (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a deployment strategy instance

Parameters:

  • strategy_name (String)

    Strategy name ('zero_downtime', 'rolling', 'basic')

  • adapter (Adapter::Base)

    Adapter instance

  • environment (String)

    Target environment name

Returns:

Raises:

Since:

  • 0.1.0



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/gjallarhorn/deployer.rb', line 198

def create_deployment_strategy(strategy_name, adapter, environment)
  proxy_manager = create_proxy_manager(environment) if strategy_name == "zero_downtime"

  case strategy_name
  when "zero_downtime"
    Deployment::ZeroDowntime.new(adapter, proxy_manager, @logger)
  when "basic"
    Deployment::Basic.new(adapter, proxy_manager, @logger)
  when "legacy"
    Deployment::Legacy.new(adapter, proxy_manager, @logger)
  else
    raise DeploymentError, "Deployment strategy '#{strategy_name}' not yet implemented"
  end
end

#create_proxy_manager(environment) ⇒ Proxy::Manager? (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a proxy manager instance for zero-downtime deployments

Parameters:

  • environment (String)

    Target environment name

Returns:

  • (Proxy::Manager, nil)

    Proxy manager instance or nil if not configured

Since:

  • 0.1.0



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/gjallarhorn/deployer.rb', line 218

def create_proxy_manager(environment)
  env_config = @configuration.environment(environment)
  proxy_config = env_config["proxy"]

  return nil unless proxy_config

  Proxy::Manager.create(proxy_config.transform_keys(&:to_sym), @logger)
rescue StandardError => e
  @logger.warn "Failed to create proxy manager: #{e.message}"
  nil
end

#deploy(environment, image, strategy: "zero_downtime") ⇒ void

This method returns an undefined value.

Deploy a container image to the specified environment

Parameters:

  • environment (String)

    Target environment name (e.g., 'production', 'staging')

  • image (String)

    Container image tag to deploy (e.g., 'myapp:v1.2.3')

  • strategy (String) (defaults to: "zero_downtime")

    Deployment strategy to use ('zero_downtime', 'rolling', 'basic')

Raises:

  • (DeploymentError)

    If the deployment fails or provider is not supported

Since:

  • 0.1.0



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gjallarhorn/deployer.rb', line 74

def deploy(environment, image, strategy: "zero_downtime")
  # Record deployment start
  @history.record_deployment(
    environment: environment,
    image: image,
    status: "started",
    strategy: strategy
  )

  adapter = create_adapter(environment)
  deployment_strategy = create_deployment_strategy(strategy, adapter, environment)

  @logger.info "Deploying #{image} to #{environment} using #{adapter.class.name} with #{strategy} strategy"

  deployment_strategy.deploy(
    image: image,
    environment: environment,
    services: @configuration.services_for(environment)
  )

  @logger.info "Deployment completed successfully"

  # Record successful deployment
  @history.record_deployment(
    environment: environment,
    image: image,
    status: "success",
    strategy: strategy
  )
rescue StandardError => e
  # Record failed deployment
  @history.record_deployment(
    environment: environment,
    image: image,
    status: "failed",
    strategy: strategy,
    error: e.message
  )
  raise
end

#deploy_with_strategy(environment, image, strategy) ⇒ void

This method returns an undefined value.

Deploy with specific strategy (convenience method)

Parameters:

  • environment (String)

    Target environment name

  • image (String)

    Container image tag to deploy

  • strategy (String)

    Deployment strategy to use

Since:

  • 0.1.0



121
122
123
# File 'lib/gjallarhorn/deployer.rb', line 121

def deploy_with_strategy(environment, image, strategy)
  deploy(environment, image, strategy: strategy)
end

#rollback(environment, version) ⇒ void

This method returns an undefined value.

Rollback services in the environment to a previous version

Parameters:

  • environment (String)

    Target environment name

  • version (String)

    Version to rollback to (e.g., 'v1.2.2')

Raises:

Since:

  • 0.1.0



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gjallarhorn/deployer.rb', line 141

def rollback(environment, version)
  # Record rollback start
  @history.record_deployment(
    environment: environment,
    image: version,
    status: "started",
    strategy: "rollback"
  )

  adapter = create_adapter(environment)
  adapter.rollback(version: version)

  # Record successful rollback
  @history.record_deployment(
    environment: environment,
    image: version,
    status: "success",
    strategy: "rollback"
  )
rescue StandardError => e
  # Record failed rollback
  @history.record_deployment(
    environment: environment,
    image: version,
    status: "failed",
    strategy: "rollback",
    error: e.message
  )
  raise
end

#status(environment) ⇒ Hash

Get the current status of services in the specified environment

Parameters:

  • environment (String)

    Target environment name

Returns:

  • (Hash)

    Status information for all services in the environment

Raises:

Since:

  • 0.1.0



130
131
132
133
# File 'lib/gjallarhorn/deployer.rb', line 130

def status(environment)
  adapter = create_adapter(environment)
  adapter.status
end