Module: Occi::Cli::Helpers::CreateHelper

Defined in:
lib/occi/cli/helpers/create_helper.rb

Constant Summary collapse

MIN_TIMEOUT =
5
WAIT_FOR_STATES =
%w(active online).freeze

Instance Method Summary collapse

Instance Method Details

#helper_create(options, output = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/occi/cli/helpers/create_helper.rb', line 6

def helper_create(options, output = nil)
  location = nil

  if resource_types.include?(options.resource) || resource_type_identifiers.include?(options.resource)
    location = helper_create_resource(options)
  else
    Occi::Cli::Log.warn "I have no idea what #{options.resource.inspect} is ..."
    raise "Unknown resource #{options.resource.inspect}, there is nothing to create here!"
  end

  return location if output.nil?

  puts location
end

#helper_create_attach_context_vars(options, res) ⇒ Object



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
# File 'lib/occi/cli/helpers/create_helper.rb', line 114

def helper_create_attach_context_vars(options, res)
  # TODO: find a better/universal way to do contextualization
  return unless options.context_vars
  Occi::Cli::Log.debug "with context variables: #{options.context_vars.inspect}"

  options.context_vars.each_pair do |var, val|
    schema = nil
    mxn_attrs = Occi::Core::Attributes.new

    case var
    when 'public_key', :public_key
      schema = "http://schemas.openstack.org/instance/credentials#"
      mxn_attrs['org.openstack.credentials.publickey.name'] = {}
      mxn_attrs['org.openstack.credentials.publickey.data'] = {}
    when 'user_data', :user_data
      schema = "http://schemas.openstack.org/compute/instance#"
      mxn_attrs['org.openstack.compute.user_data'] = {}
    else
      Occi::Cli::Log.warn "Unknown context variable! #{var.to_s.inspect}"
      schema = "http://schemas.ogf.org/occi/core#"
    end

    mxn = Occi::Core::Mixin.new(schema, var.to_s, 'OS contextualization mixin', mxn_attrs)
    res.mixins << mxn

    case var
    when 'public_key', :public_key
      # Not setting `publickey.name` to avoid conflicts with already registered keys
      # res.attributes['org.openstack.credentials.publickey.name'] = 'Public SSH key'
      res.attributes['org.openstack.credentials.publickey.data'] = val
    when 'user_data', :user_data
      res.attributes['org.openstack.compute.user_data'] = val
    else
      Occi::Cli::Log.warn "Not setting attributes for an unknown context variable! #{var.to_s.inspect}"
    end
  end
end


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
# File 'lib/occi/cli/helpers/create_helper.rb', line 70

def helper_create_attach_links(options, res)
  return unless options.links
  Occi::Cli::Log.debug "with links: #{options.links.inspect}"

  options.links.each do |link|
    if link.start_with? options.endpoint
      link.gsub!(options.endpoint.chomp('/'), '')
    end

    if link.include? "/storage/"
      Occi::Cli::Log.debug "Adding storagelink to #{options.resource.inspect}"
      res.storagelink link
    elsif link.include? "/network/"
      Occi::Cli::Log.debug "Adding networkinterface to #{options.resource.inspect}"
      res.networkinterface link
    elsif link.include? "/securitygroup/"
      Occi::Cli::Log.debug "Adding securitygrouplink to #{options.resource.inspect}"
      kind = 'http://schemas.ogf.org/occi/infrastructure#securitygrouplink'
      sec_grp_kind = 'http://schemas.ogf.org/occi/infrastructure#securitygroup'
      res.link link, kind, [], Occi::Core::Attributes.new, sec_grp_kind
    else
      raise "Unknown link type #{link.inspect}, stopping here!"
    end
  end
end

#helper_create_attach_mixins(options, res) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/occi/cli/helpers/create_helper.rb', line 96

def helper_create_attach_mixins(options, res)
  return unless options.mixins
  Occi::Cli::Log.debug "with mixins: #{options.mixins.inspect}"

  options.mixins.to_a.each do |mxn|
    Occi::Cli::Log.debug "Adding mixin #{mxn.inspect} to #{options.resource.inspect}"

    orig_mxn = model.get_by_id(mxn.type_identifier)
    if orig_mxn.blank?
      orig_mxn = mixin(mxn.term, mxn.scheme.chomp('#'), true)
      raise Occi::Cli::Errors::MixinLookupError,
          "The specified mixin is not declared in the model! #{mxn.type_identifier.inspect}" if orig_mxn.blank?
    end

    res.mixins << orig_mxn
  end
end

#helper_create_resource(options) ⇒ Object



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
# File 'lib/occi/cli/helpers/create_helper.rb', line 21

def helper_create_resource(options)
  Occi::Cli::Log.debug "#{options.resource.inspect} is a resource type."

  res = resource(options.resource)

  Occi::Cli::Log.debug "Creating #{options.resource.inspect}: #{res.inspect}"

  helper_create_attach_mixins(options, res)

  if res.kind_of? Occi::Infrastructure::Compute
    helper_create_attach_links(options, res)
    # TODO: context vars are only attributes!
    helper_create_attach_context_vars(options, res)
  end

  options.attributes.names.each_pair do |attribute, value|
    res.attributes[attribute.to_s] = value
  end

  # TODO: OCCI-OS uses occi.compute.hostname instead of title
  if res.kind_of? Occi::Infrastructure::Compute
    res.hostname = options.attributes["occi.core.title"] if res.hostname.blank?
  end

  # TODO: enable check
  #res.check

  Occi::Cli::Log.debug "Creating #{options.resource.inspect}: #{res.inspect}"
  new_link = create(res)
  helper_create_wait(new_link, options.wait_for_active) if options.wait_for_active > 0

  new_link
end

#helper_create_wait(resource_link, timeout) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/occi/cli/helpers/create_helper.rb', line 55

def helper_create_wait(resource_link, timeout)
  begin
    Timeout::timeout(timeout) {
      Occi::Cli::Log.debug "Starting #{timeout}s wait period for #{resource_link.inspect} to become active"
      while true
        desrc = describe(resource_link).first
        break if !desrc.respond_to?(:state) || WAIT_FOR_STATES.include?(desrc.state)
        sleep MIN_TIMEOUT
      end
    }
  rescue Timeout::Error
    Occi::Cli::Log.warn "Attempted to wait #{timeout}s for #{resource_link.inspect} to become active"
  end
end