serverless_redirector 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +32 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/serverless-redirector +26 -0
- data/lib/serverless_redirector/applicator.rb +29 -0
- data/lib/serverless_redirector/controller.rb +75 -0
- data/lib/serverless_redirector/destination.rb +116 -0
- data/lib/serverless_redirector/manifest.rb +48 -0
- data/lib/serverless_redirector/operations.rb +23 -0
- data/lib/serverless_redirector/renderer.rb +24 -0
- data/lib/serverless_redirector/syncer.rb +68 -0
- data/lib/serverless_redirector/version.rb +3 -0
- data/lib/serverless_redirector.rb +11 -0
- data/serverless_redirector.gemspec +28 -0
- data/templates/error.erb +0 -0
- data/templates/redirect.erb +14 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c64dac3aefe5c9647aaf7ab969af38b2ce3b7a10
|
4
|
+
data.tar.gz: 2f3fffa8a113a5291712efa8cc733106f3fc6204
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: be4b8fbc4f28a21b8909d4479f2f91d55cc45fbe22dd9a9a343b1eab0ab6a05443d17a27c742328c25bd5dc3e988c54da0321fc070e92a0ac1f32923d83edd29
|
7
|
+
data.tar.gz: b65a9da86edc6fbe884dcb3893d2b0e48bd849807bd5f3c99c35a96fc86296607da2074c813bbecc998b8c5c5f5f01c28b67ad89c9373b822c5cf7d5ac877e9b
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at sutto@sutto.net. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Darcy Laycock
|
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,32 @@
|
|
1
|
+
# ServerlessRedirector
|
2
|
+
|
3
|
+
**NB:** This is WIP software, and not yet ready for public consuption.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Install the tool as:
|
9
|
+
|
10
|
+
$ gem install serverless_redirector
|
11
|
+
|
12
|
+
And you'll then be able to invoke the serverless redirector as needed.
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
TODO: Write usage instructions here
|
17
|
+
|
18
|
+
## Development
|
19
|
+
|
20
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
21
|
+
|
22
|
+
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).
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Sutto/serverless-redirector. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
27
|
+
|
28
|
+
|
29
|
+
## License
|
30
|
+
|
31
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
32
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "serverless_redirector"
|
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,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
require 'serverless_redirector'
|
5
|
+
|
6
|
+
class ServerlessRedirectorCLI < Thor
|
7
|
+
|
8
|
+
desc "sync MANIFEST_PATH DESTINATION_URI", "syncs URLs in the manifest path to the destination path."
|
9
|
+
method_options dry_run: :boolean, log_path: :string
|
10
|
+
def sync(manifest_path, destination_uri)
|
11
|
+
controller = ServerlessRedirector::Controller.new(options.merge(manifest_path: manifest_path, destination_uri: destination_uri))
|
12
|
+
controller.invoke!
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "print MANIFEST_PATH", "Prints the manifest details and what it means"
|
16
|
+
def print(manifest_path)
|
17
|
+
manifest = ServerlessRedirector::Manifest.load_file! manifest_path
|
18
|
+
size = manifest.redirects.map { |r| r.path.length }.max
|
19
|
+
manifest.redirects.each do |redirect|
|
20
|
+
puts "#{redirect.path.ljust(size)} - #{redirect.url}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
ServerlessRedirectorCLI.start
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
3
|
+
module ServerlessRedirector
|
4
|
+
class Applicator
|
5
|
+
|
6
|
+
attr_reader :destination, :logger, :logger_mutex
|
7
|
+
|
8
|
+
def initialize(destination, logger)
|
9
|
+
@destination = destination
|
10
|
+
@logger = logger
|
11
|
+
@logger_mutex = Mutex.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def apply(operation)
|
15
|
+
case operation
|
16
|
+
when ServerlessRedirector::Operations::RemoveRedirect
|
17
|
+
destination.remove_key operation.redirect.path
|
18
|
+
when ServerlessRedirector::Operations::CreateRedirect, ServerlessRedirector::Operations::UpdateRedirect
|
19
|
+
rendered = ServerlessRedirector::Renderer.new('redirect.erb').render(url: operation.redirect.url)
|
20
|
+
destination.write_key operation.redirect.path, operation.redirect.url, rendered
|
21
|
+
end
|
22
|
+
logger_mutex.synchronize { logger.info "=> Applied #{operation.summarize}" }
|
23
|
+
rescue => e
|
24
|
+
p e
|
25
|
+
raise
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
require 'serverless_redirector/destination'
|
4
|
+
require 'serverless_redirector/syncer'
|
5
|
+
require 'serverless_redirector/manifest'
|
6
|
+
|
7
|
+
module ServerlessRedirector
|
8
|
+
class Controller
|
9
|
+
|
10
|
+
# Configuration has some options:
|
11
|
+
|
12
|
+
attr_accessor :destination_uri, # Target for the syncing...
|
13
|
+
:manifest_path, # Where can we find out manifest?
|
14
|
+
:log_path, # If present, where do we log? (STDOUT, by default)
|
15
|
+
:dry_run # Is this a dry run?
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
@dry_run = false
|
19
|
+
@log_path = nil
|
20
|
+
@destination_uri = nil
|
21
|
+
@manifest_path = nil
|
22
|
+
unpack_options options
|
23
|
+
end
|
24
|
+
|
25
|
+
def unpack_options(options)
|
26
|
+
@dry_run = !!options[:dry_run] if options.key?(:dry_run)
|
27
|
+
|
28
|
+
@manifest_path = options.fetch(:manifest_path) if options.key?(:manifest_path)
|
29
|
+
@log_path = options.fetch(:log_path) if options.key?(:log_path)
|
30
|
+
@destination_uri = options.fetch(:destination_uri) if options.key?(:destination_uri)
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate
|
34
|
+
raise ArgumentError.new("Manifest path does not exist") unless manifest_path && ::File.exist?(manifest_path)
|
35
|
+
raise ArgumentError.new("Destination URI Invalid") unless destination_uri && ::URI.parse(destination_uri).present?
|
36
|
+
# Otherwise fine here...
|
37
|
+
end
|
38
|
+
|
39
|
+
def invoke!
|
40
|
+
logger = create_logger
|
41
|
+
manifest = create_manifest
|
42
|
+
destination = create_destination
|
43
|
+
|
44
|
+
syncer = ServerlessRedirector::Syncer.new(manifest, destination, logger)
|
45
|
+
syncer.run dry_run
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def create_logger
|
51
|
+
::Logger.new(log_path || STDOUT)
|
52
|
+
end
|
53
|
+
|
54
|
+
def create_destination
|
55
|
+
parsed_uri = URI.parse(destination_uri)
|
56
|
+
case parsed_uri.scheme
|
57
|
+
when "file"
|
58
|
+
ServerlessRedirector::LocalDestination.new(parsed_uri)
|
59
|
+
when "s3"
|
60
|
+
ServerlessRedirector::S3Destination.new(parsed_uri)
|
61
|
+
when "nop"
|
62
|
+
# Mock / NOOP destination....
|
63
|
+
ServerlessRedirector::Destination.new
|
64
|
+
else
|
65
|
+
raise ArgumentError.new("We do not support #{parsed_uri.scheme} as a known URI scheme.")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def create_manifest
|
70
|
+
ServerlessRedirector::Manifest.load_file! manifest_path
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
|
3
|
+
module ServerlessRedirector
|
4
|
+
class Destination
|
5
|
+
|
6
|
+
REDIRECT_HEADER_KEY = "x-redirector-target".freeze
|
7
|
+
|
8
|
+
# Lists existing redirects on the bucket
|
9
|
+
def existing
|
10
|
+
[]
|
11
|
+
end
|
12
|
+
|
13
|
+
def remove_key(key)
|
14
|
+
p [:remove_key, key]
|
15
|
+
end
|
16
|
+
|
17
|
+
def write_key(key, location, contents)
|
18
|
+
p [:write_key, key, location, contents]
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
class LocalDestination < Destination
|
24
|
+
|
25
|
+
attr_reader :uri, :path
|
26
|
+
|
27
|
+
def initialize(uri)
|
28
|
+
@uri = uri
|
29
|
+
@path = File.join uri.host, uri.path
|
30
|
+
end
|
31
|
+
|
32
|
+
def existing
|
33
|
+
Dir[File.join(@path, "**/*.json")].map do |json_path|
|
34
|
+
contents = JSON.parse(File.read(json_path))
|
35
|
+
ServerlessRedirector::Manifest::Redirect.new(contents)
|
36
|
+
end.compact
|
37
|
+
end
|
38
|
+
|
39
|
+
def remove_key(key)
|
40
|
+
html_path = File.join(@path, "#{key}.html")
|
41
|
+
File.delete(html_path) if File.exist?(html_path)
|
42
|
+
json_path = File.join(@path, "#{key}.json")
|
43
|
+
File.delete(json_path) if File.exist?(json_path)
|
44
|
+
end
|
45
|
+
|
46
|
+
def write_key(key, location, contents)
|
47
|
+
html_path = File.join(@path, "#{key}.html")
|
48
|
+
FileUtils.mkdir_p File.dirname(html_path)
|
49
|
+
File.write html_path, contents
|
50
|
+
json_path = File.join(@path, "#{key}.json")
|
51
|
+
File.write json_path, JSON.dump(url: location, path: key)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
class S3Destination < Destination
|
57
|
+
|
58
|
+
attr_reader :uri, :bucket_name, :prefix
|
59
|
+
|
60
|
+
def initialize(uri)
|
61
|
+
@uri = uri
|
62
|
+
@bucket_name = uri.host
|
63
|
+
@prefix = uri.path.to_s.empty? ? nil : ::File.join(uri.path[1..-1], "")
|
64
|
+
end
|
65
|
+
|
66
|
+
def existing
|
67
|
+
bucket.objects(prefix: prefix).map(&:object).select do |o|
|
68
|
+
!o.metadata[REDIRECT_HEADER_KEY].to_s.empty?
|
69
|
+
end.map do |o|
|
70
|
+
path = o.key.dup
|
71
|
+
path.gsub! /^#{Regexp.escape(prefix)}\/?/, '' if prefix
|
72
|
+
ServerlessRedirector::Manifest::Redirect.new 'path' => path, 'url' => o.metadata["x-redirector-target"].to_s
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def remove_key(key)
|
77
|
+
path = build_path key
|
78
|
+
bucket.object(path).delete
|
79
|
+
end
|
80
|
+
|
81
|
+
def write_key(key, location, contents)
|
82
|
+
# Remove the prefix to the path...
|
83
|
+
path = build_path(key).gsub(/\A\/+/, '')
|
84
|
+
bucket.put_object({
|
85
|
+
acl: "public-read",
|
86
|
+
content_type: "text/html",
|
87
|
+
body: contents,
|
88
|
+
key: path,
|
89
|
+
website_redirect_location: location,
|
90
|
+
metadata: {
|
91
|
+
REDIRECT_HEADER_KEY => location
|
92
|
+
}
|
93
|
+
})
|
94
|
+
puts "WRITTEN"
|
95
|
+
end
|
96
|
+
|
97
|
+
protected
|
98
|
+
|
99
|
+
def bucket
|
100
|
+
@bucket ||= begin
|
101
|
+
resource = Aws::S3::Resource.new
|
102
|
+
resource.bucket bucket_name
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def build_path(path)
|
107
|
+
if prefix
|
108
|
+
::File.join prefix, path
|
109
|
+
else
|
110
|
+
path
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ServerlessRedirector
|
2
|
+
class Manifest
|
3
|
+
|
4
|
+
Redirect = Struct.new(:path, :url) do
|
5
|
+
|
6
|
+
def initialize(h = {})
|
7
|
+
super h.fetch('path'), h.fetch('url')
|
8
|
+
end
|
9
|
+
|
10
|
+
def serializable_hash(options = {})
|
11
|
+
{
|
12
|
+
'path' => path,
|
13
|
+
'url' => url
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
# Conditions:
|
20
|
+
# 1. Everything has a valid path.
|
21
|
+
# 2. All URLs are valid
|
22
|
+
|
23
|
+
attr_reader :redirects
|
24
|
+
|
25
|
+
def initialize(contents)
|
26
|
+
@redirects = contents.to_a.map { |item| Redirect.new item }.freeze
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate
|
30
|
+
@redirects.each do |item|
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.dump_file!(path)
|
35
|
+
File.open path, 'w+' do |out|
|
36
|
+
@redirects.each do |redirect|
|
37
|
+
out.puts JSON.dump(redirect.serializable_hash)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.load_file!(path)
|
43
|
+
contents = File.readlines(path).lazy.map { |line| JSON.parse(line) }
|
44
|
+
new contents
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ServerlessRedirector
|
2
|
+
module Operations
|
3
|
+
|
4
|
+
class RedirectOperation
|
5
|
+
|
6
|
+
attr_reader :redirect
|
7
|
+
|
8
|
+
def initialize(redirect)
|
9
|
+
@redirect = redirect
|
10
|
+
end
|
11
|
+
|
12
|
+
def summarize
|
13
|
+
"#{self.class.name.split("::").last} on #{redirect.path} => #{redirect.url}"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class RemoveRedirect < RedirectOperation; end
|
19
|
+
class UpdateDestination < RedirectOperation; end
|
20
|
+
class CreateRedirect < RedirectOperation; end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
module ServerlessRedirector
|
5
|
+
class Renderer
|
6
|
+
|
7
|
+
BASE_PATH = ::File.expand_path '../../templates', File.dirname(__FILE__)
|
8
|
+
|
9
|
+
attr_reader :template_name, :template_path
|
10
|
+
|
11
|
+
def initialize(template_name)
|
12
|
+
@template_name = template_name
|
13
|
+
@template_path = ::File.join BASE_PATH, template_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def render(context = {})
|
17
|
+
contents = ::File.read(template_path)
|
18
|
+
template = ERB.new contents
|
19
|
+
ctx_binding = OpenStruct.new(context).instance_eval { binding }
|
20
|
+
template.result ctx_binding
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'concurrent'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module ServerlessRedirector
|
6
|
+
class Syncer
|
7
|
+
|
8
|
+
attr_reader :manifest, :destination, :logger
|
9
|
+
|
10
|
+
def initialize(manifest, destination, logger = ::Logger.new(STDOUT))
|
11
|
+
@manifest = manifest
|
12
|
+
@destination = destination
|
13
|
+
@logger = logger
|
14
|
+
end
|
15
|
+
|
16
|
+
def run(dry_run = false)
|
17
|
+
operations = calculate_operations
|
18
|
+
|
19
|
+
if operations.empty?
|
20
|
+
logger.info "Nothing to do, bailing."
|
21
|
+
return
|
22
|
+
end
|
23
|
+
|
24
|
+
debug_operations operations
|
25
|
+
|
26
|
+
if !dry_run
|
27
|
+
logger.info "Applying #{operations.size} operations"
|
28
|
+
apply_operations operations
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def calculate_operations
|
34
|
+
contents = destination.existing.map { |r| [r.path, r] }.to_h
|
35
|
+
targets = manifest.redirects.map { |r| [r.path, r] }.to_h
|
36
|
+
|
37
|
+
removed = (contents.keys - targets.keys).map { |k| contents[k] }
|
38
|
+
added = (targets.keys - contents.keys).map { |k| targets[k] }
|
39
|
+
changed = (targets.keys & contents.keys).select { |k| targets[k] != contents[k] }.map { |k| targets[k] }
|
40
|
+
|
41
|
+
[].tap do |operations|
|
42
|
+
operations.concat removed.map { |r| Operations::RemoveRedirect.new(r) }
|
43
|
+
operations.concat added.map { |r| Operations::CreateRedirect.new(r) }
|
44
|
+
operations.concat changed.map { |r| Operations::UpdateDestination.new(r) }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def apply_operations(operations)
|
49
|
+
logger_mutex = Mutex.new
|
50
|
+
pool = Concurrent::ThreadPoolExecutor.new min_threads: 1, max_threads: 8, max_queue: 0
|
51
|
+
|
52
|
+
applicator = ServerlessRedirector::Applicator.new(destination, logger)
|
53
|
+
operations.each do |operation|
|
54
|
+
pool.post { applicator.apply(operation) }
|
55
|
+
end
|
56
|
+
pool.shutdown
|
57
|
+
pool.wait_for_termination
|
58
|
+
end
|
59
|
+
|
60
|
+
def debug_operations(operations)
|
61
|
+
logger.info "Applying #{operations.size} operations:"
|
62
|
+
operations.each do |op|
|
63
|
+
logger.info "=> #{op.summarize}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "serverless_redirector/version"
|
2
|
+
|
3
|
+
module ServerlessRedirector
|
4
|
+
require 'serverless_redirector/operations'
|
5
|
+
require 'serverless_redirector/manifest'
|
6
|
+
require 'serverless_redirector/destination'
|
7
|
+
require 'serverless_redirector/renderer'
|
8
|
+
require 'serverless_redirector/applicator'
|
9
|
+
require 'serverless_redirector/syncer'
|
10
|
+
require 'serverless_redirector/controller'
|
11
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'serverless_redirector/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "serverless_redirector"
|
8
|
+
spec.version = ServerlessRedirector::VERSION
|
9
|
+
spec.authors = ["Darcy Laycock"]
|
10
|
+
spec.email = ["sutto@sutto.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{S3-Based URL Redirection}
|
13
|
+
spec.description = %q{Manages a S3 bucket providing url redirects based on JSON manifests. Serverless - oh my!}
|
14
|
+
spec.homepage = "https://github.com/Sutto/serverless-redirector"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
|
25
|
+
spec.add_dependency 'concurrent-ruby', '~> 1.0'
|
26
|
+
spec.add_dependency 'thor', '~> 0.19.0'
|
27
|
+
spec.add_dependency 'aws-sdk', '~> 2.0'
|
28
|
+
end
|
data/templates/error.erb
ADDED
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<html lang=en>
|
3
|
+
<head>
|
4
|
+
<meta charset=utf-8>
|
5
|
+
<title>Redirect</title>
|
6
|
+
<meta http-equiv="refresh" content="1;url=<%= url %>">
|
7
|
+
<script type="text/javascript">
|
8
|
+
window.location.href = "<%= url %>"
|
9
|
+
</script>
|
10
|
+
</head>
|
11
|
+
<body>
|
12
|
+
<p>Redirecting you to <a href="<%= url %>"><%= url %></a>...</p>
|
13
|
+
</body>
|
14
|
+
</html>
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serverless_redirector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Darcy Laycock
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: concurrent-ruby
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: thor
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.19.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.19.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: aws-sdk
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
description: Manages a S3 bucket providing url redirects based on JSON manifests.
|
84
|
+
Serverless - oh my!
|
85
|
+
email:
|
86
|
+
- sutto@sutto.net
|
87
|
+
executables:
|
88
|
+
- serverless-redirector
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- exe/serverless-redirector
|
101
|
+
- lib/serverless_redirector.rb
|
102
|
+
- lib/serverless_redirector/applicator.rb
|
103
|
+
- lib/serverless_redirector/controller.rb
|
104
|
+
- lib/serverless_redirector/destination.rb
|
105
|
+
- lib/serverless_redirector/manifest.rb
|
106
|
+
- lib/serverless_redirector/operations.rb
|
107
|
+
- lib/serverless_redirector/renderer.rb
|
108
|
+
- lib/serverless_redirector/syncer.rb
|
109
|
+
- lib/serverless_redirector/version.rb
|
110
|
+
- serverless_redirector.gemspec
|
111
|
+
- templates/error.erb
|
112
|
+
- templates/redirect.erb
|
113
|
+
homepage: https://github.com/Sutto/serverless-redirector
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.5.1
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: S3-Based URL Redirection
|
137
|
+
test_files: []
|