Class: Shippy::Service

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

Constant Summary collapse

ATTRIBUTES =
%i[
  build command depends_on devices entrypoint environment hostname image
  labels logging networks user ports restart volumes volumes_from working_dir
  security_opt
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, app:, &block) ⇒ Service

Returns a new instance of Service.



21
22
23
24
25
26
27
# File 'lib/shippy/service.rb', line 21

def initialize(name, app:, &block)
  @name = name
  @app = app
  @data = {}

  instance_eval(&block)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/shippy/service.rb', line 9

def app
  @app
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/shippy/service.rb', line 9

def name
  @name
end

Instance Method Details

#build_ihxator_labels(service:) ⇒ Object



99
100
101
102
103
104
# File 'lib/shippy/service.rb', line 99

def build_ihxator_labels(service:)
  [
    "ihxator.enable=true",
    "ihxator.service=#{service}"
  ]
end

#build_traefik_labels(name:, port: nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/shippy/service.rb', line 70

def build_traefik_labels(name:, port: nil)
  [
    "traefik.http.routers.websecure-#{name}.rule=Host(`#{name}.#{wildcard_domain}`)",
    "traefik.http.routers.websecure-#{name}.entrypoints=websecure",
    "traefik.http.routers.websecure-#{name}.tls=true",
    "traefik.http.routers.websecure-#{name}.tls.certresolver=ssl-resolver",
    "traefik.http.routers.websecure-#{name}.tls.domains[0].main=#{wildcard_domain}",
    "traefik.http.routers.websecure-#{name}.tls.domains[0].sans=*.#{wildcard_domain}",

    "traefik.http.routers.web-#{name}.rule=Host(`#{name}.#{wildcard_domain}`)",
    "traefik.http.routers.web-#{name}.entrypoints=web",
    "traefik.http.routers.web-#{name}.middlewares=secure-redirect-scheme@file",

    "traefik.http.routers.#{name}-local.rule=Host(`#{name}.#{local_domain}`)",
    "traefik.http.routers.#{name}-local.entrypoints=web",
    "traefik.http.middlewares.#{name}-local.redirectregex.regex=^http://#{name}.#{local_domain}",
    "traefik.http.middlewares.#{name}-local.redirectregex.replacement=https://#{name}.#{wildcard_domain}",
    "traefik.http.middlewares.#{name}-local.redirectregex.permanent=true",
    "traefik.http.routers.#{name}-local.middlewares=#{name}-local@docker"
  ].tap do |labels|
    if port
      labels << "traefik.http.services.#{name}.loadbalancer.server.port=#{port}"
      labels << "traefik.http.routers.websecure-#{name}.service=#{name}"
      labels << "traefik.http.routers.web-#{name}.service=#{name}"
      labels << "traefik.http.routers.#{name}-local.service=#{name}"
    end
  end
end

#hooksObject



113
114
115
116
117
118
119
# File 'lib/shippy/service.rb', line 113

def hooks
  if block_given?
    @hooks = normalize_hooks(yield)
  else
    @hooks.to_a.map { |data| [name, data[:options], data[:cmd]] }
  end
end

#secrets(name) ⇒ Object



106
107
108
# File 'lib/shippy/service.rb', line 106

def secrets(name)
  @app.secrets(name)
end

#to_hashObject



29
30
31
# File 'lib/shippy/service.rb', line 29

def to_hash
  {@name => @data}
end

#use_default_loggingObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shippy/service.rb', line 43

def use_default_logging
  logging do
    {
      driver: "json-file",
      options: {
        "max-size": "10m",
        "max-file": "5"
      }
    }
  end
end

#use_default_networksObject



55
56
57
# File 'lib/shippy/service.rb', line 55

def use_default_networks
  networks { ["lan_access"] }
end

#use_default_optionsObject



33
34
35
36
37
# File 'lib/shippy/service.rb', line 33

def use_default_options
  use_default_restart
  use_default_networks
  use_default_logging
end

#use_default_restartObject



39
40
41
# File 'lib/shippy/service.rb', line 39

def use_default_restart
  restart { "unless-stopped" }
end

#use_traefik(name:, alt: nil, port: nil, healthcheck: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/shippy/service.rb', line 59

def use_traefik(name:, alt: nil, port: nil, healthcheck: false)
  labels do
    all_labels = ["traefik.enable=true"]
    all_labels += build_traefik_labels(name: name, port: port)
    all_labels += build_traefik_labels(name: alt, port: port) if alt
    all_labels += build_ihxator_labels(service: "https://#{name}.#{wildcard_domain}") if healthcheck

    all_labels
  end
end