lib/setup.rb in forj-0.0.34 vs lib/setup.rb in forj-0.0.35

- old
+ new

@@ -26,56 +26,94 @@ # # Setup module call the hpcloud functions # module Setup - def setup - # delegate the initial configuration to hpcloud (unix_cli) - Kernel.system('hpcloud account:setup') - setup_credentials - save_cloud_fog - #Kernel.system('hpcloud keypairs:add nova') - end -end + def setup(sProvider, oConfig, options ) + begin -def setup_credentials - hpcloud_os_user = ask('Enter hpcloud username: ') - hpcloud_os_key = ask('Enter hpcloud password: ') { |q| q.echo = '*'} + raise 'No provider specified.' if not sProvider - home = File.expand_path('~') - Helpers.create_directory('%s/.cache/forj/' % [home]) - creds = '%s/.cache/forj/creds' % [home] + sAccountName = sProvider # By default, the account name uses the same provider name. + sAccountName = options[:account_name] if options[:account_name] - values = {:credentials => {:hpcloud_os_user=> hpcloud_os_user, :hpcloud_os_key=> hpcloud_os_key}} + if sProvider != 'hpcloud' + raise "forj setup support only hpcloud. '%s' is currently not supported." % sProvider + end - YamlParse.dump_values(values, creds) + # TODO: Support of multiple providers thanks to fog. + # TODO: Replace this code by our own forj account setup, inspired/derived from hpcloud account::setup + # delegate the initial configuration to hpcloud (unix_cli) + hpcloud_data=File.expand_path('~/.hpcloud/accounts') + if File.exists?(File.join(hpcloud_data, 'hp')) and not File.exists?(File.join(hpcloud_data, sAccountName)) and sAccountName != 'hp' + Logging.info("hpcloud: Copying 'hp' account setup to '%s'" % sAccountName) + Kernel.system('hpcloud account:copy hp %s' % [sAccountName]) + end + + case Kernel.system('hpcloud account:setup %s' % [sAccountName] ) + when false + raise "Unable to setup your hpcloud account" + when nil + raise "Unable to execute 'hpcloud' cli. Please check hpcloud installation." + end + + if not oConfig.yConfig['default'].has_key?('account') + oConfig.LocalSet('account',sAccountName) + oConfig.SaveConfig + end + + # Implementation of simple credential encoding for build.sh/maestro + save_maestro_creds(sAccountName) + rescue RuntimeError => e + Logging.fatal(1,e.message) + rescue => e + Logging.fatal(1,"%s\n%s" % [e.message,e.backtrace.join("\n")]) + end + end end +def save_maestro_creds(sAccountName) -def save_cloud_fog - home = File.expand_path('~') + # TODO Be able to load the previous username if the g64 file exists. + hpcloud_os_user = ask('Enter hpcloud username: ') do |q| + q.validate = /\w+/ + q.default = '' + end - cloud_fog = '%s/.cache/forj/master.forj-13.5' % [home] - local_creds = '%s/.cache/forj/creds' % [home] + hpcloud_os_key = ask('Enter hpcloud password: ') do |q| + q.echo = '*' + q.validate = /.+/ + end - creds = '%s/.hpcloud/accounts/hp' % [home] - template = YAML.load_file(creds) - local_template = YAML.load_file(local_creds) + add_creds = {:credentials => {:hpcloud_os_user=> hpcloud_os_user, :hpcloud_os_key=> hpcloud_os_key}} + sForjCache=File.expand_path('~/.cache/forj/') + cloud_fog = '%s/%s.g64' % [sForjCache, sAccountName] - access_key = template[:credentials][:account_id] - secret_key = template[:credentials][:secret_key] - os_user = local_template[:credentials][:hpcloud_os_user] - os_key = local_template[:credentials][:hpcloud_os_key] + Helpers.create_directory(sForjCache) if not File.directory?(sForjCache) - File.open(cloud_fog, 'w') {|file| - file.write('HPCLOUD_OS_USER=%s' % [os_user] + "\n") - file.write('HPCLOUD_OS_KEY=%s' % [os_key] + "\n") - file.write('DNS_KEY=%s' % [access_key] + "\n") - file.write('DNS_SECRET=%s' % [secret_key]) - } + # Security fix: Remove old temp file with clear password. + old_file = '%s/master.forj-13.5' % [sForjCache] + File.delete(old_file) if File.exists?(old_file) + old_file = '%s/creds' % [sForjCache] + File.delete(old_file) if File.exists?(old_file) - command = 'cat %s | gzip -c | base64 -w0 > %s.g64' % [cloud_fog, cloud_fog] - Kernel.system(command) -end \ No newline at end of file + hpcloud_creds = File.expand_path('~/.hpcloud/accounts/%s' % [sAccountName]) + creds = YAML.load_file(hpcloud_creds) + + access_key = creds[:credentials][:account_id] + secret_key = creds[:credentials][:secret_key] + + os_user = add_creds[:credentials][:hpcloud_os_user] + os_key = add_creds[:credentials][:hpcloud_os_key] + + IO.popen('gzip -c | base64 -w0 > %s' % [cloud_fog], 'r+') {|pipe| + pipe.puts('HPCLOUD_OS_USER=%s' % [os_user] ) + pipe.puts('HPCLOUD_OS_KEY=%s' % [os_key] ) + pipe.puts('DNS_KEY=%s' % [access_key] ) + pipe.puts('DNS_SECRET=%s' % [secret_key]) + pipe.close_write + } + Logging.info("'%s' written." % cloud_fog) +end