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.



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

def initialize
  super
end

Instance Attribute Details

#domain_namesObject (readonly)

Returns the value of attribute domain_names.



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

def domain_names
  @domain_names
end

#instance_profileObject (readonly)

Returns the value of attribute instance_profile.



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

def instance_profile
  @instance_profile
end

#instance_setObject (readonly)

Returns the value of attribute instance_set.



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

def instance_set
  @instance_set
end

#load_balancerObject (readonly)

Returns the value of attribute load_balancer.



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

def load_balancer
  @load_balancer
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#portsObject (readonly)

Returns the value of attribute ports.



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

def ports
  @ports
end

#zoneObject (readonly)

Returns the value of attribute zone.



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

def zone
  @zone
end

Class Method Details

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



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

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

.find_in(vpc, name) ⇒ Object



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

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

Instance Method Details

#allow_scrape(vpc, ports, security_group) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/terrafying/components/service.rb', line 175

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.gsub(%r{^(\d)}, '_\1'),
             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



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
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
164
165
166
167
168
169
170
171
172
173
# File 'lib/terrafying/components/service.rb', line 43

def create_in(vpc, name, options = {})
  options = {
    ami: aws.ami('base-image-fc-75aa2aef', owners = ['477284023816']),
    ignition: true,
    instance_type: 't3a.micro',
    ports: [],
    instances: [{}],
    zone: vpc.zone,
    cross_zone_load_balancing: false,
    iam_policy_statements: [],
    security_groups: [],
    keypairs: [],
    volumes: [],
    units: [],
    files: [],
    tags: {},
    users: [],
    ssh_group: vpc.ssh_group,
    subnets: vpc.subnets.fetch(:private, []),
    startup_grace_period: 300,
    depends_on: [],
    metadata_options: nil,
    audit_role: "arn:aws:iam::#{aws.}:role/auditd_logging",
    metrics_ports: [],
    vpc_endpoints_egress: []
  }.merge(options)

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

  unless options.key? :user_data 
    if options[:ignition] == true 
      options[:user_data] = Ignition.generate(options) 
    elsif options[:ignition] == false 
      options[:user_data] = Cloudconfig.generate(options) 
    end 
  end

  unless options.key?(:loadbalancer_subnets)
    options[:loadbalancer_subnets] = options[:subnets]
  end

  unless options[:instances].is_a?(Hash) || 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.compact
  if options.key? :instance_profile
    @instance_profile = options[:instance_profile]
  else
    iam_statements = options[:iam_policy_statements] + options[:keypairs].map { |kp| kp[:iam_statement] }.compact
    @instance_profile = add! InstanceProfile.create(ident, statements: iam_statements)
  end

   = options[:metadata_options]

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

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

  if options.key?(:loadbalancer) # explicitly requested or rejected a loadbalancer
    wants_load_balancer = options[:loadbalancer]
  elsif options[:cross_zone_load_balancing] # indirect request for an LB
    wants_load_balancer = true
  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,
    metadata_options: ,
    tags: tags
  }

  if wants_load_balancer && @ports.any? { |p| p.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,
                   cross_zone_load_balancing: options[:cross_zone_load_balancing]
                 )
    )

    @load_balancer.attach(@instance_set)

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

    @zone.add_alias_in(self, name, @load_balancer.alias_config)
  elsif set == StaticSet
    @zone.add_record_in(self, name, @instance_set.instances.map(&:ip_address))
    @instance_set.instances.each do |i|
      @domain_names << vpc.zone.qualify(i.name)
      @zone.add_record_in(self, i.name, [i.ip_address])
    end
  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



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

def find_in(_vpc, _name)
  raise 'unimplemented'
end

#with_endpoint_service(options = {}) ⇒ Object



189
190
191
192
193
194
# File 'lib/terrafying/components/service.rb', line 189

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