Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
artyomb committed Sep 12, 2024
1 parent df6a278 commit 100b6be
Show file tree
Hide file tree
Showing 24 changed files with 111 additions and 65 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.16)
dry-stack (0.1.17)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 3 additions & 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.16
Version: 0.1.17
Usage:
dry-stack -s stackfile [options] COMMAND
cat stackfile | dry-stack COMMAND
Expand Down Expand Up @@ -102,6 +102,7 @@ services:
networks:
default:
ingress_routing:
init: true
image: frontend
ports:
- 5000:5000
Expand All @@ -116,6 +117,7 @@ services:
deploy:
labels:
- stack.product=product A
init: true
image: backend
volumes:
- database:/var/lib/postgresql/data
Expand Down
38 changes: 20 additions & 18 deletions lib/dry-stack/command_swarm_deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@ def run(stack, params, args, extra)
_params = stack.options.merge params
stack.name = _params[:name] if _params[:name]

if params[:context_endpoint]
name = params[:context_endpoint].gsub( /[\/.:@]/,'_').gsub( '__','_')
name = "dry-#{name}".to_sym
endpoint = params[:context_endpoint]
contexts = {}
exec_o_lines 'docker context ls --format json' do |line|
ctx = JSON.parse line, symbolize_names: true
contexts[ctx[:Name].to_sym] = ctx # {"Current":false,"Description":"","DockerEndpoint":"ssh://[email protected]","Error":"","Name":"prod-swarm"}
end

if contexts[name] && contexts[name][:DockerEndpoint] != endpoint
raise "context '#{name}' has different host value: #{contexts[name][:DockerEndpoint]} != #{endpoint}"
end

exec_i "docker context create #{name} --docker host=#{endpoint}" unless contexts[name]

ENV['DOCKER_CONTEXT'] = name.to_s
raise 'Context endpoint required' unless params[:context_endpoint]

name = params[:context_endpoint].gsub( /[\/.:@]/,'_').gsub( '__','_')
name = "dry-#{name}".to_sym
endpoint = params[:context_endpoint]
$stdout.puts "Looking for docker context '#{name}'"

contexts = {}
exec_o_lines 'docker context ls --format json' do |line|
ctx = JSON.parse line, symbolize_names: true
contexts[ctx[:Name].to_sym] = ctx # {"Current":false,"Description":"","DockerEndpoint":"ssh://[email protected]","Error":"","Name":"prod-swarm"}
end

if contexts[name] && contexts[name][:DockerEndpoint] != endpoint
raise "context '#{name}' has different host value: #{contexts[name][:DockerEndpoint]} != #{endpoint}"
end

exec_i "docker context create #{name} --docker host=#{endpoint}" unless contexts[name]

ENV['DOCKER_CONTEXT'] = name.to_s

# substitute ENV variables
yaml = stack.to_compose(_params).lines[1..].join
# system " echo \"#{yaml.gsub("`", '\\\`')}\" | docker stack deploy -c - #{stack.name} --prune --resolve-image changed"

# --prune --resolve-image changed
exec_i "docker stack deploy -c - #{stack.name} #{extra}", yaml
exec_i "docker stack deploy -c - --with-registry-auth #{extra}" #{stack.name} , yaml
system "docker config rm $(docker config ls --filter label=com.docker.stack.namespace=#{stack.name} --format \"{{.ID}}\")"

exec_i "docker config rm #{stack.name}_readme || echo 'failed to remove config #{stack.name}_readme'"
Expand Down
7 changes: 6 additions & 1 deletion lib/dry-stack/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

module Dry
EMPTY_HASH = :empty_hash
DEFAULT_INIT_SERVICE = true # add "init: true" option

class ::Hash
def deep_merge!(second)
Expand Down Expand Up @@ -381,7 +382,11 @@ def _ServiceImplementation(name, opts = {}, &)
opts[:ports] = [opts[:ports]].flatten if opts.key? :ports
opts[:environment] = opts.delete(:env) if opts.key? :env

service = @services[name.to_sym] ||= {environment: {}, deploy: {labels: []}, networks: {}}
service = @services[name.to_sym] ||= begin
s = {environment: {}, deploy: {labels: []}, networks: {}}
DEFAULT_INIT_SERVICE ? s.merge(init: true) : s
end

service.deep_merge! opts
ServiceFunction.new(service, &) if block_given?
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.16'
VERSION = '0.1.17'
end
end
17 changes: 11 additions & 6 deletions spec/data/stack1-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
- traefik.http.middlewares.simple_stack_admin_auth.basicauth.users=admin:$apr1$i7hdbc9g$Rkocxo9snhmuESvUg0TTv/
- traefik.http.routers.simple_stack_admin.middlewares=simple_stack_admin_auth
- stack.product=product A
init: true
image: frontend
ports:
- 4000:5000
Expand All @@ -32,8 +33,9 @@ services:
- traefik.http.services.simple_stack_operator-0.loadbalancer.server.port=5000
- traefik.http.routers.simple_stack_operator-0.rule=HostRegexp(`{name:operator\..*}`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: frontend
ports:
- 4001:5000
Expand All @@ -51,8 +53,9 @@ services:
- traefik.http.services.simple_stack_navigator-0.loadbalancer.server.port=5000
- traefik.http.routers.simple_stack_navigator-0.rule=HostRegexp(`{name:navigator\..*}`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: frontend
ports:
- 4002:5000
Expand Down Expand Up @@ -83,8 +86,9 @@ services:
cpus: 4
memory: 500M
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: backend
user: root
logging:
Expand All @@ -103,6 +107,7 @@ services:
deploy:
labels:
- stack.product=product A
init: true
image: reports:0.1
ports:
- 7000:7000
Expand Down
1 change: 1 addition & 0 deletions spec/data/stack10-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
PGDATA: "/var/lib/postgresql/data/pgdata"
STACK_NAME: stack
STACK_SERVICE_NAME: db
init: true
image: postgres:13.5
volumes:
- database:/var/lib/postgresql/data
Expand Down
1 change: 1 addition & 0 deletions spec/data/stack11-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ services:
environment:
STACK_NAME: stack
STACK_SERVICE_NAME: hidden
init: true
image: dummy/hidden
6 changes: 4 additions & 2 deletions spec/data/stack12-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ services:
cpus: '4'
memory: 500M
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: frontend
ports:
- 5000:5000
Expand Down Expand Up @@ -59,6 +60,7 @@ services:
deploy:
labels:
- stack.product=product A
init: true
image: backend
volumes:
- database:/var/lib/postgresql/data
Expand Down
5 changes: 3 additions & 2 deletions spec/data/stack13-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ services:
- traefik.http.routers.stack_name_dev_admin-1.rule=HostRegexp(`{name:admin2\..*}`)
&& PathPrefix(`/api`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: frontend
networks:
ingress_routing:
Expand Down
5 changes: 3 additions & 2 deletions spec/data/stack13-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ services:
- traefik.http.routers.stack_name_admin-2.tls.domains[0].main=prod_admin.swarm_x.com
- traefik.http.routers.stack_name_admin-2.rule=HostRegexp(`{name:prod_admin\..*}`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: frontend
networks:
ingress_routing:
Expand Down
5 changes: 3 additions & 2 deletions spec/data/stack13-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ services:
- traefik.http.routers.stack_name_admin-1.rule=HostRegexp(`{name:admin2\..*}`)
&& PathPrefix(`/api`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: frontend
networks:
ingress_routing:
Expand Down
4 changes: 4 additions & 0 deletions spec/data/stack14-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
limits:
cpus: '4'
memory: 500M
init: true
image: "$REGISTRY_HOST/frontend"
logging:
driver: loki
Expand All @@ -24,6 +25,7 @@ services:
environment:
STACK_NAME: stack
STACK_SERVICE_NAME: test
init: true
image: "$REGISTRY_HOST/test"
logging:
driver: loki
Expand All @@ -36,6 +38,7 @@ services:
environment:
STACK_NAME: stack
STACK_SERVICE_NAME: test2
init: true
image: "$REGISTRY_HOST/test"
logging:
driver: loki
Expand All @@ -48,6 +51,7 @@ services:
environment:
STACK_NAME: stack
STACK_SERVICE_NAME: test3
init: true
image: "$REGISTRY_HOST/test"
logging:
driver: loki
Expand Down
1 change: 1 addition & 0 deletions spec/data/stack15-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
APP: admin
STACK_NAME: stack_name
STACK_SERVICE_NAME: admin
init: true
image: frontend
dns:
- 8.8.8.8
Expand Down
12 changes: 7 additions & 5 deletions spec/data/stack16-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ services:
STACK_NAME: stack_name
STACK_SERVICE_NAME: front
networks:
grafana_network:
default:
grafana_network:
default:
init: true
image: frontend
logging:
driver: loki
Expand Down Expand Up @@ -48,8 +49,9 @@ services:
test:
aliases:
- admin2
grafana_network:
default:
grafana_network:
default:
init: true
image: frontend
dns:
- 8.8.8.8
Expand All @@ -65,7 +67,7 @@ services:
loki-max-backoff: 1000ms
loki-timeout: 1s
networks:
hello.world:
hello.world:
grafana_network:
external: true
name: grafana-network
5 changes: 3 additions & 2 deletions spec/data/stack17-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ services:
- traefik.http.services.stack_name_admin-0.loadbalancer.server.port=5000
- traefik.http.routers.stack_name_admin-0.rule=PathPrefix(`frontend\..*`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: admin
networks:
ingress_routing:
Expand Down
30 changes: 18 additions & 12 deletions spec/data/stack2-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ services:
constraints:
- node.role == manager
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: service-image
logging:
driver: fluentd
Expand All @@ -44,8 +45,9 @@ services:
constraints:
- node.role == manager
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: postgres:13.5-alpine
command: bash -c "apk add curl && docker-entrypoint.sh -c \"shared_buffers=256MB\"
-c \"max_connections=200\" "
Expand All @@ -69,8 +71,9 @@ services:
- traefik.http.services.name1_svc3-1.loadbalancer.server.port=4000
- traefik.http.routers.name1_svc3-1.rule=HostRegexp(`{name:svc3-1\..*}`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: service-image
svc4:
environment:
Expand All @@ -86,8 +89,9 @@ services:
- traefik.http.services.name1_svc4-1.loadbalancer.server.port=4000
- traefik.http.routers.name1_svc4-1.rule=HostRegexp(`{name:svc4-1\..*}`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: service-image
svc5:
environment:
Expand All @@ -103,8 +107,9 @@ services:
- traefik.http.services.name1_svc5-1.loadbalancer.server.port=4000
- traefik.http.routers.name1_svc5-1.rule=HostRegexp(`{name:svc5-1\..*}`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: postgres:13.5-alpine
svc6:
environment:
Expand All @@ -120,8 +125,9 @@ services:
- traefik.http.services.name1_svc6-1.loadbalancer.server.port=4000
- traefik.http.routers.name1_svc6-1.rule=HostRegexp(`{name:svc6-1\..*}`)
networks:
default:
ingress_routing:
default:
ingress_routing:
init: true
image: postgres:13.5-alpine
networks:
ingress_routing:
Expand Down
1 change: 1 addition & 0 deletions spec/data/stack3-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ services:
DB_URL: "$DB_URL"
STACK_NAME: stack
STACK_SERVICE_NAME: reports
init: true
image: reports
Loading

0 comments on commit 100b6be

Please sign in to comment.