varnish_rest_api_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed0ee33825fdec916966a89c700bcf9ecccb2533
4
+ data.tar.gz: 27d74cd3617152e15b58c16ead03990c17e9db95
5
+ SHA512:
6
+ metadata.gz: 6fffa5dacb8acd3b6a49c6b75d9788d1ab1922358f87839731a1989b7cec15a08b893f463650a7607d5981d7a5212616662fd14b48bdb224221914f7f441a9ca
7
+ data.tar.gz: 8d0e8daa03e2174b79aa501285cb7d302e01cb0c4844992eb80f7e73c6e2e376c99c816881fc983ef7d3684f7b94ad11acd9298af485a397e65db687af395a0d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.project
11
+ /.buildpath
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in varnish_rest_api_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jonathan Colby
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # VarnishRestApiClient
2
+
3
+ The varnish rest api client is a command-line client program to call the varnish_rest_api.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'varnish_rest_api_client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install varnish_rest_api_client
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ varnishapiclient.rb
25
+
26
+ Commands:
27
+ varnishapiclient.rb help [COMMAND] # Describe available commands or one specific command
28
+ varnishapiclient.rb in BACKEND # set health of this varnish BACKEND to auto
29
+ varnishapiclient.rb list PATTERN # display all varnish backends
30
+ varnishapiclient.rb out BACKEND # set health of this varnish BACKEND to sick.
31
+ varnishapiclient.rb show # show varnish nodes registered with zookeeper
32
+
33
+ Options:
34
+ V, [--varnish=varnish1:10001 varnish2:10001] # varnish nodes(s)
35
+ z, [--zkserver=ZKSERVER] # zookeeper server:port
36
+ # Default: autodeploy38-2:2181
37
+ P, [--zkpath=ZKPATH] # zookeeper varnish root path
38
+ # Default: /varnish
39
+ ```
40
+
41
+ ## Development
42
+
43
+ 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.
44
+
45
+ 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).
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/[my-github-username]/varnish_rest_api_client/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "varnish_rest_api_client"
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
data/bin/setup ADDED
@@ -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 "varnish_rest_api_client/client"
2
+
3
+ VarnishRestApiClient::Client.start(ARGV)
@@ -0,0 +1,133 @@
1
+ require "thor"
2
+ require "json"
3
+ require 'open-uri'
4
+ require 'zk'
5
+ require_relative "version"
6
+
7
+ module VarnishRestApiClient
8
+
9
+ class Client < Thor
10
+ class_option :varnish, :aliases => "V", :desc => "varnish server(s)", :type => :array
11
+ class_option :zkserver, :aliases => "z", :desc => "zookeeper server:port", :default => "autodeploy38-2:2181"
12
+ class_option :zkpath, :aliases => "P", :desc => "zookeeper varnish root path", :default => "/varnish"
13
+
14
+ # alternate use invoke
15
+ def initialize(*args)
16
+ super
17
+ @nodes = Array.new
18
+ @use_zookeeper = use_zookeeper
19
+ if @use_zookeeper
20
+ @nodes = get_zk_nodes
21
+ else
22
+ @nodes = options[:varnish]
23
+ end
24
+ end
25
+
26
+ desc "out BACKEND", "set health of this varnish BACKEND to sick."
27
+ def out(backend)
28
+ @nodes.each do |api|
29
+ p = call_rest(api)
30
+ puts p.call("#{backend}/out")
31
+ end
32
+ end
33
+
34
+ desc "in BACKEND", "set health of this varnish BACKEND to auto"
35
+ def in(backend)
36
+ @nodes.each do |api|
37
+ p = call_rest(api)
38
+ puts p.call("#{backend}/in")
39
+ end
40
+ end
41
+
42
+ desc "show", "show varnish hosts registered with zookeeper"
43
+ def show
44
+ puts @nodes.join("\n")
45
+ end
46
+
47
+ desc "list PATTERN", "display all varnish backends"
48
+ def list(pattern=nil)
49
+ backends_found = Array.new
50
+ @nodes.each do |api|
51
+ uri = pattern ? "list/#{pattern}" : "list"
52
+ p = call_rest(api)
53
+ result = p.call(uri)
54
+ next if result.empty?
55
+ if result.first.class != Hash
56
+ puts "error from #{api}: #{result}"
57
+ end
58
+
59
+ backends_found << result
60
+ end
61
+ puts backends_found.empty? ? "no backends found for pattern #{pattern}" : backends_found
62
+ end
63
+
64
+ no_commands do
65
+
66
+ def use_zookeeper
67
+ if options[:varnish].nil? || options[:varnish].empty?
68
+ return true
69
+ end
70
+ return false
71
+ end
72
+
73
+ def get_zk_nodes
74
+ begin
75
+ @zk = ZK.new(options[:zkserver])
76
+ rescue ArgumentError, RuntimeError => e
77
+ $stderr.puts "\n\tcould not connect to zookeeper server: #{options[:zkserver]}\n\n"
78
+ help
79
+ exit(1)
80
+ end
81
+
82
+ begin
83
+ nodes = @zk.children(options[:zkpath], :watch => false)
84
+ rescue ZK::Exceptions::NoNode => e
85
+ abort "no nodes found in path #{options[:zkpath]} on zookeeper server #{options[:zkserver]}"
86
+ end
87
+ return nodes.collect do |node|
88
+ data, stat = @zk.get("/varnish/#{node}", :watch => false)
89
+ data.chomp
90
+ end
91
+ end
92
+
93
+ def call_rest(node)
94
+ return Proc.new do |action|
95
+ begin
96
+ buffer = open("http://#{node}/#{action}").read
97
+ result = JSON.parse(buffer)
98
+ result.collect! { |e| e["varnishhost"] = node ; e }
99
+ rescue SocketError => e
100
+ abort "problem connecting rest api at #{node}: #{e.message}"
101
+ rescue OpenURI::HTTPError => e
102
+ abort "problem calling rest api at #{node}: #{e.message}"
103
+ end
104
+ end
105
+ end
106
+
107
+ def call_restb(url,node_name)
108
+ begin
109
+ buffer = open(url).read
110
+ result = JSON.parse(buffer)
111
+ result.collect! { |e| e["varnishhost"] = node_name ; e }
112
+ rescue SocketError => e
113
+ abort "problem connecting rest api at #{url}: #{e.message}"
114
+ rescue OpenURI::HTTPError => e
115
+ abort "problem calling rest api at #{url}: #{e.message}"
116
+ end
117
+ if block_given?
118
+ yield result
119
+ else
120
+ result
121
+ end
122
+ end
123
+
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+
130
+ # https://gist.github.com/kyletcarlson/7911188
131
+ # http://blog.paracode.com/2012/05/17/building-your-tools-with-thor/
132
+ # print_table ! ask !
133
+ # http://www.rubydoc.info/gems/user_config/0.0.4/frames
@@ -0,0 +1,3 @@
1
+ module VarnishRestApiClient
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "varnish_rest_api_client/version"
2
+ require "varnish_rest_api_client/client"
3
+
4
+ module VarnishRestApiClient
5
+
6
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'varnish_rest_api_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "varnish_rest_api_client"
8
+ spec.version = VarnishRestApiClient::VERSION
9
+ spec.authors = ["Jonathan Colby"]
10
+ spec.email = ["jcolby@team.mobile.de"]
11
+
12
+ spec.summary = %q{A command line client for the varnish rest api.}
13
+ spec.description = %q{A command line client for the varnish rest api.}
14
+ spec.homepage = "http://homepage.com"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.files << ["bin/varnishapiclient.rb"]
19
+ spec.bindir = "bin"
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ if spec.respond_to?(:metadata)
24
+ spec.metadata['allowed_push_host']
25
+ end
26
+
27
+ spec.add_dependency "thor"
28
+ spec.add_dependency "json"
29
+ spec.add_dependency "zk"
30
+ spec.add_dependency "zookeeper"
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.9"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: varnish_rest_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Colby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: zk
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: zookeeper
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: A command line client for the varnish rest api.
98
+ email:
99
+ - jcolby@team.mobile.de
100
+ executables:
101
+ - console
102
+ - setup
103
+ - varnishapiclient.rb
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".gitignore"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - bin/varnishapiclient.rb
117
+ - lib/varnish_rest_api_client.rb
118
+ - lib/varnish_rest_api_client/client.rb
119
+ - lib/varnish_rest_api_client/version.rb
120
+ - varnish_rest_api_client.gemspec
121
+ homepage: http://homepage.com
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.5
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A command line client for the varnish rest api.
145
+ test_files: []
146
+ has_rdoc: