Permalink
Browse files

Export services and start/stop them

  • Loading branch information...
1 parent 6643ddc commit d2ecf9ef6fd2115335f40332558102f0b2799088 @peterkeen committed Aug 19, 2012
Showing with 63 additions and 18 deletions.
  1. +37 −12 lib/dokuen/application.rb
  2. +11 −3 lib/dokuen/cli.rb
  3. +15 −3 lib/dokuen/remote.rb
View
@@ -10,6 +10,10 @@ def initialize(remote, name)
@name = name
end
+ def app_path
+ "#{remote.path}/apps/#{name}"
+ end
+
def run(*args)
remote.run(*args)
end
@@ -18,33 +22,33 @@ def sudo(*args)
remote.sudo(*args)
end
- def create!
+ def create!(port)
return if remote.application_exists?(name)
remote.create_user(name)
dirs = [
'releases',
- 'env',
'logs',
'build'
]
dirs.each do |dir|
- sudo("mkdir -p #{remote.path}/apps/#{name}/#{dir}")
+ sudo("mkdir -p #{app_path}/#{dir}")
end
- sudo("chown -R #{name}.#{name} #{remote.path}/apps/#{name}")
+ sudo("chown -R #{name}.#{name} #{app_path}")
+ remote.put_as(port, "#{app_path}/base_port", name)
end
def destroy!
return unless remote.application_exists?(name)
- sudo("rm -rf #{remote.path}/apps/#{name}")
+ sudo("rm -rf #{app_path}")
sudo("userdel #{name}")
end
def push_code
remote.log "Creating release directory"
now = DateTime.now.new_offset(0).strftime("%Y%m%dT%H%M%S")
- release_path = "#{remote.path}/apps/#{name}/releases/#{now}"
+ release_path = "#{app_path}/releases/#{now}"
sudo("mkdir #{release_path}", :as => name)
remote.log "Pushing code"
command = "tar --exclude=.git -c -z -f - . | ssh #{remote.server_name} sudo -u #{name} tar -C #{release_path} -x -z -f -"
@@ -56,7 +60,7 @@ def push_code
end
def build(app_type, buildpack, release_path)
- remote.stream("#{remote.path}/buildpacks/#{buildpack}/bin/compile #{release_path} #{remote.path}/apps/#{name}/build", :as => name, :via => :sudo)
+ remote.stream("#{remote.path}/buildpacks/#{buildpack}/bin/compile #{release_path} #{app_path}/build", :as => name, :via => :sudo)
release_info = YAML::load(remote.capture("#{remote.path}/buildpacks/#{buildpack}/bin/release #{release_path}"), :as => name, :via => :sudo)
put_env(release_info, release_path)
put_procfile(release_info, release_path)
@@ -67,7 +71,7 @@ def put_env(release_info, release_path)
release_info['config_vars'].each do |k, v|
a << "#{k}=#{v}"
end
- }.join("\n") + "\n"
+ }.join("\n") + "\nHOME=#{release_path}\n"
remote.put_as(vars, "#{release_path}/.env", name)
end
@@ -88,10 +92,7 @@ def put_procfile(release_info, release_path)
proc = Foreman::Procfile.new(f.path)
- f.close
- f.unlink
-
- proc.entries { |p| existing_entries[p.name] = 1 }
+ proc.entries { |p| existing_entries[p] = 1 }
end
release_info["default_process_types"].each do |k,v|
@@ -100,14 +101,38 @@ def put_procfile(release_info, release_path)
end
remote.put_as(proc.to_s, proc_path, name)
+ end
+ def concurrency
+ remote.get("#{app_path}/concurrency") || "web=1"
+ end
+
+ def concurrency=(val)
+ remote.put_as(val, "#{app_path}/concurrency", name)
+ end
+
+ def port
+ remote.get("#{app_path}/base_port") || 5000
+ end
+
+ def generate_init_files(release_path)
+ remote.log("Generating init files")
+ remote.foreman_export(release_path, concurrency, name, port)
+ end
+
+ def start_service
+ remote.log("Starting service")
+ remote.start_service(name)
end
def push!
release_path = push_code
app_type, buildpack = remote.detect_buildpack(release_path)
remote.log("Detected #{app_type} app")
build(app_type, buildpack, release_path)
+ generate_init_files(release_path)
+ start_service
+
release_path
end
View
@@ -76,8 +76,8 @@ def remove(remote, name)
end
- desc "create REMOTE NAME", "Create an application"
- def create(remote, name)
+ desc "create REMOTE NAME PORT", "Create an application with base port PORT"
+ def create(remote, name, port)
verify_remote(remote)
say "Creating application #{name} on #{remote}"
@@ -86,7 +86,7 @@ def create(remote, name)
raise Thor::Error.new "Application #{name} already exists on remote #{remote}"
end
app = Dokuen::Application.new(@remote, name)
- app.create!
+ app.create!(port)
end
desc "destroy REMOTE NAME", "Destroy an application"
@@ -116,6 +116,14 @@ def push(remote, name)
say "Created release #{release}"
end
+ desc "scale REMOTE NAME VALUE", "Scale to VALUE concurrency"
+ def scale(remote, name, value)
+ say "Scaling #{name} on #{remote} to #{value}"
+ verify_remote(remote)
+ verify_application(name)
+ @app.concurrency = value
+ end
+
register(Remote, 'remote', 'remote <command>', 'Manipulate Dokuen remotes')
register(Buildpack, 'buildpack', 'buildpack <command>', 'Manipulate Dokuen buildpacks')
View
@@ -36,7 +36,7 @@ def sudo(*args)
def stream(command, options={})
@cap.invoke_command(command, options.merge(:eof => true)) do |ch, stream, out|
print out if stream == :out
- @cap.warn "[err :: #{ch[:server]}] #{out}" if stream == :err
+ print "[err :: #{ch[:server]}] #{out}" if stream == :err
end
end
@@ -81,6 +81,8 @@ def remove_buildpack(name)
end
def detect_buildpack(release)
+ count = capture("ls #{path}/buildpacks | wc -l").to_i
+ raise Thor::Error.new("No buildpacks defined. Add some using `dokuen buildpack add`.") if count == 0
output = capture("for b in `ls #{path}/buildpacks`; do #{path}/buildpacks/$b/bin/detect #{release} && echo $b && exit 0; done; echo").strip.split("\n")
output.select { |l| l != "no" }
end
@@ -90,7 +92,7 @@ def application_exists?(name)
end
def create_user(name)
- sudo("useradd --home #{path}/apps/#{name} --shell /usr/bin/false #{name}")
+ sudo("useradd --home #{path}/apps/#{name} #{name}")
end
def put_as(data, path, user, perms="0644")
@@ -100,7 +102,8 @@ def put_as(data, path, user, perms="0644")
end
def get(path)
- capture("test -f #{path} && cat #{path}; echo")
+ val = capture("test -f #{path} && cat #{path}; echo")
+ val.strip == '' ? nil : val
end
def log(msg)
@@ -110,4 +113,13 @@ def log(msg)
def indent(msg)
puts " #{msg}"
end
+
+ def start_service(name)
+ sudo("/usr/sbin/service #{name} restart || /usr/sbin/service #{name} start")
+ end
+
+ def foreman_export(release_path, concurrency, name, port)
+ stream("bash -c 'cd #{release_path} && foreman export -p #{port} -c #{concurrency} -a #{name} -l #{path}/apps/#{name}/logs upstart /etc/init'", :via => :sudo)
+ end
+
end

0 comments on commit d2ecf9e

Please sign in to comment.