Class: VagrantSubutai::Blueprint::EnvironmentController
- Inherits:
-
Object
- Object
- VagrantSubutai::Blueprint::EnvironmentController
- Defined in:
- lib/vagrant-subutai/blueprint/environment_controller.rb
Instance Attribute Summary collapse
-
#ansible ⇒ Object
Environment name.
-
#container_ids ⇒ Object
Environment name.
-
#id ⇒ Object
Environment name.
-
#log ⇒ Object
Environment name.
-
#name ⇒ Object
Environment name.
-
#peer_os_token ⇒ Object
Environment name.
-
#tracker_id ⇒ Object
Environment name.
Instance Method Summary collapse
- #build(url, token, rh_id, peer_id, mode) ⇒ Object
-
#check_free_quota(resource) ⇒ Object
Checks peer available resource ram, disk.
-
#list(url, token) ⇒ Object
Gets Environment from Subutai Console REST API.
Instance Attribute Details
#ansible ⇒ Object
Environment name
9 10 11 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 9 def ansible @ansible end |
#container_ids ⇒ Object
Environment name
9 10 11 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 9 def container_ids @container_ids end |
#id ⇒ Object
Environment name
9 10 11 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 9 def id @id end |
#log ⇒ Object
Environment name
9 10 11 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 9 def log @log end |
#name ⇒ Object
Environment name
9 10 11 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 9 def name @name end |
#peer_os_token ⇒ Object
Environment name
9 10 11 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 9 def peer_os_token @peer_os_token end |
#tracker_id ⇒ Object
Environment name
9 10 11 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 9 def tracker_id @tracker_id end |
Instance Method Details
#build(url, token, rh_id, peer_id, mode) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 17 def build(url, token, rh_id, peer_id, mode) variable = VagrantSubutai::Blueprint::VariablesController.new(@free_ram, @free_disk, mode) variable. = token # needs cookies while reserving domain to Bazaar variable.check_required_quota if mode == Configs::Blueprint::MODE::PEER variable.user_variables if variable.has_ansible? @ansible = variable.ansible end params = variable.params(rh_id, peer_id) @name = params['name'] response = Rest::SubutaiConsole.environment(url, token, params.to_json) case response when Net::HTTPAccepted json = JSON.parse(response.body) Put.warn "\nStarted \"#{@name}\" environment building ...... \n" @id = json['environmentId'] @tracker_id = json['trackerId'] @log = VagrantSubutai::Rest::SubutaiConsole.log(url, token, @tracker_id) @log = JSON.parse(@log.body) decoded_log = Base64.decode64(@log['log']) logs = decoded_log.split(/\{(.*?)\}\,/) @logs_last_index = nil # this saves last logs index (for not showing duplicated logs) @temp_last_index = nil logs.each_with_index do |v, i| v = v.split(',') v.shift Put.info "#{v[1]} #{v[0]}" unless v.empty? @temp_last_index = i end @logs_last_index = @temp_last_index until @log['state'] == Configs::EnvironmentState::SUCCEEDED || @log['state'] == Configs::EnvironmentState::FAILED @log = VagrantSubutai::Rest::SubutaiConsole.log(url, token, @tracker_id) begin @log = JSON.parse(@log.body) decoded_log = Base64.decode64(@log['log']) logs = decoded_log.split(/\{(.*?)\}\,/) logs.each_with_index do |v, i| if @logs_last_index < i v = v.split(',') v.shift Put.info "#{v[1]} #{v[0]}" unless v.empty? end @temp_last_index = i end @logs_last_index = @temp_last_index rescue JSON::ParserError => e Put.error e end sleep 5 # sleep 5 seconds end if @log['state'] == Configs::EnvironmentState::SUCCEEDED Put.success "\nEnvironment State: #{@log['state']}" if variable.has_ansible? env = list(url, token) ansible = VagrantSubutai::Blueprint::AnsibleController.new(@ansible, env, url, token) ansible.hosts ansible.download ansible.run else list(url, token) end domain = variable.domain unless domain.nil? response = VagrantSubutai::Rest::SubutaiConsole.domain(url, token, @id, domain.name) case response when Net::HTTPOK response = VagrantSubutai::Rest::SubutaiConsole.port(url, token, @id, @container_ids[domain.container_hostname], domain.internal_port) unless response.code == 200 Put.error response. Put.error response.body end ip = url.gsub("https://", "") ip = ip.gsub(':8443', '') if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ Put.warn "MESSAGE You're environment has been setup for a *local* #{domain.name}. You can map this domain to the IP address #{ip} in your C:\\Windows\\System32\\drivers\\etc\\hosts file or to your local DNS." else Put.warn "MESSAGE You're environment has been setup for a *local* #{domain.name}. You can map this domain to the IP address #{ip} in your /etc/hosts file or to your local DNS." end else Put.error response.body Put.error response.code Put.error response. end end else Put.error "\nEnvironment State: #{@log['state']}" end else Put.error "Error: #{response.body}" end elsif mode == Configs::Blueprint::MODE::BAZAAR # Bazaar new REST API to build blueprint provisioning response = VagrantSubutai::Rest::Bazaar.variables(variable.json, peer_id, token) case response when Net::HTTPOK variables = JSON.parse(response.body) Put.debug("Bazaar variables rest response: #{variables}") conf_user_variables = SubutaiConfig.get(:USER_VARIABLES) if conf_user_variables.nil? conf_user_variables = {} else if conf_user_variables.kind_of?(String) begin conf_user_variables = JSON.parse(SubutaiConfig.get(:USER_VARIABLES)) rescue JSON::ParserError => e Put.error e return end end end params = [] Put.debug "JSON parsed variables response: #{variables}" variables.each do |var| Put.debug("For each in variables: #{var}") temp = var if conf_user_variables[var['name']].nil? temp['value'] = variable.get_input_bazaar(var) else temp['value'] = conf_user_variables[var['name']] end params << temp end Put.debug("Bazaar blueprint rest endpoint params: ") Put.debug("Variable: #{variable.json}") Put.debug("params: #{params.to_json}") Put.debug("peer_id: #{peer_id}") response = Rest::Bazaar.blueprint(variable.json, params, peer_id, token) case response when Net::HTTPAccepted json = JSON.parse(response.body) Put.debug("Bazaar blueprint rest endpoint response accepted: #{json}") hub_id = json['id'] # Hub id subutai_id = json['subutai_id'] # Subutai id @id = subutai_id Put.warn "\nStarted environment building ...... \n" # Track environment create state logs @log = Rest::Bazaar.log(token, subutai_id) @log = JSON.parse(@log.body) timer = Time.now + (60 * 60 * 17) # 17 hours @last_peer_state = nil until (@log['environment_status'] == Configs::EnvironmentState::HEALTHY || @log['environment_status'] == Configs::EnvironmentState::UNHEALTHY) && Time.now <= timer @log = Rest::Bazaar.log(token, subutai_id) begin @log = JSON.parse(@log.body) environment_peers = @log['environment_peers'] environment_peers.each_with_index do |v, i| if (@last_peer_state != v['peer_state']) Put.info v['peer_state'] Put.info v['peer_message'] end @last_peer_state = v['peer_state'] end rescue JSON::ParserError => e Put.error e end sleep 5 # sleep 5 seconds end if @log['environment_status'] == Configs::EnvironmentState::HEALTHY Put.success "\nEnvironment State: #{@log['environment_status']}" # Track ansible logs unless @log['environment_applications'].empty? arr = @log['environment_applications'] arr.each_with_index do |environment_application, i| @tmp = nil until (@log['environment_applications'][i])['application_state'] == Configs::ApplicationState::INSTALLED @log = Rest::Bazaar.log(token, subutai_id) begin @log = JSON.parse(@log.body) if @tmp.nil? Put.info (@log['environment_applications'][i])['application_log'] else msg = (@log['environment_applications'][i])['application_log'] if @tmp.length < msg.length msg = msg[(@tmp.length)..(msg.length-1)] Put.info msg end end @tmp = (@log['environment_applications'][i])['application_log'] rescue JSON::ParserError => e Put.error e end sleep 5 # sleep 5 seconds end end end elsif @log['environment_status'] == Configs::EnvironmentState::UNHEALTHY Put.error "\nEnvironment State: #{@log['environment_status']}" elsif timer < Time.now Put.error "\nEnvironment State: Timeout environment creating" else Put.error "\nEnvironment State: #{@log['environment_status']}" end else Put.debug("Bazaar blueprint rest endpoint error: ") Put.error response.body end else Put.debug "Failed bazaar variable rest endpoint" Put.error response.body Put.error response. end end end |
#check_free_quota(resource) ⇒ Object
Checks peer available resource ram, disk
272 273 274 275 276 277 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 272 def check_free_quota(resource) resource = JSON.parse(resource) @free_ram = resource['RAM']['free'].to_f / 1073741824 # convert bytes to gb @free_disk = (resource['Disk']['total'].to_f - resource['Disk']['used'].to_f) / 1073741824 # convert bytes to gb end |
#list(url, token) ⇒ Object
Gets Environment from Subutai Console REST API
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/vagrant-subutai/blueprint/environment_controller.rb', line 280 def list(url, token) env = VagrantSubutai::Models::Console::Environment.new response = VagrantSubutai::Rest::SubutaiConsole.environments(url, token) @container_ids = {} case response when Net:: HTTPOK environments = JSON.parse(response.body) environments.each do |environment| if environment['id'] == @id env.id = @id env.name = environment['name'] env.status = environment['status'] env.containers = [] environment['containers'].each do |container| if container['templateName'] == VagrantSubutai::Configs::Ansible::TEMPLATE_NAME env.ansible_host_id = container['id'] env.ansible_container_state = container['state'] else cont = VagrantSubutai::Models::Console::Container.new cont.id = container['id'] cont.environmentId = container['environmentId'] cont.hostname = container['hostname'] cont.ip = container['ip'] cont.templateName = container['templateName'] cont.templateId = container['templateId'] cont.type = container['type'] cont.arch = container['arch'] cont.peerId = container['peerId'] cont.hostId = container['hostId'] cont.local = container['local'] cont.state = container['state'] cont.rhId = container['rhId'] cont.quota = container['quota'] cont.dataSource = container['dataSource'] cont.containerName = container['containerName'] @container_ids[cont.hostname] = cont.id env.containers << cont end end end end else Put.error response.body raise 'Can\'t get Environment lists from Subutai Console' end env end |