Class: Algo::Dsl::Service::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/algo/dsl/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cluster) ⇒ Context

Returns a new instance of Context.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/algo/dsl/service.rb', line 60

def initialize name, cluster
  @cluster = cluster
  @context = {
    'Name' => "#{cluster_prefix}#{name}",
    'TaskTemplate' => {
      'ContainerSpec' => {
        'Image' => nil
      }
    },
    'Mode' => {
      'Replicated' => {
        'Replicas' => 1
      }
    },
    'Labels' => @cluster['labels']
  }
  @context['TaskTemplate']['ContainerSpec']['Env'] = @cluster['env'] if @cluster['env'].present?
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



58
59
60
# File 'lib/algo/dsl/service.rb', line 58

def context
  @context
end

Instance Method Details

#args(*items) ⇒ Object



89
90
91
# File 'lib/algo/dsl/service.rb', line 89

def args *items
  @context['TaskTemplate']['ContainerSpec']['Args'] = items
end

#command(*item) ⇒ Object



85
86
87
# File 'lib/algo/dsl/service.rb', line 85

def command *item
  @context['TaskTemplate']['ContainerSpec']['Command'] = item
end

#constraint(condition) ⇒ Object

Placement



174
175
176
177
178
# File 'lib/algo/dsl/service.rb', line 174

def constraint condition
  @context['TaskTemplate']['Placement'] ||= {}
  @context['TaskTemplate']['Placement']['Constraints'] ||= []
  @context['TaskTemplate']['Placement']['Constraints'] << condition
end

#endpoint_mode(mode) ⇒ Object

Parameters:

  • mode (String)

    vip or dnsrr



201
202
203
# File 'lib/algo/dsl/service.rb', line 201

def endpoint_mode mode
  @context['EndpointSpec'] = { 'Mode' => mode }
end

#env(key, val) ⇒ Object



93
94
95
96
# File 'lib/algo/dsl/service.rb', line 93

def env key, val
  @context['TaskTemplate']['ContainerSpec']['Env'] ||= []
  @context['TaskTemplate']['ContainerSpec']['Env'] << "#{key}=#{val}"
end

#globalObject



194
195
196
# File 'lib/algo/dsl/service.rb', line 194

def global
  @context['Mode'] = { 'Global' => {} }
end

#image(image_name) ⇒ Object

ContainerSpec



81
82
83
# File 'lib/algo/dsl/service.rb', line 81

def image image_name
  @context['TaskTemplate']['ContainerSpec']['Image'] = image_name
end

#label(key, val) ⇒ Object

Label



183
184
185
186
# File 'lib/algo/dsl/service.rb', line 183

def label key, val
  @context['Labels'] ||= {}
  @context['Labels'][key] = val
end

#limit_cpu(decimal) ⇒ Object

Resources



122
123
124
125
126
# File 'lib/algo/dsl/service.rb', line 122

def limit_cpu decimal
  @context['TaskTemplate']['Resources'] ||= {}
  @context['TaskTemplate']['Resources']['Limits'] ||= {}
  @context['TaskTemplate']['Resources']['Limits']['NanoCPUs'] = decimal * 1e9
end

#limit_memory(memory) ⇒ Object

Parameters:

  • memory (String)

    num with unit like 1B 20KB 30MB 1GB



129
130
131
132
133
# File 'lib/algo/dsl/service.rb', line 129

def limit_memory memory
  @context['TaskTemplate']['Resources'] ||= {}
  @context['TaskTemplate']['Resources']['Limits'] ||= {}
  @context['TaskTemplate']['Resources']['Limits']['MemoryBytes'] = memory_from_string memory
end

#network(name) ⇒ Object

Networks



232
233
234
235
# File 'lib/algo/dsl/service.rb', line 232

def network name
  @context['Networks'] ||= []
  @context['Networks'] << { 'Target' => "#{cluster_prefix}#{name}" }
end

#publish(port) ⇒ Object

Parameters:

  • port (String)

    like 80 or 80:80 or 80/udp



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/algo/dsl/service.rb', line 206

def publish port
  port, protocol = *port.split('/')  if '/'.in? port
  target, publish = *port.split(':')  if ':'.in? port
  @context['EndpointSpec'] ||= {}
  @context['EndpointSpec']['Ports'] ||= []
  @context['EndpointSpec']['Ports'] << {
    'Protocol' => protocol,
    'TargetPort' => target,
    'PublishedPort' => publish
  }.compact
end

#replicas(replica_size) ⇒ Object

Mode



190
191
192
# File 'lib/algo/dsl/service.rb', line 190

def replicas replica_size
  @context['Mode'] = { 'Replicated' => { 'Replicas' => replica_size } }
end

#reserve_cpu(decimal) ⇒ Object



135
136
137
138
139
# File 'lib/algo/dsl/service.rb', line 135

def reserve_cpu decimal
  @context['TaskTemplate']['Resources'] ||= {}
  @context['TaskTemplate']['Resources']['Reservation'] ||= {}
  @context['TaskTemplate']['Resources']['Reservation']['NanoCPUs'] = decimal * 1e9
end

#reserve_memory(memory) ⇒ Object

Parameters:

  • memory (String)

    num with unit like 1B 20KB 30MB 1GB



142
143
144
145
146
# File 'lib/algo/dsl/service.rb', line 142

def reserve_memory memory
  @context['TaskTemplate']['Resources'] ||= {}
  @context['TaskTemplate']['Resources']['Reservation'] ||= {}
  @context['TaskTemplate']['Resources']['Reservation']['MemoryBytes'] = memory_from_string memory
end

#restart_condition(name) ⇒ Object

Parameters:

  • name (String)

    none, on-failure or any



151
152
153
154
# File 'lib/algo/dsl/service.rb', line 151

def restart_condition name
  @context['TaskTemplate']['RestartPolicy'] ||= {}
  @context['TaskTemplate']['RestartPolicy']['Condition'] = name
end

#restart_delay(period) ⇒ Object



156
157
158
159
# File 'lib/algo/dsl/service.rb', line 156

def restart_delay period
  @context['TaskTemplate']['RestartPolicy'] ||= {}
  @context['TaskTemplate']['RestartPolicy']['Delay'] = second_from_string(period) * 10e9
end

#restart_max_attempts(value) ⇒ Object

Parameters:

  • value (Integer)


162
163
164
165
# File 'lib/algo/dsl/service.rb', line 162

def restart_max_attempts value
  @context['TaskTemplate']['RestartPolicy'] ||= {}
  @context['TaskTemplate']['RestartPolicy']['Attempts'] = value
end

#restart_window(value) ⇒ Object



167
168
169
170
# File 'lib/algo/dsl/service.rb', line 167

def restart_window value
  @context['TaskTemplate']['RestartPolicy'] ||= {}
  @context['TaskTemplate']['RestartPolicy']['Window'] = second_from_string(period) * 10e9
end

#stop_grace_period(period) ⇒ Object

Parameters:

  • period (String)

    period string like 30s, 1m, 4h



107
108
109
# File 'lib/algo/dsl/service.rb', line 107

def stop_grace_period period
  @context['TaskTemplate']['ContainerSpec']['StopGracePeriod'] = second_from_string(period) * 1e9
end

#update_delay(n) ⇒ Object



225
226
227
228
# File 'lib/algo/dsl/service.rb', line 225

def update_delay n
  @context['UpdateConfig'] ||= {}
  @context['UpdateConfig']['Delay']= n
end

#update_parallelism(n) ⇒ Object

UpdateConfig



220
221
222
223
# File 'lib/algo/dsl/service.rb', line 220

def update_parallelism n
  @context['UpdateConfig'] ||= {}
  @context['UpdateConfig']['Parallelism']= n
end

#user(name) ⇒ Object



102
103
104
# File 'lib/algo/dsl/service.rb', line 102

def user name
  @context['TaskTemplate']['ContainerSpec']['User'] = name
end

#volume(&block) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/algo/dsl/service.rb', line 111

def volume &block
  raise 'should be called in cluster' unless @context
  ctx = Service::VolumeContext.new(@cluster, @context).tap do |ctx|
    ctx.instance_eval(&block)
  end
  @context['TaskTemplate']['ContainerSpec']['Mounts'] ||= []
  @context['TaskTemplate']['ContainerSpec']['Mounts'] << ctx.context
end

#workdir(name) ⇒ Object



98
99
100
# File 'lib/algo/dsl/service.rb', line 98

def workdir name
  @context['TaskTemplate']['ContainerSpec']['Dir'] = name
end