Sha256: 921f34197fbb5c69f40dc46b48517ac62aead57dcba89c19c6264c3ea4ef50e5

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

require 'yaml'
module Larrow
  module Runner
    module Session
      extend self

      FILE = "#{ENV['HOME']}/.larrow"
      def login
        return unless check_file
        puts "The larrow config will be generated at #{FILE}."
        data = nil
        loop do
          data = [:qy_access_key_id,
                  :qy_secret_access_key,
                  :zone_id,
                  :keypair_id].
                  reduce({}){|s,key| s.update key => value_for(key)}

          tmp_cloud = Service::Cloud.new data
          begin
            tmp_cloud.check_available
            RunLogger.info "login success! write to ~/.larrow"
            break
          rescue Exception => e
            RunLogger.info "login fail: #{e.message}"
            return unless ask "try again"
          end
        end
        content={'qingcloud' => data}
        File.write FILE, YAML.dump(content)
      end

      def cloud
        @cloud ||= begin
                     args = YAML.
                       load(File.read FILE).
                       with_indifferent_access[:qingcloud]
                     Service::Cloud.new(args).check_available
                   rescue
                     fail(InvalidConfigFile,
                          {file:FILE,
                           wiki:"setup-cloud-account"}
                         )
                   end
      end

      def value_for name
        print sprintf("%25s: ", name)
        v = $stdin.gets.strip
        v.empty? ? nil : v
      end

      def check_file
        return true unless File.exist?(FILE)
        puts "#{FILE} does exist: "
        puts File.read(FILE)
        RunOption[:force] || ask("overwrite #{FILE}")
      end

      def ask title
        print "#{title} ? (yes/[no]) "
        v = $stdin.gets.strip
        ['yes','y','Y','Yes','YES'].include? v
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
larrow-runner-0.0.3 lib/larrow/runner/session.rb
larrow-runner-0.0.2 lib/larrow/runner/session.rb