ruboty-fastly 0.1.0

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
+ SHA256:
3
+ metadata.gz: 51bf2c3bc9d454f3f1f04c0f813484baad6b5c7e92bec8e55a0e75f25bf64cb2
4
+ data.tar.gz: 249839627c4bd7ad63c81ea6ca337e7849d0fe5aa64694a8e5769b80235797cb
5
+ SHA512:
6
+ metadata.gz: 9864c59d53ea2d10eeb727434edd9cebb3460207445d88ec36f4d46d20968cac1b8f6e6a32c6dab2f0927fe00082f48db7ba1c93a5721dbb036aa63ed1ecafe2
7
+ data.tar.gz: fdeb443c54c4840c76732f7fe03f6397496bc5ddbd9159be8edebc70c7cc50919d678f39db08003c3eb20bf9308681c63dbe61d63514fcf76d4e46c38e38a8a5
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ruboty-fastly.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruboty-fastly (0.1.0)
5
+ fastly
6
+ ruboty
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (5.1.4)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (~> 0.7)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ concurrent-ruby (1.0.5)
17
+ dotenv (2.2.1)
18
+ fastly (1.13.1)
19
+ i18n (0.9.1)
20
+ concurrent-ruby (~> 1.0)
21
+ mem (0.1.5)
22
+ minitest (5.10.3)
23
+ rake (12.3.0)
24
+ ruboty (1.3.0)
25
+ activesupport
26
+ bundler
27
+ dotenv
28
+ mem
29
+ slop
30
+ slop (4.6.1)
31
+ thread_safe (0.3.6)
32
+ tzinfo (1.2.4)
33
+ thread_safe (~> 0.1)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler
40
+ rake
41
+ ruboty-fastly!
42
+
43
+ BUNDLED WITH
44
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Sorah Fukumori
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,48 @@
1
+ # Ruboty::Fastly
2
+
3
+ ## Installation
4
+
5
+ ```ruby
6
+ gem 'ruboty-fastly'
7
+ ```
8
+
9
+ Then set an API key to `$FASTLY_API_KEY`.
10
+
11
+ ## Usage
12
+
13
+
14
+ ```
15
+ > ruboty fst list
16
+ - `SERVICE_ID` NAME
17
+
18
+ > ruboty fst purge http://...
19
+ Purged http://...
20
+
21
+ > ruboty fst purge key SERVICE_ID KEY1 KEY2...
22
+ Purged Fastly SERVICE_ID by key: `KEY1`
23
+ Purged Fastly SERVICE_ID by key: `KEY2`
24
+
25
+ > ruboty fst purge all SERVICE_ID
26
+ Purging Fastly `SERVICE_ID` after next 15 seconds, cancel by saying: `fastly cancel`
27
+ Purged all from Fastly `SERVICE_ID`
28
+
29
+ > ruboty fst cancel
30
+ Cancelled the pending purge requests.
31
+
32
+ > ruboty fst add alias SERVICE_ID NAME
33
+ > ruboty fst remove alias NAME
34
+ ```
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sorah/ruboty-fastly.
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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 "ruboty/fastly"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require 'ruboty-fastly/version'
@@ -0,0 +1,3 @@
1
+ module RubotyFastly
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'ruboty-fastly/version'
2
+ require 'ruboty/handlers/fastly'
@@ -0,0 +1,116 @@
1
+ require "ruboty-fastly/version"
2
+ require 'ruboty/handlers/base'
3
+ require 'fastly'
4
+
5
+ module Ruboty
6
+ module Handlers
7
+ class Fastly < Base
8
+
9
+ env :FASTLY_API_KEY, "Fastly API key"
10
+
11
+ VERSION = RubotyFastly::VERSION
12
+
13
+ on(/(?:fastly|fst)\s+list(\s+services?)?/, name: 'list_services', description: 'List Fastly services')
14
+ on(/(?:fastly|fst)\s+(?:set|add)\s+alias\s+(?<service_id>.+?)\s+(?<name>.+?)$/, name: 'set_alias', description: 'Set alias to the Fastly service')
15
+ on(/(?:fastly|fst)\s+remove\s+alias\s+(?<service_id>.+?)\s+(?<name>.+?)$/, name: 'remove_alias', description: 'Remove alias to the Fastly service')
16
+ on(/(?:fastly|fst)\s+(?<soft>soft\s+)?purge\s+(?<url>http.+?)$/, name: 'purge_url', description: 'Purge single URL from Fastly')
17
+ on(/(?:fastly|fst)\s+(?<soft>soft\s+)?purge\s+key\s+(?<service_id>.+?)\s+(?<keys>.+?)$/, name: 'purge_key', description: 'Purge by keys from Fastly')
18
+ on(/(?:fastly|fst)\s+purge\s+all\s+(?<service_id>.+?)$/, name: 'purge_all', description: 'Purge all from Fastly service')
19
+ on(/(?:fastly|fst)\s+(?:purge\s+)?cancel$/, name: 'purge_cancel', description: 'Cancel pending purge alls from Fastly')
20
+
21
+ def list_services(message)
22
+ reversed_aliases = aliases.to_a.group_by(&:last).map { |sid, _| [sid, _.map(&:first)] }.to_h
23
+
24
+ list = fastly.list_services.map do |service|
25
+ "- `#{service.id}` #{service.name}#{reversed_aliases[service.id] ? " (alias: #{reversed_aliases[service.id].join(', ')})" : nil}"
26
+ end
27
+ message.reply list.join("\n")
28
+ rescue => e
29
+ message.reply e.inspect
30
+ end
31
+
32
+ def set_alias(message)
33
+ aliases[message[:name]] = message[:service_id]
34
+ message.reply "Added an alias: #{message[:name]} > `#{message[:service_id]}`"
35
+ rescue => e
36
+ message.reply e.inspect
37
+ end
38
+
39
+ def remove_alias(message)
40
+ if aliases.delete(message[:name])
41
+ message.reply "Removed alias #{message[:name]}"
42
+ else
43
+ message.reply "Alias #{message[:name]} doesn't exist"
44
+ end
45
+ rescue => e
46
+ message.reply e.inspect
47
+ end
48
+
49
+ def purge_url(message)
50
+ soft = !!message[:soft]
51
+ fastly.purge(message[:url], soft)
52
+ message.reply "#{soft ? 'Soft ' : nil}Purged `#{message[:url]}`"
53
+ rescue => e
54
+ message.reply e.inspect
55
+ end
56
+
57
+ def purge_key(message)
58
+ soft = !!message[:soft]
59
+ service = service_for(message[:service_id])
60
+ keys = message[:keys].split(/\s+/)
61
+
62
+ keys.each do |key|
63
+ service.purge_by_key(key, soft)
64
+ message.reply "#{soft ? 'Soft ' : nil}Purged Fastly `#{service.id}` by key: `#{key}`"
65
+ end
66
+ rescue => e
67
+ message.reply e.inspect
68
+ end
69
+
70
+ def purge_all(message)
71
+ service = service_for(message[:service_id])
72
+
73
+ Thread.new(purge_cancellations) do |count|
74
+ begin
75
+ sleep 15
76
+ next if count != purge_cancellations
77
+ service.purge_all()
78
+ message.reply "Purged all from Fastly `#{service.id}`"
79
+ rescue => e
80
+ message.reply e.inspect
81
+ end
82
+ end
83
+ message.reply "Purging Fastly `#{service.id}` after next 15 seconds, cancel by saying: `fastly cancel`"
84
+ rescue => e
85
+ message.reply e.inspect
86
+ end
87
+
88
+ def purge_cancel(message)
89
+ cancel_all_pending_purges
90
+ message.reply "Cancelled the pending purge requests."
91
+ end
92
+
93
+ private
94
+
95
+ def fastly
96
+ ::Fastly.new(api_key: ENV.fetch('FASTLY_API_KEY'))
97
+ end
98
+
99
+ def service_for(name)
100
+ ::Fastly::Service.new({ id: aliases[name] || name }, fastly)
101
+ end
102
+
103
+ def aliases
104
+ robot.brain.data['fastly.aliases'] ||= {}
105
+ end
106
+
107
+ def purge_cancellations
108
+ @purge_cancellations ||= 0
109
+ end
110
+
111
+ def cancel_all_pending_purges
112
+ @purge_cancellations = purge_cancellations + 1
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ruboty-fastly/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruboty-fastly"
8
+ spec.version = RubotyFastly::VERSION
9
+ spec.authors = ["Sorah Fukumori"]
10
+ spec.email = ["sorah@cookpad.com"]
11
+
12
+ spec.summary = %q{Ruboty plugin for Fastly chat-ops}
13
+ spec.homepage = "https://github.com/sorah/ruboty-fastly"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "ruboty"
24
+ spec.add_dependency "fastly"
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake"
28
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-fastly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sorah Fukumori
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
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: fastly
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - sorah@cookpad.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/ruboty-fastly.rb
85
+ - lib/ruboty-fastly/version.rb
86
+ - lib/ruboty/fastly.rb
87
+ - lib/ruboty/handlers/fastly.rb
88
+ - ruboty-fastly.gemspec
89
+ homepage: https://github.com/sorah/ruboty-fastly
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.7.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Ruboty plugin for Fastly chat-ops
113
+ test_files: []