Class: Terrafying::Components::Service

Inherits:
Terrafying::Context
  • Object
show all
Includes:
Usable
Defined in:
lib/terrafying/components/service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Usable

#egress_security_group, #ingress_security_group, #path_mtu_setup!, #pingable_by, #pingable_by_cidr, #security_group, #used_by, #used_by_cidr

Constructor Details

#initializeService

Returns a new instance of Service.



36
37
38
# File 'lib/terrafying/components/service.rb', line 36

def initialize()
  super
end

Instance Attribute Details

#domain_namesObject (readonly)

Returns the value of attribute domain_names.



24
25
26
# File 'lib/terrafying/components/service.rb', line 24

def domain_names
  @domain_names
end

#instance_profileObject (readonly)

Returns the value of attribute instance_profile.



24
25
26
# File 'lib/terrafying/components/service.rb', line 24

def instance_profile
  @instance_profile
end

#instance_setObject (readonly)

Returns the value of attribute instance_set.



24
25
26
# File 'lib/terrafying/components/service.rb', line 24

def instance_set
  @instance_set
end

#load_balancerObject (readonly)

Returns the value of attribute load_balancer.



24
25
26
# File 'lib/terrafying/components/service.rb', line 24

def load_balancer
  @load_balancer
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/terrafying/components/service.rb', line 24

def name
  @name
end

#portsObject (readonly)

Returns the value of attribute ports.



24
25
26
# File 'lib/terrafying/components/service.rb', line 24

def ports
  @ports
end

#zoneObject (readonly)

Returns the value of attribute zone.



24
25
26
# File 'lib/terrafying/components/service.rb', line 24

def zone
  @zone
end

Class Method Details

.create_in(vpc, name, options = {}) ⇒ Object



28
29
30
# File 'lib/terrafying/components/service.rb', line 28

def self.create_in(vpc, name, options={})
  Service.new.create_in vpc, name, options
end

.find_in(vpc, name) ⇒ Object



32
33
34
# File 'lib/terrafying/components/service.rb', line 32

def self.find_in(vpc, name)
  Service.new.find_in vpc, name
end

Instance Method Details

#allow_scrape(vpc, ports, security_group) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/terrafying/components/service.rb', line 165

def allow_scrape(vpc, ports, security_group)
  prom = Prometheus.find_in(vpc: vpc)
  ports.each do |port|
    sg_rule_ident = Digest::SHA256.hexdigest("#{vpc.name}-#{port}-#{security_group}-#{prom.security_group}")
    resource :aws_security_group_rule, sg_rule_ident, {
      security_group_id: security_group,
      type: 'ingress',
      from_port: port,
      to_port: port,
      protocol: 'tcp',
      source_security_group_id: prom.security_group
    }
  end
end

#create_in(vpc, name, options = {}) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/terrafying/components/service.rb', line 44

def create_in(vpc, name, options={})
  options = {
    ami: aws.ami("base-image-59a5b709", owners=["136393635417"]),
    instance_type: "t2.micro",
    ports: [],
    instances: [{}],
    zone: vpc.zone,
    iam_policy_statements: [],
    security_groups: [],
    keypairs: [],
    volumes: [],
    units: [],
    files: [],
    tags: {},
    ssh_group: vpc.ssh_group,
    subnets: vpc.subnets.fetch(:private, []),
    startup_grace_period: 300,
    depends_on: [],
    audit_role: "arn:aws:iam::#{aws.}:role/auditd_logging",
    metrics_ports: [],
  }.merge(options)

  unless options[:audit_role].nil?
    fluentd_conf = Auditd.fluentd_conf(options[:audit_role], options[:tags].keys)
    options = options.merge_with_arrays_merged(fluentd_conf)
  end

  if ! options.has_key? :user_data
    options[:user_data] = Ignition.generate(options)
  end

  if ! options.has_key?(:loadbalancer_subnets)
    options[:loadbalancer_subnets] = options[:subnets]
  end

  unless options[:instances].is_a?(Hash) or options[:instances].is_a?(Array)
    raise 'Unknown instances option, should be hash or array'
  end

  ident = "#{tf_safe(vpc.name)}-#{name}"

  @name = ident
  @zone = options[:zone]
  @ports = enrich_ports(options[:ports])
  @domain_names = [ options[:zone].qualify(name) ]

  depends_on = options[:depends_on] + options[:keypairs].map{ |kp| kp[:resources] }.flatten
  if options.key? :instance_profile
    @instance_profile = options[:instance_profile]
  else
    iam_statements = options[:iam_policy_statements] + options[:keypairs].map { |kp| kp[:iam_statement] }
    @instance_profile = add! InstanceProfile.create(ident, { statements: iam_statements })
  end

  tags = options[:tags].merge({ service_name: name })

  set = options[:instances].is_a?(Hash) ? DynamicSet : StaticSet

  if options.has_key?(:loadbalancer) # explicitly requested or rejected a loadbalancer
    wants_load_balancer = options[:loadbalancer]
  else
    # by default we want one if we are an ASG with exposed ports
    wants_load_balancer = set == DynamicSet && @ports.count > 0
  end

  instance_set_options = {
    instance_profile: @instance_profile,
    depends_on: depends_on,
    tags: tags,
  }

  if wants_load_balancer && @ports.any? { |p| p.has_key?(:health_check) }
    instance_set_options[:health_check] = { type: "ELB", grace_period: options[:startup_grace_period] }
  end

  @instance_set = add! set.create_in(vpc, name, options.merge(instance_set_options))
  @security_group = @instance_set.security_group

  if options[:metrics_ports] && !options[:metrics_ports].empty?
    allow_scrape(vpc, options[:metrics_ports], @security_group)
  end

  if wants_load_balancer
    @load_balancer = add! LoadBalancer.create_in(
                            vpc, name, options.merge(
                              {
                                subnets: options[:loadbalancer_subnets],
                                tags: tags,
                              }
                            ),
                          )

    @load_balancer.attach(@instance_set)

    if @load_balancer.type == "application"
      @security_group = @load_balancer.security_group
      @egress_security_group = @instance_set.security_group
    end

    vpc.zone.add_alias_in(self, name, @load_balancer.alias_config)
  elsif set == StaticSet
    vpc.zone.add_record_in(self, name, @instance_set.instances.map { |i| i.ip_address })
    @instance_set.instances.each { |i|
      @domain_names << vpc.zone.qualify(i.name)
      vpc.zone.add_record_in(self, i.name, [i.ip_address])
    }
  end

  if set == DynamicSet && options[:rolling_update] == :signal
    @instance_profile.add_statement!(
      {
        Effect: "Allow",
        Action: [ "cloudformation:SignalResource" ],
        Resource: [ @instance_set.stack ],
      }
    )
  end

  self
end

#find_in(vpc, name) ⇒ Object



40
41
42
# File 'lib/terrafying/components/service.rb', line 40

def find_in(vpc, name)
  raise 'unimplemented'
end

#with_endpoint_service(options = {}) ⇒ Object



180
181
182
183
184
185
# File 'lib/terrafying/components/service.rb', line 180

def with_endpoint_service(options = {})
  add! EndpointService.create_for(@load_balancer, @name, {
                                    fqdn: @domain_names[0],
                                    zone: @zone,
                                  }.merge(options))
end