<commit_before>#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2015, <NAME>.                                            #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

require 'drivers/bursting_driver'
require 'occi-api'

include REXML

class OcciDriver < BurstingDriver

  DRIVER_CONF    = "#{ETC_LOCATION}/occi_driver.conf"
  DRIVER_DEFAULT = "#{ETC_LOCATION}/occi_driver.default"

  # Child driver specific attributes
  DRV_POLL_ATTRS = {
    :privateaddress => POLL_ATTRS[:privateaddresses],
    :publicaddress  => POLL_ATTRS[:publicaddresses]
  }
  
  # Public provider commands costants
  PUBLIC_CMD = {
    :run => {
      :cmd => :create,
      :args => {
        "OS_TPL" => {
          :opt => 'os_tpl'
        },
        "RESOURCE_TPL" => {
          :opt => 'resource_tpl'
        },
      },
    }
  }

  def initialize(host)
    
    super(host)

    @context_path   = @public_cloud_conf['context_path']
    @ca_path        = @public_cloud_conf['ca_path']
    @instance_types = @public_cloud_conf['instance_types']
    
    @hostname = host
    
    hosts = @public_cloud_conf['hosts']
    @host = hosts[host] || hosts["default"]
    
    @endpoint        = @host['endpoint']
    @x509_user_proxy = @host['x509_user_proxy']
    @voms            = @host['voms']
    @type            = @host['type']
    @context_path.concat("/#{@host['provider']}/")
    
  end

  def create_instance(vm_id, opts, context_xml)
    
    # TODO: Manage the user proxy based on the actual user requesting it
    user_cert = @x509_user_proxy
    
    occi    = get_occi_client user_cert
    compute = occi.get_resource "compute"

    ## attach chosen resources to the compute resource
    opts.each {|k,v|
      compute.mixins << v
    }
    
    ## set the title
    compute.title = "one-#{vm_id}"
    
    ## create the compute resource and get its location
    deploy_id = occi.create compute
    
    ## get the compute resource data
    compute_data = occi.describe deploy_id
    
    ## wait until the resource is "active"
    while compute_data.first.attributes.occi.compute.state == "inactive"
      sleep 1
      compute_data = occi.describe deploy_id
    end
    
    ## wait until the resource provides the IP
    while compute_data.first.links.first.attributes.occi.networkinterface.address == ""
      sleep 1
      compute_data = occi.describe deploy_id
    end
    
    # The context_id is one of the IP addresses.
    # The safest solution is to create a context for all the
    # addresses associated to the vm.
    compute_data.first.links.each { |link|
      address = link.attributes.occi.networkinterface.address
      create_context(context_xml, address.gsub(".", "-"))
    }
    
    return deploy_id
  end
  
  def destroy_instance(deploy_id)
  
    occi = get_occi_client

    begin
      compute_data = occi.describe deploy_id<commit_msg>[occi] Added support for additional storage<commit_after>#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2015, <NAME>.                                            #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

require 'drivers/bursting_driver'
require 'occi-api'

include REXML

class OcciDriver < BurstingDriver

  DRIVER_CONF    = "#{ETC_LOCATION}/occi_driver.conf"
  DRIVER_DEFAULT = "#{ETC_LOCATION}/occi_driver.default"

  # Child driver specific attributes
  DRV_POLL_ATTRS = {
    :privateaddress => POLL_ATTRS[:privateaddresses],
    :publicaddress  => POLL_ATTRS[:publicaddresses]
  }
  
  # Public provider commands costants
  PUBLIC_CMD = {
    :run => {
      :cmd => :create,
      :args => {
        "OS_TPL" => {
          :opt => 'os_tpl'
        },
        "RESOURCE_TPL" => {
          :opt => 'resource_tpl'
        },
      },
    }
  }

  def initialize(host)
    
    super(host)

    @context_path   = @public_cloud_conf['context_path']
    @ca_path        = @public_cloud_conf['ca_path']
    @instance_types = @public_cloud_conf['instance_types']
    
    @hostname = host
    
    hosts = @public_cloud_conf['hosts']
    @host = hosts[host] || hosts["default"]
    
    @endpoint        = @host['endpoint']
    @x509_user_proxy = @host['x509_user_proxy']
    @voms            = @host['voms']
    @type            = @host['type']
    @context_path.concat("/#{@host['provider']}/")
    
  end

  def create_instance(vm_id, opts, context_xml)
    
    # TODO: Manage the user proxy based on the actual user requesting it
    user_cert = @x509_user_proxy
    
    occi    = get_occi_client user_cert
    compute = occi.get_resource "compute"

    ## attach chosen resources to the compute resource
    opts.each {|k,v|
      compute.mixins << v
    }
    
    log("#{LOG_LOCATION}/#{vm_id}.log","create","Start")
    
    ## set the title
    compute.title = "one-#{vm_id}"
    
    ## create the compute resource and get its location
    deploy_id = occi.create compute
    
    ## get the compute resource data
    compute_data = occi.describe deploy_id
    
    ## wait until the resource is "active"
    while compute_data.first.attributes.occi.compute.state == "inactive"
      sleep 1
      compute_data = occi.describe deploy_id
    end
    
    ## wait until the resource provides the IP
    while compute_data.first.links.first.attributes.occi.networkinterface.address == ""
      sleep 1
      compute_data = occi.describe deploy_id
    end
    
    log("#{LOG_LOCATION}/#{vm_id}.log","create","compute #{deploy_id} created")
    
    storage_size = value_from_xml(context_xml,"STORAGE_SIZE")
    
    if storage_size
      
      log("#{LOG_LOCATION}/#{vm_id}.log","create","additional storage size #{storage_size} GB")
      
      storage = client.get_resource "storage"
      storage.size = storage_size #In GB
      storage.title = "one-#{vm_id} additional disk"
      
      storage_id = occi.create storage
      
      link = Occi::Core::Link.new("http://schemas.ogf.org/occi/infrastructure#storagelink")
      link.source = deploy_id
      link.target = storage_id

      occi.create link
      
      log("#{LOG_LOCATION}/#{vm_id}.log","create","storage #{storage_id} attached")
    end
    
    # The context_id is one of the IP addresses.
    # The safest solution is to create a context for all the
    # addresses associated to the vm.
    compute_data.first.links.each { |link|
      address = link.attributes.occi.networkinterface.address
      create_context(context_xml, address.gsub(".", "-"))
    }
    
    log("#{LOG_LOCATION}/#{vm_id}.log","create","End")
    
    return deploy_id
  end
  
  def destroy_instance(deploy_id)
  
    occi = get_occi_client

    begin
      compute_data = occi.describe deploy_id
{'fraction_non_alphanumeric': 0.12985074626865672, 'fraction_numerical': 0.004362801377726751, 'mean_word_length': 1.7759719566602932, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4152832', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2380, 'n_words': 596}