itamae-mitsurin 0.2 → 0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98af16ce485602c42d6c655f74254a1aea88e5bc
4
- data.tar.gz: 418060a72f4afda2a17d5225e414e964b40fbc65
3
+ metadata.gz: 501496a4a667232eeeb4020024fb96d25dac514a
4
+ data.tar.gz: bfb410645c364d84f975ecc1e4aae10934920355
5
5
  SHA512:
6
- metadata.gz: 117abcedca47117aeb52ec74c6498d3555989ecfc143d58926040d9d9ceee656136b8fc682799b53b79b88f6c344b46f538f99e4869a633bf669bb54f03e5f6e
7
- data.tar.gz: 9559586324ed153975bc6810d563e5d7b93fe39bb608f0e728e1a70ff6c42d3ebe7ddfeb8f8bdda611eec6b8c9d035fdf53caf6dfba0652919ddc265dd919f5c
6
+ metadata.gz: af73678cf92dbc16f8353fe3635921ded6fa3ce1b622535983032a77e71b343285208a34636aa02080c59f8e3b30a812739835c72bbe505366d7db23889a3a78
7
+ data.tar.gz: 07f48d7d5393560061191d51dd7d079372c46a7a35344cf14671d81e904b57d76d01a0faec01a934affba32aaab9c9c70fd786f00486b1805984ada978837675
data/README.md CHANGED
@@ -4,16 +4,17 @@ Customized version of Itamae and plugin
4
4
 
5
5
  ## Concept
6
6
 
7
- - More like Chef
8
- - Minimum attributes
7
+ - Like more Chef
8
+ - Little attributes
9
9
  - Support AWS Resource
10
- - Require Rake
11
10
 
12
11
  ## Installation
13
12
 
14
13
  ```
15
14
  $ gem install itamae-mitsurin
16
- $ itamae-mitsurin init [project_name]
15
+ $ mkdir project_dir
16
+ $ cd project_dir
17
+ $ itamae-mitsurin init
17
18
  ```
18
19
 
19
20
  ## Usage AWS Resource
@@ -23,13 +24,13 @@ $ itamae-mitsurin init [project_name]
23
24
 
24
25
  Aws.config[:region] = 'ap-northeast-1'
25
26
 
26
- aws_ebs_volume 'db_ebs_volume' do
27
+ aws_ebs_volume "ebs_name" do
27
28
  action [:create, :attach]
28
- availability_zone 'ap-northeast-1b'
29
- device '/dev/xvdf'
29
+ availability_zone "ap-northeast-1a"
30
+ device '/dev/xvdb'
30
31
  volume_type 'standard'
31
- size 30
32
- instance_id node['ec2']['instance-id']
32
+ size 10
33
+ instance_id 'i-xxxxxxx'
33
34
  end
34
35
  ```
35
36
 
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "itamae-mitsurin"
8
8
  spec.version = Itamae::Mitsurin::VERSION
9
9
  spec.authors = ["Akihiro Kamiyama"]
10
- spec.email = ["kamiyama@qualitia.co.jp"]
10
+ spec.email = ["akihiro.vamps@gmail.com"]
11
11
  spec.summary = %q{Customized version of Itamae and Itamae plugin}
12
12
  spec.homepage = "https://github.com/kammy1231/itamae-mitsurin"
13
13
  spec.license = "MIT"
@@ -24,9 +24,9 @@ Gem::Specification.new do |spec|
24
24
  spec.add_runtime_dependency "schash", "~> 0.1.0"
25
25
  spec.add_runtime_dependency "aws-sdk", "~> 2"
26
26
  spec.add_runtime_dependency "serverspec", [">= 2.30", "< 3.0.0"]
27
+ spec.add_runtime_dependency "rake"
27
28
 
28
29
  spec.add_development_dependency "bundler", "~> 1.10"
29
- spec.add_development_dependency "rake"
30
30
  spec.add_development_dependency "rspec", "~> 3.0"
31
31
  spec.add_development_dependency "pry-byebug"
32
32
  spec.add_development_dependency "docker-api", "~> 1.20"
@@ -1,9 +1,10 @@
1
1
  require 'itamae/mitsurin/version'
2
2
  require 'itamae/mitsurin/cli'
3
3
  require 'itamae/mitsurin/creators'
4
+ require 'aws-sdk'
4
5
  #require 'itamae/mitsurin/itamae_task'
5
6
  #require 'itamae/mitsurin/serverspec_task'
6
- require 'aws-sdk'
7
+ #require 'itamae/mitsurin/itamae_with_git_task'
7
8
 
8
9
 
9
10
  module Itamae
@@ -7,7 +7,7 @@ module Itamae
7
7
  class CLI < Thor
8
8
  CREATE_TARGETS = %w[ cookbook ].freeze
9
9
 
10
- class_option :with_git, type: :string, aliases: ['-g']
10
+ # class_option :with_git, type: :string, aliases: ['-g']
11
11
 
12
12
  desc "version", "Print version"
13
13
  def version
@@ -3,6 +3,7 @@
3
3
  "recipe[_base]"
4
4
  ],
5
5
  "environments": {
6
+ "set": "sample",
6
7
  "hostname": "sample01.localhost"
7
8
  }
8
9
  }
@@ -8,7 +8,6 @@ module Itamae
8
8
  class ItamaeTask
9
9
 
10
10
  class << self
11
-
12
11
  class ::Hash
13
12
  def deep_merge(other)
14
13
  merger = lambda {|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2}
@@ -74,8 +73,8 @@ module Itamae
74
73
 
75
74
  bname = File.basename(node_file, '.json')
76
75
  node_h = JSON.parse(File.read(node_file), symbolize_names: true)
77
- desc "Run to #{bname}"
78
76
 
77
+ desc "Itamae to #{bname}"
79
78
  task node_h[:environments][:hostname].split(".")[0] do
80
79
  begin
81
80
  recipes = []
@@ -184,16 +183,14 @@ module Itamae
184
183
  command_recipe.each {|c_recipe| run_list_noti << c_recipe.split("/") [2]}
185
184
  color.echos(:green, %!Run List to \"#{run_list_noti.uniq.join(", ")}\"!)
186
185
  puts color.echos(:white, %!#{command}!)
187
- stat = system command
188
- if ! stat then
189
- exit(false)
190
- end
186
+ st = system command
187
+ exit 1 unless st
191
188
  end
192
189
 
193
190
  end
194
191
  end
195
-
196
192
  end
193
+
197
194
  end
198
195
  end
199
196
  end
@@ -0,0 +1,213 @@
1
+ require 'specinfra'
2
+ require 'rake'
3
+ require 'json'
4
+ require 'simple_color'
5
+ require 'specinfra/helper/set'
6
+ include Specinfra::Helper::Set
7
+ include Rake::DSL if defined? Rake::DSL
8
+
9
+ module Itamae
10
+ module Mitsurin
11
+ class ItamaeTask
12
+
13
+ class << self
14
+ class ::Hash
15
+ def deep_merge(other)
16
+ merger = lambda {|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2}
17
+ self.merge(other, &merger)
18
+ end
19
+
20
+ def deep_merge!(other)
21
+ merger = lambda {|key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2}
22
+ self.merge!(other, &merger)
23
+ end
24
+ end
25
+
26
+ def self.get_roles(node_file)
27
+ roles = []
28
+ JSON.parse(File.read(node_file))['run_list'].each do |role|
29
+ roles << role.gsub(/role\[(.+)\]/, '\1') if /role\[(.+)\]/ === role
30
+ end
31
+ roles
32
+ end
33
+
34
+ def self.get_recipes(role)
35
+ recipes = []
36
+ JSON.parse(File.read("roles/#{role}.json"))['run_list'].each do |recipe|
37
+ if /recipe\[(.+)::(.+)\]/ === recipe
38
+ recipes << {recipe.gsub(/recipe\[(.+)::(.+)\]/, '\1') => recipe.gsub(/recipe\[(.+)::(.+)\]/, '\2')}
39
+ else
40
+ recipes << {recipe.gsub(/recipe\[(.+)\]/, '\1') => nil}
41
+ end
42
+ end
43
+ recipes
44
+ end
45
+
46
+ def self.get_node_recipes(node_file)
47
+ recipes = []
48
+ JSON.parse(File.read(node_file))['run_list'].each do |recipe|
49
+ if /recipe\[(.+)::(.+)\]/ === recipe
50
+ recipes << {recipe.gsub(/recipe\[(.+)::(.+)\]/, '\1') => recipe.gsub(/recipe\[(.+)::(.+)\]/, '\2')}
51
+ else
52
+ recipes << {recipe.gsub(/recipe\[(.+)\]/, '\1') => nil} unless /role\[(.+)\]/ === recipe
53
+ end
54
+ end
55
+ recipes
56
+ end
57
+
58
+ def self.jq(*objs)
59
+ par = nil
60
+ objs.each {|obj| par = JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)}
61
+ return par
62
+ end
63
+
64
+ def self.write_json(filename)
65
+ File.open "tmp-nodes/#{filename}.json", 'w' do |f|
66
+ f.flock File::LOCK_EX
67
+ yield f
68
+ f.flock File::LOCK_UN
69
+ end
70
+ end
71
+
72
+ color = SimpleColor.new
73
+
74
+ set :backend, :exec
75
+
76
+ namespace :itamae do
77
+ branches = Specinfra.backend.run_command('git branch')
78
+ branch = branches.stdout.split("\n").select{|a| /\*/ === a }
79
+ branch = branch.pop.gsub(/\* (.+)/, '\1')
80
+ if branch == 'staging'
81
+ branch = 'staging/**'
82
+ elsif branch == 'master'
83
+ branch = 'production/**'
84
+ else
85
+ branch = 'other/**'
86
+ end
87
+ end
88
+
89
+ Dir.glob("nodes/#{branch}/*.json").each do |node_file|
90
+
91
+ bname = File.basename(node_file, '.json')
92
+ node_h = JSON.parse(File.read(node_file), symbolize_names: true)
93
+
94
+ desc "Itamae to #{bname}"
95
+ task node_h[:environments][:hostname].split(".")[0] do
96
+ begin
97
+ recipes = []
98
+ get_roles(node_file).each do |role|
99
+ recipes << get_recipes(role)
100
+ end
101
+ get_node_recipes(node_file).each do |recipe|
102
+ recipes << recipe
103
+ end
104
+ rescue Exception => e
105
+ puts e.class.to_s + ", " + e.backtrace[0].to_s
106
+ puts "nodefile or role error, nodefile:#{node_file} reason:#{e.message}"
107
+ exit 1
108
+ else
109
+ recipes.flatten!
110
+ end
111
+
112
+ # get env attr
113
+ begin
114
+ env_set = node_h[:environments][:set]
115
+ env_h = JSON.parse(File.read("environments/#{env_set}.json"), symbolize_names: true)
116
+ rescue Exception => e
117
+ puts e.class.to_s + ", " + e.backtrace[0].to_s
118
+ puts "nodefile or environments error, nodefile:#{node_file} reason:#{e.message}"
119
+ exit 1
120
+ end
121
+
122
+ # get recipe attr
123
+ recipe_attr_file = []
124
+ recipes.each do |recipe_h|
125
+ if recipe_h["#{recipe_h.keys.join}"].nil?
126
+ recipe_attr_file.insert 0,
127
+ Dir.glob("site-cookbooks/**/#{recipe_h.keys.join}/attributes/default.json")
128
+ else
129
+ recipe_attr_file <<
130
+ Dir.glob("site-cookbooks/**/#{recipe_h.keys.join}/attributes/#{recipe_h["#{recipe_h.keys.join}"]}.json")
131
+ end
132
+ end
133
+
134
+ recipe_attr_file.flatten!
135
+
136
+ # recipe attr other=env
137
+ recipe_env_h_a = []
138
+ recipe_attr_file.each do |file|
139
+ recipe_h = JSON.parse(File.read(file), symbolize_names: true)
140
+ recipe_env_h_a << recipe_h.deep_merge(env_h)
141
+ end
142
+
143
+ # recipe attr other=recipes
144
+ moto = recipe_env_h_a[0]
145
+ recipe_env_h_a.each {|hash| moto.deep_merge!(hash)}
146
+ recipe_env_h = moto
147
+
148
+ if recipe_env_h.nil?
149
+ # node attr other=env
150
+ node_env_h = env_h.deep_merge(node_h)
151
+ node_env_j = jq node_env_h
152
+ write_json(bname) {|file| file.puts node_env_j}
153
+ else
154
+ # node attr other=recipe_env
155
+ recipe_env_node_h = recipe_env_h.deep_merge(node_h)
156
+ recipe_env_node_j = jq recipe_env_node_h
157
+ write_json(bname) {|file| file.puts recipe_env_node_j}
158
+ end
159
+
160
+ recipes << {'_base' => nil}
161
+ node_property = JSON.parse(File.read("tmp-nodes/#{bname}.json"), symbolize_names: true)
162
+ node = node_property[:environments][:hostname]
163
+ ssh_user = node_property[:environments][:ssh_user]
164
+ ssh_password = node_property[:environments][:ssh_password]
165
+ ssh_port = node_property[:environments][:ssh_port]
166
+ ssh_key = node_property[:environments][:ssh_key]
167
+
168
+ ENV['TARGET_HOST'] = node
169
+ ENV['NODE_FILE'] = node_file
170
+ ENV['SSH_PASSWORD'] = ssh_password
171
+
172
+ command = "bundle exec itamae ssh"
173
+ command << " -h #{node}"
174
+ command << " -u #{ssh_user}"
175
+ command << " -p #{ssh_port}"
176
+ command << " -i keys/#{ssh_key}" unless ssh_key.nil?
177
+ command << " -j tmp-nodes/#{bname}.json"
178
+ command << " --shell=bash"
179
+ command << " --ask-password" unless ssh_password.nil?
180
+ command << " --dry-run" if ENV['dry_run'] == "true"
181
+ command << " -l debug" if ENV['debug'] == "true"
182
+
183
+ # recipe load to_command
184
+ command_recipe = []
185
+ recipes.each do |recipe_h|
186
+ if recipe_h["#{recipe_h.keys.join}"].nil?
187
+ command_recipe <<
188
+ " #{Dir.glob("site-cookbooks/**/#{recipe_h.keys.join}/recipes/default.rb").join}"
189
+ else
190
+ command_recipe <<
191
+ " #{Dir.glob("site-cookbooks/**/#{recipe_h.keys.join}/recipes/#{recipe_h["#{recipe_h.keys.join}"]}.rb").join}"
192
+ end
193
+ end
194
+ command_recipe.sort_by! {|item| File.dirname(item)}
195
+ command << command_recipe.join
196
+
197
+ color.echos(:red ,%!Run Itamae to \"#{bname}\"!)
198
+ color.echos(:blue, %!Set Roles to \"#{get_roles(node_file).join(", ")}\"!)
199
+ run_list_noti = []
200
+ command_recipe.each {|c_recipe| run_list_noti << c_recipe.split("/") [2]}
201
+ color.echos(:green, %!Run List to \"#{run_list_noti.uniq.join(", ")}\"!)
202
+ puts color.echos(:white, %!#{command}!)
203
+ st = system command
204
+ exit 1 unless st
205
+ end
206
+
207
+ end
208
+ end
209
+ end
210
+
211
+ end
212
+ end
213
+ end
@@ -1,6 +1,4 @@
1
- require 'serverspec'
2
1
  require 'rake'
3
- require 'rspec/core/rake_task'
4
2
  require 'json'
5
3
  require 'simple_color'
6
4
  include Rake::DSL if defined? Rake::DSL
@@ -52,7 +50,9 @@ module Itamae
52
50
 
53
51
  file_name = File.basename(node_file, '.json')
54
52
  node_attr = JSON.parse(File.read(node_file), symbolize_names: true)
55
- desc "Run to #{file_name}"
53
+
54
+ desc "Spec to #{file_name}"
55
+ task node_attr[:environments][:hostname].split(".")[0] do
56
56
 
57
57
  begin
58
58
  recipes = []
@@ -81,24 +81,33 @@ module Itamae
81
81
  all << node_short
82
82
 
83
83
  desc "Run spec to #{file_name}"
84
- RSpec::Core::RakeTask.new(node_short.to_sym) do |t|
85
84
  ENV['TARGET_HOST'] = node_name
86
85
  ENV['NODE_FILE'] = node_file
87
86
  ENV['SSH_PASSWORD'] = ssh_password
88
87
  ENV['SSH_KEY'] = "keys/#{ssh_key}"
89
88
 
90
- specs = []
91
- spec_recips = []
92
- recipes.each {|hash|
93
- specs << hash.keys.join
94
- spec_recips << hash.values.join unless hash.values.join.empty?
95
- }
89
+ specs = "bundle exec rspec"
96
90
 
97
- t.pattern = "site-cookbooks/**/\{#{specs.join(',')}\}/spec/\{default,#{spec_recips.join(',')}\}_spec.rb"
98
- t.fail_on_error = true
99
- color = SimpleColor.new
100
- color.echos(:red, "Run Serverspec to #{node_name}")
101
- color.echos(:green, "Run List to #{specs.uniq.sort.join(", ")}")
91
+ # recipe load to_spec
92
+ spec_pattern = []
93
+ recipes.each do |spec_h|
94
+ if spec_h["#{spec_h.keys.join}"].nil?
95
+ spec_pattern <<
96
+ " #{Dir.glob("site-cookbooks/**/#{spec_h.keys.join}/spec/default_spec.rb").join}"
97
+ else
98
+ spec_pattern <<
99
+ " #{Dir.glob("site-cookbooks/**/#{spec_h.keys.join}/spec/#{spec_h["#{spec_h.keys.join}"]}_spec.rb").join}"
100
+ end
101
+ end
102
+ spec_pattern.sort_by! {|item| File.dirname(item)}
103
+ specs << spec_pattern.join
104
+ run_list_noti = []
105
+ spec_pattern.each {|c_spec| run_list_noti << c_spec.split("/") [2]}
106
+ color.echos(:red ,%!Run Serverspec to \"#{node_name}\"!)
107
+ color.echos(:green, %!Run List to \"#{run_list_noti.uniq.join(", ")}\"!)
108
+ puts color.echos(:white, %!#{specs}!)
109
+ st = system specs
110
+ exit 1 unless st
102
111
  end
103
112
  task :all => all
104
113
  task :default => :all
@@ -1 +1 @@
1
- 0.2
1
+ 0.3
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itamae-mitsurin
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akihiro Kamiyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-01 00:00:00.000000000 Z
11
+ date: 2016-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -121,33 +121,33 @@ dependencies:
121
121
  - !ruby/object:Gem::Version
122
122
  version: 3.0.0
123
123
  - !ruby/object:Gem::Dependency
124
- name: bundler
124
+ name: rake
125
125
  requirement: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - ~>
127
+ - - '>='
128
128
  - !ruby/object:Gem::Version
129
- version: '1.10'
130
- type: :development
129
+ version: '0'
130
+ type: :runtime
131
131
  prerelease: false
132
132
  version_requirements: !ruby/object:Gem::Requirement
133
133
  requirements:
134
- - - ~>
134
+ - - '>='
135
135
  - !ruby/object:Gem::Version
136
- version: '1.10'
136
+ version: '0'
137
137
  - !ruby/object:Gem::Dependency
138
- name: rake
138
+ name: bundler
139
139
  requirement: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - '>='
141
+ - - ~>
142
142
  - !ruby/object:Gem::Version
143
- version: '0'
143
+ version: '1.10'
144
144
  type: :development
145
145
  prerelease: false
146
146
  version_requirements: !ruby/object:Gem::Requirement
147
147
  requirements:
148
- - - '>='
148
+ - - ~>
149
149
  - !ruby/object:Gem::Version
150
- version: '0'
150
+ version: '1.10'
151
151
  - !ruby/object:Gem::Dependency
152
152
  name: rspec
153
153
  requirement: !ruby/object:Gem::Requirement
@@ -220,7 +220,7 @@ dependencies:
220
220
  version: '0'
221
221
  description:
222
222
  email:
223
- - kamiyama@qualitia.co.jp
223
+ - akihiro.vamps@gmail.com
224
224
  executables:
225
225
  - itamae
226
226
  - itamae-mitsurin
@@ -272,6 +272,7 @@ files:
272
272
  - lib/itamae/mitsurin/creators/templates/project/Rakefile
273
273
  - lib/itamae/mitsurin/creators/templates/project/environments/.keep
274
274
  - lib/itamae/mitsurin/creators/templates/project/environments/sample.json
275
+ - lib/itamae/mitsurin/creators/templates/project/keys/.keep
275
276
  - lib/itamae/mitsurin/creators/templates/project/nodes/.keep
276
277
  - lib/itamae/mitsurin/creators/templates/project/nodes/sample01.json
277
278
  - lib/itamae/mitsurin/creators/templates/project/roles/.keep
@@ -288,6 +289,7 @@ files:
288
289
  - lib/itamae/mitsurin/creators/templates/site-cookbooks/spec/.keep
289
290
  - lib/itamae/mitsurin/creators/templates/site-cookbooks/templates/.keep
290
291
  - lib/itamae/mitsurin/itamae_task.rb
292
+ - lib/itamae/mitsurin/itamae_with_git_task.rb
291
293
  - lib/itamae/mitsurin/serverspec_task.rb
292
294
  - lib/itamae/mitsurin/version.rb
293
295
  - lib/itamae/mitsurin/version.txt