Class: Dashboard

Inherits:
Object
  • Object
show all
Defined in:
lib/rscalr/model/dashboard.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Dashboard

Returns a new instance of Dashboard.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/rscalr/model/dashboard.rb', line 3

def initialize(config=nil)
  if config.is_a?(Scalr)
    @client = config
  elsif config.nil? || config.is_a?(Hash) 
    @client = Scalr.new(config)
  else
    raise 'Dashboard may only be initialized with a config Hash, Scalr object, or nil (load config from environment)'
  end
  
  @env_id = @client.env_id
end

Instance Method Details

#get_environment(name) ⇒ Object



86
87
88
89
90
91
# File 'lib/rscalr/model/dashboard.rb', line 86

def get_environment(name)
  if @environments.nil?
    load_environments
  end
  @environments[name]
end

#get_farm(name) ⇒ Object



15
16
17
18
19
20
# File 'lib/rscalr/model/dashboard.rb', line 15

def get_farm(name)
  if @farms.nil?
    load_farms
  end
  @farms[name]
end

#get_farm_by_id(farm_id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/rscalr/model/dashboard.rb', line 22

def get_farm_by_id(farm_id)
  if @farms.nil?
    load_farms
  end
  @farms.each { |name,farm|
    return farm if farm.id == farm_id
  }
  
  nil
end

#get_role(name) ⇒ Object



113
114
115
116
117
118
# File 'lib/rscalr/model/dashboard.rb', line 113

def get_role(name)
  if @roles.nil?
    load_roles
  end
  @roles[name]
end

#get_script(name) ⇒ Object



55
56
57
58
59
60
# File 'lib/rscalr/model/dashboard.rb', line 55

def get_script(name)
  if @scripts.nil?
    load_scripts
  end
  @scripts[name]
end

#load_environmentsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rscalr/model/dashboard.rb', line 93

def load_environments
  @environments = {}
  
  scalr_response = @client.environments_list
  scalr_response.root.each_element('EnvironmentSet/Item') { |row| 
    environment = Environment.new
    
    row.each_element { |prop| 
      if "ID" == prop.name
        environment.id = prop.text
      elsif "Name" == prop.name
        environment.name = prop.text
      end
    }
    @environments[environment.name] = environment
  }
  
  @environments
end

#load_farmsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rscalr/model/dashboard.rb', line 33

def load_farms
  @farms = {}
  
  scalr_response = @client.farms_list
  scalr_response.root.each_element('FarmSet/Item') { |row| 
    farm = Farm.new @client
    
    row.each_element { |prop| 
      if "ID" == prop.name
        farm.id = prop.text
      elsif "Name" == prop.name
        farm.name = prop.text
      elsif "Status" == prop.name
        farm.status = prop.text
      end
    }
    @farms[farm.name] = farm
  }
  
  @farms
end

#load_rolesObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rscalr/model/dashboard.rb', line 120

def load_roles
  @roles = {}
  
  scalr_response = @client.roles_list
  scalr_response.root.each_element('RoleSet/Item') { |row| 
    role = Role.new
    
    row.each_element { |prop| 
      if "ID" == prop.name
        role.id = prop.text
      elsif "Name" == prop.name
        role.name = prop.text
      end
    }
    @roles[role.name] = role
  }
  
  @roles
end

#load_scriptsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rscalr/model/dashboard.rb', line 62

def load_scripts
  @scripts = {}
  
  scalr_response = @client.scripts_list
  scalr_response.root.each_element('ScriptSet/Item') { |row| 
    script = Script.new @client
    
    row.each_element { |prop| 
      if "ID" == prop.name
        script.id = prop.text
      elsif "Name" == prop.name
        script.name = prop.text
      elsif "Description" == prop.name
        script.description = prop.text
      elsif "LatestRevision" == prop.name
        script.latest_revision = prop.text.to_i
      end
    }
    @scripts[script.name] = script
  }
  
  @scripts
end

#verbose=(setting) ⇒ Object



193
194
195
# File 'lib/rscalr/model/dashboard.rb', line 193

def verbose= setting
  @client.verbose = setting
end

#verify_script_success(script_execution, exit_value = 0, timeout_sec = 60) ⇒ Object

Blocks up to timeout seconds waiting on log response(s) of script execution. Returns true iff script executed successfully (returned) exit_value on all servers.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rscalr/model/dashboard.rb', line 142

def verify_script_success(script_execution, exit_value=0, timeout_sec=60)
  return false unless script_execution.success?
  
  sleep_time = 5
  
  start_time = Time.now.to_i
  
  # 1. get list of servers that this execution should run on
  farm = get_farm_by_id(script_execution.farm_id)
  if !script_execution.server_id.nil? 
    server = farm.get_server(script_execution.server_id)
    script_execution.add_server(server.server_id)
  elsif !script_execution.farm_role_id.nil?
    role_servers = farm.get_servers_for_role_id(script_execution.farm_role_id) 
    role_servers.each { |server|
      script_execution.add_server(server.server_id)
    }
  else
    farm_servers = farm.get_all_servers
    farm_servers.each { |server|
      script_execution.add_server(server.server_id)
    }
  end
      
  # 2. Call logs list and match results to server IDs
  success = false
  finished = false
  begin
    start = 0
    begin
      loglist = farm.load_script_logs(script_execution.server_id, script_execution.event_id, start, 20)
      total_records = loglist.total_records
  
      loglist.each { |log|
        script_execution.set_server_result(log)
        start += 1
      }
    end while start < total_records
  
    # 3. If not all servers have responses logged, repeat Step 2 until done or timeout_sec seconds have expired
    success = true
    finished = true
    script_execution.server_results.each { |server_id, result|
      success &&= (!result.nil? && result.exec_exit_code == exit_value)
      finished &&= !result.nil?
    }
  end while !finished && (Time.now.to_i - start_time) < (timeout_sec + sleep_time) && sleep(sleep_time)
  
  success
end