def self.create_app(libra_server, net_http, user_info, app_name, app_type, rhlogin, password, repo_dir=nil, no_dns=false, no_git=false, is_embedded_jenkins=false, gear_size='small',scale=false)
Struct.new('FakeResponse',:body,:code,:content_type)
domains = user_info['user_info']['domains']
if domains.empty?
emessage = "Please create a domain with 'rhc domain create -n <namespace>' before creating applications."
print_response_err(Struct::FakeResponse.new(emessage,403))
end
namespace = domains[0]['namespace']
puts "Creating application: #{app_name} in #{namespace}"
data = {:cartridge => app_type,
:action => 'configure',
:node_profile => gear_size,
:app_name => app_name,
:rhlogin => rhlogin
}
if @mydebug
data[:debug] = true
end
if scale
end_point = "https://#{libra_server}/broker/rest/api"
client = Rhc::Rest::Client.new(end_point, rhlogin, password)
domain = client.find_domain(user_info['user_info']['domains'][0]['namespace']).first
namespace = domain.id
begin
application = domain.add_application(app_name,{:cartridge => app_type, :scale => true, :gear_profile => gear_size})
app_uuid = application.uuid
result = "Successfully created application: #{app_name}"
health_check_path =
case app_type
when /^php/
"health_check.php"
when /^perl/
"health_check.pl"
else
"health"
end
puts "DEBUG: '#{app_name}' creation returned success." if @mydebug
rescue Rhc::Rest::ResourceAccessException => e
print_response_err(Struct::FakeResponse.new(e.message,e.code))
rescue Rhc::Rest::ValidationException => e
print_response_err(Struct::FakeResponse.new(e.message,406))
rescue Rhc::Rest::ServerErrorException => e
if e.message =~ /^Failed to create application .* due to:Scalable app cannot be of type/
puts "Can not create a scaling app of type #{app_type}, either disable scaling or choose another app type"
exit 1
else
raise e
end
end
else
json_data = generate_json(data)
url = URI.parse("https://#{libra_server}/broker/cartridge")
response = http_post(net_http, url, json_data, password)
if response.code == '200'
json_resp = JSON.parse(response.body)
print_response_success(json_resp)
json_data = JSON.parse(json_resp['data'])
health_check_path = json_data['health_check_path']
app_uuid = json_data['uuid']
result = json_resp['result']
puts "DEBUG: '#{app_name}' creation returned success." if @mydebug
else
print_response_err(response)
end
end
at_exit do
unless $!.nil? || $!.is_a?(SystemExit) && $!.success?
puts "Cleaning up application"
destroy_app(libra_server, net_http, app_name, rhlogin, password)
end
end
rhc_domain = user_info['user_info']['rhc_domain']
fqdn = "#{app_name}-#{namespace}.#{rhc_domain}"
loop = 0
unless no_dns
puts "Now your new domain name is being propagated worldwide (this might take a minute)..."
sleep 15
sleep_time = 2
while loop < MAX_RETRIES && !hostexist?(fqdn)
sleep sleep_time
loop+=1
print CLEAR_LINE + " retry # #{loop} - Waiting for DNS: #{fqdn}"
$stdout.flush
sleep_time = delay(sleep_time)
end
end
if loop > 0
puts
end
git_url = "ssh://#{app_uuid}@#{app_name}-#{namespace}.#{rhc_domain}/~/git/#{app_name}.git/"
if loop >= MAX_RETRIES
puts "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\nWARNING: We weren't able to lookup your hostname (\#{fqdn}) \nin a reasonable amount of time. This can happen periodically and will just\ntake an extra minute or two to propagate depending on where you are in the\nworld. Once you are able to access your application in a browser, you can then\nclone your git repository.\n\n Application URL: http://\#{fqdn}\n\n Git Repository URL: \#{git_url}\n\n Git Clone command: \n git clone \#{git_url} \#{repo_dir}\n\nIf you can't get your application '\#{app_name}' running in the browser, you can\nalso try destroying and recreating the application as well using:\n\n rhc app destroy -a \#{app_name} -l \#{rhlogin}\n\nIf this doesn't work for you, let us know in the forums or in IRC and we'll\nmake sure to get you up and running.\n\n Forums: https://www.redhat.com/openshift/forums/express\n\n IRC: #openshift (on Freenode)\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"
exit 0
end
unless no_git
puts "Pulling new repo down" if @mydebug
puts "git clone --quiet #{git_url} #{repo_dir}" if @mydebug
quiet = (@mydebug ? ' ' : '--quiet ')
git_clone = %x<git clone #{quiet} #{git_url} #{repo_dir}>
if $?.exitstatus != 0
puts "Error in git clone"
puts git_clone
exit 216
end
else
if is_embedded_jenkins
if @mydebug
puts "
Note: There is a git repo for your Jenkins application '#{app_name}'
but it isn't being downloaded as part of this process. In most cases
it isn't needed but you can always clone it later.
"
end
else
puts "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\nIMPORTANT: Since the -n flag was specified, no local repo has been created.\nThis means you can't make changes to your published application until after\nyou clone the repo yourself. See the git url below for more information.\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"
end
end
unless no_git
at_exit do
unless $!.nil? || $!.is_a?(SystemExit) && $!.success?
puts "Cleaning up git repo"
FileUtils.rm_rf repo_dir
end
end
end
return {:app_name => app_name,
:fqdn => fqdn,
:health_check_path => health_check_path,
:git_url => git_url,
:repo_dir => repo_dir,
:result => result
}
end