rails_pwnerer 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1 +1,3 @@
1
+ v0.2. Supports 'install' to install application from svn. No configuration outside of config/main.rb.
2
+
1
3
  v0.1. Initial release. Supports 'den00b' to setup the environment.
data/Manifest CHANGED
@@ -1,10 +1,16 @@
1
1
  bin/rpwn
2
2
  CHANGELOG
3
+ lib/pwnage/app/cluster_config.rb
4
+ lib/pwnage/app/database.rb
5
+ lib/pwnage/app/main.rb
6
+ lib/pwnage/app/nginx_config.rb
7
+ lib/pwnage/app/svn.rb
3
8
  lib/pwnage/base/dirs.rb
4
9
  lib/pwnage/base/gems.rb
5
10
  lib/pwnage/base/packages.rb
6
11
  lib/pwnage/base/startup.rb
7
12
  lib/pwnage/base.rb
13
+ lib/pwnage/config/main.rb
8
14
  lib/pwnage/executor.rb
9
15
  lib/pwnage/scaffolds/dirs.rb
10
16
  lib/pwnage/scaffolds/gems.rb
data/README CHANGED
@@ -1,9 +1,26 @@
1
- This is the ruby driver for the Trusted Execution Module prototype produced at MIT. The best feature of the
2
- ruby driver is the very powerful DSL (domain-specific language) that TEM procedures are compiled from.
1
+ I want this gem to be Pure Pwnage with Rails.
3
2
 
4
- This work is not ready for publication. If you come across this document and/or software, please do not use or
5
- distribute it until this notice is removed.
3
+ I built +rails_pwnerer+ because I hate the messy process of putting a rails
4
+ application into production. I don't enjoy following 10-page guides, so I'd
5
+ much rather having ruby do that for me.
6
6
 
7
- Running coverage tests:
8
- gem install rcov
9
- rcov -Ilib test/*.rb
7
+ Right now, +rails_pwnerer+ assumes a fresh Ubuntu 7.10 installation. You're
8
+ welcome to test/update for other systems, and I'll be happy to merge patches,
9
+ or add you to the RubyForge project.
10
+
11
+
12
+ Quick start with +rails_pwnerer+:
13
+
14
+ 1) Install rubygems:
15
+ sudo apt-get install rubygems
16
+
17
+ 2) Install +rails_pwnerer+:
18
+ sudo gem install rails_pwnerer
19
+
20
+ 3) Get your server production-ready:
21
+ sudo rpwn den00b
22
+
23
+ 4) Install your application:
24
+ sudo rpwn install svn+ssh://your.repo/path/to/your_application
25
+
26
+ That's it. Reboot, and you're ready.
@@ -0,0 +1,45 @@
1
+ # builds the mongrel cluster configuration
2
+
3
+ require 'fileutils'
4
+
5
+ class RailsPwnage::App::ClusterConfig
6
+ include RailsPwnage::Base
7
+
8
+ def fix_permissions(app_name)
9
+ pwnerer_user = RailsPwnage::Config.pwnerer_user
10
+ pwnerer_uid = uid_for_username(pwnerer_user)
11
+ pwnerer_group = group_for_username(pwnerer_user)
12
+
13
+ with_dir(RailsPwnage::Config.path_to(app_name)) do
14
+ %w(log tmp public).each do |writable_dir|
15
+ FileUtils.chown_R(pwnerer_uid, pwnerer_group, writable_dir)
16
+ end
17
+ end
18
+ end
19
+
20
+ def configure_mongrels(app_name)
21
+ app_path = RailsPwnage::Config.path_to app_name
22
+ pwnerer_user = RailsPwnage::Config.pwnerer_user
23
+ pwnerer_group = group_for_username(pwnerer_user)
24
+
25
+ instances = RailsPwnage::Config.app_instances(app_name)
26
+ first_port = RailsPwnage::Config.app_port0(app_name)
27
+
28
+ with_dir(app_path) do
29
+ # create the mongrel_cluster configuration
30
+ system "mongrel_rails cluster::configure -e production -N #{instances} -p #{first_port} -a 127.0.0.1 -c #{app_path} --user #{pwnerer_user} --group #{pwnerer_group}"
31
+
32
+ # copy the configuration to the launch directory
33
+ FileUtils.cp('config/mongrel_cluster.yml', "#{RailsPwnage::Config.path_to :mongrel_configs}#{File::SEPARATOR}#{app_name}.yml")
34
+ end
35
+ end
36
+
37
+ def setup(app_name)
38
+ configure_mongrels app_name
39
+ fix_permissions app_name
40
+ end
41
+
42
+ def self.setup(app_name)
43
+ self.new.setup(app_name)
44
+ end
45
+ end
@@ -0,0 +1,50 @@
1
+ # sets up the application database
2
+
3
+ class RailsPwnage::App::Database
4
+ include RailsPwnage::Base
5
+
6
+ # creates the mysql database for the application
7
+ def create_databse(app_name)
8
+ db_name = RailsPwnage::Config.app_dbname(app_name)
9
+ db_user = RailsPwnage::Config.app_db_user(app_name)
10
+ db_pass = RailsPwnage::Config.app_db_password(app_name)
11
+
12
+ with_dir(RailsPwnage::Config.path_to(app_name)) do
13
+ # put together the creation script
14
+ sql_commands = <<ENDSQL
15
+ CREATE DATABASE #{db_name};
16
+ GRANT ALL ON #{db_name}.* TO '#{db_user}'@'localhost' IDENTIFIED BY '#{db_pass}' WITH GRANT OPTION;
17
+ ENDSQL
18
+
19
+ # run it
20
+ File.open('tmp/create_db.sql', 'w') { |f| f.write sql_commands }
21
+ system "mysql -uroot < tmp/create_db.sql"
22
+
23
+ # cleanup
24
+ File.delete('tmp/create_db.sql')
25
+ end
26
+ end
27
+
28
+ # configures rails to use the database in the production environment
29
+ def configure_rails(app_name)
30
+ # TODO: write rails configuration file
31
+ end
32
+
33
+ # migrates the database so it's ready
34
+ def migrate_database(app_name)
35
+ with_dir(RailsPwnage::Config.path_to(app_name)) do
36
+ # now migrate the database
37
+ system "rake db:migrate RAILS_ENV=production"
38
+ end
39
+ end
40
+
41
+ def setup(app_name)
42
+ create_databse app_name
43
+ configure_rails app_name
44
+ migrate_database app_name
45
+ end
46
+
47
+ def self.setup(app_name)
48
+ self.new.setup(app_name)
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ module RailsPwnage::App
2
+ # installs an application given its SVN path
3
+ def self.install(svn_path)
4
+ app_name = Svn.setup(svn_path)
5
+ Database.setup(app_name)
6
+ ClusterConfig.setup(app_name)
7
+ NginxConfig.setup(app_name)
8
+ end
9
+ end
@@ -0,0 +1,63 @@
1
+ # builds the nginx configuration
2
+
3
+ class RailsPwnage::App::NginxConfig
4
+ include RailsPwnage::Base
5
+
6
+ # writes the nginx configuration for this server
7
+ def config_nginx(app_name)
8
+ first_port = RailsPwnage::Config.app_port0(app_name)
9
+ hostname_filter = RailsPwnage::Config.app_servername(app_name)
10
+
11
+ File.open(RailsPwnage::Config.path_to(:nginx_configs) + File::SEPARATOR + app_name, 'w') do |f|
12
+ # link to the mongrels
13
+ f << " upstream #{app_name} {\n"
14
+ RailsPwnage::Config.app_instances(app_name).times do |instance|
15
+ f << " server 127.0.0.1:#{first_port + instance};\n"
16
+ end
17
+ f << " }\n\n"
18
+
19
+ # server configuration -- big and ugly
20
+ f << <<NGINX_CONFIG
21
+ server {
22
+ listen 80;
23
+ #{(hostname_filter.nil? ? '' : "server_name " + hostname_filter + ";")}
24
+ root #{RailsPwnage::Config.path_to(app_name)};
25
+ client_max_body_size #{RailsPwnage::Config.app_max_request_mb(app_name)}M;
26
+ location / {
27
+ proxy_set_header X-Real-IP $remote_addr;
28
+ proxy_set_header Host $host;
29
+ proxy_redirect false;
30
+ proxy_connect_timeout 2;
31
+ proxy_read_timeout 86400;
32
+
33
+ if (-f $request_filename/index.html) {
34
+ rewrite (.*) $1/index.html break;
35
+ }
36
+ if (-f $request_filename.html) {
37
+ rewrite (.*) $1.html break;
38
+ }
39
+ if (!-f $request_filename) {
40
+ proxy_pass http://#{app_name};
41
+ break;
42
+ }
43
+ }
44
+ }
45
+ NGINX_CONFIG
46
+ end
47
+ end
48
+
49
+ # removes the default configuration stub (so nginx doesn't stumble upon it)
50
+ def remove_nginx_stub
51
+ stub_file = RailsPwnage::Config.path_to(:nginx_configs) + File::SEPARATOR + 'default'
52
+ File.delete(stub_file) if File.exists?(stub_file)
53
+ end
54
+
55
+ def setup(app_name)
56
+ config_nginx(app_name)
57
+ remove_nginx_stub
58
+ end
59
+
60
+ def self.setup(app_name)
61
+ self.new.setup(app_name)
62
+ end
63
+ end
@@ -0,0 +1,33 @@
1
+ # checks out the application
2
+
3
+ require 'fileutils'
4
+
5
+ class RailsPwnage::App::Svn
6
+ include RailsPwnage::Base
7
+
8
+ def checkout(svn_path)
9
+ app_name = File.basename(svn_path)
10
+
11
+ prod_apps = RailsPwnage::Config.path_to :prod_apps
12
+ with_dir(prod_apps) do
13
+ # checkout application
14
+ system "svn co #{svn_path} #{app_name}"
15
+ end
16
+
17
+ return app_name
18
+ end
19
+
20
+ def setup(svn_path)
21
+ app_name = checkout(svn_path)
22
+
23
+ # TODO: fetch configuration, feed to system
24
+
25
+ return app_name
26
+ end
27
+
28
+ def self.setup(svn_path)
29
+ self.new.setup(svn_path)
30
+ end
31
+
32
+ # TODO: uninstallation
33
+ end
@@ -4,7 +4,7 @@ require 'etc'
4
4
 
5
5
  module RailsPwnage::Base
6
6
  # runs the associated block within a certain directory
7
- def self.with_dir(dir)
7
+ def with_dir(dir)
8
8
  old_dir = Dir.pwd()
9
9
  Dir.chdir(dir)
10
10
 
@@ -16,8 +16,22 @@ module RailsPwnage::Base
16
16
  end
17
17
 
18
18
  # gets the UID associated with the username
19
- def self.uid_for_username(name)
20
- passwd_entry = getpwnam(name)
19
+ def uid_for_username(name)
20
+ passwd_entry = Etc.getpwnam(name)
21
21
  return (passwd_entry.nil?) ? nil : passwd_entry.uid
22
- end
23
- end
22
+ end
23
+
24
+ # gets the GID associated with the username
25
+ def gid_for_username(name)
26
+ passwd_entry = Etc.getpwnam(name)
27
+ return (passwd_entry.nil?) ? nil : passwd_entry.gid
28
+ end
29
+
30
+ # gets the main group of the given user
31
+ def group_for_username(name)
32
+ gid = gid_for_username(name)
33
+ return nil if gid.nil?
34
+ group_entry = Etc.getgrgid(gid)
35
+ return (group_entry.nil?) ? nil : group_entry.name
36
+ end
37
+ end
@@ -16,4 +16,16 @@ module RailsPwnage::Base
16
16
  # add to boot sequence
17
17
  system "update-rc.d #{script_name} defaults"
18
18
  end
19
+
20
+ def control_boot_script(script_name, action = :restart)
21
+ path_to_script = "/etc/init.d/#{script_name}"
22
+ case action
23
+ when :stop
24
+ system "#{path_to_script} stop"
25
+ when :start
26
+ system "#{path_to_script} start"
27
+ when :restart
28
+ system "#{path_to_script} restart"
29
+ end
30
+ end
19
31
  end
@@ -0,0 +1,68 @@
1
+ module RailsPwnage::Config
2
+ # the path to something important (e.g. :prod_apps --> path to all production applications)
3
+ def self.path_to(what = :prod)
4
+ case what
5
+ when :prod
6
+ # the directory containing all the production data
7
+ '/prod'
8
+ when :prod_apps
9
+ # the directory containing the production apps
10
+ '/prod/apps'
11
+ when :prod_config
12
+ # the directory containing the config files
13
+ '/prod/config'
14
+ when :mongrel_configs
15
+ # the directory containing the mongrel_cluster config files
16
+ '/etc/mongrel_cluster'
17
+ when :nginx_configs
18
+ # the directory containing the nginx config files
19
+ '/etc/nginx/sites-enabled'
20
+ when String
21
+ # an application
22
+ self.path_to(:prod_apps) + File::SEPARATOR + what
23
+ end
24
+ end
25
+
26
+ # the user which will receive the keys to the production system
27
+ def self.pwnerer_user
28
+ 'victor'
29
+ end
30
+
31
+ # the number of instances for the given application
32
+ def self.app_instances(app_name)
33
+ 4
34
+ end
35
+
36
+ # the first internal port for the given application
37
+ def self.app_port0(app_name)
38
+ 8000
39
+ end
40
+
41
+ # the name of the database for the given application
42
+ def self.app_dbname(app_name)
43
+ # NOTE: convention, this doesn't have to change
44
+ app_name + '_prod'
45
+ end
46
+
47
+ # the datbase user for the given application
48
+ def self.app_db_user(app_name)
49
+ # NOTE: convention, this doesn't have to change
50
+ app_name
51
+ end
52
+
53
+ # the password of the database user for the given application
54
+ def self.app_db_password(app_name)
55
+ 'superpass'
56
+ end
57
+
58
+ # a DNS name for server-based filtering (multiple apps on the same box)
59
+ def self.app_servername(app_name)
60
+ # "#{app_name}.zergling.net"
61
+ nil
62
+ end
63
+
64
+ # the maximum request size (megabytes) to be accepted by an application
65
+ def self.app_max_request_mb(app_name)
66
+ 48
67
+ end
68
+ end
@@ -18,6 +18,10 @@ class RailsPwnage::Executor
18
18
  Gems.go
19
19
  Dirs.go
20
20
  HookMongrels.go
21
+
22
+ when 'install'
23
+ svn_path = args[1]
24
+ RailsPwnage::App.install svn_path
21
25
  end
22
26
  end
23
27
 
@@ -1,16 +1,18 @@
1
1
  # sets up the required directory structure
2
2
 
3
+ require 'fileutils'
4
+
3
5
  class RailsPwnage::Scaffolds::Dirs
4
6
  include RailsPwnage::Base
5
7
 
6
8
  # runner
7
9
  def run
10
+ pwnerer_uid = uid_for_username(RailsPwnage::Config.pwnerer_user)
8
11
  with_dir('/') do
9
- Dir.mkdir('prod')
10
- Dir.mkdir('prod/apps')
11
- File.chown(uid_for_username('victor'), nil, 'prod/apps')
12
- Dir.mkdir('prod/config')
13
- File.chown(uid_for_username('victor'), nil, 'prod/config')
12
+ [:prod_apps, :prod_config].map { |k| RailsPwnage::Config.path_to k }.each do |path|
13
+ FileUtils.mkpath path
14
+ File.chown(pwnerer_uid, nil, path)
15
+ end
14
16
  end
15
17
  end
16
18
 
@@ -21,7 +21,7 @@ class RailsPwnage::Scaffolds::HookMongrels
21
21
 
22
22
  # build the configuration
23
23
  def make_config
24
- Dir.mkdir('/etc/mongrel_cluster')
24
+ Dir.mkdir(RailsPwnage::Config.path_to(:mongrel_configs))
25
25
  end
26
26
 
27
27
  # runner
data/lib/rails_pwnage.rb CHANGED
@@ -10,9 +10,17 @@ require 'pwnage/base/gems.rb'
10
10
  require 'pwnage/base/packages.rb'
11
11
  require 'pwnage/base/startup.rb'
12
12
 
13
+ require 'pwnage/config/main.rb'
14
+
13
15
  require 'pwnage/executor.rb'
14
16
 
15
17
  require 'pwnage/scaffolds/dirs.rb'
16
18
  require 'pwnage/scaffolds/gems.rb'
17
19
  require 'pwnage/scaffolds/hook_mongrels.rb'
18
20
  require 'pwnage/scaffolds/packages.rb'
21
+
22
+ require 'pwnage/app/main.rb'
23
+ require 'pwnage/app/svn.rb'
24
+ require 'pwnage/app/database.rb'
25
+ require 'pwnage/app/cluster_config.rb'
26
+ require 'pwnage/app/nginx_config.rb'
@@ -1,10 +1,10 @@
1
1
 
2
- # Gem::Specification for Rails_pwnerer-0.1
2
+ # Gem::Specification for Rails_pwnerer-0.2
3
3
  # Originally generated by Echoe
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = %q{rails_pwnerer}
7
- s.version = "0.1"
7
+ s.version = "0.2"
8
8
 
9
9
  s.specification_version = 2 if s.respond_to? :specification_version=
10
10
 
@@ -15,8 +15,8 @@ Gem::Specification.new do |s|
15
15
  s.description = %q{Rails deployment tool/hack.}
16
16
  s.email = %q{victor@costan.us}
17
17
  s.executables = ["rpwn"]
18
- s.extra_rdoc_files = ["bin/rpwn", "CHANGELOG", "lib/pwnage/base/dirs.rb", "lib/pwnage/base/gems.rb", "lib/pwnage/base/packages.rb", "lib/pwnage/base/startup.rb", "lib/pwnage/base.rb", "lib/pwnage/executor.rb", "lib/pwnage/scaffolds/dirs.rb", "lib/pwnage/scaffolds/gems.rb", "lib/pwnage/scaffolds/hook_mongrels.rb", "lib/pwnage/scaffolds/packages.rb", "lib/rails_pwnage.rb", "LICENSE", "README"]
19
- s.files = ["bin/rpwn", "CHANGELOG", "lib/pwnage/base/dirs.rb", "lib/pwnage/base/gems.rb", "lib/pwnage/base/packages.rb", "lib/pwnage/base/startup.rb", "lib/pwnage/base.rb", "lib/pwnage/executor.rb", "lib/pwnage/scaffolds/dirs.rb", "lib/pwnage/scaffolds/gems.rb", "lib/pwnage/scaffolds/hook_mongrels.rb", "lib/pwnage/scaffolds/packages.rb", "lib/rails_pwnage.rb", "LICENSE", "Manifest", "README", "RUBYFORGE", "rails_pwnerer.gemspec"]
18
+ s.extra_rdoc_files = ["bin/rpwn", "CHANGELOG", "lib/pwnage/app/cluster_config.rb", "lib/pwnage/app/database.rb", "lib/pwnage/app/main.rb", "lib/pwnage/app/nginx_config.rb", "lib/pwnage/app/svn.rb", "lib/pwnage/base/dirs.rb", "lib/pwnage/base/gems.rb", "lib/pwnage/base/packages.rb", "lib/pwnage/base/startup.rb", "lib/pwnage/base.rb", "lib/pwnage/config/main.rb", "lib/pwnage/executor.rb", "lib/pwnage/scaffolds/dirs.rb", "lib/pwnage/scaffolds/gems.rb", "lib/pwnage/scaffolds/hook_mongrels.rb", "lib/pwnage/scaffolds/packages.rb", "lib/rails_pwnage.rb", "LICENSE", "README"]
19
+ s.files = ["bin/rpwn", "CHANGELOG", "lib/pwnage/app/cluster_config.rb", "lib/pwnage/app/database.rb", "lib/pwnage/app/main.rb", "lib/pwnage/app/nginx_config.rb", "lib/pwnage/app/svn.rb", "lib/pwnage/base/dirs.rb", "lib/pwnage/base/gems.rb", "lib/pwnage/base/packages.rb", "lib/pwnage/base/startup.rb", "lib/pwnage/base.rb", "lib/pwnage/config/main.rb", "lib/pwnage/executor.rb", "lib/pwnage/scaffolds/dirs.rb", "lib/pwnage/scaffolds/gems.rb", "lib/pwnage/scaffolds/hook_mongrels.rb", "lib/pwnage/scaffolds/packages.rb", "lib/rails_pwnage.rb", "LICENSE", "Manifest", "README", "RUBYFORGE", "rails_pwnerer.gemspec"]
20
20
  s.has_rdoc = true
21
21
  s.homepage = %q{http://www.costan.us/rails_pwnage}
22
22
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rails_pwnerer", "--main", "README"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_pwnerer
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Costan
@@ -22,11 +22,17 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - bin/rpwn
24
24
  - CHANGELOG
25
+ - lib/pwnage/app/cluster_config.rb
26
+ - lib/pwnage/app/database.rb
27
+ - lib/pwnage/app/main.rb
28
+ - lib/pwnage/app/nginx_config.rb
29
+ - lib/pwnage/app/svn.rb
25
30
  - lib/pwnage/base/dirs.rb
26
31
  - lib/pwnage/base/gems.rb
27
32
  - lib/pwnage/base/packages.rb
28
33
  - lib/pwnage/base/startup.rb
29
34
  - lib/pwnage/base.rb
35
+ - lib/pwnage/config/main.rb
30
36
  - lib/pwnage/executor.rb
31
37
  - lib/pwnage/scaffolds/dirs.rb
32
38
  - lib/pwnage/scaffolds/gems.rb
@@ -38,11 +44,17 @@ extra_rdoc_files:
38
44
  files:
39
45
  - bin/rpwn
40
46
  - CHANGELOG
47
+ - lib/pwnage/app/cluster_config.rb
48
+ - lib/pwnage/app/database.rb
49
+ - lib/pwnage/app/main.rb
50
+ - lib/pwnage/app/nginx_config.rb
51
+ - lib/pwnage/app/svn.rb
41
52
  - lib/pwnage/base/dirs.rb
42
53
  - lib/pwnage/base/gems.rb
43
54
  - lib/pwnage/base/packages.rb
44
55
  - lib/pwnage/base/startup.rb
45
56
  - lib/pwnage/base.rb
57
+ - lib/pwnage/config/main.rb
46
58
  - lib/pwnage/executor.rb
47
59
  - lib/pwnage/scaffolds/dirs.rb
48
60
  - lib/pwnage/scaffolds/gems.rb