capifony 2.1.7 → 2.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ require "fileutils"
2
+ require "zlib"
3
+
4
+ namespace :database do
5
+ namespace :dump do
6
+ desc "Dumps remote database"
7
+ task :remote do
8
+ filename = "#{application}.remote_dump.#{Time.now.to_i}.sql.gz"
9
+ file = "/tmp/#{filename}"
10
+ sqlfile = "#{application}_dump.sql"
11
+ config = ""
12
+
13
+ run "cat #{current_path}/app/config/parameters.yml" do |ch, st, data|
14
+ config = load_database_config data, symfony_env_prod
15
+ end
16
+
17
+ case config['database_driver']
18
+ when "pdo_mysql", "mysql"
19
+ run "mysqldump -u#{config['database_user']} --password='#{config['database_password']}' #{config['database_name']} | gzip -c > #{file}" do |ch, stream, data|
20
+ puts data
21
+ end
22
+ when "pdo_pgsql", "pgsql"
23
+ run "pg_dump -U #{config['database_user']} #{config['database_name']} | gzip -c > #{file}" do |ch, stream, data|
24
+ puts data
25
+ end
26
+ end
27
+
28
+
29
+ FileUtils.mkdir_p("backups")
30
+ get file, "backups/#{filename}"
31
+ begin
32
+ FileUtils.ln_sf(filename, "backups/#{application}.remote_dump.latest.sql.gz")
33
+ rescue NotImplementedError # hack for windows which doesnt support symlinks
34
+ FileUtils.cp_r("backups/#{filename}", "backups/#{application}.remote_dump.latest.sql.gz")
35
+ end
36
+ run "rm #{file}"
37
+ end
38
+
39
+ desc "Dumps local database"
40
+ task :local do
41
+ filename = "#{application}.local_dump.#{Time.now.to_i}.sql.gz"
42
+ tmpfile = "backups/#{application}_dump_tmp.sql"
43
+ file = "backups/#{filename}"
44
+ config = load_database_config IO.read('app/config/parameters.yml'), symfony_env_local
45
+ sqlfile = "#{application}_dump.sql"
46
+
47
+ FileUtils::mkdir_p("backups")
48
+ case config['database_driver']
49
+ when "pdo_mysql", "mysql"
50
+ `mysqldump -u#{config['database_user']} --password=\"#{config['database_password']}\" #{config['database_name']} > #{tmpfile}`
51
+ when "pdo_pgsql", "pgsql"
52
+ `pg_dump -U #{config['database_user']} #{config['database_name']} > #{tmpfile}`
53
+ end
54
+
55
+ File.open(tmpfile, "r+") do |f|
56
+ gz = Zlib::GzipWriter.open(file)
57
+ while (line = f.gets)
58
+ gz << line
59
+ end
60
+ gz.flush
61
+ gz.close
62
+ end
63
+
64
+ begin
65
+ FileUtils.ln_sf(filename, "backups/#{application}.local_dump.latest.sql.gz")
66
+ rescue NotImplementedError # hack for windows which doesnt support symlinks
67
+ FileUtils.cp_r("backups/#{filename}", "backups/#{application}.local_dump.latest.sql.gz")
68
+ end
69
+ FileUtils.rm(tmpfile)
70
+ end
71
+ end
72
+
73
+ namespace :move do
74
+ desc "Dumps remote database, downloads it to local, and populates here"
75
+ task :to_local do
76
+ filename = "#{application}.remote_dump.latest.sql.gz"
77
+ config = load_database_config IO.read('app/config/parameters.yml'), symfony_env_local
78
+ sqlfile = "#{application}_dump.sql"
79
+
80
+ database.dump.remote
81
+
82
+ f = File.new("backups/#{sqlfile}", "a+")
83
+ gz = Zlib::GzipReader.new(File.open("backups/#{filename}", "r"))
84
+ f << gz.read
85
+ f.close
86
+
87
+ case config['database_driver']
88
+ when "pdo_mysql", "mysql"
89
+ `mysql -u#{config['database_user']} --password=\"#{config['database_password']}\" #{config['database_name']} < backups/#{sqlfile}`
90
+ when "pdo_pgsql", "pgsql"
91
+ `psql -U #{config['database_user']} --password=\"#{config['database_password']}\" #{config['database_name']} < backups/#{sqlfile}`
92
+ end
93
+ FileUtils.rm("backups/#{sqlfile}")
94
+ end
95
+
96
+ desc "Dumps local database, loads it to remote, and populates there"
97
+ task :to_remote do
98
+ filename = "#{application}.local_dump.latest.sql.gz"
99
+ file = "backups/#{filename}"
100
+ sqlfile = "#{application}_dump.sql"
101
+ config = ""
102
+
103
+ database.dump.local
104
+
105
+ upload(file, "/tmp/#{filename}", :via => :scp)
106
+ run "gunzip -c /tmp/#{filename} > /tmp/#{sqlfile}"
107
+
108
+ run "cat #{shared_path}/config/databases.yml" do |ch, st, data|
109
+ config = load_database_config data, symfony_env_prod
110
+ end
111
+
112
+ case config['database_driver']
113
+ when "pdo_mysql", "mysql"
114
+ run "mysql -u#{config['database_user']} --password='#{config['database_password']}' #{config['database_name']} < /tmp/#{sqlfile}" do |ch, stream, data|
115
+ puts data
116
+ end
117
+ when "pdo_pgsql", "pgsql"
118
+ run "psql -U #{config['database_user']} --password='#{config['database_password']}' #{config['database_name']} < /tmp/#{sqlfile}" do |ch, stream, data|
119
+ puts data
120
+ end
121
+ end
122
+
123
+ run "rm /tmp/#{filename}"
124
+ run "rm /tmp/#{sqlfile}"
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,86 @@
1
+ # Overrided Capistrano tasks
2
+ namespace :deploy do
3
+ desc "Symlinks static directories and static files that need to remain between deployments"
4
+ task :share_childs do
5
+ if shared_children
6
+ pretty_print "--> Creating symlinks for shared directories"
7
+
8
+ shared_children.each do |link|
9
+ run "mkdir -p #{shared_path}/#{link}"
10
+ run "if [ -d #{release_path}/#{link} ] ; then rm -rf #{release_path}/#{link}; fi"
11
+ run "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}"
12
+ end
13
+
14
+ puts_ok
15
+ end
16
+
17
+ if shared_files
18
+ pretty_print "--> Creating symlinks for shared files"
19
+
20
+ shared_files.each do |link|
21
+ link_dir = File.dirname("#{shared_path}/#{link}")
22
+ run "mkdir -p #{link_dir}"
23
+ run "touch #{shared_path}/#{link}"
24
+ run "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}"
25
+ end
26
+
27
+ puts_ok
28
+ end
29
+ end
30
+
31
+ desc "Updates latest release source path"
32
+ task :finalize_update, :except => { :no_release => true } do
33
+ run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
34
+
35
+ pretty_print "--> Creating cache directory"
36
+
37
+ run "if [ -d #{latest_release}/#{cache_path} ] ; then rm -rf #{latest_release}/#{cache_path}; fi"
38
+ run "mkdir -p #{latest_release}/#{cache_path} && chmod -R 0777 #{latest_release}/#{cache_path}"
39
+ run "chmod -R g+w #{latest_release}/#{cache_path}"
40
+
41
+ puts_ok
42
+
43
+ share_childs
44
+
45
+ if fetch(:normalize_asset_timestamps, true)
46
+ stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
47
+ asset_paths = asset_children.map { |p| "#{latest_release}/#{p}" }.join(" ")
48
+
49
+ if asset_paths.chomp.empty?
50
+ puts " No asset paths found, skipped".yellow
51
+ else
52
+ pretty_print "--> Normalizing asset timestamps"
53
+
54
+ run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
55
+ puts_ok
56
+ end
57
+ end
58
+ end
59
+
60
+ desc <<-DESC
61
+ Deploys and starts a `cold' application. This is useful if you have not \
62
+ deployed your application before.
63
+ DESC
64
+ task :cold do
65
+ update
66
+ start
67
+ end
68
+
69
+ desc "Deploys the application and runs the test suite"
70
+ task :testall do
71
+ update_code
72
+ create_symlink
73
+ run "cd #{latest_release} && phpunit -c #{app_path} src"
74
+ end
75
+
76
+ desc "Runs the Symfony2 migrations"
77
+ task :migrate do
78
+ if model_manager == "doctrine"
79
+ symfony.doctrine.migrations.migrate
80
+ else
81
+ if model_manager == "propel"
82
+ puts " Propel doesn't have built-in migration for now".yellow
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,118 @@
1
+ namespace :symfony do
2
+ namespace :doctrine do
3
+ namespace :cache do
4
+ desc "Clears all metadata cache for a entity manager"
5
+ task :clear_metadata do
6
+ pretty_print "--> Clearing Doctrine metadata cache"
7
+
8
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-metadata --env=#{symfony_env_prod}"
9
+ puts_ok
10
+ end
11
+
12
+ desc "Clears all query cache for a entity manager"
13
+ task :clear_query do
14
+ pretty_print "--> Clearing Doctrine query cache"
15
+
16
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-query --env=#{symfony_env_prod}"
17
+ puts_ok
18
+ end
19
+
20
+ desc "Clears result cache for a entity manager"
21
+ task :clear_result do
22
+ pretty_print "--> Clearing Doctrine result cache"
23
+
24
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-result --env=#{symfony_env_prod}"
25
+ puts_ok
26
+ end
27
+ end
28
+
29
+ namespace :database do
30
+ desc "Creates the configured databases"
31
+ task :create, :roles => :db, :only => { :primary => true } do
32
+ pretty_print "--> Creating databases"
33
+
34
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:database:create --env=#{symfony_env_prod}"
35
+ puts_ok
36
+ end
37
+
38
+ desc "Drops the configured databases"
39
+ task :drop, :roles => :db, :only => { :primary => true } do
40
+ pretty_print "--> Dropping databases"
41
+
42
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:database:drop --env=#{symfony_env_prod}"
43
+ puts_ok
44
+ end
45
+ end
46
+
47
+ namespace :schema do
48
+ desc "Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output"
49
+ task :create do
50
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:create --env=#{symfony_env_prod}"
51
+ end
52
+
53
+ desc "Drops the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output"
54
+ task :drop do
55
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:drop --env=#{symfony_env_prod}"
56
+ end
57
+ end
58
+
59
+ namespace :migrations do
60
+ desc "Executes a migration to a specified version or the latest available version"
61
+ task :migrate, :roles => :db, :only => { :primary => true } do
62
+ currentVersion = nil
63
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:status --env=#{symfony_env_prod}" do |ch, stream, out|
64
+ if stream == :out and out =~ /Current Version:[^$]+\(([\w]+)\)/
65
+ currentVersion = Regexp.last_match(1)
66
+ end
67
+ if stream == :out and out =~ /Current Version:\s*0\s*$/
68
+ currentVersion = 0
69
+ end
70
+ end
71
+
72
+ if currentVersion == nil
73
+ raise "Could not find current database migration version"
74
+ end
75
+ puts " Current database version: #{currentVersion}"
76
+
77
+ on_rollback {
78
+ if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database back to version #{currentVersion}? (y/N)")
79
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:migrate #{currentVersion} --env=#{symfony_env_prod} --no-interaction"
80
+ end
81
+ }
82
+
83
+ if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database? (y/N)")
84
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:migrate --env=#{symfony_env_prod} --no-interaction"
85
+ end
86
+ end
87
+
88
+ desc "Views the status of a set of migrations"
89
+ task :status do
90
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:status --env=#{symfony_env_prod}"
91
+ end
92
+ end
93
+
94
+ namespace :mongodb do
95
+ namespace :schema do
96
+ desc "Allows you to create databases, collections and indexes for your documents"
97
+ task :create do
98
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:mongodb:schema:create --env=#{symfony_env_prod}"
99
+ end
100
+
101
+ desc "Allows you to drop databases, collections and indexes for your documents"
102
+ task :drop do
103
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:mongodb:schema:drop --env=#{symfony_env_prod}"
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ namespace :init do
110
+ desc "Mounts ACL tables in the database"
111
+ task :acl, :roles => :db, :only => { :primary => true } do
112
+ pretty_print "--> Mounting Doctrine ACL tables"
113
+
114
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} init:acl --env=#{symfony_env_prod}"
115
+ puts_ok
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,44 @@
1
+ require 'colored'
2
+
3
+ # Be less verbose by default
4
+ logger.level = Logger::IMPORTANT
5
+
6
+ STDOUT.sync
7
+ def pretty_print(msg)
8
+ if logger.level == Logger::IMPORTANT
9
+ pretty_errors
10
+
11
+ msg << '.' * (55 - msg.size)
12
+ print msg
13
+ else
14
+ puts msg.green
15
+ end
16
+ end
17
+
18
+ def puts_ok
19
+ if logger.level == Logger::IMPORTANT
20
+ puts '✔'.green
21
+ end
22
+ end
23
+
24
+ $pretty_errors_defined = false
25
+
26
+ def pretty_errors
27
+ if !$pretty_errors_defined
28
+ $pretty_errors_defined = true
29
+
30
+ class << $stderr
31
+ @@firstLine = true
32
+ alias _write write
33
+
34
+ def write(s)
35
+ if @@firstLine
36
+ s = '✘' << "\n" << s
37
+ @@firstLine = false
38
+ end
39
+
40
+ _write(s.red)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,80 @@
1
+ namespace :symfony do
2
+ namespace :propel do
3
+ namespace :database do
4
+ desc "Creates the configured databases"
5
+ task :create, :roles => :db, :only => { :primary => true } do
6
+ pretty_print "--> Creating databases"
7
+
8
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:database:create --env=#{symfony_env_prod}"
9
+ puts_ok
10
+ end
11
+
12
+ desc "Drops the configured databases"
13
+ task :drop, :roles => :db, :only => { :primary => true } do
14
+ pretty_print "--> Dropping databases"
15
+
16
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:database:drop --env=#{symfony_env_prod}"
17
+ puts_ok
18
+ end
19
+ end
20
+
21
+ namespace :build do
22
+ desc "Builds the Model classes"
23
+ task :model do
24
+ command = "propel:model:build"
25
+ if /2\.0\.[0-9]+.*/ =~ symfony_version
26
+ command = "propel:build-model"
27
+ end
28
+
29
+ pretty_print "--> Generating Propel classes"
30
+
31
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} #{command} --env=#{symfony_env_prod}"
32
+ puts_ok
33
+ end
34
+
35
+ desc "Builds SQL statements"
36
+ task :sql do
37
+ command = "propel:sql:build"
38
+ if /2\.0\.[0-9]+.*/ =~ symfony_version
39
+ command = "propel:build-sql"
40
+ end
41
+
42
+ pretty_print "--> Generating Propel SQL"
43
+
44
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} #{command} --env=#{symfony_env_prod}"
45
+ puts_ok
46
+ end
47
+
48
+ desc "Inserts SQL statements"
49
+ task :sql_load, :roles => :db, :only => { :primary => true } do
50
+ command = "propel:sql:insert"
51
+ if /2\.0\.[0-9]+.*/ =~ symfony_version
52
+ command = "propel:insert-sql"
53
+ end
54
+
55
+ pretty_print "--> Inserting Propel SQL"
56
+
57
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} #{command} --force --env=#{symfony_env_prod}"
58
+ puts_ok
59
+ end
60
+
61
+ desc "Builds the Model classes, SQL statements and insert SQL"
62
+ task :all_and_load do
63
+ pretty_print "--> Setting up Propel (classes, SQL)"
64
+
65
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:build --insert-sql --env=#{symfony_env_prod}"
66
+ puts_ok
67
+ end
68
+
69
+ desc "Generates ACLs models"
70
+ task :acl do
71
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:acl:init --env=#{symfony_env_prod}"
72
+ end
73
+
74
+ desc "Inserts propel ACL tables"
75
+ task :acl_load, :roles => :db, :only => { :primary => true } do
76
+ run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:acl:init --env=#{symfony_env_prod} --force"
77
+ end
78
+ end
79
+ end
80
+ end