Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
artyomb committed Jul 29, 2024
1 parent c94c6e7 commit b82cc36
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
dry-stack (0.1.4)
dry-stack (0.1.5)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This gem allows ...
cat simple_stack.drs | dry-stack -e to_compose | docker stack deploy -c - simple_stack
$ dry-stack
Version: 0.1.4
Version: 0.1.5
Usage:
dry-stack -s stackfile [options] COMMAND
cat stackfile | dry-stack COMMAND
Expand Down
6 changes: 5 additions & 1 deletion lib/dry-stack/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def load_env
end

def safe_eval(drs, params)
Dry::Stack(params[:name], params[:configuration]) { eval drs, self.binding }
Dry::Stack(params[:name], params[:configuration]) do
eval drs, self.binding
@after_blocks&.each { instance_exec &_1 }
end
end

def run(args)
Expand Down Expand Up @@ -82,6 +85,7 @@ def run(args)

params.transform_keys!{_1.to_s.gsub('-','_').to_sym}
params[:traefik_tls] = true if params[:tls_domain]
Dry.cmd_params = params

raise 'Stack file not defined' if $stdin.tty? && !params[:stack]

Expand Down
12 changes: 12 additions & 0 deletions lib/dry-stack/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def each_recursive(parent, each_ =-> { _1.respond_to?(:each) ? _1.each : [] }, p
end
end

attr_accessor :cmd_params
@cmd_params = {}

def Stack(name = nil, configuration = nil, &)
Stack.last_stack = Stack.new name
Stack.last_stack.instance_exec(&) if block_given?
Expand Down Expand Up @@ -303,6 +306,15 @@ def to_compose(opts = @options)
JSON.parse(compose.to_json).to_yaml
end

def Include(file_name)
dir = Dry.cmd_params[:stack] ? File.dirname(Dry.cmd_params[:stack]) : '.'
eval IO.read( File.join dir, file_name )
end

def After(&block)
(@after_blocks ||=[]) << block
end

def PublishPorts(ports)
@publish_ports.merge! ports.to_h { |k, v| [k,[v].flatten] }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Dry
class Stack
VERSION = '0.1.4'
VERSION = '0.1.5'
end
end
16 changes: 16 additions & 0 deletions spec/data/base.drs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
After do
Logging @services.keys, driver: 'loki', options: {
'loki-url': 'http://localhost:3100/loki/api/v1/push',
'loki-batch-size': 100, 'loki-retries': 2, 'loki-max-backoff': '1000ms', 'loki-timeout': '1s',
# 'loki-pipeline-stages': %{ [{regex: {expression: "(level|lvl|severity)=(?P<level>\\\\\\w+)"}}, {labels: {level: null}}]}
}

Environment @services.keys, {
OTEL_EXPORTER_OTLP_ENDPOINT_FRONT: '/otel',
OTEL_EXPORTER_OTLP_ENDPOINT: 'http://otel/',
OTEL_RESOURCE_ATTRIBUTES: 'deployment.environment=%{stack-name},service.name=%{service-name}',
OTEL_LOG_LEVEL: 'error',
OTEL_TRACES_EXPORTER:'otlp',
REPLICA: "{{.Task.Slot}}"
}
end
27 changes: 27 additions & 0 deletions spec/data/stack16-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
version: '3.8'
services:
admin:
environment:
OTEL_EXPORTER_OTLP_ENDPOINT_FRONT: "/otel"
OTEL_EXPORTER_OTLP_ENDPOINT: http://otel/
OTEL_RESOURCE_ATTRIBUTES: deployment.environment=stack_name,service.name=stack_name_admin
OTEL_LOG_LEVEL: error
OTEL_TRACES_EXPORTER: otlp
REPLICA: "{{.Task.Slot}}"
STACK_NAME: stack_name
STACK_SERVICE_NAME: admin
image: frontend
dns:
- 8.8.8.8
- 8.8.4.4
- 7.7.4.4
- 6.6.4.4
logging:
driver: loki
options:
loki-url: http://localhost:3100/loki/api/v1/push
loki-batch-size: 100
loki-retries: 2
loki-max-backoff: 1000ms
loki-timeout: 1s
12 changes: 12 additions & 0 deletions spec/data/stack16.drs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Description <<~DSC
Stack description
DSC

Options name: 'stack_name', traefik: true
Include 'base.drs'

Service :admin, image: 'frontend:latest', ports: 5000 do
dns '8.8.8.8'
dns '8.8.4.4'
dns ['7.7.4.4','6.6.4.4']
end
52 changes: 27 additions & 25 deletions spec/simple_stack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@
it 'converts to compose' do
ENV.delete_if { _1 =~/BUNDLE|DEBUG|IDE_PROCESS_DISPATCHER/ }

Dir[File.expand_path('data/*.drs', __dir__)].each do |stack_file|
drs = File.read(stack_file)
Dry::Stack() { eval drs }
puts "stack_file: #{stack_file}"

stack = Stack.last_stack
# CAN'T USE: The Options are aplyied only in CMD mode
# stack_compose = Stack.last_stack.to_compose

stack.instance_eval do ( [nil] + @configurations.keys )
end.each do |configuration|
cc = configuration ? "-c #{configuration}" : ''
stack_compose_shell = "---\n" + `bundle exec ./bin/dry-stack -s #{stack_file} #{cc} to_compose` # 2> /dev/null

sufix = configuration ? "-compose-#{configuration}.yml" : '-compose.yml'
# expect(stack_compose).to eq(stack_compose_shell)
compose = YAML.load_file stack_file.gsub('.drs', sufix), aliases: true rescue ''
# puts stack_compose_shell

# unless stack_compose_shell == compose.to_yaml
# yaml = YAML.load stack_compose_shell, aliases: true
# File.write stack_file.gsub('.drs', sufix), yaml.to_yaml
# compose = YAML.load_file stack_file.gsub('.drs', sufix), aliases: true
# end
expect(stack_compose_shell).to eq(compose.to_yaml)
Dir.chdir File.expand_path('data',__dir__) do
Dir['stack*.drs'].each do |stack_file|
drs = File.read(stack_file)
Dry::Stack() { eval drs }
puts "stack_file: #{stack_file}"

stack = Stack.last_stack
# CAN'T USE: The Options are applied only in CMD mode
# stack_compose = Stack.last_stack.to_compose

stack.instance_eval do ( [nil] + @configurations.keys )
end.each do |configuration|
cc = configuration ? "-c #{configuration}" : ''
stack_compose_shell = "---\n" + `bundle exec ../bin/dry-stack -s #{stack_file} #{cc} to_compose` # 2> /dev/null

sufix = configuration ? "-compose-#{configuration}.yml" : '-compose.yml'
# expect(stack_compose).to eq(stack_compose_shell)
compose = YAML.load_file stack_file.gsub('.drs', sufix), aliases: true rescue ''
# puts stack_compose_shell

# unless stack_compose_shell == compose.to_yaml
# yaml = YAML.load stack_compose_shell, aliases: true
# File.write stack_file.gsub('.drs', sufix), yaml.to_yaml
# compose = YAML.load_file stack_file.gsub('.drs', sufix), aliases: true
# end
expect(stack_compose_shell).to eq(compose.to_yaml)
end
end
end
end
Expand Down

0 comments on commit b82cc36

Please sign in to comment.