Module: AutoStacker24::Preprocessor

Defined in:
lib/autostacker24/template_preprocessor.rb

Class Method Summary collapse

Class Method Details

.adjust_tags_for_asg(tags) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/autostacker24/template_preprocessor.rb', line 72

def self.adjust_tags_for_asg(tags)
  asg_tags = tags.inject([]) { |t,element| t << element.dup }

  asg_tags.each {|(tag)|
    tag["PropagateAtLaunch"] = "true"
  }

  asg_tags
end

.preprocess(template, tags = nil) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/autostacker24/template_preprocessor.rb', line 8

def self.preprocess(template, tags = nil)
  if template =~ /^\s*\/{2}\s*/i
    template = template.gsub(/(\s*\/\/.*$)|(".*")/) {|m| m[0] == '"' ? m : ''} # replace comments
    template = preprocess_json(JSON(template))
    template = preprocess_tags(template, tags).to_json
  end
  template
end

.preprocess_json(json) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/autostacker24/template_preprocessor.rb', line 82

def self.preprocess_json(json)
  if json.is_a?(Hash)
    json.inject({}){|m, (k, v)| m.merge(k => preprocess_json(v))}
  elsif json.is_a?(Array)
    json.map{|v| preprocess_json(v)}
  elsif json.is_a?(String)
    preprocess_string(json)
  else
    json
  end
end

.preprocess_string(s) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/autostacker24/template_preprocessor.rb', line 94

def self.preprocess_string(s)
  parts = tokenize(s).map do |token|
    case token
      when '@@' then '@'
      when /^@/ then {'Ref' => token[1..-1]}
      else token
    end
  end

  # merge neighboured strings
  parts = parts.reduce([])do |m, p|
    if m.last.is_a?(String) && p.is_a?(String)
      m[-1] += p
    else
      m << p
    end
    m
  end

  if parts.length == 1
    parts.first
  else # we need a join construct
    {'Fn::Join' => ['', parts]}
  end
end

.preprocess_tags(template, tags = nil) ⇒ Object



17
18
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
67
68
69
70
# File 'lib/autostacker24/template_preprocessor.rb', line 17

def self.preprocess_tags(template, tags = nil)

  supportedTypes = [
    'AWS::AutoScaling::AutoScalingGroup',
    'AWS::CloudTrail::Trail',
    'AWS::EC2::CustomerGateway',
    'AWS::EC2::DHCPOptions',
    'AWS::EC2::Instance',
    'AWS::EC2::InternetGateway',
    'AWS::EC2::NetworkAcl',
    'AWS::EC2::NetworkInterface',
    'AWS::EC2::RouteTable',
    'AWS::EC2::SecurityGroup',
    'AWS::EC2::Subnet',
    'AWS::EC2::Volume',
    'AWS::EC2::VPC',
    'AWS::EC2::VPCPeeringConnection',
    'AWS::EC2::VPNConnection',
    'AWS::EC2::VPNGateway',
    'AWS::ElasticBeanstalk::Environment',
    'AWS::ElasticLoadBalancing::LoadBalancer',
    'AWS::RDS::DBCluster',
    'AWS::RDS::DBClusterParameterGroup',
    'AWS::RDS::DBInstance',
    'AWS::RDS::DBParameterGroup',
    'AWS::RDS::DBSecurityGroup',
    'AWS::RDS::DBSubnetGroup',
    'AWS::RDS::OptionGroup',
    'AWS::S3::Bucket'
  ]

  unless tags.nil?

    tags_for_asg = adjust_tags_for_asg(tags)

    template["Resources"].each {|(key, value)|

      tags_to_apply = tags
      if value["Type"] == 'AWS::AutoScaling::AutoScalingGroup'
        tags_to_apply = tags_for_asg
      end

      if supportedTypes.include? value["Type"]
        if value["Properties"]["Tags"].nil?
          value["Properties"]["Tags"] = tags_to_apply
        else
          value["Properties"]["Tags"] = (tags_to_apply + value["Properties"]["Tags"]).uniq { |s| s.first }
        end
      end
    }
  end

  template
end

.tokenize(s) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/autostacker24/template_preprocessor.rb', line 120

def self.tokenize(s)
  pattern = /@@|@((\w+(::)?\w+)+)/
  tokens = []
  loop do
    m = pattern.match(s)
    if m
      tokens << m.pre_match unless m.pre_match.empty?
      tokens << m.to_s
      s = m.post_match
    else
      tokens << s unless s.empty? && !tokens.empty?
      break
    end
  end
  tokens
end