From b82cc366fdf180fe395b3492d24c437c5a06c829 Mon Sep 17 00:00:00 2001 From: "artem.borodkin@gmail.com" Date: Tue, 30 Jul 2024 01:25:34 +0300 Subject: [PATCH] update --- Gemfile.lock | 2 +- README.md | 2 +- lib/dry-stack/command_line.rb | 6 +++- lib/dry-stack/stack.rb | 12 ++++++++ lib/version.rb | 2 +- spec/data/base.drs | 16 +++++++++++ spec/data/stack16-compose.yml | 27 ++++++++++++++++++ spec/data/stack16.drs | 12 ++++++++ spec/simple_stack_spec.rb | 52 ++++++++++++++++++----------------- 9 files changed, 102 insertions(+), 29 deletions(-) create mode 100644 spec/data/base.drs create mode 100644 spec/data/stack16-compose.yml create mode 100644 spec/data/stack16.drs diff --git a/Gemfile.lock b/Gemfile.lock index 8269ae9..b97620d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - dry-stack (0.1.4) + dry-stack (0.1.5) GEM remote: https://rubygems.org/ diff --git a/README.md b/README.md index a3c40a7..e320296 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/dry-stack/command_line.rb b/lib/dry-stack/command_line.rb index 48ef20a..5c8b76d 100644 --- a/lib/dry-stack/command_line.rb +++ b/lib/dry-stack/command_line.rb @@ -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) @@ -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] diff --git a/lib/dry-stack/stack.rb b/lib/dry-stack/stack.rb index e8ac0b8..df5c033 100644 --- a/lib/dry-stack/stack.rb +++ b/lib/dry-stack/stack.rb @@ -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? @@ -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 diff --git a/lib/version.rb b/lib/version.rb index 99fd635..b3460ef 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -1,5 +1,5 @@ module Dry class Stack - VERSION = '0.1.4' + VERSION = '0.1.5' end end diff --git a/spec/data/base.drs b/spec/data/base.drs new file mode 100644 index 0000000..1c56fec --- /dev/null +++ b/spec/data/base.drs @@ -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\\\\\\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 \ No newline at end of file diff --git a/spec/data/stack16-compose.yml b/spec/data/stack16-compose.yml new file mode 100644 index 0000000..303126a --- /dev/null +++ b/spec/data/stack16-compose.yml @@ -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 diff --git a/spec/data/stack16.drs b/spec/data/stack16.drs new file mode 100644 index 0000000..41cc07a --- /dev/null +++ b/spec/data/stack16.drs @@ -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 diff --git a/spec/simple_stack_spec.rb b/spec/simple_stack_spec.rb index ccab904..a25bfef 100644 --- a/spec/simple_stack_spec.rb +++ b/spec/simple_stack_spec.rb @@ -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