2012-10-24 18:22:03 +02:00
#!/usr/bin/env ruby
2012-10-24 18:49:00 +02:00
# -------------------------------------------------------------------------- #
2024-07-29 14:25:20 +02:00
# Copyright 2002-2024, OpenNebula Project, OpenNebula Systems #
2012-10-24 18:49:00 +02:00
# #
# 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. #
#--------------------------------------------------------------------------- #
2012-10-24 18:22:03 +02:00
require 'fileutils'
require 'tmpdir'
2024-09-02 17:15:09 +02:00
VERSION = '6.99.0'
2017-10-16 17:28:36 +02:00
def version
v = VERSION
2023-06-22 04:40:29 -06:00
_, min, incr = v.split('.').collect {|e| e.to_i }
2017-10-16 17:28:36 +02:00
if min >= 80 || incr >= 80
2023-06-22 04:40:29 -06:00
v += '.pre'
2017-10-16 17:28:36 +02:00
end
v
end
2012-10-24 18:22:03 +02:00
DEFAULTS={
2017-10-16 17:28:36 +02:00
:version => version,
2023-06-22 04:40:29 -06:00
:date => Time.now.strftime('%Y-%m-%d'),
2012-10-24 18:22:03 +02:00
:dependencies => []
}
2023-06-22 04:40:29 -06:00
TEMPLATE=<<~EOT
# This file is automatically generated
Gem::Specification.new do |s|
s.name = '$(NAME)'
s.version = '$(VERSION)'
s.date = '$(DATE)'
s.summary = "$(SUMMARY)"
s.description = "$(DESCRIPTION)"
s.authors = ["OpenNebula"]
s.email = 'contact@opennebula.io'
s.license = 'Apache-2.0'
s.files = [
$(FILES)
]
s.homepage = 'http://opennebula.io'
$(EXECUTABLES)
$(DEPENDENCIES)
end
2012-10-24 18:22:03 +02:00
EOT
GEM_DESCRIPTION={
:oca => {
2013-04-01 12:17:19 +02:00
:name => 'opennebula',
2012-10-24 18:22:03 +02:00
:files => [
2012-12-20 05:18:51 -06:00
['src/oca/ruby/opennebula.rb', '/lib'],
2016-06-14 11:54:44 +02:00
# Needed for onevcenter
2021-02-19 14:04:46 +01:00
# TODO: respect source dir. hierarchy, this is terrible mess!
2016-06-14 11:54:44 +02:00
['src/vmm_mad/remotes/vcenter/vcenter_driver.rb', '/lib'],
2021-02-19 14:04:46 +01:00
['src/vmm_mad/remotes/lib/vcenter_driver/**/*.rb', '/lib'],
['src/vmm_mad/remotes/nsx/nsx_driver.rb', '/lib'],
['src/vmm_mad/remotes/lib/nsx_driver/*.rb', '/lib'],
2016-06-14 11:54:44 +02:00
# vcenter_driver.rb dependencies
['src/mad/ruby/VirtualMachineDriver.rb', '/lib'],
['src/mad/ruby/OpenNebulaDriver.rb', '/lib'],
['src/mad/ruby/CommandManager.rb', '/lib'],
['src/mad/ruby/ActionManager.rb', '/lib'],
2023-06-22 04:40:29 -06:00
['src/mad/ruby/HostSyncManager.rb', '/lib'],
2016-06-14 11:54:44 +02:00
['src/mad/ruby/DriverExecHelper.rb', '/lib'],
2021-02-19 14:04:46 +01:00
['src/mad/ruby/scripts_common.rb', '/lib'],
# other depenencies
['src/flow/lib/models.rb', '/lib'],
['src/flow/lib/models/*.rb', '/lib/models'],
2016-06-14 11:54:44 +02:00
2012-12-20 05:18:51 -06:00
['src/oca/ruby/opennebula/*.rb', '/lib/opennebula'],
2021-02-19 14:04:46 +01:00
['src/oca/ruby/opennebula/flow/*.rb', '/lib/opennebula/flow'],
2012-12-20 08:05:29 -06:00
['src/authm_mad/remotes/**/*.rb', '/lib/opennebula'],
2015-09-03 19:03:38 +02:00
['src/cloud/common/CloudClient.rb', '/lib/cloud'],
2012-11-19 15:54:46 +01:00
['NOTICE', ''],
['LICENSE', '']
2012-10-24 18:22:03 +02:00
],
:summary => 'OpenNebula Client API',
:description => 'Libraries needed to talk to OpenNebula',
:dependencies => [
2024-03-26 12:41:01 +01:00
['nokogiri','<1.16'],
2014-11-20 15:04:36 +01:00
'json',
2023-12-13 03:46:33 -07:00
'rbvmomi2',
2021-02-19 14:04:46 +01:00
'xmlrpc',
'treetop', # oneflow
'ipaddress', # oneflow
'parse-cron' # oneflow
2012-10-24 18:22:03 +02:00
]
},
:cli => {
2013-04-01 12:17:19 +02:00
:name => 'opennebula-cli',
2012-10-24 18:22:03 +02:00
:files => [
2020-07-14 17:42:30 +02:00
# Needed for JSON and YAML outputs
['share/doc/xsd/*.xsd', '/share/schemas/xsd'],
2021-02-19 14:04:46 +01:00
['src/cli/one[a-z]*', '/bin', 'one(provider|provision)'],
2012-10-24 18:22:03 +02:00
['src/cli/*.rb', '/lib'],
2021-02-19 14:04:46 +01:00
['src/cli/one_helper/*.rb', '/lib/one_helper', 'one(provider|provision)'],
2012-11-19 15:54:46 +01:00
['NOTICE', ''],
['LICENSE', '']
2012-10-24 18:22:03 +02:00
],
2013-04-02 11:04:59 +02:00
:summary => 'OpenNebula Command Line Interface',
2012-10-24 18:22:03 +02:00
:description => 'Commands used to talk to OpenNebula',
:dependencies => [
2020-07-14 17:42:30 +02:00
['opennebula', "= #{DEFAULTS[:version]}"],
'activesupport'
2012-10-24 18:22:03 +02:00
]
}
}
def sane_prefix(prefix)
return '' if !prefix or prefix.empty?
p=prefix
2023-06-22 04:40:29 -06:00
p.slice!(0) if p[0, 1]=='/'
p<<'/' if p[-1, 1]!='/'
2012-10-24 18:22:03 +02:00
p
end
def file_list(description)
2023-06-22 04:40:29 -06:00
files=[]
2012-10-24 18:22:03 +02:00
2018-12-19 15:26:12 +01:00
description[:files].each do |f, prefix, exclude|
Dir.glob(f).each do |source|
2023-06-22 04:40:29 -06:00
next if !exclude.nil? and source.match(exclude)
2012-10-24 18:22:03 +02:00
2018-12-19 15:26:12 +01:00
files << [source, sane_prefix(prefix)+File.basename(source)]
2012-10-24 18:22:03 +02:00
end
end
files
end
2023-06-22 04:40:29 -06:00
def copy_files(files, source_prefix = '', destination_prefix = '')
2012-10-24 18:22:03 +02:00
files.each do |file|
source=source_prefix+file[0]
destination=destination_prefix+file[1]
dir=File.dirname destination
2023-06-22 04:40:29 -06:00
FileUtils.mkdir_p(dir) unless File.exist?(dir)
2012-10-24 18:22:03 +02:00
FileUtils.cp(source, destination)
end
end
def generate_gem_file_list(files)
files.map do |f|
" '#{f.last}'"
end.join(",\n")
end
def generate_gem_executable_list(files)
2023-06-22 04:40:29 -06:00
executables=files
.select {|f| f.last.match(%r{^bin/}) }
.map {|f| "'#{File.basename(f.last)}'" }
.join(', ')
2012-10-24 18:22:03 +02:00
if !executables.empty?
" s.executables=[#{executables}]"
else
nil
end
end
def generate_dependencies(dependencies)
dependencies.map do |d|
2023-06-22 04:40:29 -06:00
line=' s.add_runtime_dependency '
if d.is_a?(Array)
line<<d.map {|part| "'#{part}'" }.join(', ')
2012-10-24 18:22:03 +02:00
else
line<<"'#{d}'"
end
line
end.join("\n")
end
def populate_gem_template(description, files)
reg=/\$\((\w+)\)/
TEMPLATE.gsub(reg) do |chunk|
m=chunk.match(reg)
var=m[1].downcase.to_sym
if var==:files
generate_gem_file_list(files)
elsif var==:dependencies
generate_dependencies(
2023-06-22 04:40:29 -06:00
description[:dependencies]||DEFAULTS[:dependencies]
)
2012-10-24 18:22:03 +02:00
elsif var==:executables
generate_gem_executable_list(files)
elsif description[var]
description[var]
else
DEFAULTS[var]
end
end
end
def generate_gem_file(gem_file, description, files)
2023-06-22 04:40:29 -06:00
File.open(gem_file, 'w') do |f|
2012-10-24 18:22:03 +02:00
f.write(populate_gem_template(description, files))
end
end
def generate_gem(description)
Dir.mktmpdir do |tmp|
files=file_list(description)
2023-06-22 04:40:29 -06:00
copy_files(files, '', tmp+'/')
2012-10-24 18:22:03 +02:00
2023-06-22 04:40:29 -06:00
gem_file=tmp+'/gem.gemspec'
2012-10-24 18:22:03 +02:00
generate_gem_file(gem_file, description, files)
pwd=Dir.pwd
Dir.chdir(tmp) do
2023-06-22 04:40:29 -06:00
system('gem build gem.gemspec')
2012-10-24 18:22:03 +02:00
if $?.exitstatus!=0
2023-06-22 04:40:29 -06:00
puts 'Error generating gem'
2012-10-24 18:22:03 +02:00
exit(-1)
end
2023-06-22 04:40:29 -06:00
Dir.glob('*.gem').each do |f|
2017-10-16 17:28:36 +02:00
FileUtils.cp(f, pwd)
2012-10-24 18:22:03 +02:00
end
end
end
end
# Go to source code root dir
root=Dir.pwd
2023-06-22 04:40:29 -06:00
root.gsub!(%r{/share/rubygems$}, '')
2012-10-24 18:22:03 +02:00
Dir.chdir(root)
2023-06-22 04:40:29 -06:00
GEM_DESCRIPTION.each {|_name, gem| generate_gem(gem) }