kumogata2-plugin-ruby 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b7da594babc54b57d21509b419a1300a69b7059
4
+ data.tar.gz: 2d79b2777c48da249873ee5af704d198280b6aff
5
+ SHA512:
6
+ metadata.gz: d6c7cb6bee7612738f6170f2d9b4561af264e9cf8d1e3aea32ce08ff4cf9d27ba1a4406d3b6a2ec8ba782a22cc3870acd942def11e0c05a599643d829b8fa9ce
7
+ data.tar.gz: cbdf7381fb6d55e31859ccb54239fa5fc0927d4f05499aebb616ea8bf90eb8d5699178d315e2c491efcabfe4395fbad0b06c5142ba0bf17c115d802db7c3c63a
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kumogata2-plugin-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 winebarrel
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.
@@ -0,0 +1,128 @@
1
+ # kumogata2-plugin-ruby
2
+
3
+ It is the Ruby plug-in of [Kumogata2](https://github.com/winebarrel/kumogata2).
4
+
5
+ It convert the Ruby DSL to JSON.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kumogata2-plugin-ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kumogata2-plugin-ruby
22
+
23
+ ## Example
24
+
25
+ ```ruby
26
+ template do
27
+ AWSTemplateFormatVersion "2010-09-09"
28
+
29
+ Description (<<-EOS).undent
30
+ Kumogata Sample Template
31
+ You can use Here document!
32
+ EOS
33
+
34
+ Parameters do
35
+ InstanceType do
36
+ Default "t2.micro"
37
+ Description "Instance Type"
38
+ Type "String"
39
+ end
40
+ end
41
+
42
+ Resources do
43
+ myEC2Instance do
44
+ Type "AWS::EC2::Instance"
45
+ Properties do
46
+ ImageId "ami-XXXXXXXX"
47
+ InstanceType { Ref "InstanceType" }
48
+ KeyName "your_key_name"
49
+
50
+ UserData do
51
+ Fn__Base64 (<<-EOS).undent
52
+ #!/bin/bash
53
+ yum install -y httpd
54
+ service httpd start
55
+ EOS
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ ```
62
+
63
+ * `::` is converted to `__`
64
+ * `Fn::GetAtt` => `Fn__GetAtt`
65
+ * `_{ ... }` is convered to Hash
66
+ * `SecurityGroups [_{Ref "WebServerSecurityGroup"}]` => `{"SecurityGroups": [{"Ref": "WebServerSecurityGroup"}]}`
67
+ * `_path()` creates Hash that has a key of path
68
+ * `_path("/etc/passwd-s3fs") { content "..." }` => `{"/etc/passwd-s3fs": {"content": "..."}}`
69
+
70
+
71
+ ### Split a template file
72
+
73
+ * template.rb
74
+
75
+ ```ruby
76
+ template do
77
+ Resources do
78
+ _include 'template2.rb', :ami_id => 'ami-XXXXXXXX'
79
+ end
80
+ end
81
+ ```
82
+
83
+ * template2.rb
84
+
85
+ ```ruby
86
+ myEC2Instance do
87
+ Type "AWS::EC2::Instance"
88
+ Properties do
89
+ ImageId args[:ami_id]
90
+ InstanceType { Ref "InstanceType" }
91
+ KeyName "your_key_name"
92
+ end
93
+ end
94
+ ```
95
+
96
+ * Converted JSON template
97
+
98
+ ```javascript
99
+ {
100
+ "Resources": {
101
+ "myEC2Instance": {
102
+ "Type": "AWS::EC2::Instance",
103
+ "Properties": {
104
+ "ImageId": "ami-XXXXXXXX",
105
+ "InstanceType": {
106
+ "Ref": "InstanceType"
107
+ },
108
+ "KeyName": "your_key_name"
109
+ }
110
+ }
111
+ }
112
+ }
113
+ ```
114
+
115
+ ### Post hook
116
+
117
+ You can run ruby script after building servers using `post()`.
118
+
119
+ ```ruby
120
+ template do
121
+ ...
122
+ end
123
+
124
+ post do |output|
125
+ puts output
126
+ #=> '{"WebsiteURL"=>"http://ec2-XX-XX-XX-XX.ap-northeast-1.compute.amazonaws.com"}'
127
+ end
128
+ ```
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kumogata2/plugin/ruby"
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,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,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kumogata2/plugin/ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'kumogata2-plugin-ruby'
8
+ spec.version = Kumogata2::Plugin::Ruby::VERSION
9
+ spec.authors = ['winebarrel']
10
+ spec.email = ['sgwr_dts@yahoo.co.jp']
11
+
12
+ spec.summary = %q{Kumogata2 ruby plugin.}
13
+ spec.description = %q{Kumogata2 ruby plugin.}
14
+ spec.homepage = 'https://github.com/winebarrel/kumogata2-plugin-ruby'
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_dependency 'kumogata2'
23
+ spec.add_dependency 'dslh'
24
+
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ end
@@ -0,0 +1,74 @@
1
+ require 'base64'
2
+ require 'dslh'
3
+
4
+ require 'kumogata2'
5
+ require 'kumogata2/plugin/ruby/version'
6
+ require 'kumogata2/plugin/ruby/string_ext'
7
+ require 'kumogata2/plugin/ruby/context'
8
+
9
+ class Kumogata2::Plugin::Ruby
10
+ Kumogata2::Plugin.register(:ruby, ['rb'], self)
11
+
12
+ def initialize(options)
13
+ @options = options
14
+ end
15
+
16
+ def parse(str)
17
+ context = Kumogata2::Plugin::Ruby::Context.new(@options)
18
+ context.instance_eval(str, @options.path_or_url)
19
+ @post = context.instance_variable_get(:@_post)
20
+ context.instance_variable_get(:@_template)
21
+ end
22
+
23
+ def dump(hash)
24
+ devaluate_template(hash).colorize_as(:ruby)
25
+ end
26
+
27
+ def post(outputs)
28
+ if @post
29
+ @post.call(outputs)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def devaluate_template(template)
36
+ exclude_key = proc do |k|
37
+ k = k.to_s.gsub('::', '__')
38
+ k !~ /\A[_a-z]\w+\Z/i and k !~ %r|\A/\S*\Z|
39
+ end
40
+
41
+ key_conv = proc do |k|
42
+ k = k.to_s
43
+
44
+ if k =~ %r|\A/\S*\Z|
45
+ proc do |v, nested|
46
+ if nested
47
+ "_path(#{k.inspect}) #{v}"
48
+ else
49
+ "_path #{k.inspect}, #{v}"
50
+ end
51
+ end
52
+ else
53
+ k.gsub('::', '__')
54
+ end
55
+ end
56
+
57
+ value_conv = proc do |v|
58
+ if v.kind_of?(String) and v =~ /\A(?:0|[1-9]\d*)\Z/
59
+ v.to_i
60
+ else
61
+ v
62
+ end
63
+ end
64
+
65
+ dsl = Dslh.deval(template, :key_conv => key_conv, :value_conv => value_conv, :exclude_key => exclude_key)
66
+ dsl.gsub!(/^/, ' ').strip!
67
+
68
+ <<-EOS
69
+ template do
70
+ #{dsl}
71
+ end
72
+ EOS
73
+ end
74
+ end
@@ -0,0 +1,59 @@
1
+ class Kumogata2::Plugin::Ruby::Context
2
+ def initialize(options)
3
+ @options = options
4
+ end
5
+
6
+ def template(&block)
7
+ key_converter = proc do |key|
8
+ key = key.to_s
9
+ key.gsub!('__', '::') unless @options.skip_replace_underscore?
10
+ key
11
+ end
12
+
13
+ value_converter = proc do |v|
14
+ case v
15
+ when Hash, Array
16
+ v
17
+ else
18
+ v.to_s
19
+ end
20
+ end
21
+
22
+ @_template = Dslh.eval(nil, {
23
+ :key_conv => key_converter,
24
+ :value_conv => value_converter,
25
+ :scope_hook => proc {|scope|
26
+ define_template_func(scope, @options.path_or_url)
27
+ },
28
+ :filename => @options.path_or_url,
29
+ }, &block)
30
+ end
31
+
32
+ def post(&block)
33
+ @_post = block
34
+ end
35
+
36
+ private
37
+
38
+ def define_template_func(scope, path_or_url)
39
+ scope.instance_eval(<<-EOS)
40
+ def _include(file, args = {})
41
+ path = file.dup
42
+
43
+ unless path =~ %r|\\A/| or path =~ %r|\\A\\w+://|
44
+ path = File.expand_path(File.join(File.dirname(#{path_or_url.inspect}), path))
45
+ end
46
+
47
+ open(path) {|f| instance_eval(f.read) }
48
+ end
49
+
50
+ def _path(path, value = nil, &block)
51
+ if block
52
+ value = Dslh::ScopeBlock.nest(binding, 'block')
53
+ end
54
+
55
+ @__hash__[path] = value
56
+ end
57
+ EOS
58
+ end
59
+ end
@@ -0,0 +1,132 @@
1
+ module Kumogata2::Plugin::Ruby::StringExt
2
+ def encode64
3
+ Base64.encode64(self).delete("\n")
4
+ end
5
+
6
+ def undent
7
+ min_space_num = self.split("\n").delete_if {|s| s =~ /^\s*$/ }.map {|s| (s[/^\s+/] || '').length }.min
8
+
9
+ if min_space_num and min_space_num > 0
10
+ gsub(/^[ \t]{,#{min_space_num}}/, '')
11
+ else
12
+ self
13
+ end
14
+ end
15
+
16
+ def fn_join(options = {})
17
+ options = {
18
+ :undent => true,
19
+ :trim_mode => nil,
20
+ }.merge(options)
21
+
22
+ data = self.dup
23
+ data = data.undent if options[:undent]
24
+ trim_mode = options[:trim_mode]
25
+ null = "\0"
26
+
27
+ data = Object.new.instance_eval(<<-EOS)
28
+ @__functions__ = []
29
+
30
+ @__value_conv__ = proc do |v|
31
+ case v
32
+ when Array, Hash
33
+ v
34
+ else
35
+ v.to_s
36
+ end
37
+ end
38
+
39
+ def Fn__Base64(value)
40
+ value = {'Fn::Base64' => @__value_conv__[value]}
41
+
42
+ case @__functions__
43
+ when Array
44
+ @__functions__ << value
45
+ when Hash
46
+ @__functions__.update(value)
47
+ end
48
+
49
+ #{null.inspect}
50
+ end
51
+
52
+ def Fn__FindInMap(map_name, top_level_key, second_level_key)
53
+ value = {'Fn::FindInMap' => [
54
+ map_name, top_level_key, second_level_key].map(&@__value_conv__)}
55
+
56
+ case @__functions__
57
+ when Array
58
+ @__functions__ << value
59
+ when Hash
60
+ @__functions__.update(value)
61
+ end
62
+
63
+ #{null.inspect}
64
+ end
65
+
66
+ def Fn__GetAtt(logical_name, attr_name)
67
+ value = {'Fn::GetAtt' => [
68
+ logical_name, attr_name].map(&@__value_conv__)}
69
+
70
+ case @__functions__
71
+ when Array
72
+ @__functions__ << value
73
+ when Hash
74
+ @__functions__.update(value)
75
+ end
76
+
77
+ #{null.inspect}
78
+ end
79
+
80
+ def Fn__GetAZs(region)
81
+ value = {'Fn::GetAZs' => @__value_conv__[region]}
82
+
83
+ case @__functions__
84
+ when Array
85
+ @__functions__ << value
86
+ when Hash
87
+ @__functions__.update(value)
88
+ end
89
+
90
+ #{null.inspect}
91
+ end
92
+
93
+ def Ref(value)
94
+ value = {'Ref' => value}
95
+
96
+ case @__functions__
97
+ when Array
98
+ @__functions__ << value
99
+ when Hash
100
+ @__functions__.update(value)
101
+ end
102
+
103
+ #{null.inspect}
104
+ end
105
+
106
+ def _(&block)
107
+ __functions__orig = @__functions__
108
+ @__functions__ = {}
109
+ block.call if block
110
+ value = @__functions__
111
+ @__functions__ = __functions__orig
112
+ return value
113
+ end
114
+
115
+ ERB.new(#{data.inspect}, nil, #{trim_mode.inspect}).result(binding).split(#{null.inspect}).zip(@__functions__)
116
+ EOS
117
+
118
+ data = data.flatten.select {|i| not i.nil? }.map {|i|
119
+ if i.kind_of?(String)
120
+ i.lines.to_a
121
+ else
122
+ i
123
+ end
124
+ }.flatten
125
+
126
+ return {
127
+ 'Fn::Join' => ['', data]
128
+ }
129
+ end
130
+ end
131
+
132
+ String.include(Kumogata2::Plugin::Ruby::StringExt)
@@ -0,0 +1,7 @@
1
+ module Kumogata2
2
+ module Plugin
3
+ class Ruby
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kumogata2-plugin-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - winebarrel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kumogata2
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: dslh
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
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Kumogata2 ruby plugin.
84
+ email:
85
+ - sgwr_dts@yahoo.co.jp
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - kumogata2-plugin-ruby.gemspec
100
+ - lib/kumogata2/plugin/ruby.rb
101
+ - lib/kumogata2/plugin/ruby/context.rb
102
+ - lib/kumogata2/plugin/ruby/string_ext.rb
103
+ - lib/kumogata2/plugin/ruby/version.rb
104
+ homepage: https://github.com/winebarrel/kumogata2-plugin-ruby
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Kumogata2 ruby plugin.
128
+ test_files: []