Class: Gitchefsync::EnvSync

Inherits:
Object
  • Object
show all
Defined in:
lib/gitchefsync/env_sync.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo_list) ⇒ EnvSync

Returns a new instance of EnvSync.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gitchefsync/env_sync.rb', line 73

def initialize(repo_list)
  options = Gitchefsync.options
  config = Gitchefsync.configuration

  FS.knifeReady(config['stage_dir'],options[:knife_config])

  repo_list.each do |repo|
    repo.validate_structure
  end

  @knife = config['knife']
  @stage_filepath = config['stage_dir']
  @force_upload = config['force_upload']
  @repo_list = repo_list
  @audit = Audit.new(config['stage_dir'], 'env' )
  @audit_keep_trim = config['audit_keep_trim']
  @audit_keep_trim ||= 20
  @env_file_list = Array.new()
  @db_file_list = Array.new()
  @role_file_list = Array.new()
end

Instance Method Details

#cleanup_json_filesObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/gitchefsync/env_sync.rb', line 192

def cleanup_json_files
  Gitchefsync.logger.info "cleanup_json_files"

  #list env and compare on the server, deleting ones that aren't in git
  knifeUtil = KnifeUtil.new(@knife, @stage_filepath)

  delta_env_list = knifeUtil.listEnv() - @env_file_list
  Gitchefsync.logger.info "event_id=env_diff:delta=#{delta_env_list}"
  # MAND-672
  delta_env_list.each do |env_name|
    # TODO: Audit file may not be correct if someone manually
    #       uploaded an environment file with json 'name' variable different
    #       then actual environment filename.
    a = AuditItem.new(env_name,'',nil)
    a.setAction "DEL"
    FS.cmd "#{@knife} environment delete #{env_name} --yes"
    Gitchefsync.logger.info "event_id=environment_deleted:env_name=#{env_name}"
    @audit.add(a)
  end
 
  delta_db_list = knifeUtil.listDB() - @db_file_list
  Gitchefsync.logger.info "event_id=data_bag_item_diff:delta=#{delta_db_list}"
  delta_db_list.each do |bag, item|
    # TODO: Audit file may not be correct if someone manually
    #       uploaded an data bag with item json 'id' variable different
    #       then actual json filename.
    a = AuditItem.new("BAG: #{bag} ITEM: #{item}",'',nil)
    a.setAction "DEL"
    FS.cmd "#{@knife} data bag delete #{bag} #{item} --yes"
    Gitchefsync.logger.info "event_id=data_bag_item_deleted:bag=#{bag}:item=#{item}"
    @audit.add(a)
    
    items_remaining = knifeUtil.showDBItem(bag)
    if items_remaining.empty?
      a = AuditItem.new("BAG: #{bag}",'',nil)
      a.setAction "DEL"
      FS.cmd "#{@knife} data bag delete #{bag} --yes"
      Gitchefsync.logger.info "event_id=data_bag_deleted:bag=#{bag}"
      @audit.add(a)
    end
  end
 
  delta_role_list = knifeUtil.listRole() - @role_file_list
  Gitchefsync.logger.info "event_id=role_diff:delta=#{delta_role_list}"
  delta_role_list.each do |role_name|
    # TODO: Audit file may not be correct if someone manually
    #       uploaded an role file with json 'name' variable different
    #       then actual role filename.
    a = AuditItem.new(role_name,'',nil)
    a.setAction "DEL"
    FS.cmd "#{@knife} role delete #{role_name} --yes"
    Gitchefsync.logger.info "event_id=role_deleted:role_name=#{role_name}"
    @audit.add(a)
  end

  #TODO: must create audit for removal

  @audit.write
  #trim the audit file
  @audit.trim(@audit_keep_trim)
end

#data_bag_iden(fullpath) ⇒ Object

Raises:



140
141
142
143
144
145
146
147
148
# File 'lib/gitchefsync/env_sync.rb', line 140

def data_bag_iden fullpath
  chef_repo, data_bag = false, false
  fullpath.split(File::SEPARATOR).each do |dir|
    return dir if chef_repo && data_bag
    chef_repo = dir.eql? "chef-repo" unless chef_repo
    data_bag = dir.eql? "data_bags" if chef_repo
  end
  raise ValidationError, "event_id=invalid_path_to_data_bag_json:path=#{fullpath}"
end

#json_type(file) ⇒ Object



107
108
109
110
111
# File 'lib/gitchefsync/env_sync.rb', line 107

def json_type file
  return "env" unless FS.getBasePath(file, "environments").nil?
  return "db" unless FS.getBasePath(file, "data_bags").nil?
  return "role" unless FS.getBasePath(file, "roles").nil?
end

#reject_json(file) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gitchefsync/env_sync.rb', line 95

def reject_json file
  file_json = nil
  begin
    json = File.read file
    file_json = JSON.parse json
  rescue Exception => e
    Gitchefsync.logger.error "event_id=env_parse_error:file=#{file}"
    @audit.addEnv(file,'UPDATE', e )
  end
  file_json
end

#update_json_filesObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/gitchefsync/env_sync.rb', line 254

def update_json_files
  Gitchefsync.logger.info "event_id=update_json_files"
  @env_file_list.clear
  @env_file_list << "_default"
  @db_file_list.clear
  @role_file_list.clear

  @repo_list.each do |repo|
    env_dir = repo.chef_path + "/**/*json"

    Dir.glob(env_dir).each  do |file|

      file_attr = Hash.new()
      file_attr['json'] = reject_json(file)
      next if file_attr['json'].nil?
      file_attr['type'] = json_type(file)
      file_attr['filename'] = File.basename(file)
      file_attr['basename'] = File.basename(file).chomp(".json")
      file_attr['fullpath'] = file

      if file_attr['type'].eql? "env"
        upload_env(file_attr, repo.git_delta)
      elsif file_attr['type'].eql? "db"
        upload_db(file_attr, repo.git_delta)
      elsif file_attr['type'].eql? "role"
        upload_role(file_attr, repo.git_delta)
      end
    end
  end

  self.cleanup_json_files
end

#upload_db(f, git_delta) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gitchefsync/env_sync.rb', line 150

def upload_db(f, git_delta)
  Gitchefsync.logger.debug "event_id=upload_data_bag:filepath=#{f['fullpath']}"
  db_iden = data_bag_iden(f['fullpath'])
  begin
    validate_json(f, 'id')
    @db_file_list << [db_iden, f['json']['id']]
    show_out = FS.cmdNoError "#{@knife} data bag show #{db_iden} #{f['json']['id']}"
    if show_out.match("ERROR:") || git_delta || @force_upload
      FS.cmd "#{@knife} data bag create #{db_iden}"
      FS.cmd "#{@knife} data bag from file #{db_iden} #{f['fullpath']}"
      Gitchefsync.logger.info "event_id=databag_uploaded:file_json_name=#{f['json']['id']}:file=#{f['fullpath']}"
      @audit.addEnv(f['fullpath'],'UPDATE' )
    else
      Gitchefsync.logger.debug "event_id=data_bag_not_uploaded:file_json_name=#{f['json']['id']}:file=#{f['fullpath']}"
      @audit.addEnv(f['fullpath'],'EXISTING')
    end
  rescue ValidationError => e
    Gitchefsync.logger.error("event_id=validation_error:msg=#{e.message}")
    @audit.addEnv(f['fullpath'],'UPDATE', e )
  end
end

#upload_env(f, git_delta) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gitchefsync/env_sync.rb', line 120

def upload_env(f, git_delta)
  Gitchefsync.logger.debug "event_id=upload_env:filepath=#{f['fullpath']}"
  begin
    validate_json(f, 'name')
    @env_file_list << f['json']['name']
    show_out = FS.cmdNoError "#{@knife} environment show #{f['json']['name']}"
    if show_out.match("ERROR:") || git_delta || @force_upload
      FS.cmd "#{@knife} environment from file #{f['fullpath']} --yes"
      Gitchefsync.logger.info "event_id=environment_uploaded:file_json_name=#{f['json']['name']}:file=#{f['fullpath']}"
      @audit.addEnv(f['fullpath'],'UPDATE' )
    else
      Gitchefsync.logger.debug "event_id=environment_not_uploaded:file_json_name=#{f['json']['name']}:file=#{f['fullpath']}"
      @audit.addEnv(f['fullpath'],'EXISTING' )
    end
  rescue ValidationError => e
    Gitchefsync.logger.error("event_id=validation_error:msg=#{e.message}")
    @audit.addEnv(f['fullpath'],'UPDATE', e )
  end
end

#upload_role(f, git_delta) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/gitchefsync/env_sync.rb', line 172

def upload_role(f, git_delta)
  Gitchefsync.logger.debug "event_id=upload_role:fullpath=#{f['fullpath']}"
  begin
    validate_json(f, 'name')
    @role_file_list << f['json']['name']
    show_out = FS.cmdNoError "#{@knife} role show #{f['json']['name']}"
    if show_out.match("ERROR:") || git_delta || @force_upload
      FS.cmd "#{@knife} role from file #{f['fullpath']} --yes"
      Gitchefsync.logger.info "event_id=role_uploaded:file_json_name=#{f['json']['name']}:file=#{f['fullpath']}"
      @audit.addEnv(f['fullpath'],'UPDATE' )
    else
      Gitchefsync.logger.debug "event_id=role_not_uploaded:file_json_name=#{f['json']['name']}:file=#{f['fullpath']}"
      @audit.addEnv(f['fullpath'],'EXISTING' )
    end
  rescue ValidationError => e
    Gitchefsync.logger.error("event_id=validation_error:msg=#{e.message}")
    @audit.addEnv(f['fullpath'],'UPDATE', e )
  end
end

#validate_json(f, iden) ⇒ Object



113
114
115
116
117
118
# File 'lib/gitchefsync/env_sync.rb', line 113

def validate_json(f, iden)
  if f['basename'] != f['json'][iden]
    raise ValidationError, "The file json's #{iden} attribute does not match basename: #{f['basename']}"
  end
  Gitchefsync.logger.debug "event_id=json_is_valid:iden=#{iden}:basename=#{f['basename']}"
end