Class: Subspace::Commands::Inventory

Inherits:
Base
  • Object
show all
Defined in:
lib/subspace/commands/inventory.rb

Instance Method Summary collapse

Methods inherited from Base

#confirm_overwrite, #copy, #dest_dir, #gem_path, #inventory, #pass_through_params, #playbook_dir, #project_name, #project_path, #require_configuration, #set_subspace_version, #template, #template!, #template_dir

Methods included from Ansible

#ansible_command, #ansible_playbook

Constructor Details

#initialize(args, options) ⇒ Inventory

Returns a new instance of Inventory.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/subspace/commands/inventory.rb', line 3

def initialize(args, options)
  command = args.first
  @env = options.env
  case command
  when "capistrano"
    capistrano_deployrb
  when "list"
    list_inventory
  when "keyscan"
    keyscan_inventory
  else
    say "Unknown or missing command to inventory: #{command}"
    say "try subspace inventory [list, capistrano]"
  end
end

Instance Method Details

#capistrano_deployrbObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/subspace/commands/inventory.rb', line 33

def capistrano_deployrb
  if @env.nil?
    puts "Please provide an environment e.g: subspace inventory capistrano --env production"
    exit
  end

  say "# config/deploy/#{@env}.rb"
  say "# Generated by Subspace"
  inventory.find_hosts!(@env).each do |host|
    host = inventory.hosts[host.name]
    db_role = false
    roles = host.group_list.map do |group_name|
      if group_name =~ /web/
        ["web", "app"]
      elsif group_name =~ /worker/
        ["app", db_role ? nil : "db"]
      end
    end.compact.uniq
    say "server '#{host.vars["ansible_host"]}', user: 'deploy', roles: %w{#{roles.join(' ')}} # #{host.vars["hostname"]}"
  end
end

#keyscan_inventoryObject



25
26
27
28
29
30
31
# File 'lib/subspace/commands/inventory.rb', line 25

def keyscan_inventory
  inventory.find_hosts!(@env || "all").each do |host|
    ip = host.vars["ansible_host"]
    system %Q(ssh-keygen -R #{ip})
    system %Q(ssh-keyscan -Ht ed25519 #{ip} >> "$HOME/.ssh/known_hosts")
  end
end

#list_inventoryObject



19
20
21
22
23
# File 'lib/subspace/commands/inventory.rb', line 19

def list_inventory
  inventory.find_hosts!(@env || "all").each do |host|
    puts "#{host.name}\t#{host.vars["ansible_host"]}\t(#{host.group_list.join ','})"
  end
end