Class: CapGCE::GCEHandler

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/cap-gce/gce-handler.rb

Instance Method Summary collapse

Methods included from Utils

all_network_interfaces, contact_point, contact_point_mapping, nat_ip, #project_tag, #roles_tag, #stages_tag, #tag_value

Constructor Details

#initializeGCEHandler

Returns a new instance of GCEHandler.



9
10
11
12
13
# File 'lib/cap-gce/gce-handler.rb', line 9

def initialize
  load_config
  @authorizer = fetch_authorizer
  load_gce
end

Instance Method Details

#applicationObject



61
62
63
# File 'lib/cap-gce/gce-handler.rb', line 61

def application
  Capistrano::Configuration.env.fetch(:application).to_s
end

#defined_rolesObject



53
54
55
# File 'lib/cap-gce/gce-handler.rb', line 53

def defined_roles
  roles(:all).flat_map(&:roles_array).uniq.sort
end

#get_servers_for_role(role) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cap-gce/gce-handler.rb', line 69

def get_servers_for_role(role)
  servers = []
  @gce.each do |_, gce|
    servers += gce.items.select do |i|
      instance_has_tag?(i, roles_tag, role) &&
        instance_has_tag?(i, stages_tag, stage) &&
        instance_has_tag?(i, project_tag, application)
    end
  end
  servers.sort_by(&:name)
end

#instance_idsObject



44
45
46
47
48
49
50
51
# File 'lib/cap-gce/gce-handler.rb', line 44

def instance_ids
  puts defined_roles
    .map { |r| get_servers_for_role(r) }
    .flatten
    .uniq(&:id)
    .map(&:id)
    .join("\n")
end

#load_gceObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cap-gce/gce-handler.rb', line 15

def load_gce
  compute = Google::Apis::ComputeV1::ComputeService.new
  compute.authorization = @authorizer
  configured_zones = get_zones(fetch(:gce_zone))
  @gce = {}
  configured_zones.each do |zone|
    @gce[zone] = compute.list_instances(
      fetch(:gce_project_id),
      zone,
      filter: fetch(:gce_filter)
    )
  end
end

#server_namesObject



35
36
37
38
39
40
41
42
# File 'lib/cap-gce/gce-handler.rb', line 35

def server_names
  puts defined_roles
    .map { |r| get_servers_for_role(r) }
    .flatten
    .uniq(&:id)
    .map(&:name)
    .join("\n")
end

#stageObject



57
58
59
# File 'lib/cap-gce/gce-handler.rb', line 57

def stage
  Capistrano::Configuration.env.fetch(:stage).to_s
end

#status_tableObject



29
30
31
32
33
# File 'lib/cap-gce/gce-handler.rb', line 29

def status_table
  CapGCE::StatusTable.new(
    defined_roles.map { |r| get_servers_for_role(r) }.flatten.uniq(&:id)
  )
end

#tag(tag_name) ⇒ Object



65
66
67
# File 'lib/cap-gce/gce-handler.rb', line 65

def tag(tag_name)
  "tag:#{tag_name}"
end