Class: VagrantPlugins::OpenNebulaProvider::Helpers::RocciApi

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/opennebula-provider/helpers/rocci.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine, provider_config) ⇒ RocciApi

Returns a new instance of RocciApi.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/opennebula-provider/helpers/rocci.rb', line 15

def initialize(machine, provider_config)
  @logger = Log4r::Logger.new('vagrant::provider::opennebula::helpers::rocciapi')
  @config = provider_config
  options = {
    endpoint: @config.endpoint,
    auth: {
      type: @config.auth,
      username: @config.username,
      password: @config.password
    }
#              :log => {
#              :out   => STDERR,
#              :level => Occi::Api::Log::DEBUG
#            }
  }
  begin
    @rocci = Occi::Api::Client::ClientHttp.new(options)
  rescue Errno::ECONNREFUSED => e
    raise Errors::ConnectError, error: e
  rescue Occi::Api::Client::Errors::AuthnError => e
    raise Errors::AuthError, error: e
  end
end

Instance Method Details

#computeObject



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
# File 'lib/opennebula-provider/helpers/rocci.rb', line 39

def compute
  @logger.info 'compute!'
  cmpt = @rocci.get_resource 'compute'

  os = @rocci.get_mixin @config.os_tpl, 'os_tpl'
  unless os
    mixins = @rocci.get_mixins 'os_tpl'
    mixins.each do |mixin|
      if mixin.title == @config.os_tpl
        os = @rocci.get_mixin mixin.term, 'os_tpl'
      end
    end
    fail Errors::ComputeError, error: I18n.t('opennebula_provider.compute.os_missing', template: @config.os_tpl) unless os
  end

  size = @rocci.get_mixin @config.resource_tpl, 'resource_tpl'
  unless size
    fail Errors::ComputeError, error: I18n.t('opennebula_provider.compute.resource_size_missing', template: @config.resource_tpl)
  end

  cmpt.mixins << os << size
  cmpt.title = @config.title if @config.title
  cmpt_loc = @rocci.create cmpt
  @logger.info "Location of new compute resource: #{cmpt_loc}"
  cmpt_loc
rescue RuntimeError => e
  case e.message
  when /(?<ms>\[VirtualMachineAllocate\])/
    message_scope = $LAST_MATCH_INFO[:ms]
    case e.message
    when /quota/
      raise Errors::QuotaError, error: e.message.split(message_scope)[1].to_s
    else
      raise Errors::AllocateError, error: e
    end
  end
  raise Errors::ComputeError, error: e
end

#delete(id) ⇒ Object



88
89
90
91
# File 'lib/opennebula-provider/helpers/rocci.rb', line 88

def delete(id)
  @logger.info 'delete!'
  @rocci.delete id
end

#machine_state(id) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/opennebula-provider/helpers/rocci.rb', line 93

def machine_state(id)
  if id
    begin
      desc = @rocci.describe id
    rescue ArgumentError => e
      raise Errors::ResourceError, error: e
    end
    return :not_created if desc.empty?
    return desc.first.attributes.occi.compute.state
  else
    return :not_created
  end
end

#ssh_info(id) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/opennebula-provider/helpers/rocci.rb', line 107

def ssh_info(id)
  desc = describe_resource(id)
  networkinterface = desc.first.links.map do |link|
    if link.attributes.occi.key?(:networkinterface)
      link.attributes.occi.networkinterface
    else
      nil
    end
  end.reject! { |n| n.nil? }.first
  networkinterface[:address]
end

#start(id) ⇒ Object



83
84
85
86
# File 'lib/opennebula-provider/helpers/rocci.rb', line 83

def start(id)
  @logger.info 'start!'
  @rocci.trigger id, action_instance(id, 'start')
end

#stop(id) ⇒ Object



78
79
80
81
# File 'lib/opennebula-provider/helpers/rocci.rb', line 78

def stop(id)
  @logger.info 'stop!'
  @rocci.trigger id, action_instance(id, 'stop')
end

#wait_for_state(env, state) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/opennebula-provider/helpers/rocci.rb', line 119

def wait_for_state(env, state)
  retryable(tries: 100, sleep: 6) do
    next if env[:interrupted]
    result = machine_state(env[:machine].id)

    yield result if block_given?
    fail Errors::ComputeError, error: 'Not ready' if result != state
  end
end