fuel-astute/bin/astute

118 lines
3.6 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# Copyright 2013 Mirantis, Inc.
#
# 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.
if RUBY_VERSION < "1.9"
puts "Astute tested and works only on Ruby 1.9.3 but you use #{RUBY_VERSION}"
puts "Please run astute using ruby -rubygems bin/astute.rb"
end
require 'optparse'
require 'yaml'
require 'astute'
require 'astute/version'
require 'astute/enviroment'
class ConsoleReporter
def report(msg)
puts msg.inspect
end
end
opts = {}
optparse = OptionParser.new do |o|
o.banner = "Usage: bin/astute -c COMMAND -f FILENAME "
o.on("-v", "--[no-]verbose", "Run verbosely") do |v|
opts[:verbose] = v
end
o.on("-f FILENAME", "Environment in YAML format. Samples are in examples directory.") do |f|
opts[:filename] = f
end
o.on_tail("-h", "--help", "Show this message") { puts o; exit }
o.on_tail("--version", "Show version") { puts Astute::VERSION; exit }
o.on("-c", "--command COMMAND", [:provision, :deploy, :provision_and_deploy],
"Select operation: provision, deploy or provision_and_deploy") do |c|
opts[:command] = c
end
end
optparse.parse!(ARGV)
if opts[:filename].nil?
puts optparse
ERROR_CODE_COMMAND_USAGE = 64
exit ERROR_CODE_COMMAND_USAGE
end
reporter = ConsoleReporter.new
Astute.logger = Logger.new(STDOUT) if opts[:verbose]
environment = Astute::Enviroment.load_file(opts[:filename])
p environment['nodes'][0]['ip']
deploy_engine = nil
if environment['attributes'] && environment['attributes']['deployment_engine']
case environment['attributes']['deployment_engine']
when 'nailyfact'
deploy_engine = Astute::DeploymentEngine::NailyFact
when 'simplepuppet'
deploy_engine = Astute::DeploymentEngine::SimplePuppet # It just calls puppet and doesn't do any magic
end
end
if [:deploy, :provision, :provision_and_deploy].include? opts[:command]
orchestrator = Astute::Orchestrator.new(deploy_engine, log_parsing=false)
end
def console_provision(orchestrator, reporter, environment)
res = orchestrator.fast_provision(reporter, environment['engine'], environment['nodes'])
if res == Astute::SUCCESS
puts "restarting nodes..."
sleep 25
puts "start watching progress"
res = orchestrator.provision(reporter, environment['task_uuid'], environment['nodes'])
end
res
end
result = Astute::SUCCESS
begin
result = case opts[:command]
when :deploy
orchestrator.deploy(reporter, environment['task_uuid'], environment['nodes'], environment['attributes'])
when :provision
console_provision(orchestrator, reporter, environment)
when :provision_and_deploy
res = console_provision(orchestrator, reporter, environment)
if res == Astute::SUCCESS
res = orchestrator.deploy(reporter, environment['task_uuid'], environment['nodes'], environment['attributes'])
end
res
end
rescue => e
result = Astute::FAIL
puts "Error: #{e.inspect}"
puts "Hint: use astute with --verbose or check log (#{Astute::LOG_PATH}) for more details" unless opts[:verbose]
Astute.logger.error e.backtrace.join("\n")
end
exit result