Module: Larrow::Runner::Session

Extended by:
Session
Included in:
Session
Defined in:
lib/larrow/runner/session.rb

Constant Summary collapse

FILE =
"#{ENV['HOME']}/.larrow"

Instance Method Summary collapse

Instance Method Details

#ask(title) ⇒ Object



60
61
62
63
64
# File 'lib/larrow/runner/session.rb', line 60

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

#check_fileObject



53
54
55
56
57
58
# File 'lib/larrow/runner/session.rb', line 53

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

#cloudObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/larrow/runner/session.rb', line 33

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

#loginObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/larrow/runner/session.rb', line 8

def 
  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

#value_for(name) ⇒ Object



47
48
49
50
51
# File 'lib/larrow/runner/session.rb', line 47

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