Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Merge "Templates for CF config files. Also change deployment config f…
Browse files Browse the repository at this point in the history
…rom json to yml."
  • Loading branch information
mahpat authored and testazuretrain committed Jul 29, 2011
2 parents d68eee3 + ff543c0 commit d9136f9
Show file tree
Hide file tree
Showing 64 changed files with 964 additions and 270 deletions.
178 changes: 91 additions & 87 deletions dev_setup/bin/chefsolo_launch.rb
Expand Up @@ -5,29 +5,30 @@
require 'tempfile'
require 'uri'
require 'fileutils'
require 'yaml'
require 'pp'
require File.expand_path('vcap_defs', File.dirname(__FILE__))

script_dir = File.expand_path(File.dirname(__FILE__))
vcap_dir = File.expand_path(File.join(script_dir, "../.."))
deployment_config = File.expand_path(File.join(script_dir, "..", DEFAULT_DEPLOYMENT_CONFIG))
deployment_home = DEFAULT_DEPLOYMENT_HOME
deployment_name = DEFAULT_DEPLOYMENT_NAME
vcap_path = File.expand_path(File.join(script_dir, "..", ".."))
deployment_spec = File.expand_path(File.join(script_dir, "..", DEPLOYMENT_DEFAULT_SPEC))
deployment_name = DEPLOYMENT_DEFAULT_NAME
deployment_home = Deployment.get_home(deployment_name)
deployment_user = ENV["USER"]
deployment_group = `id -g`.strip
download_cloudfoundry = false

unless ARGV[0].nil?
deployment_config = ARGV[0]
unless File.exists?(deployment_config)
puts "Cannont find deployment config file #{deployment_config}"
exit 1
end
deployment_spec = ARGV[0] if ARGV[0]

unless File.exists?(deployment_spec)
puts "Cannot find deployment spec #{deployment_spec}"
puts "Usage: #{$0} [deployment_spec]"
exit 1
end

env_erb = ERB.new(File.read(deployment_config))
spec_erb = ERB.new(File.read(deployment_spec))

JSON.parse(env_erb.dup.result).each do |package, properties|
YAML.load(spec_erb.result).each do |package, properties|
# Find out the versions of the various packages
case properties
when Hash
Expand All @@ -42,96 +43,99 @@
# Update config defaults
case package
when "cloudfoundry"
vcap_dir = File.expand_path(properties["path"])
download_cloudfoundry = true
puts "vcap_dir is now #{vcap_dir}"
when "deployment_name"
deployment_name = properties
when "deployment_home"
deployment_home=properties
when "deployment_user"
deployment_user = properties
when "deployment_group"
deployment_group = properties
vcap_path = File.expand_path(properties["path"])
download_cloudfoundry = true unless properties["revision"].nil?
puts "vcap_path is now #{vcap_path}"
when "deployment"
deployment_name = properties["name"] || deployment_name
deployment_home = properties["home"] || deployment_home
deployment_user = properties["user"] || deployment_user
deployment_group = properties["group"] || deployment_group
end
end
deployment_config_path = Deployment.get_config_path(deployment_home)

`mkdir -p #{deployment_home}/deploy;`
`mkdir -p #{deployment_home}/sys/log`
`chown #{deployment_user} #{deployment_home} #{deployment_home}/deploy #{deployment_home}/sys/log`
puts "Installing deployment #{deployment_name}, deployment home dir is #{deployment_home}, vcap dir is #{vcap_dir}"
FileUtils.mkdir_p(File.join(deployment_home, "deploy"))
FileUtils.mkdir_p(File.join(deployment_home, "sys", "log"))
FileUtils.chown(deployment_user, deployment_group, [deployment_home, File.join(deployment_home, "deploy"), File.join(deployment_home, "sys", "log")])
puts "Installing deployment #{deployment_name}, deployment home dir is #{deployment_home}, vcap dir is #{vcap_path}"

run_list = Array.new
run_list << "role[dea]"
run_list << "role[router]"
run_list << "role[cc]"
run_list = []
run_list << "role[cloudfoundry]" if download_cloudfoundry

# generate the chef-solo attributes
env = env_erb.result
run_list << "role[router]"
run_list << "role[cloud_controller]"
run_list << "role[dea]"
run_list << "recipe[health_manager]"
run_list << "recipe[services]"

# Fill in default config attributes
spec = YAML.load(spec_erb.result)
spec["run_list"] = run_list
spec["deployment"] ||= {}
spec["deployment"]["name"] = deployment_name
spec["deployment"]["home"] = deployment_home
spec["deployment"]["user"] = deployment_user
spec["deployment"]["group"] = deployment_group
spec["deployment"]["config_path"] = deployment_config_path
spec["cloudfoundry"] ||= {}
spec["cloudfoundry"]["path"] = vcap_path

# Deploy all the cf components
Dir.mktmpdir { |tmpdir|
Dir.mktmpdir do |tmpdir|
# Create chef-solo config file
File.open("#{tmpdir}/solo.rb", "w") { |f|
f.puts("cookbook_path \"#{File.expand_path("../cookbooks", script_dir)}\"")
f.puts("role_path \"#{File.expand_path("../roles", script_dir)}\"")
File.open(File.join(tmpdir, "solo.rb"), "w") do |f|
f.puts("cookbook_path \"#{File.expand_path(File.join("..", "cookbooks"), script_dir)}\"")
f.puts("role_path \"#{File.expand_path(File.join("..", "roles"), script_dir)}\"")

%w[ http_proxy https_proxy].each do |proxy|
unless ENV[proxy].nil?
uri = URI.parse(ENV[proxy])
f.puts("#{proxy} \"#{uri.scheme}://#{uri.host}:#{uri.port}\"")
unless uri.userinfo.nil?
f.puts("http_proxy_user \"#{uri.userinfo.split(":")[0]}\"")
f.puts("http_proxy_pass \"#{uri.userinfo.split(":")[1]}\"")
end
if ENV[proxy]
uri = URI.parse(ENV[proxy])
f.puts("#{proxy} \"#{uri.scheme}://#{uri.host}:#{uri.port}\"")
if uri.userinfo
f.puts("http_proxy_user \"#{uri.userinfo.split(":")[0]}\"")
f.puts("http_proxy_pass \"#{uri.userinfo.split(":")[1]}\"")
end
end
end
unless ENV["no_proxy"].nil?
f.puts("no_proxy \"#{ENV["no_proxy"]}\"")
if ENV["no_proxy"]
f.puts("no_proxy \"#{ENV["no_proxy"]}\"")
end
}

end

# Create chef-solo attributes file
json_attribs = "#{tmpdir}/solo.json"
File.open(json_attribs, "w") { |f|
f.puts(env)
}

FileUtils.cp("#{tmpdir}/solo.rb", "/tmp/solo.rb")
FileUtils.cp("#{json_attribs}", "/tmp/solo.json")

id = fork {
proxy_env = Array.new
proxy_env << "http_proxy=#{ENV["http_proxy"]}" unless ENV["http_proxy"].nil?
proxy_env << "https_proxy=#{ENV["https_proxy"]}" unless ENV["https_proxy"].nil?
proxy_env << "no_proxy=#{ENV["no_proxy"]}" unless ENV["no_proxy"].nil?
exec("sudo env #{proxy_env.join(" ")} chef-solo -c #{tmpdir}/solo.rb -j #{json_attribs} -l debug")
}
pid, status = Process.waitpid2(id)
if status.exitstatus == 0
# save the config of this deployment
config_base_dir = File.expand_path(File.join(vcap_dir, CONFIG_BASE_DIR))
FileUtils.mkdir_p(config_base_dir)
`chown #{deployment_user} #{config_base_dir}`

config_dir = File.join(config_base_dir, deployment_name)
FileUtils.mkdir_p(config_dir)
`chown #{deployment_user} #{config_dir}`

File.open(File.join(config_dir, DEPLOYMENT_FILE), "w") { |f|
f.puts(env)
}

puts "Cloudfoundry setup was successful."
if deployment_name != DEFAULT_DEPLOYMENT_NAME
puts "Config file for this deployment is in #{config_dir}."
json_attribs = File.join(tmpdir, "solo.json")
File.open(json_attribs, "w") { |f| f.puts(spec.to_json) }

id = fork do
proxy_env = []
# Setup proxy
%w(http_proxy https_proxy no_proxy).each do |env_var|
if ENV[env_var] || ENV[env_var.upcase]
[env_var, env_var.upcase].each do |v|
proxy = "#{v}=#{ENV[v.downcase] || ENV[v.upcase]}"
proxy_env << proxy
end
end
end
exec("sudo env #{proxy_env.join(" ")} chef-solo -c #{File.join(tmpdir, "solo.rb")} -j #{json_attribs} -l debug")
end

env = JSON.parse(env)
ruby_path = "#{env["ruby"]["path"]}/bin"
puts "Note: You may need to add #{ruby_path} to your path to use vmc"
pid, status = Process.waitpid2(id)
if status.exitstatus != 0
exit status.exitstatus
end
exit status.exitstatus
}

# save the config of this deployment
FileUtils.mkdir_p(deployment_config_path)
FileUtils.chown(deployment_user, deployment_group, deployment_config_path)

File.open(Deployment.get_config_file(deployment_config_path), "w") { |f| f.puts(spec.to_json) }

puts "Cloudfoundry setup was successful."
if deployment_name != DEPLOYMENT_DEFAULT_NAME
puts "Config file for this deployment is in #{deployment_config_path}."
end

ruby_path = File.join(spec["ruby"]["path"], "bin")
puts "Note: You may need to add #{ruby_path} to your path to use vmc"
end
24 changes: 19 additions & 5 deletions dev_setup/bin/vcap_defs.rb
@@ -1,6 +1,20 @@
DEFAULT_DEPLOYMENT_CONFIG = "deployments/singlehost.json.erb"
DEFAULT_DEPLOYMENT_NAME = "singlehost"
DEFAULT_DEPLOYMENT_HOME = File.expand_path(File.join(ENV["HOME"], ".cloudfoundry", DEFAULT_DEPLOYMENT_NAME))
DEPLOYMENT_DEFAULT_SPEC = "deployments/devbox.yml.erb"
DEPLOYMENT_DEFAULT_NAME = "devbox"
DEPLOYMENT_CONFIG_DIR_NAME = "config"
DEPLOYMENT_CONFIG_FILE_NAME = "deploy.json"

CONFIG_BASE_DIR = "dev_setup/.config"
DEPLOYMENT_FILE = "deploy.json"
class Deployment
class << self
def get_home(name)
File.expand_path(File.join(ENV["HOME"], ".cloudfoundry", name))
end

def get_config_path(home)
File.expand_path(File.join(home, DEPLOYMENT_CONFIG_DIR_NAME))
end

def get_config_file(config_path)
File.expand_path(File.join(config_path, DEPLOYMENT_CONFIG_FILE_NAME))
end
end
end

0 comments on commit d9136f9

Please sign in to comment.