Class: Nomade::Deployer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nomad_endpoint, opts = {}) ⇒ Deployer

Returns a new instance of Deployer.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nomade/deployer.rb', line 7

def initialize(nomad_endpoint, opts = {})
  @nomad_endpoint = nomad_endpoint
  @http = Nomade::Http.new(@nomad_endpoint)
  @job_builder = Nomade::JobBuilder.new(@http)
  @logger = opts.fetch(:logger, Nomade.logger)

  @timeout = opts.fetch(:timeout, 60 * 3)

  @hooks = {
    Nomade::Hooks::DEPLOY_RUNNING => [],
    Nomade::Hooks::DEPLOY_FINISHED => [],
    Nomade::Hooks::DEPLOY_FAILED => [],

    Nomade::Hooks::DISPATCH_RUNNING => [],
    Nomade::Hooks::DISPATCH_FINISHED => [],
    Nomade::Hooks::DISPATCH_FAILED => [],
  }
  add_hook(Nomade::Hooks::DEPLOY_FAILED, lambda {|hook_type, nomad_job, messages|
    @logger.error "Failing deploy:"
    messages.each do |message|
      @logger.error "- #{message}"
    end
  })
  add_hook(Nomade::Hooks::DISPATCH_FAILED, lambda {|hook_type, nomad_job, messages|
    @logger.error "Failing dispatch:"
    messages.each do |message|
      @logger.error "- #{message}"
    end
  })

  self
end

Instance Attribute Details

#nomad_jobObject (readonly)

Returns the value of attribute nomad_job.



5
6
7
# File 'lib/nomade/deployer.rb', line 5

def nomad_job
  @nomad_job
end

Instance Method Details

#add_hook(hook, hook_method) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nomade/deployer.rb', line 57

def add_hook(hook, hook_method)
  if Nomade::Hooks::DEPLOY_RUNNING == hook
    @hooks[Nomade::Hooks::DEPLOY_RUNNING] << hook_method
  elsif Nomade::Hooks::DEPLOY_FINISHED == hook
    @hooks[Nomade::Hooks::DEPLOY_FINISHED] << hook_method
  elsif Nomade::Hooks::DEPLOY_FAILED == hook
    @hooks[Nomade::Hooks::DEPLOY_FAILED] << hook_method
  elsif Nomade::Hooks::DISPATCH_RUNNING == hook
    @hooks[Nomade::Hooks::DISPATCH_RUNNING] << hook_method
  elsif Nomade::Hooks::DISPATCH_FINISHED == hook
    @hooks[Nomade::Hooks::DISPATCH_FINISHED] << hook_method
  elsif Nomade::Hooks::DISPATCH_FAILED == hook
    @hooks[Nomade::Hooks::DISPATCH_FAILED] << hook_method
  else
    raise "#{hook} not supported!"
  end
end

#deploy!Object



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
# File 'lib/nomade/deployer.rb', line 75

def deploy!
  check_for_job_init

  run_hooks(Nomade::Hooks::DEPLOY_RUNNING, @nomad_job, nil)
  _plan
  _deploy
  run_hooks(Nomade::Hooks::DEPLOY_FINISHED, @nomad_job, nil)
rescue Nomade::NoModificationsError => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "No modifications to make, exiting!"].compact.uniq)
rescue Nomade::GeneralError => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "GeneralError hit, exiting!"].compact.uniq)
  exit(1)
rescue Nomade::AllocationFailedError => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "Allocation failed with errors, exiting!"].compact.uniq)
  exit(3)
rescue Nomade::UnsupportedDeploymentMode => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "Deployment failed with errors, exiting!"].compact.uniq)
  exit(4)
rescue Nomade::FailedTaskGroupPlan => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "Couldn't plan correctly, exiting!"].compact.uniq)
  exit(5)
rescue Nomade::DeploymentFailedError => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "Couldn't deploy succesfully, exiting!"].compact.uniq)
  exit(6)
rescue Nomade::HttpConnectionError => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "Http connection error, exiting!"].compact.uniq)
  exit(7)
rescue Nomade::HttpBadResponse => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "Http bad response, exiting!"].compact.uniq)
  exit(8)
rescue Nomade::HttpBadContentType => e
  run_hooks(Nomade::Hooks::DEPLOY_FAILED, @nomad_job, [e.class.to_s, e.message, "Http unexpected content type!"].compact.uniq)
  exit(9)
end

#dispatch!(payload_data: nil, payload_metadata: {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/nomade/deployer.rb', line 110

def dispatch!(payload_data: nil, payload_metadata: {})
  check_for_job_init

  run_hooks(Nomade::Hooks::DISPATCH_RUNNING, @nomad_job, nil)
  _dispatch(payload_data, )
  run_hooks(Nomade::Hooks::DISPATCH_FINISHED, @nomad_job, nil)
rescue Nomade::DispatchMetaDataFormattingError => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Metadata wrongly formatted, exiting!"].compact.uniq)
  exit(10)
rescue Nomade::DispatchMissingMetaData => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Required metadata missing, exiting!"].compact.uniq)
  exit(11)
rescue Nomade::DispatchUnknownMetaData => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Unknown metadata sent to server, exiting!"].compact.uniq)
  exit(12)
rescue Nomade::DispatchMissingPayload => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Job requires payload, but payload isn't set!"].compact.uniq)
  exit(20)
rescue Nomade::DispatchPayloadNotAllowed => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Job does not allow payload!"].compact.uniq)
  exit(21)
rescue Nomade::DispatchPayloadUnknown => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "API error!"].compact.uniq)
  exit(22)
rescue Nomade::DispatchWrongJobType => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Job has wrong job-type"].compact.uniq)
  exit(30)
rescue Nomade::DispatchNotParamaterized => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Job is not paramaterized"].compact.uniq)
  exit(31)
rescue Nomade::AllocationFailedError => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Allocation failed with errors, exiting!"].compact.uniq)
  exit(40)
rescue Nomade::GeneralError => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "GeneralError hit, exiting!"].compact.uniq)
  exit(1)
rescue Nomade::HttpConnectionError => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Http connection error, exiting!"].compact.uniq)
  exit(7)
rescue Nomade::HttpBadResponse => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Http bad response, exiting!"].compact.uniq)
  exit(8)
rescue Nomade::HttpBadContentType => e
  run_hooks(Nomade::Hooks::DISPATCH_FAILED, @nomad_job, [e.class.to_s, e.message, "Http unexpected content type!"].compact.uniq)
  exit(9)
end

#init_job(template_file, image_full_name, template_variables = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nomade/deployer.rb', line 40

def init_job(template_file, image_full_name, template_variables = {})
  @nomad_job = @job_builder.build(template_file, image_full_name, template_variables)
  @evaluation_id = nil
  @deployment_id = nil

  self
rescue Nomade::HttpConnectionError => e
  [e.class.to_s, e.message, "Http connection error, exiting!"].compact.uniq.map{|l| @logger.error(l)}
  exit(7)
rescue Nomade::HttpBadResponse => e
  [e.class.to_s, e.message, "Http bad response, exiting!"].compact.uniq.map{|l| @logger.error(l)}
  exit(8)
rescue Nomade::HttpBadContentType => e
  [e.class.to_s, e.message, "Http unexpected content type!"].compact.uniq.map{|l| @logger.error(l)}
  exit(9)
end

#stop!(purge = false) ⇒ Object



157
158
159
# File 'lib/nomade/deployer.rb', line 157

def stop!(purge = false)
  @http.stop_job(@nomad_job, purge)
end