itamae-spec 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32c0a5e4298c6d97786f447e64e6edea3071ed874f8f347532e33bfa92330fdf
4
- data.tar.gz: d69af9290654e7d104d9decb50ff9f6c8afb80587af0189ab1e933264abec152
3
+ metadata.gz: c331c48127fb1a98218a1a3c3558ced75f5be9c8ca8498a5a5cc90d1f317314c
4
+ data.tar.gz: 61b144fafd16ca07d70edbfd545021433a47db68872ddf61d44ad7a42c93b752
5
5
  SHA512:
6
- metadata.gz: 1234cfda80bca59a5e53251046f7c743587d692097f80a63033c9bbe150eb18dcc161b703bd3019d416527c327703e02ea8065b486f0f7ab44beca652f46343a
7
- data.tar.gz: a3a82b93122ab5b95eddb874f09b14fb0c919e888008b834d32e501a6bb7ad070cefb88bdc11fb2cee752c75b5cda54174d7ec394cc8824cce32442de312c4c3
6
+ metadata.gz: a66032f85bcee1eb44d9fad26a0a1ccffab6d76eda4f6cb8965f1c1f22fd121519fe6347b4e3b1cfb72c04087227b4f905714865019b384841c1b8527abc4627
7
+ data.tar.gz: 51d791fe0021ca63c798441d214d018d0f74301b92cb91ccece9ce1b05edf7f00902777fa6242bf8820f0f9802332e9d662c5b90be8a1f22e58612706b0d8de2
@@ -5,6 +5,12 @@ module Itamae
5
5
  def self.source_root
6
6
  File.dirname(__FILE__) + '/templates/project'
7
7
  end
8
+
9
+ def bundle
10
+ Dir.chdir(destination_root) do
11
+ run 'bundle install'
12
+ end
13
+ end
8
14
  end
9
15
  end
10
16
  end
@@ -1,2 +1,4 @@
1
1
  require 'itamae-spec/resource/http_request'
2
+ require 'itamae-spec/resource/file'
2
3
  require 'itamae-spec/resource/s3_file'
4
+ require 'itamae-spec/resource/route53_record'
@@ -0,0 +1,44 @@
1
+
2
+ module Itamae
3
+ module Resource
4
+ class File
5
+ def send_tempfile
6
+ if !attributes.content && !content_file
7
+ @temppath = nil
8
+ return
9
+ end
10
+
11
+ begin
12
+ src = if content_file
13
+ content_file
14
+ else
15
+ f = Tempfile.open('itamae')
16
+ f.write(attributes.content)
17
+ f.close
18
+ f.path
19
+ end
20
+
21
+ @temppath = ::File.join(runner.tmpdir, Time.now.to_f.to_s)
22
+
23
+ if backend.is_a?(Itamae::Backend::Docker)
24
+ run_command(["mkdir", @temppath])
25
+ backend.send_file(src, @temppath)
26
+ @temppath = ::File.join(@temppath, ::File.basename(src))
27
+ elsif backend.is_a?(Itamae::Backend::Local)
28
+ run_command(["touch", @temppath])
29
+ run_specinfra(:change_file_mode, @temppath, '0600')
30
+ run_specinfra(:copy_file, src, @temppath)
31
+ else
32
+ run_command(["touch", @temppath])
33
+ run_specinfra(:change_file_mode, @temppath, '0600')
34
+ backend.send_file(src, @temppath)
35
+ end
36
+
37
+ run_specinfra(:change_file_mode, @temppath, '0600')
38
+ ensure
39
+ f.unlink if f
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,145 @@
1
+ require 'multi_json'
2
+
3
+ module Itamae
4
+ module Resource
5
+ class Route53Record < File
6
+ define_attribute :action, default: :create
7
+ define_attribute :region, type: String, required: true
8
+ define_attribute :profile, type: String, default: 'default'
9
+ define_attribute :hosted_zone_id, type: String, required: true
10
+ define_attribute :comment, type: String
11
+ define_attribute :record_name, type: String, default_name: true
12
+ define_attribute :type, type: String, required: true
13
+ define_attribute :ttl, type: Integer, required: true
14
+ define_attribute :value, type: [ String, Array ], required: true
15
+ # define_attribute :set_identifier, type: String
16
+ # define_attribute :weight, type: String
17
+ # define_attribute :failover, type: String
18
+ # define_attribute :health_check_id, type: String
19
+ # define_attribute :traffic_policy_instance_id, type: String
20
+
21
+ def pre_action
22
+ attributes.record_name = attributes.record_name + '.' unless attributes.record_name[-1] == '.'
23
+ credentials = Aws::SharedCredentials.new(profile_name: attributes.profile)
24
+ @route53 = Aws::Route53::Client.new(region: attributes.region, credentials: credentials)
25
+
26
+ @change_batch = define_change_batch
27
+ resource_record_set = @change_batch[:changes][0][:resource_record_set]
28
+ attributes.content = MultiJson.dump(resource_record_set, pretty: true)
29
+
30
+ @resource_record_set = compare_record_values(fetch_record)
31
+
32
+ case @current_action
33
+ when :create
34
+ attributes.exist = true
35
+ when :upsert
36
+ attributes.exist = true
37
+ when :delete
38
+ attributes.exist = false
39
+ end
40
+
41
+ send_tempfile
42
+ compare_file if @current_action == :upsert
43
+ end
44
+
45
+ def set_current_attributes
46
+ current.modified = false
47
+ end
48
+
49
+ def action_create
50
+ return if current.exist
51
+ @route53.change_resource_record_sets(
52
+ change_batch: @change_batch,
53
+ hosted_zone_id: attributes.hosted_zone_id
54
+ )
55
+ end
56
+
57
+ def action_upsert
58
+ @route53.change_resource_record_sets(
59
+ change_batch: @change_batch,
60
+ hosted_zone_id: attributes.hosted_zone_id
61
+ )
62
+ end
63
+
64
+ def action_delete
65
+ return unless current.exist
66
+ @route53.change_resource_record_sets(
67
+ change_batch: @change_batch,
68
+ hosted_zone_id: attributes.hosted_zone_id
69
+ )
70
+ rescue Aws::Route53::Errors::InvalidChangeBatch => e
71
+ Itamae.logger.warn e.inspect
72
+ end
73
+
74
+ private
75
+
76
+ def define_change_batch
77
+ if attributes.value.class == Array
78
+ resource_records = attributes.value.map do |v|
79
+ { value: v }
80
+ end
81
+ elsif attributes.value.class == String
82
+ resource_records = Array.new(1, { value: attributes.value })
83
+ end
84
+
85
+ resource_record_set = {
86
+ name: attributes.record_name,
87
+ type: attributes.type,
88
+ ttl: attributes.ttl,
89
+ resource_records: resource_records
90
+ }
91
+
92
+ changes = {
93
+ action: attributes.action.to_s.upcase,
94
+ resource_record_set: resource_record_set
95
+ }
96
+
97
+ {
98
+ changes: [ changes ],
99
+ comment: attributes.comment
100
+ }
101
+ end
102
+
103
+ def fetch_record
104
+ resp = @route53.list_resource_record_sets(
105
+ hosted_zone_id: attributes.hosted_zone_id,
106
+ start_record_name: attributes.record_name,
107
+ start_record_type: attributes.type,
108
+ start_record_identifier: attributes.set_identifier,
109
+ max_items: 1
110
+ )
111
+
112
+ resp.resource_record_sets[0]
113
+ end
114
+
115
+ def compare_record_values(resource_record_set)
116
+ if attributes.record_name == resource_record_set.name && attributes.type == resource_record_set.type
117
+ current.exist = true
118
+ resource_record_set.to_h
119
+ else
120
+ current.exist = false
121
+ {}
122
+ end
123
+ end
124
+
125
+ def compare_to
126
+ if current.exist
127
+ f = Tempfile.open('itamae')
128
+ f.write(MultiJson.dump(@resource_record_set, pretty: true))
129
+ f.close
130
+ f.path
131
+ else
132
+ '/dev/null'
133
+ end
134
+ end
135
+
136
+ def show_content_diff
137
+ if attributes.modified
138
+ Itamae.logger.info 'Convert resource record set to JSON and display the difference.'
139
+ end
140
+
141
+ super
142
+ end
143
+ end
144
+ end
145
+ end
@@ -3,7 +3,7 @@ require 'aws-sdk'
3
3
  module Itamae
4
4
  module Resource
5
5
  class S3File < File
6
- define_attribute :object_key, type: String, default_name: true
6
+ define_attribute :object_key, type: String, required: true
7
7
  define_attribute :region, type: String, required: true
8
8
  define_attribute :bucket, type: String, required: true
9
9
  define_attribute :profile, type: String, default: 'default'
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itamae-spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kammy1231
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-14 00:00:00.000000000 Z
11
+ date: 2018-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -225,7 +225,9 @@ files:
225
225
  - lib/itamae-spec/generators/templates/project/tmp-nodes/.keep
226
226
  - lib/itamae-spec/logger.rb
227
227
  - lib/itamae-spec/resource.rb
228
+ - lib/itamae-spec/resource/file.rb
228
229
  - lib/itamae-spec/resource/http_request.rb
230
+ - lib/itamae-spec/resource/route53_record.rb
229
231
  - lib/itamae-spec/resource/s3_file.rb
230
232
  - lib/itamae-spec/task/base.rb
231
233
  - lib/itamae-spec/task/base_task.rb