Class: Ec2Hosts::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2_hosts/updater.rb

Overview

Updater implements a simple state machine which is used to update content between zero or more blocks of content which start and end with pre-defined “marker” lines.

Defined Under Namespace

Modules: Marker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Updater

Returns a new instance of Updater.



15
16
17
# File 'lib/ec2_hosts/updater.rb', line 15

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/ec2_hosts/updater.rb', line 13

def options
  @options
end

Instance Method Details

#clearObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ec2_hosts/updater.rb', line 68

def clear
  old_hosts = File.read(options[:file])
  new_content = old_hosts.dup

  markers = old_hosts.each_line.map do |line|
    regex = "\# (START|END) EC2 HOSTS - (.+) \#"
    if m = line.match(/^#{regex}$/)
      m[2]
    end
  end.compact.uniq

  markers.each do |project|
    new_content = delete_vpc_hosts(new_content)
  end
  new_content.gsub!(/\s+$/, "\n")

  if options[:dry_run]
    puts new_content
  elsif new_content != old_hosts
    # backup old host file
    File.open(options[:file], 'w') { |f| f << old_hosts }
    # write new content
    File.open(options[:file], 'w') { |f| f << new_content }
  end
end

#update(new_hosts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ec2_hosts/updater.rb', line 19

def update(new_hosts)
  old_hosts = File.read(options[:file])

  if old_hosts.include?(start_marker) && old_hosts.include?(end_marker)
    # valid markers exists
    if options[:delete]
      new_content = delete_vpc_hosts(old_hosts)
    else
      new_content = gen_new_hosts(old_hosts, new_hosts)
    end

    # remove zero or more white space characters at end of file with
    # a single new-line
    new_content.gsub!(/\s+$/, "\n")

    if options[:dry_run]
      puts new_content
    elsif new_content != old_hosts
      # backup old host file
      File.open(options[:backup], 'w') { |f| f << old_hosts }
      # write new content
      File.open(options[:file], 'w') { |f| f << new_content }
    end
  elsif old_hosts.include?(start_marker) || old_hosts.include?(end_marker)
    raise UpdaterError.new("Invalid marker present in existing hosts content")
  else
    # marker doesn't exist
    if options[:delete]
      new_content = old_hosts
    else
      new_content = [old_hosts, start_marker, new_hosts, end_marker].join("\n")
    end
    # remove one or more white space characters at end of file with
    # a single new-line
    new_content.gsub!(/\s+$/, "\n")

    if options[:dry_run]
      puts new_content
    elsif new_content != old_hosts
      # backup old host file
      File.open(options[:backup], 'w') { |f| f << old_hosts }
      # write new content
      File.open(options[:file], 'w') { |f| f << new_content }
    end
  end

  true
end