Class: Synapse::Easy::Service

Inherits:
Object
  • Object
show all
Includes:
Fake::Hash
Defined in:
lib/synapse/easy/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Service

Returns a new instance of Service.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/synapse/easy/service.rb', line 15

def initialize params={}
  @default_servers ||= []
  @discovery       ||= {}
  @haproxy         ||= {}
  @port              = params[:port]
  @port            ||= Freeport.port
  @hosts             = params[:hosts]
  @hosts           ||= Resolv::Consul.services(:zookeeper).collect{|x|"#{x.address}:#{x.port}"}
  @mode              = params[:mode]
  @mode            ||= :tcp
  @uri               = params[:uri]
  @uri             ||= "/"
  @inter             = params[:inter]
  @inter           ||= 2000
  @rise              = params[:rise]
  @rise            ||= 2
  @fall              = params[:fall]
  @fall            ||= 3
  @method          ||= "zookeeper"
  @registry          = params[:registry]
  @registry        ||= "nerve"
  @customer          = params[:customer]
  @customer        ||= "global"
  @application       = params[:application]
  @application     ||= "generic"
  @function          = params[:function]
  @function        ||= "common"
  @extension         = params[:extension]
  @extension       ||= "services"
  @path              = params[:path]
  @path            ||= Pathname.new("/#{@registry}").join(@customer.to_s,@application.to_s,@function.to_s,@extension.to_s).to_s
  @name              = params[:name]
  @name            ||= [@customer,@application,@function].join("_")
  @active          ||= false
  @enable_frontend   = params[:enable_frontend]
  @enable_frontend ||= true if @enable_frontend.nil?
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def active
  @active
end

#applicationObject

Returns the value of attribute application.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def application
  @application
end

#customerObject

Returns the value of attribute customer.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def customer
  @customer
end

#default_serversObject (readonly)

Returns the value of attribute default_servers.



14
15
16
# File 'lib/synapse/easy/service.rb', line 14

def default_servers
  @default_servers
end

#enable_backendObject

Returns the value of attribute enable_backend.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def enable_backend
  @enable_backend
end

#enable_frontendObject

Returns the value of attribute enable_frontend.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def enable_frontend
  @enable_frontend
end

#extensionObject

Returns the value of attribute extension.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def extension
  @extension
end

#fallObject

Returns the value of attribute fall.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def fall
  @fall
end

#functionObject

Returns the value of attribute function.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def function
  @function
end

#hostsObject

Returns the value of attribute hosts.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def hosts
  @hosts
end

#interObject

Returns the value of attribute inter.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def inter
  @inter
end

#keep_default_serversObject

Returns the value of attribute keep_default_servers.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def keep_default_servers
  @keep_default_servers
end

#leader_electionObject

Returns the value of attribute leader_election.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def leader_election
  @leader_election
end

#methodObject

Returns the value of attribute method.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def method
  @method
end

#modeObject

Returns the value of attribute mode.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def name
  @name
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def path
  @path
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def port
  @port
end

#registryObject

Returns the value of attribute registry.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def registry
  @registry
end

#riseObject

Returns the value of attribute rise.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def rise
  @rise
end

#uriObject

Returns the value of attribute uri.



10
11
12
# File 'lib/synapse/easy/service.rb', line 10

def uri
  @uri
end

Instance Method Details

#add_default_server(name, port) ⇒ Object



52
53
54
# File 'lib/synapse/easy/service.rb', line 52

def add_default_server name, port
  @default_servers << "#{name}:#{port}"
end

#addressObject



104
105
106
# File 'lib/synapse/easy/service.rb', line 104

def address
  Resolv::DNS.new.getaddress(host.to_s).to_s
end

#aliasesObject



67
68
69
70
71
72
73
# File 'lib/synapse/easy/service.rb', line 67

def aliases
  [
    @application,
    [@application,@function].join("_"),
    [@customer,@application].join("_"),
  ]
end

#discover_via(discovery_data) ⇒ Object



58
59
60
# File 'lib/synapse/easy/service.rb', line 58

def discover_via discovery_data
  @discovery = discovery_data
end

#discoveryObject



74
75
76
77
78
79
80
# File 'lib/synapse/easy/service.rb', line 74

def discovery
  {
    method: method,
    path:   path,
    hosts:  hosts,
  }.with_indifferent_access
end

#haproxyObject



81
82
83
84
85
86
87
# File 'lib/synapse/easy/service.rb', line 81

def haproxy
  result = {}.with_indifferent_access
  result[:port] = port if enable_frontend
  result[:listen] = listen
  result[:server_options] = server_options
  result
end

#hostObject



64
65
66
# File 'lib/synapse/easy/service.rb', line 64

def host
  "localhost"
end

#listenObject



91
92
93
94
95
96
# File 'lib/synapse/easy/service.rb', line 91

def listen
  [
      "mode #{mode}",
    ( "option httpchk GET #{uri}" if uri and mode.to_s == "http" )
  ].compact
end

#provide_at(haproxy_data) ⇒ Object



61
62
63
# File 'lib/synapse/easy/service.rb', line 61

def provide_at haproxy_data
  @haproxy = haproxy_data
end

#remove_default_server(name, port) ⇒ Object



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

def remove_default_server name, port
  @default_servers.delete "#{name}:#{port}"
end

#server_optionsObject



88
89
90
# File 'lib/synapse/easy/service.rb', line 88

def server_options
  "check inter #{inter} rise #{rise} fall #{fall}" if [inter,rise,fall].compact.size == 3
end

#to_s(format = "%P://%h:%p") ⇒ Object Also known as: s



107
108
109
110
111
112
# File 'lib/synapse/easy/service.rb', line 107

def to_s format="%P://%h:%p"
  format = format.gsub('%P',mode.to_s)
  format = format.gsub('%h',host.to_s)
  format = format.gsub('%i',address.to_s)
  format.gsub('%p',port.to_s)
end

#to_synapseObject



97
98
99
100
101
102
103
# File 'lib/synapse/easy/service.rb', line 97

def to_synapse
  return application => {
    default_servers:default_servers,
    discovery: discovery,
    haproxy:   haproxy
  }
end

#to_uriObject



119
120
121
# File 'lib/synapse/easy/service.rb', line 119

def to_uri
  URI to_s()
end

#to_urlObject Also known as: url



114
115
116
117
# File 'lib/synapse/easy/service.rb', line 114

def to_url
  return unless mode.to_sym == :http
  to_s() 
end