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_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

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

#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
# File 'lib/terrafying/components/service.rb', line 43

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.account_id}:role/auditd_logging"
  }.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
  @ports = enrich_ports(options[:ports])
  @domain_names = [ options[:zone].qualify(name) ]

  depends_on = options[:depends_on] + options[:keypairs].map{ |kp| kp[:resources] }.flatten

  iam_statements = options[:iam_policy_statements] + options[:keypairs].map { |kp| kp[:iam_statement] }
  instance_profile = add! InstanceProfile.create(ident, { statements: iam_statements })

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

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

  wants_load_balancer = (set == DynamicSet && @ports.count > 0) || options[:loadbalancer]

  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 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
\
  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



140
141
142
# File 'lib/terrafying/components/service.rb', line 140

def with_endpoint_service(options = {})
  add! EndpointService.create_for(@load_balancer, @name, options)
end