~nchohan/+junk/mytools

« back to all changes in this revision

Viewing changes to lib/app_controller_client.rb

  • Committer: root
  • Date: 2010-11-03 07:43:57 UTC
  • Revision ID: root@appscale-image0-20101103074357-xea7ja3sor3x93oc
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/ruby -w
 
2
# Programmer: Chris Bunch
 
3
 
 
4
require 'openssl'
 
5
require 'soap/rpc/driver'
 
6
require 'timeout'
 
7
 
 
8
IP_REGEX = /\d+\.\d+\.\d+\.\d+/
 
9
FQDN_REGEX = /[\w\d\.\-]+/
 
10
IP_OR_FQDN = /#{IP_REGEX}|#{FQDN_REGEX}/
 
11
 
 
12
NO_TIMEOUT = -1
 
13
RETRY_ON_FAIL = true
 
14
ABORT_ON_FAIL = false
 
15
 
 
16
class AppControllerClient
 
17
  attr_reader :conn, :ip, :secret
 
18
  
 
19
  def initialize(ip, secret)
 
20
    @ip = ip
 
21
    @secret = secret
 
22
    
 
23
    @conn = SOAP::RPC::Driver.new("https://#{@ip}:17443")
 
24
    @conn.add_method("set_parameters", "djinn_locations", "database_credentials", "app_names", "secret")
 
25
    @conn.add_method("status", "secret")
 
26
    @conn.add_method("update", "app_names", "secret")
 
27
    @conn.add_method("stop_app", "app_name", "secret")    
 
28
    @conn.add_method("get_all_public_ips", "secret")
 
29
    @conn.add_method("backup_appscale", "backup_in_info", "secret")
 
30
    @conn.add_method("kill", "secret")
 
31
  end
 
32
  
 
33
  def make_call(time, retry_on_except)
 
34
    refused_count = 0
 
35
    max = 1000
 
36
 
 
37
    begin
 
38
      Timeout::timeout(time) {
 
39
        yield if block_given?
 
40
      }
 
41
    rescue Errno::ECONNREFUSED
 
42
      if refused_count > max
 
43
        abort("Connection was refused. Is the AppController running?")
 
44
      else
 
45
        refused_count += 1
 
46
        sleep(1)
 
47
        retry
 
48
      end
 
49
    rescue OpenSSL::SSL::SSLError, NotImplementedError, Timeout::Error
 
50
      retry
 
51
    rescue Exception => except
 
52
      if retry_on_except
 
53
        retry
 
54
      else
 
55
        abort("We saw an unexpected error of the type #{except.class} with the following message:\n#{except}.")
 
56
      end
 
57
    end
 
58
  end
 
59
 
 
60
  def get_userappserver_ip(verbose_level="low") 
 
61
    userappserver_ip, status, state, new_state = "", "", "", ""
 
62
    loop {
 
63
      status = get_status()
 
64
 
 
65
      new_state = status.scan(/Current State: ([\w\s\d\.,]+)\n/).flatten.to_s.chomp
 
66
      if verbose_level == "high" and new_state != state
 
67
        puts new_state
 
68
        state = new_state
 
69
      end
 
70
    
 
71
      if status == "false: bad secret"
 
72
        abort("\nWe were unable to verify your secret key with the head node specified in your locations file. Are you sure you have the correct secret key and locations file?\n\nSecret provided: [#{@secret}]\nHead node IP address: [#{@ip}]\n")
 
73
      end
 
74
        
 
75
      if status =~ /Database is at (#{IP_OR_FQDN})/ and $1 != "not-up-yet"
 
76
        userappserver_ip = $1
 
77
        break
 
78
      end
 
79
      
 
80
      sleep(10)
 
81
    }
 
82
    
 
83
    return userappserver_ip
 
84
  end
 
85
 
 
86
  def set_parameters(locations, creds, apps_to_start)
 
87
    result = ""
 
88
    make_call(10, ABORT_ON_FAIL) { 
 
89
      result = conn.set_parameters(locations, creds, apps_to_start, @secret)
 
90
    }  
 
91
    abort(result) if result =~ /Error:/
 
92
  end
 
93
 
 
94
  def status(print_output=true)
 
95
    status = get_status()
 
96
         
 
97
    if print_output
 
98
      puts "Status of node at #{ip}:"
 
99
      puts "#{status}"
 
100
    end
 
101
 
 
102
    return status
 
103
  end
 
104
 
 
105
  def get_status()
 
106
    make_call(10, RETRY_ON_FAIL) { @conn.status(@secret) }
 
107
  end
 
108
 
 
109
  def stop_app(app_name)
 
110
    make_call(30, RETRY_ON_FAIL) { @conn.stop_app(app_name, @secret) }
 
111
  end
 
112
  
 
113
  def update(app_names)
 
114
    make_call(30, RETRY_ON_FAIL) { @conn.update(app_names, @secret) }
 
115
  end
 
116
 
 
117
  def get_all_public_ips()
 
118
    make_call(30, RETRY_ON_FAIL) { @conn.get_all_public_ips(@secret) }
 
119
  end
 
120
 
 
121
  def backup_appscale(backup_info)
 
122
    make_call(NO_TIMEOUT, RETRY_ON_FAIL) { @conn.backup_appscale(backup_info, @secret) }
 
123
  end
 
124
 
 
125
  def kill()
 
126
    make_call(NO_TIMEOUT, RETRY_ON_FAIL) { @conn.kill(@secret) }
 
127
  end
 
128
end