Class: Synapse::Easy::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FakeHash

#[], #[]=, #has_key?, #include?

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
# File 'lib/synapse/easy/service.rb', line 15

def initialize params={}
  @default_servers ||= []
  @discovery       ||= {}
  @haproxy         ||= {}
  @port            ||= Freeport.port
  @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
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

#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



48
49
50
# File 'lib/synapse/easy/service.rb', line 48

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

#addressObject



100
101
102
# File 'lib/synapse/easy/service.rb', line 100

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

#aliasesObject



63
64
65
66
67
68
69
# File 'lib/synapse/easy/service.rb', line 63

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

#discover_via(discovery_data) ⇒ Object



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

def discover_via discovery_data
  @discovery = discovery_data
end

#discoveryObject



70
71
72
73
74
75
76
# File 'lib/synapse/easy/service.rb', line 70

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

#haproxyObject



77
78
79
80
81
82
83
# File 'lib/synapse/easy/service.rb', line 77

def haproxy
  {
    port: port,
    listen: listen,
    server_options: server_options,
  }.with_indifferent_access
end

#hostObject



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

def host
  "localhost"
end

#listenObject



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

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

#provide_at(haproxy_data) ⇒ Object



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

def provide_at haproxy_data
  @haproxy = haproxy_data
end

#remove_default_server(name, port) ⇒ Object



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

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

#server_optionsObject



84
85
86
# File 'lib/synapse/easy/service.rb', line 84

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



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

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



93
94
95
96
97
98
99
# File 'lib/synapse/easy/service.rb', line 93

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