refactore operation results

This commit is contained in:
Anton Chuchkalov 2016-03-23 15:54:04 +03:00
parent 39253e8605
commit a71a6edf45
4 changed files with 48 additions and 42 deletions

View File

@ -3,6 +3,7 @@ require "lib/executors/expiration_scheduler"
require "lib/puts_and_flush" require "lib/puts_and_flush"
require "hooks" require "hooks"
require 'net/ssh' require 'net/ssh'
require_relative 'server_operation_result'
module Devops module Devops
module Executor module Executor
@ -10,18 +11,6 @@ module Devops
include Hooks include Hooks
include PutsAndFlush include PutsAndFlush
ERROR_CODES = {
server_bootstrap_fail: 2,
server_cannot_update_tags: 3,
server_bootstrap_private_ip_unset: 4,
server_not_in_chef_nodes: 5,
server_bootstrap_unknown_error: 7,
deploy_unknown_error: 6,
deploy_failed: 8,
creating_server_unknown_error: 9,
creating_server_in_cloud_failed: 10
}
# waiting for 5*60 seconds (5 min) # waiting for 5*60 seconds (5 min)
MAX_SSH_RETRIES_AMOUNT = 60 MAX_SSH_RETRIES_AMOUNT = 60
@ -56,21 +45,8 @@ module Devops
@current_user = options[:current_user] @current_user = options[:current_user]
end end
def self.error_code(reason)
ERROR_CODES.fetch(reason)
end
def self.reason_from_error_code(integer_code)
ERROR_CODES.key(integer_code) || :unknown_error
end
def self.error_occured_during_bootstrap?(code)
reason = reason_from_error_code(code)
[:server_bootstrap_fail, :server_not_in_chef_nodes, :server_bootstrap_unknown_error].include?(reason)
end
def error_code(reason) def error_code(reason)
self.class.error_code(reason) ServerOperationResult.code_of_reason(reason)
end end
def create_server_object options def create_server_object options

View File

@ -0,0 +1,41 @@
module Devops
module Executor
class ServerOperationResult
RESULT_CODES = {
ok: 0,
server_bootstrap_fail: 2,
server_cannot_update_tags: 3,
server_bootstrap_private_ip_unset: 4,
server_not_in_chef_nodes: 5,
server_bootstrap_unknown_error: 7,
deploy_unknown_error: 6,
deploy_failed: 8,
creating_server_unknown_error: 9,
creating_server_in_cloud_failed: 10
}
attr_reader :code
def initialize(error_code)
@code = error_code
end
def ok?
@code == 0
end
def reason
RESULT_CODES.key(@code) || :unknown_error
end
def occured_during_bootstrap?(code)
[:server_bootstrap_fail, :server_not_in_chef_nodes, :server_bootstrap_unknown_error].include?(reason)
end
def self.code_of_reason(reason)
RESULT_CODES.fetch(reason)
end
end
end
end

View File

@ -53,18 +53,6 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
end end
describe '.reason_from_error_code' do
it 'returns symbol given an integer' do
expect(described_class.reason_from_error_code(2)).to eq :server_bootstrap_fail
end
it "returns :unknown_error if can't recognize error code" do
expect(described_class.reason_from_error_code(123)).to eq :unknown_error
end
end
describe '#create_server_object' do describe '#create_server_object' do
it 'builds Server object' do it 'builds Server object' do
server = executor.create_server_object('created_by' => 'me') server = executor.create_server_object('created_by' => 'me')

View File

@ -28,15 +28,16 @@ class StackServersBootstrapper
private private
def check_bootstrap_result!(server_id, result_code, job_id) def check_bootstrap_result!(server_id, result_code, job_id)
if result_code == 0 operation_result = Devops::Executor::ServerOperationResult.new(result_code)
if operation_result.ok?
puts_and_flush "Server '#{server_id}' has been bootstraped (job #{job_id})." puts_and_flush "Server '#{server_id}' has been bootstraped (job #{job_id})."
return return
end end
reason = Devops::Executor::ServerExecutor.reason_from_error_code(result_code) puts_and_flush "Server '#{server_id}' bootstraped failed (job #{job_id}). Reason: #{operation_result.reason}"
puts_and_flush "Server '#{server_id}' bootstraped failed (job #{job_id}). Reason: #{reason}"
if Devops::Executor::ServerExecutor.error_occured_during_bootstrap?(result_code) if operation_result.occured_during_bootstrap?(operation_result.code)
raise StackServerBootstrapError # will cause rollback of a stack raise StackServerBootstrapError # will cause rollback of a stack
else else
raise StackServerDeployError # will not cause rollback of a stack raise StackServerDeployError # will not cause rollback of a stack