mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-22 20:54:49 +09:00
1 parent
43ae1589a3
commit
f866e8d2f2
11 files changed
+264
-53
No files matched your search
@@ -173,6 +173,13 @@ class OneProviderHelper < OpenNebulaHelper::OneHelper
|
||||
raise 'Name not found' unless template['name']
|
||||
raise 'Connection info not found' unless template['connection']
|
||||
|
||||
unless OneProvision::Terraform::PROVIDERS.include?(
|
||||
template['provider']
|
||||
)
|
||||
raise 'Invalid provider, available providers: ' \
|
||||
"#{OneProvision::Terraform::PROVIDERS.join(', ')}"
|
||||
end
|
||||
|
||||
template
|
||||
rescue StandardError => e
|
||||
OpenNebula::Error.new("ERROR: #{e}")
|
||||
|
||||
@@ -93,8 +93,12 @@ module OneProvision
|
||||
# Dummy case
|
||||
return unless @body
|
||||
|
||||
conn = @body['connection']
|
||||
conn['provider'] = self['NAME']
|
||||
conn = {}
|
||||
conn['provider'] = @body['provider']
|
||||
|
||||
@body['connection'].each do |k, v|
|
||||
conn[k.upcase] = v
|
||||
end
|
||||
|
||||
conn
|
||||
end
|
||||
@@ -136,7 +140,7 @@ module OneProvision
|
||||
document = {}
|
||||
|
||||
# Create document JSON
|
||||
document['provider'] = template['name']
|
||||
document['provider'] = template['provider']
|
||||
document['connection'] = template['connection']
|
||||
document['registration_time'] = Time.now.to_i
|
||||
|
||||
|
||||
@@ -189,9 +189,8 @@ module OneProvision
|
||||
# @param action [String] Action to perform
|
||||
# @param tf [Hash] Terraform :state and :conf
|
||||
def tf_action(provision, action, tf = {})
|
||||
provider = provision.provider
|
||||
terraform = Terraform.singleton(provider, tf)
|
||||
terraform.conn = provider.connection
|
||||
provider = provision.provider
|
||||
terraform = Terraform.singleton(provider, tf)
|
||||
|
||||
terraform.generate_deployment_file(provision)
|
||||
terraform.send(action)
|
||||
|
||||
@@ -225,32 +225,10 @@ module OneProvision
|
||||
|
||||
info(true)
|
||||
|
||||
# Respect user information, only add provider info if
|
||||
# the user hasn't specified any
|
||||
cfg['defaults'] = {} unless cfg['defaults']
|
||||
|
||||
# Get user UID to see if merge is possible or not
|
||||
# only merge attributes if the provider is owned by user
|
||||
info = Nokogiri::XML(@client.call('user.info', -1))
|
||||
uid = info.xpath('/USER/ID').text
|
||||
|
||||
# Get provider connection information
|
||||
conn = provider.connection
|
||||
|
||||
if uid == provider['UID']
|
||||
cfg['defaults/provision'] = conn.merge(
|
||||
cfg['defaults/provision']
|
||||
)
|
||||
|
||||
OneProvisionLogger.debug('Merging provider connection')
|
||||
else
|
||||
cfg['defaults/provision'] = conn if conn
|
||||
|
||||
OneProvisionLogger.debug('Using provider connection')
|
||||
end
|
||||
|
||||
# read provision file
|
||||
cfg.parse
|
||||
cfg.parse(true)
|
||||
|
||||
puts
|
||||
|
||||
# @name is used for template evaluation
|
||||
@name = cfg['name']
|
||||
|
||||
@@ -55,7 +55,9 @@ module OneProvision
|
||||
end
|
||||
|
||||
# Parses configuration hash to add defaults into each section
|
||||
def parse
|
||||
#
|
||||
# @param eval_ui [Boolean] True to get values, false otherwise
|
||||
def parse(eval_ui = false)
|
||||
begin
|
||||
defaults = @config['defaults']
|
||||
|
||||
@@ -121,6 +123,13 @@ module OneProvision
|
||||
x
|
||||
end
|
||||
end
|
||||
|
||||
################################################################
|
||||
# User inputs
|
||||
################################################################
|
||||
return unless eval_ui
|
||||
|
||||
eval_user_inputs
|
||||
rescue StandardError => e
|
||||
Utils.fail("Failed to read configuration: #{e}")
|
||||
end
|
||||
@@ -384,15 +393,41 @@ module OneProvision
|
||||
matches.each do |match|
|
||||
match = match.split('.')
|
||||
|
||||
################################################################
|
||||
# Special evaluation for keys provision, provison_id and idx
|
||||
################################################################
|
||||
|
||||
if match.size == 1 && !Resource::S_EVAL_KEYS.include?(match[0])
|
||||
return [false, "key #{match[0]} invalid"]
|
||||
end
|
||||
|
||||
next if match.size == 1
|
||||
|
||||
################################################################
|
||||
# User inputs
|
||||
################################################################
|
||||
|
||||
if match.size == 2 && match[0] != 'input'
|
||||
return [false, "key #{match[0]} invalid for user inputs"]
|
||||
end
|
||||
|
||||
if match.size == 2
|
||||
unless @config['inputs']
|
||||
return [false, 'user inputs not found']
|
||||
end
|
||||
|
||||
unless @config['inputs'].find {|v| v['name'] == match[1] }
|
||||
return [false, "user input #{match[1]} not found"]
|
||||
end
|
||||
|
||||
next
|
||||
end
|
||||
|
||||
################################################################
|
||||
# Rules can only access to first level they must be
|
||||
# resource.name.attr
|
||||
################################################################
|
||||
|
||||
return [false, 'there are no 3 elements'] if match.size != 3
|
||||
|
||||
# Only a group of key words is available
|
||||
@@ -451,6 +486,181 @@ module OneProvision
|
||||
hash
|
||||
end
|
||||
|
||||
# Evaluates user inputs
|
||||
def eval_user_inputs
|
||||
return unless @config['inputs']
|
||||
|
||||
iterate(@config, true) do |value|
|
||||
matches = value.to_s.scan(/\$\{(.*?)\}/).flatten
|
||||
|
||||
unless matches.empty?
|
||||
matches.each do |match|
|
||||
# match[0]: input
|
||||
# match[1]: name
|
||||
match = match.split('.')
|
||||
|
||||
next unless match[0] == 'input'
|
||||
|
||||
input = @config['inputs'].find do |v|
|
||||
v['name'] == match[1]
|
||||
end
|
||||
|
||||
if input['value']
|
||||
i_value = input['value']
|
||||
else
|
||||
if $stdout.isatty
|
||||
i_value = ProvisionConfig.ask_user_inputs(input)
|
||||
elsif input['default']
|
||||
i_value = input['default']
|
||||
else
|
||||
Utils.fail(
|
||||
"Cannot parse user input #{input['name']}"
|
||||
)
|
||||
end
|
||||
|
||||
input['value'] = i_value
|
||||
end
|
||||
|
||||
value.gsub!("${#{match.join('.')}}", i_value.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
# Asks for user input value
|
||||
#
|
||||
# @param input [Hash] User input information
|
||||
# rubocop:disable Lint/IneffectiveAccessModifier
|
||||
def self.ask_user_inputs(input)
|
||||
# rubocop:enable Lint/IneffectiveAccessModifier
|
||||
|
||||
puts
|
||||
|
||||
case input['type']
|
||||
when 'text', 'text64'
|
||||
print "Text `#{input['name']}` (default=#{input['default']}): "
|
||||
|
||||
answer = STDIN.readline.chop
|
||||
answer = input['default'] if answer.empty?
|
||||
|
||||
if input['type'] == 'text64'
|
||||
answer = Base64.encode64(answer).strip.delete("\n")
|
||||
end
|
||||
when 'boolean'
|
||||
while %w[YES NO].include?(answer)
|
||||
print "Bool `#{input['name']}` " \
|
||||
"(default=#{input['default']}): "
|
||||
|
||||
answer = STDIN.readline.chop
|
||||
answer = input['default'] if answer.empty?
|
||||
|
||||
unless %w[YES NO].include?(answer)
|
||||
puts "Invalid boolean #{answer} " \
|
||||
'boolean has to be YES or NO'
|
||||
end
|
||||
end
|
||||
when 'password'
|
||||
print "Pass `#{input['name']}` (default=#{input['default']}): "
|
||||
|
||||
STDIN.noecho {|io| answer = io.gets }
|
||||
|
||||
answer = answer.chop! if answer
|
||||
answer = input['default'] if answer.empty?
|
||||
when 'number', 'number-float'
|
||||
valid = false
|
||||
|
||||
until valid
|
||||
print "Num `#{input['name']}` " \
|
||||
"(default=#{input['default']}): "
|
||||
|
||||
answer = STDIN.readline.chop
|
||||
answer = input['default'] if answer.empty?
|
||||
|
||||
begin
|
||||
if input['type'] == 'number'
|
||||
answer = Integer(answer)
|
||||
else
|
||||
answer = Float(answer)
|
||||
end
|
||||
rescue ArgumentError
|
||||
puts 'Wrong format'
|
||||
next
|
||||
end
|
||||
|
||||
valid = true
|
||||
end
|
||||
when 'range', 'range-float'
|
||||
min = input['min_value']
|
||||
max = input['max_value']
|
||||
valid = false
|
||||
|
||||
until valid
|
||||
print "Range `#{input['name']}` [#{min}..#{max}] " \
|
||||
"(default=#{input['default']}): "
|
||||
|
||||
answer = STDIN.readline.chop
|
||||
answer = input['default'] if answer.empty?
|
||||
|
||||
begin
|
||||
if input['type'] == 'range'
|
||||
answer = Integer(answer)
|
||||
else
|
||||
answer = Float(answer)
|
||||
end
|
||||
|
||||
if answer < min || answer > max
|
||||
puts 'Not in range'
|
||||
next
|
||||
end
|
||||
rescue ArgumentError
|
||||
puts 'Wrong format'
|
||||
next
|
||||
end
|
||||
|
||||
valid = true
|
||||
end
|
||||
when 'list'
|
||||
puts
|
||||
input['options'].each_with_index do |opt, i|
|
||||
puts " #{i} #{opt}"
|
||||
end
|
||||
|
||||
puts
|
||||
|
||||
valid = false
|
||||
|
||||
until valid
|
||||
print 'Please type the selection number ' \
|
||||
"(default=#{input['default']}): "
|
||||
|
||||
answer = STDIN.readline.chop
|
||||
answer = input['default'] if answer.empty?
|
||||
|
||||
begin
|
||||
answer = Integer(answer)
|
||||
|
||||
if answer < 0 || answer >= input['options'].size
|
||||
puts 'Index out of range'
|
||||
next
|
||||
end
|
||||
|
||||
answer = input['options'][Integer(answer)]
|
||||
rescue ArgumentError
|
||||
puts 'Wrong format'
|
||||
next
|
||||
end
|
||||
|
||||
valid = true
|
||||
end
|
||||
when 'fixed'
|
||||
answer = input['default']
|
||||
end
|
||||
|
||||
answer
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -64,7 +64,13 @@ module OneProvision
|
||||
xml.send(key.upcase, value)
|
||||
end
|
||||
|
||||
xml.send('PROVIDER', @provider['NAME'])
|
||||
# TODO: remove this and uncomment line below
|
||||
@provider.connection.each do |k, v|
|
||||
xml.send(k, v)
|
||||
end
|
||||
|
||||
# xml.send('PROVIDER', @provider.body['provider'])
|
||||
xml.send('PROVIDER_NAME', @provider['NAME'])
|
||||
end
|
||||
|
||||
if @p_template['configuration']
|
||||
|
||||
@@ -32,9 +32,10 @@ module OneProvision
|
||||
|
||||
# Class constructor
|
||||
#
|
||||
# @param state [String] Terraform state in base64
|
||||
# @param conf [String] Terraform config state in base64
|
||||
def initialize(state, conf)
|
||||
# @param provider [Provider]
|
||||
# @param state [String] Terraform state in base64
|
||||
# @param conf [String] Terraform config state in base64
|
||||
def initialize(provider, state, conf)
|
||||
@dir = "#{PROVIDERS_LOCATION}/templates/aws"
|
||||
|
||||
# User data should be encoded in base64
|
||||
|
||||
@@ -31,9 +31,10 @@ module OneProvision
|
||||
|
||||
# Class constructor
|
||||
#
|
||||
# @param state [String] Terraform state in base64
|
||||
# @param conf [String] Terraform config state in base64
|
||||
def initialize(state, conf)
|
||||
# @param provider [Provider]
|
||||
# @param state [String] Terraform state in base64
|
||||
# @param conf [String] Terraform config state in base64
|
||||
def initialize(provider, state, conf)
|
||||
@dir = "#{PROVIDERS_LOCATION}/templates/packet"
|
||||
|
||||
# User data is in plain text
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
provider "aws" {
|
||||
access_key = "<%= conn['aws_access'] %>"
|
||||
secret_key = "<%= conn['aws_secret'] %>"
|
||||
region = "<%= conn['aws_region'] %>"
|
||||
access_key = "<%= conn['AWS_ACCESS'] %>"
|
||||
secret_key = "<%= conn['AWS_SECRET'] %>"
|
||||
region = "<%= conn['AWS_REGION'] %>"
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
provider "packet" {
|
||||
auth_token = "<%= conn['packet_token'] %>"
|
||||
auth_token = "<%= conn['PACKET_TOKEN'] %>"
|
||||
}
|
||||
|
||||
@@ -38,15 +38,18 @@ module OneProvision
|
||||
# Terraform operations
|
||||
class Terraform
|
||||
|
||||
attr_accessor :conn
|
||||
# Providers that are currently available
|
||||
PROVIDERS = %w[aws packet]
|
||||
|
||||
# Class constructor
|
||||
#
|
||||
# @param state [String] Terraform state in base64
|
||||
# @param conf [String] Terraform config state in base64
|
||||
def initialize(state = nil, conf = nil)
|
||||
@state = state
|
||||
@conf = conf
|
||||
# @param provider [Provider]
|
||||
# @param state [String] Terraform state in base64
|
||||
# @param conf [String] Terraform config state in base64
|
||||
def initialize(provider = nil, state = nil, conf = nil)
|
||||
@state = state
|
||||
@conf = conf
|
||||
@provider = provider
|
||||
end
|
||||
|
||||
# Get a provider instance
|
||||
@@ -58,17 +61,17 @@ module OneProvision
|
||||
def self.singleton(provider, tf)
|
||||
return Dummy.new if Integer(provider['ID']) == -1
|
||||
|
||||
case provider['NAME']
|
||||
case provider.body['provider']
|
||||
when 'packet'
|
||||
tf_class = Packet
|
||||
when 'aws'
|
||||
tf_class = AWS
|
||||
else
|
||||
raise OneProvisionLoopException,
|
||||
"Unknown provider: #{provider['NAME']}"
|
||||
"Unknown provider: #{provider.body['provider']}"
|
||||
end
|
||||
|
||||
tf_class.new(tf[:state], tf[:conf])
|
||||
tf_class.new(provider, tf[:state], tf[:conf])
|
||||
end
|
||||
|
||||
# Generate Terraform deployment file
|
||||
@@ -80,7 +83,7 @@ module OneProvision
|
||||
@conf = ''
|
||||
|
||||
c = File.read("#{@dir}/provider.erb")
|
||||
c = ERBVal.render_from_hash(c, :conn => @conn)
|
||||
c = ERBVal.render_from_hash(c, :conn => @provider.connection)
|
||||
|
||||
@conf << c
|
||||
|
||||
@@ -288,6 +291,8 @@ module OneProvision
|
||||
|
||||
next if !p || p.empty?
|
||||
|
||||
p = p.merge(@provider.connection)
|
||||
|
||||
yield(obj) if block_given?
|
||||
|
||||
c = File.read("#{@dir}/#{erb}")
|
||||
|
||||
Reference in new issue
Block a user