capricorn 0.2.25 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.md +1 -0
- data/bin/capricorn-app-scaffolder +27 -0
- data/bin/capricorn-gem-spec +33 -0
- data/bin/capricornctl +12 -0
- data/bin/capricornd +31 -0
- data/erlang/lib/bert/doc/overview.edoc +4 -0
- data/erlang/lib/bert/ebin/bert.app +9 -0
- data/erlang/lib/bert/rebar.config +3 -0
- data/erlang/lib/bert/src/bert.erl +69 -0
- data/erlang/lib/bertio/doc/overview.edoc +4 -0
- data/erlang/lib/bertio/ebin/bertio.app +9 -0
- data/erlang/lib/bertio/rebar.config +3 -0
- data/erlang/lib/bertio/src/bertio.erl +28 -0
- data/erlang/lib/bertrpc/doc/overview.edoc +4 -0
- data/erlang/lib/bertrpc/ebin/bertrpc.app +12 -0
- data/erlang/lib/bertrpc/include/bertrpc.hrl +1 -0
- data/erlang/lib/bertrpc/rebar.config +5 -0
- data/erlang/lib/bertrpc/src/bertrpc.erl +471 -0
- data/erlang/lib/bertrpc/src/bertrpc_hello_world.erl +148 -0
- data/erlang/lib/bertrpc/src/fd_tcp.erl +376 -0
- data/erlang/lib/capricorn/doc/overview.edoc +4 -0
- data/erlang/lib/capricorn/ebin/capricorn.app +56 -0
- data/erlang/lib/capricorn/include/capricorn.hrl +76 -0
- data/erlang/lib/capricorn/rebar.config +3 -0
- data/erlang/lib/capricorn/src/cap_application.erl +170 -0
- data/erlang/lib/capricorn/src/cap_cluster.erl +121 -0
- data/erlang/lib/capricorn/src/cap_cluster_gems.erl +422 -0
- data/erlang/lib/capricorn/src/cap_config.erl +25 -0
- data/erlang/lib/capricorn/src/cap_dets_updater.erl +26 -0
- data/erlang/lib/capricorn/src/cap_event_sup.erl +35 -0
- data/erlang/lib/capricorn/src/cap_events.erl +24 -0
- data/erlang/lib/capricorn/src/cap_external_api.erl +87 -0
- data/erlang/lib/capricorn/src/cap_external_apps_api.erl +125 -0
- data/erlang/lib/capricorn/src/cap_external_gems_api.erl +85 -0
- data/erlang/lib/capricorn/src/cap_external_machines_api.erl +18 -0
- data/erlang/lib/capricorn/src/cap_gem_utils.erl +111 -0
- data/erlang/lib/capricorn/src/cap_internal_api.erl +69 -0
- data/erlang/lib/capricorn/src/cap_internal_apps_api.erl +38 -0
- data/erlang/lib/capricorn/src/cap_log.erl +113 -0
- data/erlang/lib/capricorn/src/cap_machine.erl +338 -0
- data/erlang/lib/capricorn/src/cap_machine_apps.erl +410 -0
- data/erlang/lib/capricorn/src/cap_machine_apps_sup.erl +57 -0
- data/erlang/lib/capricorn/src/cap_sup.erl +200 -0
- data/erlang/lib/capricorn/src/cap_util.erl +278 -0
- data/erlang/lib/capricorn/src/capricorn.erl +45 -0
- data/erlang/lib/capricorn/src/capricorn_app.erl +36 -0
- data/erlang/lib/emq/ebin/emq.app +17 -0
- data/erlang/lib/emq/src/emq.erl +261 -0
- data/erlang/lib/emq/src/emq_app.erl +11 -0
- data/erlang/lib/emq/src/emq_pool.erl +178 -0
- data/erlang/lib/emq/src/emq_queue.erl +332 -0
- data/erlang/lib/emq/src/emq_status.erl +148 -0
- data/erlang/lib/emq/src/emq_sup.erl +132 -0
- data/erlang/lib/gcd/ebin/gcd.app +18 -0
- data/erlang/lib/gcd/include/gcd.hrl +5 -0
- data/erlang/lib/gcd/rebar.config +3 -0
- data/erlang/lib/gcd/src/gcd.erl +51 -0
- data/erlang/lib/gcd/src/gcd_app.erl +13 -0
- data/erlang/lib/gcd/src/gcd_event.erl +39 -0
- data/erlang/lib/gcd/src/gcd_server.erl +30 -0
- data/erlang/lib/gcd/src/gcd_srv.erl +186 -0
- data/erlang/lib/gcd/src/gcd_sup.erl +18 -0
- data/erlang/rebar +0 -0
- data/erlang/rebar.config +9 -0
- data/erlang/rel/overlay/bin/capricornd +146 -0
- data/erlang/rel/overlay/erts-vsn/bin/erl +34 -0
- data/erlang/rel/overlay/erts-vsn/bin/nodetool +80 -0
- data/erlang/rel/overlay/etc/capricorn/app.config +59 -0
- data/erlang/rel/overlay/etc/capricorn/cluster-vm.args +21 -0
- data/erlang/rel/overlay/etc/capricorn/machine-vm.args +21 -0
- data/erlang/rel/reltool.config +43 -0
- data/ext/Makefile +2 -0
- data/ext/extconf.rb +1 -0
- data/lib/capricorn-client.rb +86 -0
- data/lib/capricorn-client/cli/applications.rb +256 -0
- data/lib/capricorn-client/cli/gems.rb +57 -0
- data/lib/capricorn-client/cli/machines.rb +9 -0
- data/lib/capricorn-client/helpers.rb +62 -0
- data/lib/capricorn.rb +5 -99
- data/lib/capricorn/driver.rb +86 -0
- data/lib/capricorn/recipes/apache-debian.rb +112 -0
- data/lib/capricorn/recipes/centos-plesk.rb +162 -0
- data/lib/capricorn/recipes/macports.rb +54 -0
- data/lib/capricorn/system_context.rb +49 -0
- data/lib/capricorn/version.rb +5 -0
- metadata +233 -74
- data/app_generators/engine/engine_generator.rb +0 -40
- data/app_generators/engine/templates/Gmfile +0 -20
- data/app_generators/engine/templates/MIT-LICENSE.txt +0 -20
- data/app_generators/engine/templates/README.rdoc +0 -7
- data/app_generators/engine/templates/config/initializers/rails_init.rb +0 -1
- data/app_generators/engine/templates/config/routes.rb +0 -2
- data/app_generators/engine/templates/gitignore +0 -9
- data/app_generators/engine/templates/init.rb +0 -1
- data/app_generators/engine/templates/lib/engine.rb +0 -4
- data/app_generators/engine/templates/rails/init.rb +0 -1
- data/app_generators/engine/templates/tasks/engine_tasks.rake +0 -4
- data/bin/capricorn +0 -20
- data/lib/capricorn/actor.rb +0 -23
- data/lib/capricorn/actor/actions.rb +0 -76
- data/lib/capricorn/actors/apache_actor.rb +0 -56
- data/lib/capricorn/actors/base_actor.rb +0 -335
- data/lib/capricorn/actors/host_file_actor.rb +0 -77
- data/lib/capricorn/actors/mysql_actor.rb +0 -20
- data/lib/capricorn/actors/passenger_actor.rb +0 -28
- data/lib/capricorn/actors/plesk_actor.rb +0 -228
- data/lib/capricorn/actors/sqlite3_actor.rb +0 -44
- data/lib/capricorn/app_runner.rb +0 -108
- data/lib/capricorn/apps/dev.rb +0 -15
- data/lib/capricorn/apps/engines.rb +0 -33
- data/lib/capricorn/apps/jobs.rb +0 -35
- data/lib/capricorn/apps/satellite.rb +0 -68
- data/lib/capricorn/apps/server.rb +0 -73
- data/lib/capricorn/client.rb +0 -48
- data/lib/capricorn/client/auth_token.rb +0 -98
- data/lib/capricorn/daemon.rb +0 -81
- data/lib/capricorn/exception_handler.rb +0 -79
- data/lib/capricorn/extentions/rubygems_plugin.rb +0 -27
- data/lib/capricorn/extentions/thor_extentions.rb +0 -32
- data/lib/capricorn/job_queue.rb +0 -203
- data/lib/capricorn/satellite.rb +0 -52
- data/lib/capricorn/satellite/actions.rb +0 -55
- data/lib/capricorn/satellite/dependency_loader.rb +0 -82
- data/lib/capricorn/satellite/persistence.rb +0 -50
- data/lib/capricorn/server.rb +0 -144
- data/lib/capricorn/server/daemon.rb +0 -83
- data/lib/capricorn/server/proxy.rb +0 -25
- data/lib/capricorn/server/security.rb +0 -120
- data/lib/capricorn/system.rb +0 -218
- data/lib/capricorn/system/config.rb +0 -49
- data/lib/capricorn/system/helper.rb +0 -21
- data/lib/capricorn/system/options.rb +0 -79
- data/lib/capricorn/system/process_user.rb +0 -73
- data/lib/capricorn/system/satellites.rb +0 -44
- data/lib/capricorn/system/shell.rb +0 -80
- data/lib/rubygems_plugin.rb +0 -1
- data/spec/actor/actions_spec.rb +0 -13
- data/spec/spec_helper.rb +0 -1
data/lib/capricorn.rb
CHANGED
@@ -1,101 +1,7 @@
|
|
1
|
-
|
1
|
+
module Capricorn
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
autoload :TSort, 'tsort'
|
7
|
-
autoload :Logger, 'logger'
|
8
|
-
autoload :OpenSSL, 'drb/ssl'
|
9
|
-
autoload :FileUtils, 'fileutils'
|
10
|
-
autoload :DRbUndumped, 'drb'
|
3
|
+
require 'capricorn/version'
|
4
|
+
require 'capricorn/driver'
|
5
|
+
require 'capricorn/system_context'
|
11
6
|
|
12
|
-
|
13
|
-
base = File.expand_path(File.dirname(__FILE__))
|
14
|
-
|
15
|
-
autoload :Actor, (base+'/capricorn/actor')
|
16
|
-
autoload :Server, (base+'/capricorn/server')
|
17
|
-
autoload :Client, (base+'/capricorn/client')
|
18
|
-
autoload :System, (base+'/capricorn/system')
|
19
|
-
autoload :Daemon, (base+'/capricorn/daemon')
|
20
|
-
autoload :JobQueue, (base+'/capricorn/job_queue')
|
21
|
-
autoload :Satellite, (base+'/capricorn/satellite')
|
22
|
-
autoload :AppRunner, (base+'/capricorn/app_runner')
|
23
|
-
autoload :ExceptionHandler, (base+'/capricorn/exception_handler')
|
24
|
-
|
25
|
-
module Actors
|
26
|
-
base = File.expand_path(File.dirname(__FILE__))
|
27
|
-
|
28
|
-
autoload :BaseActor, (base+'/capricorn/actors/base_actor')
|
29
|
-
autoload :MysqlActor, (base+'/capricorn/actors/mysql_actor')
|
30
|
-
autoload :PleskActor, (base+'/capricorn/actors/plesk_actor')
|
31
|
-
autoload :ApacheActor, (base+'/capricorn/actors/apache_actor')
|
32
|
-
autoload :Sqlite3Actor, (base+'/capricorn/actors/sqlite3_actor')
|
33
|
-
autoload :HostFileActor, (base+'/capricorn/actors/host_file_actor')
|
34
|
-
autoload :PassengerActor, (base+'/capricorn/actors/passenger_actor')
|
35
|
-
end
|
36
|
-
|
37
|
-
module Apps
|
38
|
-
base = File.expand_path(File.dirname(__FILE__))
|
39
|
-
|
40
|
-
autoload :Dev, (base+'/capricorn/apps/dev')
|
41
|
-
autoload :Jobs, (base+'/capricorn/apps/jobs')
|
42
|
-
autoload :Server, (base+'/capricorn/apps/server')
|
43
|
-
autoload :Engines, (base+'/capricorn/apps/engines')
|
44
|
-
autoload :Satellite, (base+'/capricorn/apps/satellite')
|
45
|
-
end
|
46
|
-
|
47
|
-
DEFAULT_ROOT_SYSTEM_DIR = '/var/capricorn'
|
48
|
-
DEFAULT_USER_SYSTEM_DIR = '~/.capricorn'
|
49
|
-
STOP_STATUS = 101
|
50
|
-
RESTART_STATUS = 102
|
51
|
-
RELOAD_STATUS = 103
|
52
|
-
QUICK_CERT = "http://segment7.net/projects/ruby/QuickCert/QuickCert-1.0.2.tar.gz"
|
53
|
-
|
54
|
-
THOR_VERSION = '= 0.9.9'
|
55
|
-
RUBIGEN_VERSION = '>= 1.5.2'
|
56
|
-
|
57
|
-
Capricorn::ExceptionHandler.setup
|
58
|
-
extend ExceptionHandler
|
59
|
-
|
60
|
-
def self.client(token=nil)
|
61
|
-
unless @client
|
62
|
-
@client = Capricorn::Client.current(token)
|
63
|
-
unless @client
|
64
|
-
puts "Failed to connect to the capricorn!"
|
65
|
-
exit(1)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
@client
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.system
|
72
|
-
@system ||= Capricorn::System.shared
|
73
|
-
end
|
74
|
-
|
75
|
-
def self.version
|
76
|
-
unless @version
|
77
|
-
if __FILE__ =~ /\/capricorn-([^\/]+)\//
|
78
|
-
@version = $1
|
79
|
-
else
|
80
|
-
@version = 'edge'
|
81
|
-
end
|
82
|
-
end
|
83
|
-
@version
|
84
|
-
end
|
85
|
-
|
86
|
-
def self.server?(value=nil)
|
87
|
-
@is_server = value unless value.nil?
|
88
|
-
@is_server
|
89
|
-
end
|
90
|
-
|
91
|
-
def self.runtime_gem(gem, version='>= 0.0.0', lib=nil)
|
92
|
-
begin
|
93
|
-
gem(gem, version)
|
94
|
-
require(lib || gem)
|
95
|
-
rescue LoadError
|
96
|
-
puts "You must install #{gem} (#{version}) to use this.\nPlease run: [sudo] gem install #{gem}"
|
97
|
-
exit(1)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
7
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'bert'
|
2
|
+
|
3
|
+
unless Object.respond_to?(:instance_exec)
|
4
|
+
class Object
|
5
|
+
module InstanceExecHelper; end
|
6
|
+
include InstanceExecHelper
|
7
|
+
def instance_exec(*args, &block) # !> method redefined; discarding old instance_exec
|
8
|
+
mname = "__instance_exec_#{Thread.current.object_id.abs}_#{object_id.abs}"
|
9
|
+
InstanceExecHelper.module_eval{ define_method(mname, &block) }
|
10
|
+
begin
|
11
|
+
ret = __send__(mname, *args)
|
12
|
+
ensure
|
13
|
+
InstanceExecHelper.module_eval{ undef_method(mname) } rescue nil
|
14
|
+
end
|
15
|
+
ret
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ErlangHelpers
|
21
|
+
|
22
|
+
def setup
|
23
|
+
return true if @setup
|
24
|
+
$erlin = IO.new(3, 'r')
|
25
|
+
$erlout = IO.new(4, 'w')
|
26
|
+
$erlin.sync = true
|
27
|
+
@setup = true
|
28
|
+
end
|
29
|
+
|
30
|
+
def send(obj)
|
31
|
+
berp = BERT.encode(obj)
|
32
|
+
$erlout.write [berp.size].pack('N')+berp
|
33
|
+
$erlout.flush
|
34
|
+
end
|
35
|
+
|
36
|
+
def receive
|
37
|
+
exit(0) if $erlin.eof?
|
38
|
+
h = $erlin.read(4)
|
39
|
+
while h == nil
|
40
|
+
sleep(0.1)
|
41
|
+
exit(0) if $erlin.eof?
|
42
|
+
h = $erlin.read(4)
|
43
|
+
end
|
44
|
+
l = h.unpack('N')[0]
|
45
|
+
exit(0) if $erlin.eof?
|
46
|
+
BERT.decode($erlin.read(l))
|
47
|
+
end
|
48
|
+
|
49
|
+
def error(*args)
|
50
|
+
raise InternalError, args
|
51
|
+
end
|
52
|
+
|
53
|
+
extend self
|
54
|
+
end
|
55
|
+
|
56
|
+
class InternalError < RuntimeError
|
57
|
+
attr_accessor :args
|
58
|
+
def initialize(args)
|
59
|
+
@args = args
|
60
|
+
super
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def Erlang(&block)
|
65
|
+
ErlangHelpers.setup
|
66
|
+
|
67
|
+
loop do
|
68
|
+
cmd = ErlangHelpers.receive
|
69
|
+
|
70
|
+
if cmd == :stop
|
71
|
+
exit(0)
|
72
|
+
end
|
73
|
+
|
74
|
+
begin
|
75
|
+
ctx = Object.new
|
76
|
+
ctx.extend ErlangHelpers
|
77
|
+
ctx.instance_exec(cmd, &block)
|
78
|
+
rescue InternalError => e
|
79
|
+
ErlangHelpers.send BERT::Tuple[:error, BERT::Tuple[*e.args]]
|
80
|
+
rescue RuntimeError => e
|
81
|
+
ErlangHelpers.send BERT::Tuple[:error, BERT::Tuple[:runtime, e.class.to_s, e.message, e.backtrace]]
|
82
|
+
rescue Exception => e
|
83
|
+
ErlangHelpers.send BERT::Tuple[:error, BERT::Tuple[:system, e.class.to_s, e.message, e.backtrace]]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
|
3
|
+
domain = application[:domain]
|
4
|
+
|
5
|
+
user = 'www-data'
|
6
|
+
|
7
|
+
db_name = application[:id].downcase.gsub(/[^a-z]+/, '_')
|
8
|
+
db_name = "#{db_name[0,58]}_#{Digest::SHA1.hexdigest(application[:id])[0,5]}"
|
9
|
+
db_user = application[:id].downcase.gsub(/[^a-z]+/, '_')
|
10
|
+
db_user = "#{db_user[0,8]}_#{Digest::SHA1.hexdigest(application[:id])[0,5]}"
|
11
|
+
db_pswd = (rand(1_000_000_000) + 10_000).to_s
|
12
|
+
|
13
|
+
httpdocs_path = "/var/www/vhosts/#{domain}/httpdocs"
|
14
|
+
application_root = box["/var/www/vhosts/#{domain}/"]
|
15
|
+
FileUtils.mkdir_p(application_root.full_path)
|
16
|
+
has_db = application_root['shared/settings/database.yml'].exists?
|
17
|
+
|
18
|
+
###############################################################################
|
19
|
+
### Create host app ###
|
20
|
+
###############################################################################
|
21
|
+
if application_root['host/'].exists?
|
22
|
+
application_root.bash %{ mv host host-#{Time.now.strftime('%Y%m%d%H%M%S')} }
|
23
|
+
end
|
24
|
+
application_root.bash %{ milkshake create.host "host" --git-data --shared-data "shared" }
|
25
|
+
|
26
|
+
unless has_db
|
27
|
+
###############################################################################
|
28
|
+
### Create Database ###
|
29
|
+
###############################################################################
|
30
|
+
begin
|
31
|
+
box.bash(%{ mysql --user=root --password=Kendydig4488 --execute="CREATE DATABASE #{db_name} ; GRANT ALL ON #{db_name}.* TO '#{db_user}'@'%' IDENTIFIED BY '#{db_pswd}' ;" })
|
32
|
+
rescue Rush::BashFailed
|
33
|
+
# ignore failure
|
34
|
+
end
|
35
|
+
|
36
|
+
###############################################################################
|
37
|
+
### Configure Database ###
|
38
|
+
###############################################################################
|
39
|
+
database_conf = application_root['shared/settings/database.yml'].create
|
40
|
+
database_conf.write %{
|
41
|
+
default: &default
|
42
|
+
adapter: mysql
|
43
|
+
database: #{db_name}
|
44
|
+
username: #{db_user}
|
45
|
+
password: #{db_pswd}
|
46
|
+
host: localhost
|
47
|
+
encoding: utf8
|
48
|
+
socket: /var/run/mysqld/mysqld.sock
|
49
|
+
|
50
|
+
development:
|
51
|
+
<<: *default
|
52
|
+
|
53
|
+
test:
|
54
|
+
<<: *default
|
55
|
+
|
56
|
+
staging:
|
57
|
+
<<: *default
|
58
|
+
|
59
|
+
production:
|
60
|
+
<<: *default
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
###############################################################################
|
65
|
+
### Configure VHost ###
|
66
|
+
###############################################################################
|
67
|
+
vhost_conf = box['/etc/apache2/sites-available/'+domain].create
|
68
|
+
vhost_conf.write %[
|
69
|
+
<VirtualHost *:80>
|
70
|
+
ServerName #{application[:domain]}
|
71
|
+
#{application[:aliases].collect {|d| 'ServerAlias '+d+"\n "}}
|
72
|
+
|
73
|
+
RailsEnv #{application[:environment]}
|
74
|
+
|
75
|
+
DocumentRoot "#{application_root.full_path}/host/public"
|
76
|
+
|
77
|
+
ErrorDocument 503 /503.html
|
78
|
+
RewriteEngine on
|
79
|
+
RewriteCond %{DOCUMENT_ROOT}/../tmp/stop.txt -f
|
80
|
+
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
81
|
+
RewriteRule ^(.*)$ /$1 [R=503,L]
|
82
|
+
|
83
|
+
<Directory "#{application_root.full_path}/host/public">
|
84
|
+
Options All
|
85
|
+
AllowOverride All
|
86
|
+
Order allow,deny
|
87
|
+
Allow from all
|
88
|
+
</Directory>
|
89
|
+
</VirtualHost>
|
90
|
+
]
|
91
|
+
|
92
|
+
###############################################################################
|
93
|
+
### Set Owner of Host ###
|
94
|
+
###############################################################################
|
95
|
+
box.bash %{ chown #{user}:#{user} "#{application_root.full_path}" }
|
96
|
+
box.bash %{ chown -R #{user}:#{user} "#{application_root.full_path}/host" }
|
97
|
+
box.bash %{ chown -R #{user}:#{user} "#{application_root.full_path}/shared" }
|
98
|
+
|
99
|
+
###############################################################################
|
100
|
+
### Reconfigure vhost in Plesk ###
|
101
|
+
###############################################################################
|
102
|
+
box.bash %{ /usr/sbin/a2ensite #{domain} }
|
103
|
+
|
104
|
+
###############################################################################
|
105
|
+
### Restart apache ###
|
106
|
+
###############################################################################
|
107
|
+
box.bash %{ /etc/init.d/apache2 restart }
|
108
|
+
|
109
|
+
set :root_path, application_root.full_path.to_s
|
110
|
+
set :www_user, user.to_s
|
111
|
+
set :www_group, user.to_s
|
112
|
+
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
|
3
|
+
basedomain = application[:domain].split('.')[-2, 2].join('.')
|
4
|
+
subdomain = application[:domain].split('.')[0..-3].join('.')
|
5
|
+
subdomain = nil if subdomain.nil? or subdomain.empty?
|
6
|
+
|
7
|
+
client = 'mh1179'
|
8
|
+
|
9
|
+
user = application[:id].downcase.gsub(/[^a-z]+/, '_')
|
10
|
+
short_user = "#{user[0,14]}_#{Digest::SHA1.hexdigest(application[:id])[0,5]}"
|
11
|
+
passwd = (rand(1_000_000_000) + 10_000).to_s
|
12
|
+
|
13
|
+
db_name = application[:id].downcase.gsub(/[^a-z]+/, '_')
|
14
|
+
db_name = "#{db_name[0,58]}_#{Digest::SHA1.hexdigest(application[:id])[0,5]}"
|
15
|
+
db_user = application[:id].downcase.gsub(/[^a-z]+/, '_')
|
16
|
+
db_user = "#{db_user[0,8]}_#{Digest::SHA1.hexdigest(application[:id])[0,5]}"
|
17
|
+
db_pswd = (rand(1_000_000_000) + 10_000).to_s
|
18
|
+
|
19
|
+
###############################################################################
|
20
|
+
### Find Unused Username ###
|
21
|
+
###############################################################################
|
22
|
+
begin
|
23
|
+
Etc.getpwnam(short_user)
|
24
|
+
rescue ArgumentError
|
25
|
+
short_user = short_user.succ
|
26
|
+
# retry
|
27
|
+
end
|
28
|
+
|
29
|
+
###############################################################################
|
30
|
+
### Create Plesk Domain ###
|
31
|
+
###############################################################################
|
32
|
+
begin
|
33
|
+
### Check if the domain exists
|
34
|
+
box.bash(%{ /usr/local/psa/bin/domain -i #{basedomain} })
|
35
|
+
httpdocs_path = "/var/www/vhosts/#{basedomain}/httpdocs"
|
36
|
+
user = Etc.getpwuid(File.stat(httpdocs_path).uid).name
|
37
|
+
short_user = user
|
38
|
+
|
39
|
+
rescue Rush::BashFailed, Errno::ENOENT
|
40
|
+
### try to create the base domain
|
41
|
+
box.bash(%{ /usr/local/psa/bin/domain -c #{basedomain} -clogin #{client} -status enabled -hosting true -hst_type phys -dns true -www true -login #{short_user} -passwd #{passwd} -shell /bin/bash })
|
42
|
+
end
|
43
|
+
application_root = box["/var/www/vhosts/#{basedomain}/"]
|
44
|
+
|
45
|
+
###############################################################################
|
46
|
+
### Create Plesk Subdomain ###
|
47
|
+
###############################################################################
|
48
|
+
if subdomain
|
49
|
+
begin
|
50
|
+
### check is the subdomain exists
|
51
|
+
box.bash(%{ /usr/local/psa/bin/subdomain -i -s #{subdomain} -d #{basedomain} })
|
52
|
+
|
53
|
+
rescue Rush::BashFailed
|
54
|
+
### try to create the subdomain
|
55
|
+
box.bash(%{ /usr/local/psa/bin/subdomain -c #{subdomain} -d #{basedomain} })
|
56
|
+
end
|
57
|
+
application_root = application_root["subdomains/#{subdomain}/"]
|
58
|
+
end
|
59
|
+
|
60
|
+
###############################################################################
|
61
|
+
### Add Domain Aliasses ###
|
62
|
+
###############################################################################
|
63
|
+
unless subdomain
|
64
|
+
application[:aliases].each do |aliasd|
|
65
|
+
begin
|
66
|
+
box.bash(%{ /usr/local/psa/bin/domalias --create #{aliasd} -domain #{basedomain} -status enabled -mail true -web true -dns true })
|
67
|
+
rescue Rush::BashFailed
|
68
|
+
# ignore failed aliases
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
has_db = application_root['shared/settings/database.yml'].exists?
|
74
|
+
|
75
|
+
###############################################################################
|
76
|
+
### Create host app ###
|
77
|
+
###############################################################################
|
78
|
+
if application_root['host/'].exists?
|
79
|
+
application_root.bash %{ mv host host-#{Time.now.strftime('%Y%m%d%H%M%S')} }
|
80
|
+
end
|
81
|
+
application_root.bash %{ milkshake create.host "host" --git-data --shared-data "shared" }
|
82
|
+
|
83
|
+
unless has_db
|
84
|
+
###############################################################################
|
85
|
+
### Create Database ###
|
86
|
+
###############################################################################
|
87
|
+
begin
|
88
|
+
box.bash(%{ /usr/local/psa/bin/database -c #{db_name} -domain #{basedomain} -server localhost:3306 -add_user #{db_user} -passwd #{db_pswd} })
|
89
|
+
rescue Rush::BashFailed
|
90
|
+
# ignore failure
|
91
|
+
end
|
92
|
+
|
93
|
+
###############################################################################
|
94
|
+
### Configure Database ###
|
95
|
+
###############################################################################
|
96
|
+
database_conf = application_root['shared/settings/database.yml'].create
|
97
|
+
database_conf.write %{
|
98
|
+
default: &default
|
99
|
+
adapter: mysql
|
100
|
+
database: #{db_name}
|
101
|
+
username: #{db_user}
|
102
|
+
password: #{db_pswd}
|
103
|
+
host: localhost
|
104
|
+
encoding: utf8
|
105
|
+
socket: /var/lib/mysql/mysql.sock
|
106
|
+
|
107
|
+
development:
|
108
|
+
<<: *default
|
109
|
+
|
110
|
+
test:
|
111
|
+
<<: *default
|
112
|
+
|
113
|
+
staging:
|
114
|
+
<<: *default
|
115
|
+
|
116
|
+
production:
|
117
|
+
<<: *default
|
118
|
+
}
|
119
|
+
end
|
120
|
+
|
121
|
+
###############################################################################
|
122
|
+
### Configure VHost ###
|
123
|
+
###############################################################################
|
124
|
+
vhost_conf = application_root['conf/vhost.conf'].create
|
125
|
+
vhost_conf.write %{
|
126
|
+
DocumentRoot #{application_root.full_path}/host/public
|
127
|
+
RailsEnv #{application[:environment] || 'production'}
|
128
|
+
|
129
|
+
ErrorDocument 503 /503.html
|
130
|
+
RewriteEngine on
|
131
|
+
RewriteCond %{DOCUMENT_ROOT}/../tmp/stop.txt -f
|
132
|
+
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
133
|
+
RewriteRule ^(.*)$ /$1 [R=503,L]
|
134
|
+
|
135
|
+
<Directory "#{application_root.full_path}/host/public">
|
136
|
+
Options All
|
137
|
+
AllowOverride All
|
138
|
+
Order allow,deny
|
139
|
+
Allow from all
|
140
|
+
</Directory>
|
141
|
+
}
|
142
|
+
|
143
|
+
###############################################################################
|
144
|
+
### Set Owner of Host ###
|
145
|
+
###############################################################################
|
146
|
+
box.bash %{ chown #{short_user}:psaserv "#{application_root.full_path}" }
|
147
|
+
box.bash %{ chown -R #{short_user}:psaserv "#{application_root.full_path}/host" }
|
148
|
+
box.bash %{ chown -R #{short_user}:psaserv "#{application_root.full_path}/shared" }
|
149
|
+
|
150
|
+
###############################################################################
|
151
|
+
### Reconfigure vhost in Plesk ###
|
152
|
+
###############################################################################
|
153
|
+
box.bash %{ /usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=#{basedomain} }
|
154
|
+
|
155
|
+
###############################################################################
|
156
|
+
### Restart apache ###
|
157
|
+
###############################################################################
|
158
|
+
box.bash %{ /etc/init.d/httpd restart }
|
159
|
+
|
160
|
+
set :root_path, application_root.full_path.to_s
|
161
|
+
set :www_user, short_user.to_s
|
162
|
+
set :www_group, 'psaserv'
|