synapse-easy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b59812fd52fcc24ea66fa92c46e7d84e4b15ef1
4
+ data.tar.gz: aadeaf32376705d6a28f22fd5a134e8abb7f719c
5
+ SHA512:
6
+ metadata.gz: 9fb7ea5e1772937c451df3d0611bc04389c12c98c79e446ad4816259c97834fd6e29a86a8ac4ca41d32a0e6f46525adceb9aff88188d44b85cd2d9838eae6cb4
7
+ data.tar.gz: 0df3758781d78ef3d57f316f675fcf848da535189fc9b79159357115a790f3d48b22ad6dcee6e96e2862776fb39c315a8c3579dd353eef55dfefa2d34746ade9
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in synapse-easy.gemspec
4
+ gemspec
@@ -0,0 +1,39 @@
1
+ # Synapse::Easy
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/synapse/easy`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'synapse-easy'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install synapse-easy
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/synapse-easy/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "synapse/easy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ require "synapse/easy/version"
2
+ require "synapse/easy/service"
3
+ require "synapse/easy/haproxy"
@@ -0,0 +1,18 @@
1
+ module Synapse
2
+ module Easy
3
+ module FakeHash
4
+ def [] key
5
+ self.send key.to_sym if self.respond_to? key.to_sym
6
+ end
7
+ def []= key, value
8
+ self.send :"#{key}=", value if self.respond_to? :"#{key}="
9
+ end
10
+ def include? key
11
+ self.respond_to? key.to_sym
12
+ end
13
+ def has_key? key
14
+ self.respond_to? key.to_sym
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,119 @@
1
+ require "fileutils"
2
+ require "resolv/consul"
3
+ require "pathname"
4
+ require "synapse/easy/fake_hash"
5
+ require "active_support/core_ext/hash/indifferent_access"
6
+
7
+ module Synapse
8
+ module Easy
9
+ class Haproxy
10
+ include FakeHash
11
+ class << self
12
+ def instance_id size=3
13
+ @instance_id ||= SecureRandom.hex(size)
14
+ end
15
+ end
16
+ attr_accessor :reload_command, :do_writes, :do_reloads, :do_socket,
17
+ :frontends, :socket_file_path, :config_file_path,
18
+ :pid_file_path, :global_maxconn, :default_maxconn, :nbproc,
19
+ :default_log, :global_log_server, :global_log_level,
20
+ :default_retries, :default_log,:default_options,
21
+ :default_balance, :default_timeouts
22
+ attr_reader :defaults, :global, :workdir
23
+ def initialize params={}
24
+ @do_writes ||= true
25
+ @do_reloads ||= true
26
+ @do_socket ||= true
27
+
28
+ @reload_command ||= "haproxy -f '#{config_file_path}' -p '#{pid_file_path}' -st $(cat '#{pid_file_path}' 2>/dev/null|| true)"
29
+
30
+ @global ||= []
31
+ @defaults ||= []
32
+ @extra_sections ||= {}
33
+
34
+ @nbproc ||= 2
35
+ @global_maxconn ||= 4096
36
+ @global_log_server = params[:global_log_server]
37
+ @global_log_server ||= Resolv::Consul.services(:syslog).sample(1).map{|x| "#{x.address}:#{x.port}"}.first
38
+ @global_log_server ||= "127.0.0.1:514"
39
+ @global_log_level ||= "local0"
40
+
41
+ @default_log ||= "global"
42
+ @default_retries ||= 3
43
+ @default_maxconn ||= 2048
44
+ @default_options ||= [
45
+ "option dontlognull",
46
+ "option redispatch",
47
+ ]
48
+ @default_balance ||= "roundrobin"
49
+ @default_timeouts ||= {
50
+ "connect" => 5,
51
+ "client" => 60,
52
+ "server" => 60,
53
+ }
54
+ end
55
+ def instance_id
56
+ self.class.instance_id
57
+ end
58
+ def workdir= workdir
59
+ @workdir = workdir if workdir.is_a? Pathname
60
+ @workdir = Pathname.new(workdir) if workdir.is_a? String
61
+ end
62
+ def workdir
63
+ @workdir ||= Pathname.new("/tmp")
64
+ end
65
+ def config_file_name
66
+ "#{instance_id}.cfg"
67
+ end
68
+ def socket_file_name
69
+ "#{instance_id}.sock"
70
+ end
71
+ def pid_file_name
72
+ "#{instance_id}.pid"
73
+ end
74
+ def config_file_path
75
+ workdir.join(config_file_name).to_s
76
+ end
77
+ def socket_file_path
78
+ workdir.join(socket_file_name).to_s
79
+ end
80
+ def pid_file_path
81
+ workdir.join(pid_file_name).to_s
82
+ end
83
+ def global
84
+ [
85
+ "daemon",
86
+ "log #{global_log_server} #{global_log_level}",
87
+ "maxconn #{global_maxconn}",
88
+ "stats socket #{socket_file_path} mode 600 level admin"
89
+ ]
90
+ end
91
+ def defaults
92
+ defaults = @default_options
93
+ defaults << "log #{default_log}"
94
+ defaults << "maxconn #{default_maxconn}"
95
+ defaults << "retries #{default_retries}"
96
+ defaults << "balance #{default_balance}"
97
+ defaults += @default_timeouts.collect{|l,t| "timeout #{l} #{t}"}
98
+ defaults
99
+ end
100
+ def extra_sections
101
+ @extra_sections
102
+ end
103
+ def to_synapse
104
+ FileUtils.mkdir_p workdir.to_s unless Dir.exists? workdir.to_s
105
+ {
106
+ reload_command: reload_command,
107
+ do_writes: do_writes,
108
+ do_reloads: do_reloads,
109
+ do_socket: do_socket,
110
+ config_file_path: config_file_path,
111
+ socket_file_path: socket_file_path,
112
+ global: global,
113
+ defaults: defaults,
114
+ extra_sections: extra_sections
115
+ }.with_indifferent_access
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,106 @@
1
+ require "freeport"
2
+ require "resolv/consul"
3
+ require "synapse/easy/fake_hash"
4
+ require "active_support/core_ext/hash/indifferent_access"
5
+
6
+ module Synapse
7
+ module Easy
8
+ class Service
9
+ include FakeHash
10
+ attr_accessor :port, :mode, :uri, :inter, :rise, :fall, :method, :path,
11
+ :hosts, :registry, :customer, :application, :function,
12
+ :extension, :name, :leader_election, :keep_default_servers,
13
+ :active
14
+ attr_reader :default_servers
15
+ def initialize params={}
16
+ @default_servers ||= []
17
+ @discovery ||= {}
18
+ @haproxy ||= {}
19
+ @port ||= Freeport.port
20
+ @hosts ||= Resolv::Consul.services(:zookeeper).collect{|x|"#{x.address}:#{x.port}"}
21
+ @mode ||= :tcp
22
+ @uri ||= "/"
23
+ @inter ||= 2000
24
+ @rise ||= 2
25
+ @fall ||= 3
26
+ @method ||= "zookeeper"
27
+ @registry = params[:registry]
28
+ @registry ||= "nerve"
29
+ @customer = params[:customer]
30
+ @customer ||= "global"
31
+ @application = params[:application]
32
+ @application ||= "generic"
33
+ @function = params[:function]
34
+ @function ||= "common"
35
+ @extension = params[:extension]
36
+ @extension ||= "services"
37
+ @path = params[:path]
38
+ @path ||= Pathname.new("/#{@registry}").join(@customer.to_s,@application.to_s,@function.to_s,@extension.to_s).to_s
39
+ @name = params[:name]
40
+ @name ||= [@customer,@application,@function].join("_")
41
+ @active ||= false
42
+ end
43
+ def add_default_server name, port
44
+ @default_servers << "#{name}:#{port}"
45
+ end
46
+ def remove_default_server name, port
47
+ @default_servers.delete "#{name}:#{port}"
48
+ end
49
+ def discover_via discovery_data
50
+ @discovery = discovery_data
51
+ end
52
+ def provide_at haproxy_data
53
+ @haproxy = haproxy_data
54
+ end
55
+ def host
56
+ "localhost"
57
+ end
58
+ def aliases
59
+ [
60
+ @application,
61
+ [@application,@function].join("_"),
62
+ [@customer,@application].join("_"),
63
+ ]
64
+ end
65
+ def discovery
66
+ {
67
+ method: method,
68
+ path: path,
69
+ hosts: hosts,
70
+ }.with_indifferent_access
71
+ end
72
+ def haproxy
73
+ {
74
+ port: port,
75
+ listen: listen,
76
+ server_options: server_options,
77
+ }.with_indifferent_access
78
+ end
79
+ def server_options
80
+ "check inter #{inter} rise #{rise} fall #{fall}" if [inter,rise,fall].compact.size == 3
81
+ end
82
+ def listen
83
+ [
84
+ "mode #{mode}",
85
+ ( "option httpchk #{uri}" if uri and mode.to_s == "http" )
86
+ ].compact
87
+ end
88
+ def to_synapse
89
+ return application => {
90
+ default_servers:default_servers,
91
+ discovery: discovery,
92
+ haproxy: haproxy
93
+ }
94
+ end
95
+ def address
96
+ Resolv::DNS.new.getaddress(host.to_s).to_s
97
+ end
98
+ def to_s format="%P://%h:%p"
99
+ format = format.gsub('%P',mode.to_s)
100
+ format = format.gsub('%h',host.to_s)
101
+ format = format.gsub('%i',address.to_s)
102
+ format.gsub('%p',port.to_s)
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,5 @@
1
+ module Synapse
2
+ module Easy
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'synapse/easy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "synapse-easy"
8
+ spec.version = Synapse::Easy::VERSION
9
+ spec.authors = ["Mathias Kaufmann"]
10
+ spec.email = ["me@stei.gr"]
11
+
12
+ spec.summary = %q{Synapse Config Generator for HAProxy and Services.}
13
+ spec.description = %q{Synapse Config Generator for HAProxy and Services.}
14
+ spec.homepage = "http://synapse.stei.gr/easy"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ # if spec.respond_to?(:metadata)
22
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
23
+ # end
24
+
25
+ spec.add_dependency "synapse-config", "~> 0.1"
26
+ spec.add_dependency "resolv-consul", "~> 0.1"
27
+ spec.add_dependency "freeport"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.8"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: synapse-easy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mathias Kaufmann
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: synapse-config
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: resolv-consul
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: freeport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: Synapse Config Generator for HAProxy and Services.
84
+ email:
85
+ - me@stei.gr
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - lib/synapse/easy.rb
98
+ - lib/synapse/easy/fake_hash.rb
99
+ - lib/synapse/easy/haproxy.rb
100
+ - lib/synapse/easy/service.rb
101
+ - lib/synapse/easy/version.rb
102
+ - synapse-easy.gemspec
103
+ homepage: http://synapse.stei.gr/easy
104
+ licenses: []
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.6
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Synapse Config Generator for HAProxy and Services.
126
+ test_files: []