comodule 0.0.4 → 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.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +6 -0
  4. data/Gemfile +0 -2
  5. data/README.md +115 -43
  6. data/comodule.gemspec +4 -2
  7. data/copy_to_rails.sh +7 -0
  8. data/lib/comodule.rb +32 -2
  9. data/lib/comodule/config_support.rb +95 -18
  10. data/lib/comodule/deployment.rb +2 -0
  11. data/lib/comodule/deployment/base.rb +186 -0
  12. data/lib/comodule/deployment/helper.rb +9 -0
  13. data/lib/comodule/deployment/helper/aws.rb +96 -0
  14. data/lib/comodule/deployment/helper/aws/base.rb +14 -0
  15. data/lib/comodule/deployment/helper/aws/cloud_formation.rb +146 -0
  16. data/lib/comodule/deployment/helper/aws/ec2.rb +37 -0
  17. data/lib/comodule/deployment/helper/aws/rds.rb +46 -0
  18. data/lib/comodule/deployment/helper/aws/s3.rb +52 -0
  19. data/lib/comodule/deployment/helper/aws/ssl.rb +111 -0
  20. data/lib/comodule/deployment/helper/base.rb +18 -0
  21. data/lib/comodule/deployment/helper/shell_command.rb +76 -0
  22. data/lib/comodule/deployment/helper/system_utility.rb +80 -0
  23. data/lib/comodule/deployment/helper/uploader.rb +155 -0
  24. data/lib/comodule/deployment/platform.rb +156 -0
  25. data/lib/comodule/deployment/platform/default_files/.gitignore.erb +5 -0
  26. data/lib/comodule/deployment/platform/default_files/aws_config.yml.erb +2 -0
  27. data/lib/comodule/version.rb +1 -1
  28. data/lib/tasks/comodule.rake +94 -0
  29. data/spec/comodule/config_support_spec.rb +102 -0
  30. data/spec/comodule/deployment/helper/aws/cloud_formation_spec.rb +71 -0
  31. data/spec/comodule/deployment/helper/aws/s3_spec.rb +85 -0
  32. data/spec/comodule/deployment/helper/shell_command_spec.rb +176 -0
  33. data/spec/comodule/deployment/helper/system_utility_spec.rb +250 -0
  34. data/spec/comodule/deployment/helper/uploader_spec.rb +274 -0
  35. data/spec/comodule/deployment/platform_spec.rb +310 -0
  36. data/spec/comodule_spec.rb +1 -1
  37. data/spec/rails/experiment/.gitignore +16 -0
  38. data/spec/rails/experiment/Gemfile +43 -0
  39. data/spec/rails/experiment/README.rdoc +28 -0
  40. data/spec/rails/experiment/Rakefile +6 -0
  41. data/spec/rails/experiment/app/assets/images/.keep +0 -0
  42. data/spec/rails/experiment/app/assets/javascripts/application.js +16 -0
  43. data/spec/rails/experiment/app/assets/javascripts/experiences.js.coffee +3 -0
  44. data/spec/rails/experiment/app/assets/stylesheets/application.css +15 -0
  45. data/spec/rails/experiment/app/assets/stylesheets/experiences.css.scss +3 -0
  46. data/spec/rails/experiment/app/assets/stylesheets/scaffolds.css.scss +69 -0
  47. data/spec/rails/experiment/app/controllers/application_controller.rb +5 -0
  48. data/spec/rails/experiment/app/controllers/concerns/.keep +0 -0
  49. data/spec/rails/experiment/app/controllers/experiences_controller.rb +74 -0
  50. data/spec/rails/experiment/app/helpers/application_helper.rb +2 -0
  51. data/spec/rails/experiment/app/helpers/experiences_helper.rb +2 -0
  52. data/spec/rails/experiment/app/mailers/.keep +0 -0
  53. data/spec/rails/experiment/app/models/.keep +0 -0
  54. data/spec/rails/experiment/app/models/concerns/.keep +0 -0
  55. data/spec/rails/experiment/app/models/experience.rb +2 -0
  56. data/spec/rails/experiment/app/views/experiences/_form.html.slim +12 -0
  57. data/spec/rails/experiment/app/views/experiences/edit.html.slim +8 -0
  58. data/spec/rails/experiment/app/views/experiences/index.html.slim +21 -0
  59. data/spec/rails/experiment/app/views/experiences/index.json.jbuilder +4 -0
  60. data/spec/rails/experiment/app/views/experiences/new.html.slim +5 -0
  61. data/spec/rails/experiment/app/views/experiences/show.html.slim +9 -0
  62. data/spec/rails/experiment/app/views/experiences/show.json.jbuilder +1 -0
  63. data/spec/rails/experiment/app/views/layouts/application.html.erb +14 -0
  64. data/spec/rails/experiment/bin/bundle +3 -0
  65. data/spec/rails/experiment/bin/rails +8 -0
  66. data/spec/rails/experiment/bin/rake +8 -0
  67. data/spec/rails/experiment/bin/spring +18 -0
  68. data/spec/rails/experiment/config.ru +4 -0
  69. data/spec/rails/experiment/config/application.rb +23 -0
  70. data/spec/rails/experiment/config/boot.rb +4 -0
  71. data/spec/rails/experiment/config/database.yml +25 -0
  72. data/spec/rails/experiment/config/environment.rb +5 -0
  73. data/spec/rails/experiment/config/environments/development.rb +37 -0
  74. data/spec/rails/experiment/config/environments/production.rb +78 -0
  75. data/spec/rails/experiment/config/environments/test.rb +39 -0
  76. data/spec/rails/experiment/config/experiment_unicorn.rb +104 -0
  77. data/spec/rails/experiment/config/initializers/assets.rb +8 -0
  78. data/spec/rails/experiment/config/initializers/backtrace_silencers.rb +7 -0
  79. data/spec/rails/experiment/config/initializers/cookies_serializer.rb +3 -0
  80. data/spec/rails/experiment/config/initializers/filter_parameter_logging.rb +4 -0
  81. data/spec/rails/experiment/config/initializers/inflections.rb +16 -0
  82. data/spec/rails/experiment/config/initializers/mime_types.rb +4 -0
  83. data/spec/rails/experiment/config/initializers/session_store.rb +3 -0
  84. data/spec/rails/experiment/config/initializers/wrap_parameters.rb +14 -0
  85. data/spec/rails/experiment/config/locales/en.yml +23 -0
  86. data/spec/rails/experiment/config/routes.rb +59 -0
  87. data/spec/rails/experiment/config/secrets.yml +22 -0
  88. data/spec/rails/experiment/db/migrate/20141004050218_create_experiences.rb +9 -0
  89. data/spec/rails/experiment/db/schema.rb +22 -0
  90. data/spec/rails/experiment/db/seeds.rb +7 -0
  91. data/spec/rails/experiment/lib/assets/.keep +0 -0
  92. data/spec/rails/experiment/lib/tasks/.keep +0 -0
  93. data/spec/rails/experiment/log/.keep +0 -0
  94. data/spec/rails/experiment/platform/.gitignore +5 -0
  95. data/spec/rails/experiment/platform/ami/cloud_formation/.keep +0 -0
  96. data/spec/rails/experiment/platform/ami/cloud_formation/template.json.erb +138 -0
  97. data/spec/rails/experiment/platform/ami/config.yml +5 -0
  98. data/spec/rails/experiment/platform/ami/config/.keep +0 -0
  99. data/spec/rails/experiment/platform/cloud_formation/.keep +0 -0
  100. data/spec/rails/experiment/platform/config.yml +18 -0
  101. data/spec/rails/experiment/platform/config/.keep +0 -0
  102. data/spec/rails/experiment/platform/config/nginx/conf.d/default.conf.erb +46 -0
  103. data/spec/rails/experiment/platform/config/nginx/nginx.conf.erb +53 -0
  104. data/spec/rails/experiment/platform/deployment/cloud_formation/.keep +0 -0
  105. data/spec/rails/experiment/platform/deployment/cloud_formation/template.json.erb +107 -0
  106. data/spec/rails/experiment/platform/deployment/config.yml +16 -0
  107. data/spec/rails/experiment/platform/deployment/config/.keep +0 -0
  108. data/spec/rails/experiment/public/404.html +67 -0
  109. data/spec/rails/experiment/public/422.html +67 -0
  110. data/spec/rails/experiment/public/500.html +66 -0
  111. data/spec/rails/experiment/public/favicon.ico +0 -0
  112. data/spec/rails/experiment/public/robots.txt +5 -0
  113. data/spec/rails/experiment/test/controllers/.keep +0 -0
  114. data/spec/rails/experiment/test/controllers/experiences_controller_test.rb +49 -0
  115. data/spec/rails/experiment/test/fixtures/.keep +0 -0
  116. data/spec/rails/experiment/test/fixtures/experiences.yml +7 -0
  117. data/spec/rails/experiment/test/helpers/.keep +0 -0
  118. data/spec/rails/experiment/test/helpers/experiences_helper_test.rb +4 -0
  119. data/spec/rails/experiment/test/integration/.keep +0 -0
  120. data/spec/rails/experiment/test/mailers/.keep +0 -0
  121. data/spec/rails/experiment/test/models/.keep +0 -0
  122. data/spec/rails/experiment/test/models/experience_test.rb +7 -0
  123. data/spec/rails/experiment/test/test_helper.rb +10 -0
  124. data/spec/rails/experiment/vendor/assets/javascripts/.keep +0 -0
  125. data/spec/rails/experiment/vendor/assets/stylesheets/.keep +0 -0
  126. data/spec/spec_helper.rb +3 -0
  127. data/spec/support/deployment/helper/shell_command_support.rb +34 -0
  128. data/spec/support/deployment/helper/uploader_support.rb +21 -0
  129. data/spec/support/deployment/platform_support.rb +32 -0
  130. metadata +262 -21
  131. data/lib/comodule/customize_class/hash_custom.rb +0 -20
  132. data/spec/comodule/customize_class/hash_custom_spec.rb +0 -32
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fda5e5972ccfc052cf2bd83e2b68dbf65969fd3
4
- data.tar.gz: cecaed5a265d776050d1cd107fe429eaa1482795
3
+ metadata.gz: 23d9fa321c17d22373381ce0242591cd400c8172
4
+ data.tar.gz: b31138a32bce6be47ef27abda6507a51f3a32c55
5
5
  SHA512:
6
- metadata.gz: 83a663b9a20534b3e7020516a89634e3d433306e9da169e99fe9e68a33553bade483e0949234cd5f3ce3ad87d4462bed0a6a3632e1d0562f75935c9711727ad0
7
- data.tar.gz: 42b180d2dfda6868fb9cecac6ddae4afeb6720abe8b3f3b150bd75ec2d0a0b797f10ddfa076f7b3a318c5cc3fc61e30bdf63d1c41544fb79548d7a31ce6fa624
6
+ metadata.gz: 0f117e97feb9fe3ee562e7de964eb4d8268a3ed2dd04b607d71622e4aa692e6e91c6430f2e5b461ef14e538a23db559b7710f4ce5ba817bb4c304cdb76d77352
7
+ data.tar.gz: f763340017a7d42c297d3dccfae559c8a5f11cb0270e76edc2896ad682282e1d68d9fac7db762f1f9875ec821cf27e9c29825f7ae22d84da526e6027f9c25673
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ spec/rails/experiment/vendor/gems
@@ -0,0 +1,6 @@
1
+ ## 0.1.0 (2014-10-08)
2
+
3
+ - Features
4
+
5
+ - Implemented rake tasks
6
+ - Add a rails project for tests
data/Gemfile CHANGED
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in comodule.gemspec
4
4
  gemspec
5
-
6
- gem 'rails', '~> 4.0.0'
data/README.md CHANGED
@@ -6,20 +6,78 @@ comodule
6
6
  ## 概要
7
7
 
8
8
  * モジュール
9
+ * Deployment
9
10
  * ConfigSupport
10
11
  * UniArray
11
12
 
12
13
  * クラスの拡張
13
- * Hash
14
- * pullout, pullout!
15
14
  * String
16
15
  * standardize
17
16
 
18
17
  ## モジュール
19
18
 
19
+ ### Deployment
20
+
21
+ AWS CloudFormation を使って、AWS 上へのデプロイを容易にします。
22
+ `Rails.root` の直下にデプロイに必要なファイルを格納する `platform` ディレクトリを作り、構築する環境の名前のディレクトリをその中に作ります。ここでは、`staging` とします。
23
+
24
+ ```ruby
25
+ platform = Comodule::Deployment::Platfrom.new 'staging'
26
+
27
+ # ローカルのソースを S3 に acl: :private でアップする。
28
+ platform.archive_repository
29
+ platform.upload_archive
30
+
31
+ # スタックの作成
32
+ platform.create_stack
33
+ ```
34
+
35
+ これで `platform/cloud_formation/template.json.erb` を使って CloudFormation のスタックを作成します。この際、変数 `config` で `config.yml` または `secret_config.yml` で設定した項目にアクセスできます。
36
+ 例えば、
37
+
38
+ ```yaml
39
+ stack_name_prefix: trial
40
+ application: &application Trial
41
+ project_root: !str /ec2-user/projects
42
+ rails_root: !str /ec2-user/projects/trial
43
+
44
+ ec2_instance:
45
+ instance_type: m3.medium
46
+ ami: ami-a1bec3a0
47
+ name: *application
48
+ key_name: ssh_key
49
+ iam_role: dev_master
50
+ security_group: aa-1a2b3c4d
51
+
52
+ aws_access_credentials:
53
+ cloud_formation:
54
+ region: ap-northeast-1
55
+ ```
56
+
57
+ ```json
58
+ {
59
+ "AWSTemplateFormatVersion": "2010-09-09",
60
+ "Description": "It creates a Rails stack.",
61
+
62
+ "Resources": {
63
+ "System": {
64
+ "Type": "AWS::EC2::Instance",
65
+ "Properties": {
66
+ "InstanceType": "<%= config.ec2_instance.instance_type %>",
67
+ "ImageId": "<%= config.ec2_instance.ami %>",
68
+ "KeyName": "<%= config.ec2_instance.key_name %>",
69
+ "IamInstanceProfile": { "Ref": "InstanceProfile" },
70
+ "SecurityGroupIds": ["<%= config.ec2_instance.security_group %>"],
71
+ "Tags": [{"Key": "Name", "Value": "<%= config.ec2_instance.name %>"}],
72
+ }
73
+ }
74
+ }
75
+ }
76
+ ```
77
+
20
78
  ### ConfigSupport
21
79
 
22
- 定義なしにアトリビュートを保存できる初期情報などを扱うのに適した汎用オブジェクト。
80
+ 定義なしにアトリビュートを保存できる初期情報などを扱うのに適した汎用オブジェクトです。
23
81
 
24
82
  ```ruby
25
83
  config = Comodule::ConfigSupport::Config.new
@@ -33,6 +91,15 @@ config.port
33
91
  # => "3000"
34
92
  ```
35
93
 
94
+ 但し、あくまでも `Object` のサブクラスなので、`Object#methods`, `Object#protected_methods`, `Object#private_methods` に含まれる名前のディレクティブを作ることはできません。
95
+
96
+ ```ruby
97
+ config = Comodule::ConfigSupport::Config.new(
98
+ system: 'app_name'
99
+ )
100
+ # => ArgumentError
101
+ ```
102
+
36
103
  デフォルトでは、設定されていないディレクティブは nil を返します。
37
104
 
38
105
  ```ruby
@@ -103,6 +170,51 @@ config.to_hash
103
170
  # => {:host=>"example.com", :port=>"3000"}
104
171
  ```
105
172
 
173
+ `#merge` で二つの `Config` オブジェクトをマージできます。`#+()` はそのエイリアスです。マージは再帰的に行われます。
174
+
175
+ ```ruby
176
+ config = Comodule::ConfigSupport::Config.new(
177
+ host: 'example.com',
178
+ db: {
179
+ host: 'rds',
180
+ database: 'app_development',
181
+ username: 'ec2-user',
182
+ schedule: {
183
+ boot: '08-00-00',
184
+ }
185
+ }
186
+ )
187
+
188
+ config2 = Comodule::ConfigSupport::Config.new(
189
+ port: '3000',
190
+ db: {
191
+ host: 'rds',
192
+ password: 'secret',
193
+ schedule: {
194
+ shutdown: '22-00-00'
195
+ }
196
+ }
197
+ )
198
+
199
+ config = config1 + config2
200
+
201
+ config.to_hash
202
+ # => {
203
+ host: 'example.com',
204
+ port: '3000',
205
+ db: {
206
+ host: 'rds',
207
+ database: 'app_development',
208
+ username: 'ec2-user',
209
+ password: 'secret',
210
+ schedule: {
211
+ boot: '08-00-00',
212
+ shutdown: '22-00-00'
213
+ }
214
+ }
215
+ }
216
+ ```
217
+
106
218
  ### UniArray
107
219
 
108
220
  要素の重複をさせない `Array` のサブクラス。スニペットと言っていいくらい簡単なコードで出来ています。
@@ -160,46 +272,6 @@ Comodule::CustomizeClass.customize
160
272
  として、読み込みます。このメソッドは複数回呼び出すとエラーになりますので、気をつけてください。
161
273
  尚、Comodule は既存のメソッドをオーバーライドできません。
162
274
 
163
- ### Hash
164
-
165
- #### pullout, pullout!
166
-
167
- ハッシュから指定要素を抜き出して新しいハッシュを返す。
168
-
169
- ```ruby
170
- hsh = {
171
- date: "20130322",
172
- title: "The Tell-Tale Brain",
173
- secret_token: "it-is-secret"
174
- }
175
- result_hsh = hsh.pullout(:date, :title)
176
- # => {date: "20130322", title: "The Tell-Tale Brain"}
177
- ```
178
-
179
- デフォルトでは未定義の要素は無視します。
180
-
181
- ```ruby
182
- hsh = {
183
- date: "20130322",
184
- title: "The Tell-Tale Brain",
185
- secret_token: "it-is-secret"
186
- }
187
- result_hsh = hsh.pullout(:date, :title, :author)
188
- # => {date: "20130322", title: "The Tell-Tale Brain"}
189
- ```
190
-
191
- 未定義要素へのアクセス時に例外を挙げたいときは `pullout!` を使います。
192
-
193
- ```ruby
194
- hsh = {
195
- date: "20130322",
196
- title: "The Tell-Tale Brain",
197
- secret_token: "it-is-secret"
198
- }
199
- result_hsh = hsh.pullout!(:date, :title, :author)
200
- # => ArgumentError: Comodule::CustomizeClass::HashCustom cannot find key 'author' is a Symbol.
201
- ```
202
-
203
275
  ### String
204
276
 
205
277
  #### standardize
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_runtime_dependency "aws-sdk", "~> 1.54"
25
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
24
26
  end
@@ -0,0 +1,7 @@
1
+ # /bin/bash
2
+
3
+ rm -rf spec/rails/experiment/vendor/gems/comodule
4
+
5
+ mkdir -p spec/rails/experiment/vendor/gems/comodule/
6
+
7
+ ls -a | grep -v -E '^copy_to_rails.sh$|[.]DS_Store$|^[.]$|^[.][.]$|^spec$|^[.]git$' | xargs -I{} cp -R {} spec/rails/experiment/vendor/gems/comodule/
@@ -1,13 +1,43 @@
1
1
  require "comodule/version"
2
- require "rails"
2
+ require 'active_support/all'
3
3
 
4
4
  module Comodule
5
+
6
+ if defined?(Rails)
7
+ class Railtie < Rails::Railtie
8
+ rake_tasks { load "tasks/comodule.rake" }
9
+ end
10
+ end
11
+
5
12
  autoload :UniArray, 'comodule/uni_array'
6
13
  autoload :ConfigSupport, 'comodule/config_support'
7
14
  autoload :CustomizeClass, 'comodule/customize_class'
15
+ autoload :Deployment, 'comodule/deployment'
8
16
 
9
17
  module CustomizeClass
10
- autoload :HashCustom, 'comodule/customize_class/hash_custom'
11
18
  autoload :StringCustom, 'comodule/customize_class/string_custom'
12
19
  end
20
+
21
+ module Deployment
22
+ autoload :Base, 'comodule/deployment/base'
23
+ autoload :Platform, 'comodule/deployment/platform'
24
+ autoload :Helper, 'comodule/deployment/helper'
25
+
26
+ module Helper
27
+ autoload :Base, 'comodule/deployment/helper/base'
28
+ autoload :SystemUtility, 'comodule/deployment/helper/system_utility'
29
+ autoload :Aws, 'comodule/deployment/helper/aws'
30
+ autoload :ShellCommand, 'comodule/deployment/helper/shell_command'
31
+ autoload :Uploader, 'comodule/deployment/helper/uploader'
32
+
33
+ module Aws
34
+ autoload :Base, 'comodule/deployment/helper/aws/base'
35
+ autoload :S3, 'comodule/deployment/helper/aws/s3'
36
+ autoload :CloudFormation, 'comodule/deployment/helper/aws/cloud_formation'
37
+ autoload :Ec2, 'comodule/deployment/helper/aws/ec2'
38
+ autoload :Ssl, 'comodule/deployment/helper/aws/ssl'
39
+ autoload :Rds, 'comodule/deployment/helper/aws/rds'
40
+ end
41
+ end
42
+ end
13
43
  end
@@ -1,51 +1,128 @@
1
1
  module Comodule::ConfigSupport
2
2
  class Config
3
+ class << self
4
+ def original_methods
5
+ return @original_methods if @original_methods
6
+
7
+ @original_methods = public_instance_methods
8
+ @original_methods += protected_instance_methods
9
+ @original_methods += private_instance_methods
10
+ end
11
+
12
+ def combine(config1, config2)
13
+ new_obj = new
14
+
15
+ config1.each do |key, value|
16
+ new_obj[key] = value
17
+ end
18
+
19
+ config2.each do |key, value|
20
+ if self === new_obj[key] && self === value
21
+ new_obj[key] = combine(new_obj[key], value)
22
+ else
23
+ new_obj[key] = value
24
+ end
25
+ end
26
+
27
+ new_obj
28
+ end
29
+ end
30
+
3
31
  def initialize(config_hash={})
4
32
  config_hash[:configure_type] ||= :soft
33
+
5
34
  config_hash.each do |directive, value|
6
35
  value = value.to_sym if directive == :configure_type
36
+
7
37
  if Hash === value
8
38
  value[:configure_type] ||= config_hash[:configure_type]
9
39
  value = self.class.new(value)
10
40
  end
11
- instance_variable_set "@#{directive}", value
41
+
42
+ set_directive directive, value
43
+ end
44
+
45
+ if block_given?
46
+ yield self
12
47
  end
13
48
  end
14
49
 
15
50
  def method_missing(directive, arg=nil)
16
51
  if directive =~ /^(.+)=/
17
- arg = arg.to_sym if directive == :configure_type
18
- if Hash === arg
19
- arg[:configure_type] ||= configure_type
20
- arg = self.class.new(arg)
21
- end
22
- arg = self.class.new(arg) if Hash === arg
23
- return instance_variable_set("@#{$1}", arg)
24
- end
25
- value = instance_variable_get("@#{directive}")
26
- if @configure_type == :hard && !value
27
- raise ArgumentError, "Comodule::ConfigSupport::Config is missing this directive [#{directive}]."
52
+ set_directive($1, arg)
53
+ else
54
+ get_directive(directive)
28
55
  end
29
- value
30
56
  end
31
57
 
32
58
  def [](directive)
33
- send(directive)
59
+ get_directive(directive)
34
60
  end
35
61
 
36
62
  def []=(directive, arg)
37
- send("#{directive}=", arg)
63
+ set_directive(directive, arg)
38
64
  end
39
65
 
40
- def to_hash
41
- hsh = {}
66
+ def merge(other_config)
67
+ self.class.combine(self, other_config)
68
+ end
69
+
70
+ alias + merge
71
+
72
+ def each
42
73
  instance_variables.each do |variable_name|
43
74
  next if variable_name == :@configure_type
75
+
44
76
  key = variable_name.to_s.sub(/@/, '').to_sym
45
- hsh[key] = instance_variable_get(variable_name)
77
+ value = instance_variable_get(variable_name)
78
+
79
+ yield key, value
80
+ end
81
+ end
82
+
83
+ def to_hash
84
+ hsh = {}
85
+ each do |key, value|
86
+ value = value.to_hash if self.class === value
87
+ hsh[key] = value
46
88
  end
47
89
  hsh
48
90
  end
91
+
92
+ def slice(*keys)
93
+ keys.inject(self.class.new) do |new_obj, key|
94
+ new_obj[key] = self[key] if self[key]
95
+ new_obj
96
+ end
97
+ end
98
+
99
+
100
+ private
101
+
102
+ def get_directive(directive)
103
+ value = instance_variable_get("@#{directive}")
104
+ if @configure_type == :hard && !value
105
+ raise ArgumentError, "Comodule::ConfigSupport::Config is missing this directive [#{directive}]."
106
+ end
107
+ value
108
+ end
109
+
110
+ def set_directive(directive, arg)
111
+ directive = directive.to_sym
112
+
113
+ if self.class.original_methods.member?(directive)
114
+ raise ArgumentError, "You cannot use the directive [#{directive}= #{arg}] same as a member of Object#methods."
115
+ end
116
+
117
+ arg = arg.to_sym if directive == :configure_type
118
+
119
+ if Hash === arg
120
+ arg[:configure_type] ||= configure_type
121
+ arg = self.class.new(arg)
122
+ end
123
+
124
+ instance_variable_set("@#{directive}", arg)
125
+ end
49
126
  end
50
127
 
51
128
 
@@ -0,0 +1,2 @@
1
+ module Comodule::Deployment
2
+ end
@@ -0,0 +1,186 @@
1
+ module Comodule::Deployment::Base
2
+
3
+ def self.included(receiver)
4
+ unless receiver < ::Comodule::Deployment::Helper::SystemUtility
5
+ receiver.send :include, ::Comodule::Deployment::Helper::SystemUtility
6
+ end
7
+ receiver.send :include, InstanceMethods
8
+ end
9
+
10
+ module InstanceMethods
11
+
12
+ def config
13
+ return @config if @config
14
+
15
+ @config = ::Comodule::ConfigSupport::Config.new
16
+
17
+ @config += yaml_to_config(aws_config_path) if File.file?(aws_config_path)
18
+ @config += yaml_to_config(common_config_path) if File.file?(common_config_path)
19
+ @config += yaml_to_config(config_path)
20
+
21
+ @config += yaml_to_config(common_secret_config_path) if File.file?(common_secret_config_path)
22
+ @config += yaml_to_config(secret_config_path) if File.file?(secret_config_path)
23
+
24
+ if @config.config_files
25
+ @config.config_files.each do |extend_path|
26
+ path = File.join(platform_root, extend_path)
27
+ @config += yaml_to_config(path) if File.file?(path)
28
+ end
29
+ end
30
+
31
+ @config.platform_name = @name
32
+
33
+ @config
34
+ end
35
+
36
+ def config_copy
37
+ return unless config.cp
38
+
39
+ rm_rf File.join(test_dir, 'file_copy') if test?
40
+
41
+ count = 0
42
+
43
+ count += file_copy(common_config_dir)
44
+ count += file_copy(common_secret_config_dir)
45
+ count += file_copy(config_dir)
46
+ count += file_copy(secret_config_dir)
47
+
48
+ return count
49
+ end
50
+
51
+ def file_copy(dir)
52
+ count = 0
53
+
54
+ paths = Dir.glob(File.join(dir, '**', '*'))
55
+
56
+ order = config.cp.to_hash
57
+
58
+ order.each do |key, path_head_list|
59
+
60
+ [path_head_list].flatten.each do |path_head|
61
+ wanted = %r|^#{File.join(dir, key.to_s)}|
62
+
63
+ paths.each do |file_path|
64
+ next unless File.file?(file_path)
65
+ next unless file_path =~ wanted
66
+
67
+ path_tail = file_path.sub(wanted, '')
68
+
69
+ path = File.join(path_head, path_tail)
70
+ path = File.join(test_dir, 'file_copy', path) if test?
71
+
72
+ dirname, filename = File.split(path)
73
+
74
+ be_dir(dirname)
75
+
76
+ if file_path =~ /\.erb$/
77
+ File.open(path.sub(/\.erb$/, ''), 'w') do |file|
78
+ file.write render(file_path)
79
+ end
80
+ else
81
+ FileUtils.cp file_path, "#{dirname}/"
82
+ end
83
+
84
+ count += 1
85
+ end
86
+ end
87
+ end
88
+
89
+ count
90
+ end
91
+
92
+ def aws_config_path
93
+ @aws_config_path ||= File.join(platform_root, 'aws_config.yml')
94
+ end
95
+
96
+ def config_dir
97
+ @config_dir ||= File.join(platform_dir, 'config')
98
+ end
99
+
100
+ def secret_config_dir
101
+ @secret_config_dir ||= File.join(platform_dir, 'secret_config')
102
+ end
103
+
104
+ def common_config_dir
105
+ @common_config_dir ||= File.join(platform_root, 'config')
106
+ end
107
+
108
+ def common_secret_config_dir
109
+ @common_secret_config_dir ||= File.join(platform_root, 'secret_config')
110
+ end
111
+
112
+ def common_config_path
113
+ @common_config_path ||= File.join(platform_root, 'config.yml')
114
+ end
115
+
116
+ def common_secret_config_path
117
+ @common_secret_config_path ||= File.join(platform_root, 'secret_config.yml')
118
+ end
119
+
120
+ def config_path
121
+ @config_path ||= File.join(platform_dir, 'config.yml')
122
+ end
123
+
124
+ def secret_config_path
125
+ @secret_config_path ||= File.join(platform_dir, 'secret_config.yml')
126
+ end
127
+
128
+ def tmp_dir
129
+ @tmp_dir ||= be_dir(File.join(platform_dir, 'tmp'))
130
+ end
131
+
132
+ def test_dir
133
+ @test_dir ||= be_dir(File.join(platform_dir, 'test'))
134
+ end
135
+
136
+ def tmp_projects_dir
137
+ @tmp_projects_dir ||= be_dir(File.join(tmp_dir, 'projects'))
138
+ end
139
+
140
+ def tmp_project_dir
141
+ @tmp_project_dir ||= be_dir(File.join(tmp_projects_dir, project_name))
142
+ end
143
+
144
+ def project_name
145
+ File.basename project_root
146
+ end
147
+
148
+ def git_dir
149
+ @git_dir ||= File.join(project_root, '.git')
150
+ end
151
+
152
+ def file_path(*path)
153
+ path = File.join(*path)
154
+ if path =~ %r|^platform/(.*)|
155
+ File.join(platform_root, $1)
156
+ else
157
+ File.join(platform_dir, path)
158
+ end
159
+ end
160
+
161
+ def platform_dir
162
+ @platform_dir ||= File.join(platform_root, name)
163
+ end
164
+
165
+ def name
166
+ @name
167
+ end
168
+
169
+ def project_name
170
+ @project_name ||= File.basename(project_root)
171
+ end
172
+
173
+ def project_root
174
+ @project_root ||=
175
+ if defined?(Rails)
176
+ Rails.root
177
+ else
178
+ Dir.getwd
179
+ end
180
+ end
181
+
182
+ def platform_root
183
+ @platform_root ||= File.join(project_root, 'platform')
184
+ end
185
+ end
186
+ end