microstatic 0.5.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +31 -0
- data/lib/microstatic/rake.rb +2 -48
- data/lib/microstatic/rake/aws_site_setup_task.rb +42 -0
- data/lib/microstatic/rake/s3_deployer_task.rb +33 -0
- data/lib/microstatic/rake/task_environment.rb +65 -0
- data/lib/microstatic/route53_dns.rb +9 -5
- data/lib/microstatic/version.rb +1 -1
- data/spec/integration/fixtures/index.html +1 -0
- data/spec/unit/rake_task_environment_spec.rb +107 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcf8c5bbedce6a7899b24a04130c39935b24840f
|
4
|
+
data.tar.gz: 826eed6be7585f64afea10a9a19bf6239b52965d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2a0fbcf3b82e2187c0bee9cefe81002c9a783ef6246bd5eecaafe551dd8aaeeaa04e8b0b89c2645c65946d967a8a31c5c09c19061b38e7c87ea9e8cd4a3f0c9
|
7
|
+
data.tar.gz: 5987e6f0547ffec0093f29d5549aa5a76b7e0317f6a5f27aa63d48e9aa2c231b3f185cc9c178a6bd62cdb50719787ba933acfd63de809e3a98ce03db1df25ff5
|
data/Rakefile
CHANGED
@@ -1,2 +1,33 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
+
|
2
3
|
require "bundler/gem_tasks"
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift File.expand_path("../lib",__FILE__)
|
7
|
+
|
8
|
+
namespace :spec do
|
9
|
+
desc "run unit specs"
|
10
|
+
RSpec::Core::RakeTask.new(:unit) do |task|
|
11
|
+
task.pattern = "spec/unit/**/*_spec.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "run integration specs (requires AWS creds)"
|
15
|
+
RSpec::Core::RakeTask.new(:integration) do |task|
|
16
|
+
task.pattern = "spec/integration/**/*_spec.rb"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'microstatic/rake'
|
20
|
+
desc "do a test s3 deploy to a hard-coded test bucket"
|
21
|
+
Microstatic::Rake.s3_deploy_task(:test_s3_deploy) do |task|
|
22
|
+
task.bucket_name = '1.microstatic-test-bucket.thepete.net'
|
23
|
+
task.source_dir = 'spec/integration/fixtures'
|
24
|
+
task.exclude = %r|subdir|
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "setup a new AWS site [here for manual testing purposes]"
|
28
|
+
Microstatic::Rake.aws_site_setup_task do |task|
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
task :default => 'spec:unit'
|
data/lib/microstatic/rake.rb
CHANGED
@@ -1,51 +1,5 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/tasklib'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
class S3DeployTask < ::Rake::TaskLib
|
7
|
-
attr_accessor :name, :bucket_name, :source_dir, :exclude, :aws_access_key_id, :aws_secret_access_key
|
8
|
-
|
9
|
-
def initialize( opts = {} )
|
10
|
-
if opts.is_a?(String) || opts.is_a?(Symbol)
|
11
|
-
opts = { name: opts }
|
12
|
-
end
|
13
|
-
|
14
|
-
@name = opts.fetch( :name ) { :s3deploy }
|
15
|
-
@aws_access_key_id = opts.fetch( :aws_access_key_id ) { ENV.fetch('AWS_ACCESS_KEY_ID',false) }
|
16
|
-
@aws_secret_access_key = opts.fetch( :aws_secret_access_key ) { ENV.fetch('AWS_SECRET_ACCESS_KEY',false) }
|
17
|
-
@bucket_name = opts.fetch( :bucket_name, false )
|
18
|
-
@source_dir = opts.fetch( :source_dir, false )
|
19
|
-
@exclude = opts.fetch( :exclude, false )
|
20
|
-
end
|
21
|
-
|
22
|
-
def define
|
23
|
-
require 'microstatic'
|
24
|
-
|
25
|
-
raise 'must specify bucket_name' unless bucket_name
|
26
|
-
raise 'must specify source_dir' unless source_dir
|
27
|
-
|
28
|
-
desc "deploy to the '#{bucket_name}' S3 bucket" unless ::Rake.application.last_comment
|
29
|
-
task name do
|
30
|
-
|
31
|
-
raise 'must specify aws_access_key_id' unless aws_access_key_id
|
32
|
-
raise 'must specify aws_secret_access_key' unless aws_secret_access_key
|
33
|
-
aws_creds = {
|
34
|
-
:access_key_id => aws_access_key_id,
|
35
|
-
:secret_access_key => aws_secret_access_key
|
36
|
-
}
|
37
|
-
|
38
|
-
deployer = Microstatic::S3Deployer.build( source_dir, bucket_name, aws_creds )
|
39
|
-
deployer.exclude_files(@exclude) if @exclude
|
40
|
-
deployer.upload
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.s3_deploy_task(opts = {})
|
46
|
-
task = S3DeployTask.new( opts )
|
47
|
-
yield task if block_given?
|
48
|
-
task.define
|
49
|
-
end
|
50
|
-
|
51
|
-
end end
|
4
|
+
require "microstatic/rake/s3_deployer_task"
|
5
|
+
require "microstatic/rake/aws_site_setup_task"
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'microstatic/rake/task_environment'
|
2
|
+
|
3
|
+
module Microstatic module Rake
|
4
|
+
|
5
|
+
class AwsSiteSetupTask < ::Rake::TaskLib
|
6
|
+
def initialize(task_env)
|
7
|
+
@task_env = task_env
|
8
|
+
end
|
9
|
+
|
10
|
+
def define
|
11
|
+
require 'microstatic'
|
12
|
+
|
13
|
+
te = @task_env
|
14
|
+
|
15
|
+
desc "set up S3 bucket and Route53 DNS entry for a new site" unless ::Rake.application.last_comment
|
16
|
+
task te.task_name_or('aws_site_setup'), :site_name, :hosted_zone do |t,args|
|
17
|
+
site_name = args[:site_name]
|
18
|
+
hosted_zone = args.with_defaults(:hosted_zone => false)[:hosted_zone]
|
19
|
+
|
20
|
+
# TODO: check site_name looks like a site name (e.g. foo.thepete.net, not just foo)
|
21
|
+
site_name or raise "you must provide a site_name parameter to this rake task"
|
22
|
+
|
23
|
+
te.check_for_aws_creds!
|
24
|
+
|
25
|
+
|
26
|
+
# TODO: check it doesn't already exist for you
|
27
|
+
# TODO: handle the bucket name already being taken by someone else
|
28
|
+
S3BucketCreator.new( te.aws_creds ).create( site_name )
|
29
|
+
|
30
|
+
# TODO: handle DNS record already existing
|
31
|
+
Route53Dns.new( te.aws_creds ).add_s3_record_for_bucket( site_name, hosted_zone )
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.aws_site_setup_task(opts = {})
|
37
|
+
task_env = TaskEnvironment.new(opts)
|
38
|
+
yield task_env if block_given?
|
39
|
+
AwsSiteSetupTask.new( task_env ).define
|
40
|
+
end
|
41
|
+
|
42
|
+
end end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'microstatic/rake/task_environment'
|
2
|
+
|
3
|
+
module Microstatic module Rake
|
4
|
+
|
5
|
+
class S3DeployTask < ::Rake::TaskLib
|
6
|
+
def initialize(task_env)
|
7
|
+
@task_env = task_env
|
8
|
+
end
|
9
|
+
|
10
|
+
def define
|
11
|
+
require 'microstatic'
|
12
|
+
|
13
|
+
te = @task_env
|
14
|
+
te.check_for_mandatory_opts!
|
15
|
+
|
16
|
+
desc "deploy to the '#{te.bucket_name}' S3 bucket" unless ::Rake.application.last_comment
|
17
|
+
task te.task_name_or('s3deploy') do
|
18
|
+
|
19
|
+
te.check_for_aws_creds!
|
20
|
+
deployer = Microstatic::S3Deployer.build( te.source_dir, te.bucket_name, te.aws_creds )
|
21
|
+
deployer.exclude_files(te.exclude) if te.exclude
|
22
|
+
deployer.upload
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.s3_deploy_task(opts = {})
|
28
|
+
task_env = TaskEnvironment.new(opts)
|
29
|
+
yield task_env if block_given?
|
30
|
+
S3DeployTask.new( task_env ).define
|
31
|
+
end
|
32
|
+
|
33
|
+
end end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Microstatic module Rake
|
2
|
+
|
3
|
+
class TaskEnvironment
|
4
|
+
def initialize( opts, env = ENV )
|
5
|
+
if opts.is_a?(String) || opts.is_a?(Symbol)
|
6
|
+
opts = { name: opts }
|
7
|
+
end
|
8
|
+
|
9
|
+
@opts = opts
|
10
|
+
@env = env
|
11
|
+
end
|
12
|
+
|
13
|
+
def task_name_or(default)
|
14
|
+
@opts.fetch(:name,default)
|
15
|
+
end
|
16
|
+
|
17
|
+
def bucket_name
|
18
|
+
@opts.fetch(:bucket_name)
|
19
|
+
end
|
20
|
+
def bucket_name=(bn)
|
21
|
+
@opts[:bucket_name] = bn
|
22
|
+
end
|
23
|
+
|
24
|
+
def source_dir
|
25
|
+
@opts.fetch(:source_dir)
|
26
|
+
end
|
27
|
+
def source_dir=(sd)
|
28
|
+
@opts[:source_dir] = sd
|
29
|
+
end
|
30
|
+
|
31
|
+
def exclude
|
32
|
+
@opts.fetch( :exclude, false )
|
33
|
+
end
|
34
|
+
def exclude=(e)
|
35
|
+
@opts[:exclude] = e
|
36
|
+
end
|
37
|
+
|
38
|
+
def aws_access_key_id
|
39
|
+
@opts.fetch(:aws_access_key_id) { @env.fetch("AWS_ACCESS_KEY_ID") }
|
40
|
+
end
|
41
|
+
|
42
|
+
def aws_secret_access_key
|
43
|
+
@opts.fetch(:aws_secret_access_key) { @env.fetch("AWS_SECRET_ACCESS_KEY") }
|
44
|
+
end
|
45
|
+
|
46
|
+
def aws_creds
|
47
|
+
{
|
48
|
+
access_key_id: aws_access_key_id,
|
49
|
+
secret_access_key: aws_secret_access_key
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def check_for_mandatory_opts!
|
54
|
+
bucket_name rescue raise "must provide a bucket_name"
|
55
|
+
source_dir rescue raise "must provide a source_dir"
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def check_for_aws_creds!
|
60
|
+
aws_access_key_id rescue raise "must provide an aws access key id either via an :aws_access_key_id opt or an AWS_ACCESS_KEY_ID environment variable"
|
61
|
+
aws_secret_access_key rescue raise "must provide an aws secret access key either via an :aws_secret_access_key opt or an AWS_SECRET_ACCESS_KEY environment variable"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end end
|
@@ -7,10 +7,10 @@ class Route53Dns
|
|
7
7
|
check_and_store_aws_creds(aws_creds)
|
8
8
|
end
|
9
9
|
|
10
|
-
def add_s3_record_for_bucket( bucket_name,
|
11
|
-
subdomain
|
10
|
+
def add_s3_record_for_bucket( bucket_name, zone_name = false )
|
11
|
+
subdomain = bucket_name
|
12
|
+
zone = lookup_zone(zone_name || guess_zone_from_subdomain(subdomain) )
|
12
13
|
|
13
|
-
zone = parent_zone_for_subdomain( subdomain )
|
14
14
|
cname_value = website_endpoint_for_bucket_named( bucket_name )
|
15
15
|
|
16
16
|
record = zone.records.create(
|
@@ -32,11 +32,15 @@ class Route53Dns
|
|
32
32
|
bucket_website_endpoint = "#{bucket.key}.s3-website-#{bucket.location}.amazonaws.com"
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
36
|
-
zone_name
|
35
|
+
def lookup_zone( zone_name )
|
36
|
+
zone_name << "." unless zone_name.end_with?(".")
|
37
37
|
dns.zones.find{ |x| x.domain == zone_name }
|
38
38
|
end
|
39
39
|
|
40
|
+
def guess_zone_from_subdomain( subdomain )
|
41
|
+
subdomain.split(".")[1..-1].join(".")
|
42
|
+
end
|
43
|
+
|
40
44
|
end
|
41
45
|
|
42
46
|
end
|
data/lib/microstatic/version.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
<h3>Hello world</h3>
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require_relative '../../lib/microstatic/rake/task_environment'
|
2
|
+
|
3
|
+
module Microstatic module Rake
|
4
|
+
|
5
|
+
describe TaskEnvironment do
|
6
|
+
let(:some_aws_access_key_id){ "SOME AWS ACCESS KEY ID" }
|
7
|
+
let(:some_aws_secret_access_key){ "SOME AWS SECRET ACCESS KEY" }
|
8
|
+
let(:valid_task_env) { TaskEnvironment.new(valid_opts,valid_env) }
|
9
|
+
|
10
|
+
let(:valid_env) { {
|
11
|
+
'AWS_ACCESS_KEY_ID' => some_aws_access_key_id,
|
12
|
+
'AWS_SECRET_ACCESS_KEY' => some_aws_secret_access_key
|
13
|
+
} }
|
14
|
+
|
15
|
+
let(:valid_opts) { {
|
16
|
+
bucket_name: 'some bucket name',
|
17
|
+
source_dir: 'some source dir',
|
18
|
+
} }
|
19
|
+
|
20
|
+
def hash_except(hash,key_to_remove)
|
21
|
+
hash.dup.tap{ |h| h.delete(key_to_remove) }
|
22
|
+
end
|
23
|
+
def valid_opts_except(key_to_remove)
|
24
|
+
hash_except(valid_opts,key_to_remove)
|
25
|
+
end
|
26
|
+
def valid_env_except(key_to_remove)
|
27
|
+
hash_except(valid_env,key_to_remove)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'exposes the bucket name' do
|
31
|
+
expect( valid_task_env.bucket_name ).to eq('some bucket name')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'allows updating the bucket name' do
|
35
|
+
valid_task_env.bucket_name = 'new bucket name'
|
36
|
+
expect(valid_task_env.bucket_name).to eq('new bucket name')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'exposes the source dir' do
|
40
|
+
expect( valid_task_env.source_dir ).to eq('some source dir')
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'check_for_mandatory_opts!' do
|
44
|
+
it 'does nothing when everything is legit' do
|
45
|
+
expect{ valid_task_env.check_for_mandatory_opts! }.to_not raise_error
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'reports missing bucket_name' do
|
49
|
+
task_env = TaskEnvironment.new( valid_opts_except(:bucket_name), valid_env )
|
50
|
+
expect{ task_env.check_for_mandatory_opts! }.to raise_error('must provide a bucket_name')
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'reports missing source_dir' do
|
54
|
+
task_env = TaskEnvironment.new( valid_opts_except(:source_dir), valid_env )
|
55
|
+
expect{ task_env.check_for_mandatory_opts! }.to raise_error('must provide a source_dir')
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'does not check for aws creds in env' do
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'check_for_aws_creds!' do
|
63
|
+
it 'reports missing aws access key id' do
|
64
|
+
task_env = TaskEnvironment.new( valid_opts,valid_env_except("AWS_ACCESS_KEY_ID") )
|
65
|
+
expect{ task_env.check_for_aws_creds! }.to raise_error('must provide an aws access key id either via an :aws_access_key_id opt or an AWS_ACCESS_KEY_ID environment variable')
|
66
|
+
end
|
67
|
+
it 'reports missing aws secret access key' do
|
68
|
+
task_env = TaskEnvironment.new( valid_opts,valid_env_except("AWS_SECRET_ACCESS_KEY") )
|
69
|
+
expect{ task_env.check_for_aws_creds! }.to raise_error('must provide an aws secret access key either via an :aws_secret_access_key opt or an AWS_SECRET_ACCESS_KEY environment variable')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'exposes aws creds from env' do
|
74
|
+
expect( valid_task_env.aws_access_key_id ).to eq(some_aws_access_key_id)
|
75
|
+
expect( valid_task_env.aws_secret_access_key ).to eq(some_aws_secret_access_key)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'exposes combined aws creds' do
|
79
|
+
expect(valid_task_env.aws_creds).to eq({
|
80
|
+
access_key_id: some_aws_access_key_id,
|
81
|
+
secret_access_key: some_aws_secret_access_key
|
82
|
+
})
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'when aws creds specified in opts' do
|
86
|
+
let( :opts_with_access_key ) { valid_opts.merge( aws_access_key_id: 'overridden access key id', aws_secret_access_key: 'overridden secret access key' ) }
|
87
|
+
let( :task_env ) { TaskEnvironment.new(opts_with_access_key,valid_env) }
|
88
|
+
|
89
|
+
it 'exposes aws creds from opts, not to env' do
|
90
|
+
expect(task_env.aws_access_key_id).to eq('overridden access key id')
|
91
|
+
expect(task_env.aws_secret_access_key).to eq('overridden secret access key')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#task_name' do
|
96
|
+
it 'exposes what was in opts' do
|
97
|
+
task_env = TaskEnvironment.new(valid_opts.merge(name:'specified task name'),valid_env)
|
98
|
+
expect(task_env.task_name_or('default name')).to eq('specified task name')
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'uses default if nothing in opts' do
|
102
|
+
expect( valid_task_env.task_name_or('default name') ).to eq('default name')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: microstatic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pete Hodgson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fog
|
@@ -127,6 +127,9 @@ files:
|
|
127
127
|
- lib/microstatic/cli.rb
|
128
128
|
- lib/microstatic/config.rb
|
129
129
|
- lib/microstatic/rake.rb
|
130
|
+
- lib/microstatic/rake/aws_site_setup_task.rb
|
131
|
+
- lib/microstatic/rake/s3_deployer_task.rb
|
132
|
+
- lib/microstatic/rake/task_environment.rb
|
130
133
|
- lib/microstatic/route53_dns.rb
|
131
134
|
- lib/microstatic/s3_bucket_creator.rb
|
132
135
|
- lib/microstatic/s3_deployer.rb
|
@@ -135,6 +138,7 @@ files:
|
|
135
138
|
- lib/microstatic/version.rb
|
136
139
|
- microstatic.gemspec
|
137
140
|
- spec/integration/fixtures/example_file.txt
|
141
|
+
- spec/integration/fixtures/index.html
|
138
142
|
- spec/integration/fixtures/subdir/another_file.txt
|
139
143
|
- spec/integration/fixtures/subdir/foo.txt
|
140
144
|
- spec/integration/fixtures/subdir/ignored/ignored-file.txt
|
@@ -142,6 +146,7 @@ files:
|
|
142
146
|
- spec/integration/fixtures/symlink.txt
|
143
147
|
- spec/integration/spec_helper.rb
|
144
148
|
- spec/integration/upload_to_test_bucket_spec.rb
|
149
|
+
- spec/unit/rake_task_environment_spec.rb
|
145
150
|
- templates/Rakefile.erb
|
146
151
|
homepage: https://github.com/moredip/microstatic
|
147
152
|
licenses: []
|
@@ -168,6 +173,7 @@ specification_version: 4
|
|
168
173
|
summary: Generate static sites from git and deploy them to an S3 bucket.
|
169
174
|
test_files:
|
170
175
|
- spec/integration/fixtures/example_file.txt
|
176
|
+
- spec/integration/fixtures/index.html
|
171
177
|
- spec/integration/fixtures/subdir/another_file.txt
|
172
178
|
- spec/integration/fixtures/subdir/foo.txt
|
173
179
|
- spec/integration/fixtures/subdir/ignored/ignored-file.txt
|
@@ -175,3 +181,4 @@ test_files:
|
|
175
181
|
- spec/integration/fixtures/symlink.txt
|
176
182
|
- spec/integration/spec_helper.rb
|
177
183
|
- spec/integration/upload_to_test_bucket_spec.rb
|
184
|
+
- spec/unit/rake_task_environment_spec.rb
|