Class: Appforce::Spawn::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/appforce/spawn/template.rb

Class Method Summary collapse

Class Method Details

.ansible_dest_path(client_api_name) ⇒ Object



78
79
80
# File 'lib/appforce/spawn/template.rb', line 78

def self.ansible_dest_path(client_api_name)
  "#{run_path}/#{client_api_name}"
end

.ansible_source_pathObject



74
75
76
# File 'lib/appforce/spawn/template.rb', line 74

def self.ansible_source_path
  File.join(gem_path, '../../../ansible')
end

.copy_template(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/appforce/spawn/template.rb', line 21

def self.copy_template(*args)
  opts = args[1]
  unless opts[:client_api_name]
    logger.error "[#{self.name}##{__method__.to_s}] Appforce::Spawn::MissingParameters -- Missing Client API Name"
    raise Appforce::Spawn::MissingParameters, "Missing Client API Name"
  end
  logger.info "[#{self.name}##{__method__.to_s}] Copying files from Template..."
  FileUtils.cp_r("#{ansible_source_path}/.", "#{ansible_dest_path(opts[:client_api_name])}")
end

.create_dirs(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/appforce/spawn/template.rb', line 6

def self.create_dirs(*args)
  opts = args[1]
  unless opts[:client_api_name]
    logger.error "[#{self.name}##{__method__.to_s}] Appforce::Spawn::MissingParameters -- Missing Client API Name"
    raise Appforce::Spawn::MissingParameters, "Missing Client API Name"
  end
  dir = "#{ansible_dest_path(opts[:client_api_name])}"
  logger.info "[#{self.name}##{__method__.to_s}] Creating directory: #{dir}"
  begin
    FileUtils.mkdir(dir)
  rescue Errno::EEXIST
    logger.warn "[#{self.name}##{__method__.to_s}] Directory (#{dir}) already exists."
  end
end

.download_files(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/appforce/spawn/template.rb', line 31

def self.download_files(*args)
  opts = args[1]
  unless opts[:client_api_name]
    logger.error "[#{self.name}##{__method__.to_s}] Appforce::Spawn::MissingParameters -- Missing Client API Name"
    raise Appforce::Spawn::MissingParameters, "Missing Client API Name"
  end

  logger.info "[#{self.name}##{__method__.to_s}] Pulling Hosts file for Client '#{opts[:client_api_name]}'"
  write_to_file(Appforce::Spawn::Api::Call.get_hosts(*args), "#{ansible_dest_path(opts[:client_api_name])}/hosts")
  logger.info "[#{self.name}##{__method__.to_s}] Pulling Active Users file for Client '#{opts[:client_api_name]}'"
  write_to_file(Appforce::Spawn::Api::Call.get_active_users(*args), "#{ansible_dest_path(opts[:client_api_name])}/active_users.yml")
  logger.info "[#{self.name}##{__method__.to_s}] Pulling Inactive Users file for Client '#{opts[:client_api_name]}'"
  write_to_file(Appforce::Spawn::Api::Call.get_inactive_users(*args), "#{ansible_dest_path(opts[:client_api_name])}/inactive_users.yml")
  logger.info "[#{self.name}##{__method__.to_s}] Pulling Vars file for Client '#{opts[:client_api_name]}'"
  write_to_file(Appforce::Spawn::Api::Call.get_vars(*args), "#{ansible_dest_path(opts[:client_api_name])}/vars.yml")        
  logger.info "[#{self.name}##{__method__.to_s}] Pulling Main Scout file for Client '#{opts[:client_api_name]}'"
  write_to_file(Appforce::Spawn::Api::Call.get_main_scout(*args), "#{ansible_dest_path(opts[:client_api_name])}/roles/scout/vars/main.yml")
  logger.info "[#{self.name}##{__method__.to_s}] Pulling Scout Host Data for Client '#{opts[:client_api_name]}'"
  write_to_file(Appforce::Spawn::Api::Call.get_private_key(*args), "#{ansible_dest_path(opts[:client_api_name])}/private_key.pem")
  logger.info "[#{self.name}##{__method__.to_s}] Pulling Private Key Data for Client '#{opts[:client_api_name]}'"
  File.chmod(0600, "#{ansible_dest_path(opts[:client_api_name])}/private_key.pem")

  host_data = Appforce::Spawn::Api::Call.get_all_host_data(*args)
  host_data.each do |host|
    if host['scout'] == 'true'
      filename = "#{host[host['method']]}.yml"
      logger.info "[#{self.name}##{__method__.to_s}] Pulling Scout Host Data for Host '#{host[host['method']]}'"
      args[1][:host_api_name] = host['id']
      write_to_file(Appforce::Spawn::Api::Call.get_host_scout_vars(*args), "#{ansible_dest_path(opts[:client_api_name])}/roles/scout/vars/#{filename}")
      args[1].delete(:host_api_name)
    end
  end

end

.gem_pathObject



70
71
72
# File 'lib/appforce/spawn/template.rb', line 70

def self.gem_path
  File.expand_path(File.dirname(__FILE__))
end

.run_pathObject



66
67
68
# File 'lib/appforce/spawn/template.rb', line 66

def self.run_path
  Dir.pwd
end

.write_to_file(data, path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/appforce/spawn/template.rb', line 82

def self.write_to_file(data, path)
  begin
    file = File.open(path, "w")
    file.write(data) 
  rescue IOError => e
    logger.fatal "[#{self.name}##{__method__.to_s}] Exception writing to file '#{path}' -- #{e}"
    raise e
  ensure
    file.close unless file == nil
  end
end