lib/forj-account.rb in forj-0.0.40 vs lib/forj-account.rb in forj-0.0.41

- old
+ new

@@ -31,10 +31,22 @@ require 'hpcloud/config' require 'hpcloud/accounts' require 'hpcloud/connection' include HP::Cloud +class ForjAccounts + # Class to query FORJ Accounts list. + def initialize() + end + + def dump() + aAccounts=[] + Dir.foreach($FORJ_ACCOUNTS_PATH) { |x| aAccounts << x if not x.match(/^\..?$/) } + aAccounts + end +end + class ForjAccount attr_reader :sAccountName attr_reader :hAccountData @@ -42,12 +54,12 @@ def initialize(oConfig) # Initialize object @oConfig = oConfig - if @oConfig.get('account_name') - @sAccountName = @oConfig.get('account_name') + if @oConfig.get(:account_name) + @sAccountName = @oConfig.get(:account_name) else @sAccountName = 'hpcloud' end @sAccountFile = File.join($FORJ_ACCOUNTS_PATH, @sAccountName) @@ -57,33 +69,86 @@ @hAccountData = {} rhSet(@hAccountData, @sAccountName, [:account, :name]) if rhExist?(@hAccountData, [:account, :name]) != 2 rhSet(@hAccountData, sProvider, [:account, :provider]) if rhExist?(@hAccountData, [:account, :provider]) != 2 end - def get(section, key, default = nil) - @oConfig.get(key, rhGet(@hAccountData, section), default ) - end + # oForjAccount data get are retrieved from the account file under section described in defaults.yaml (:account_section_mapping), as soon as this mapping exists. + # If not found, get the data from the local configuration file. Usually ~/.forj/config.yaml + # If not found, get the data from defaults.yaml + # otherwise, use the get default parameter as value. Default is nil. + def get(key, default = nil) + return nil if not key + key = key.to_sym if key.class == String + section = rhGet(@oConfig.getAppDefault(:account_section_mapping, key), :section) + yInterm = nil + yInterm = rhGet(@hAccountData, section) if section + @oConfig.get(key, yInterm , default ) + end + + def exist?(key) + return nil if not key + + key = key.to_sym if key.class == String + section = rhGet(@oConfig.getAppDefault(:account_section_mapping, key), :section) + yInterm = nil + yInterm = rhGet(@hAccountData, section) if section + @oConfig.exist?(key, yInterm) + + end + + def set(key, value) + return nil if not key + + key = key.to_sym if key.class == String + section = rhGet(@oConfig.getAppDefault(:account_section_mapping, key), :section) + return nil if not section + rhSet(@hAccountData, value, section, key) + end + + def del(key) + return nil if not key + + key = key.to_sym if key.class == String + section = rhGet(@oConfig.getAppDefault(:account_section_mapping, key), :section) + return nil if not section + rhSet(@hAccountData, nil, section, key) + end + + def getAccountData(section, key, default=nil) + return rhGet(@hAccountData, section, key) if rhExist?(@hAccountData, section, key) == 2 + default + end + def ac_load(sAccountName = @sAccountName) # Load Account Information if sAccountName != @sAccountName @sAccountName = sAccountName @sAccountFile = File.join($FORJ_ACCOUNTS_PATH, @sAccountName) end if File.exists?(@sAccountFile) @hAccountData = @oConfig.ExtraLoad(@sAccountFile, :forj_accounts, @sAccountName) + # Check if hAccountData are using symbol or needs to be updated. sProvider = @oConfig.get(:provider, nil, 'hpcloud') rhSet(@hAccountData, @sAccountName, :account, :name) if rhExist?(@hAccountData, :account, :name) != 2 rhSet(@hAccountData, sProvider, :account, :provider) if rhExist?(@hAccountData, :account, :provider) != 2 provider_load() + if rhKeyToSymbol?(@hAccountData, 2) + @hAccountData = rhKeyToSymbol(@hAccountData, 2) + self.ac_save() + end return @hAccountData end nil end + def dump() + { :forj_account => @hAccountData, :hpc_account => provider_load() } + end + def ac_save() @oConfig.ExtraSet(:forj_accounts, @sAccountName, nil, @hAccountData) @oConfig.ExtraSave(@sAccountFile, :forj_accounts, @sAccountName) if not @oConfig.LocalDefaultExist?('account_name') @@ -107,11 +172,11 @@ # Check/create keypair self.keypair_setup() # Checking cloud connection Logging.message("Checking cloud connection") - oFC = ForjConnection.new(@oConfig) + ForjConnection.new(@oConfig) Logging.message("Setup '%s' done. Thank you." % @sAccountName) end def setup_provider_account() @@ -133,11 +198,11 @@ when nil Logging.fatal(1, "Unable to execute 'hpcloud' cli. Please check hpcloud installation.") end provider_load() # To ensure latest provider data are loaded - + setup_tenant_name() end def provider_load() # TODO: Should be provider agnostic @@ -146,14 +211,13 @@ # Maestro compute use openstack. It requires meta tenant_name (not ID). Need to query HPC to get the Project Name from the ID. @oConfig.ExtraLoad(hpc_account_file, :hpc_accounts, @sAccountName) end + # Maestro uses fog/openstack to connect to the cloud. It needs Tenant name instead of tenant ID. + # Getting it from Compute connection and set it def setup_tenant_name() - # Maestro uses fog/openstack to connect to the cloud. It needs Tenant name instead of tenant ID. - # Getting it from Compute connection and set it - oSSLError=SSLErrorMgt.new # Retry object Logging.debug("Getting tenants from hpcloud cli libraries") begin tenants = Connection.instance.tenants(@sAccountName) rescue => e @@ -184,11 +248,11 @@ yDNS = {} if not yDNS sAsk = "Optionally, you can ask Maestro to use/manage a domain name on your cloud. It requires your DNS cloud service to be enabled.\nDo you want to configure it?" if agree(sAsk) # Getting tenants - tenants = @oConfig.get('tenants') + tenants = @oConfig.get(:tenants) # Question about DNS Tenant ID # In HPCloud : credentials/tenant_id aDNS_TenantIDs = [] sDNS_TenantIDs = rhGet(yDNS, :tenant_id) @@ -246,16 +310,16 @@ # manage keypair attached to a FORJ account. def keypair_setup() # Getting Account keypair information yCreds = rhGet(@hAccountData, :credentials) - key_name = @oConfig.get('keypair_name', yCreds ) - orig_key_path = File.expand_path(@oConfig.get('keypair_path', yCreds)) - + key_name = @oConfig.get(:keypair_name, yCreds ) + orig_key_path = File.expand_path(@oConfig.get(:keypair_path, yCreds)) + Logging.warning("'keypair_path' is missing at least from defaults.yaml. To fix it, set it in your configuration file ~/.forj/config.yaml under default section") if not orig_key_path key_path = nil - while not key_path + while not key_path key_path = ask ("Please provide the SSH private key path used by default on this account:") do | q | q.default = orig_key_path q.validate = /.*+/ end keys_entered = keypair_detect(key_name, key_path) @@ -283,11 +347,11 @@ key_name = ask ("Please provide the keypair name used by default on this account:") do | q | q.default = key_name q.validate = /.*+/ end key_name = key_name.to_s - + keys = keypair_detect(key_name, key_path) Logging.info("Configuring forj keypair '%s'" % [ keys[:keypair_name] ] ) @@ -332,11 +396,11 @@ # Saving sequences if keys[:keypair_path] != $FORJ_KEYPAIRS_PATH if not File.exists?(forj_private_key_file) Logging.info("Importing key pair to FORJ keypairs list.") - FileUtils.copy(private_key_file, forj_private_key_file) + FileUtils.copy(private_key_file, forj_private_key_file) FileUtils.copy(public_key_file, forj_public_key_file) # Attaching this keypair to the account rhSet(@hAccountData, key_name, :credentials, 'keypair_name') rhSet(@hAccountData, forj_private_key_file, :credentials, 'keypair_path') @oConfig.LocalSet(key_name.to_s, private_key_file, :imported_keys) @@ -383,11 +447,11 @@ if enc_hpcloud_os_key hpcloud_os_key_hidden = '*' * Encryptor.decrypt( :value => Base64::strict_decode64(enc_hpcloud_os_key), :key => entr[:key], - :iv => entr[:iv], + :iv => entr[:iv], :salt => entr[:salt] ).length hpcloud_os_key_hidden="[%s]" % hpcloud_os_key_hidden Logging.message("A password is already set for '%s'. If you want to keep it, just press Enter" % [hpcloud_os_user]) else @@ -401,13 +465,13 @@ q.echo = '*' end if hpcloud_os_key == "" and enc_hpcloud_os_key hpcloud_os_key = Encryptor.decrypt(:value => Base64::strict_decode64(enc_hpcloud_os_key), :key => entr[:key], :iv => entr[:iv], :salt => entr[:salt]) else - Logging.message("The password cannot be empty.") if hpcloud_os_key == "" + Logging.message("The password cannot be empty.") if hpcloud_os_key == "" end end - enc_hpcloud_os_key = Base64::strict_encode64(Encryptor.encrypt(:value => hpcloud_os_key, :key => entr[:key], :iv => entr[:iv], :salt => entr[:salt])) + enc_hpcloud_os_key = Base64::strict_encode64(Encryptor.encrypt(:value => hpcloud_os_key, :key => entr[:key], :iv => entr[:iv], :salt => entr[:salt])) cloud_fog = File.join($FORJ_CREDS_PATH, @sAccountName+'.g64') # Security fix: Remove old temp file with clear password. old_file = '%s/master.forj-13.5' % [$FORJ_CREDS_PATH]