Advertisement
Guest User

Untitled

a guest
Jan 18th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. require 'yaml'
  2.  
  3. module Cronenberg
  4. class Config
  5. REQUIRED_VALUES = {
  6. names: [:host, :user, :password],
  7. env_vars: ['VCENTER_SERVER', 'VCENTER_USER', 'VCENTER_PASSWORD'],
  8. }
  9.  
  10. attr_reader :host, :user, :password, :datacenter, :insecure, :port, :ssl
  11.  
  12. DEFAULT_CONFIG_LOCATION = File.join(Dir.home, '.vcenter.conf')
  13.  
  14. def initialize(config_file_location=nil)
  15. settings = process_environment_variables || process_config_file(config_file_location || DEFAULT_CONFIG_LOCATION)
  16. if settings.nil?
  17. raise 'You must provide credentials in either environment variables or a config file.'
  18. else
  19. settings = settings.delete_if { |k, v| v.nil? }
  20. missing = REQUIRED_VALUES[:names] - settings.keys
  21. unless missing.empty?
  22. message = 'To use this module you must provide the following settings:'
  23. missing.each do |var|
  24. message += " #{var}"
  25. end
  26. raise message
  27. end
  28. @host = settings[:host]
  29. @user = settings[:user]
  30. @password = settings[:password]
  31. @datacenter = settings[:datacenter_name]
  32. @insecure = settings[:insecure].nil? ? false : settings[:insecure]
  33. @ssl = settings[:ssl].nil? ? true : settings[:ssl]
  34. @port = settings[:port]
  35. end
  36.  
  37. end
  38.  
  39. def process_environment_variables
  40. required = REQUIRED_VALUES[:env_vars]
  41. missing = required - ENV.keys
  42. if missing.size < required.size
  43. {
  44. host: ENV['VCENTER_SERVER'],
  45. user: ENV['VCENTER_USER'],
  46. password: ENV['VCENTER_PASSWORD'],
  47. datacenter_name: ENV['VCENTER_DATACENTER'],
  48. insecure: ENV['VCENTER_INSECURE'],
  49. port: ENV['VCENTER_PORT'],
  50. ssl: ENV['VCENTER_SSL'],
  51. }
  52. else
  53. nil
  54. end
  55. end
  56.  
  57.  
  58.  
  59. def process_config_file(file_path)
  60. file_present = File.file?(file_path)
  61. unless file_present
  62. nil
  63. else
  64. begin
  65. configuration = YAML.load_file(file_path)
  66. rescue Psych::SyntaxError => e
  67. raise "Your configuration file at #{file_path} is invalid. The error from the YAML parser is:\n #{e.message}"
  68. end
  69. vsphere_config = configuration['vcenter']
  70.  
  71. raise "Invalid configuration file, missing vcenter key." unless vsphere_config.keys.first == 'vcenter'
  72.  
  73. required = REQUIRED_VALUES[:names].map { |var| var.to_s }
  74. missing = required - vsphere_config.keys
  75. if missing.size < required.size
  76. {
  77. host: vsphere_config['host'],
  78. user: vsphere_config['user'],
  79. password: vsphere_config['password'],
  80. datacenter_name: vsphere_config['datacenter'],
  81. insecure: vsphere_config['insecure'],
  82. port: vsphere_config['port'],
  83. ssl: vsphere_config['ssl'],
  84. }
  85. else
  86. nil
  87. end
  88. end
  89. end
  90. end
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement