Skip to content

Commit f0f441b

Browse files
committed
Initial BDSM Ruby Extension Set
0 parents  commit f0f441b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1221
-0
lines changed

README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A BDSM extension set providing extensions for ruby

rails/CHANGELOG.md

Whitespace-only changes.

rails/LICENSE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Copyright (c) 2009-2011 Wayne E. Seguin
2+
3+
Licensed under the Apache License, Version 2.0 (the \"License\");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an \"AS IS\" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+

rails/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# BDSM Rails Extension
2+
3+
The Rails extension is used to manage all aspects of a Ruby/Rails
4+
application project deployment as a user.
5+
6+
For example, if the project name is 'todo' then for deployment
7+
you would typically create an 'todo' user on the server with
8+
home directory of /home/todo.
9+
10+
An example walkthrough of using BDSM Rails extension in conjunction
11+
with other extensions (below) to manage an application deployment.
12+
13+
root# bdsm install rails deploy unicorn nginx
14+
15+
todo$ bdsm bdsmrc
16+
todo$ vim ~/.bdsmrc # Adjust any settings necessary, set repo url.
17+
todo$ bdsm rails setup
18+
todo$ vim ~/shared/config/database.yml # Tune your database.
19+
todo$ bdsm deploy # Deploy the applicatoin using the 'deploy' ext.
20+
todo$ cd ~/current
21+
todo$ gem install bundler && bundle install # If you swing that way.
22+
todo$ bdsm unicorn install
23+
todo$ bdsm rails console
24+
todo$ bdsm unicorn start # If console loaded properly, start er up!
25+
26+
root# bdsm nginx install
27+
root# bdsm nginx server todo
28+
29+

rails/TODO.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
* Package and release tarball
3+

rails/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.3

rails/actions/.actions

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
setup=rails_setup()
2+
server=rails_server()
3+
s=rails_server()
4+
console=rails_()
5+
dbconsole=rails_dbconsole()
6+
generate=rails_generate()
7+
logtail=rails_logtail()
8+
migrate=rails_migrate()
9+
new=rails_new()
10+
bootstrap=rails_bootstrap()
11+
install=rails_install()

rails/actions/backup_database

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env ruby
2+
3+
require "yaml"
4+
require "fileutils"
5+
6+
# TODO: Load these from the ruby bdsm modules
7+
def log(message)
8+
@log_file ||= "#{ENV["extension_log_path"]}/#{ENV["action"]}.log"
9+
10+
FileUtils.mkdir_p(File.dirname(@log_file)) unless Dir.exist?(File.dirname(@log_file))
11+
12+
File.open(@log_file, 'w+') do |log_file|
13+
log_file.write "#{@timestamp} : #{@application} : #{@environment} : #{message}\n"
14+
end
15+
$stdout.puts message
16+
end
17+
18+
def trace(message)
19+
@trace_flag ||= ENV["trace_flag"].to_i
20+
log(message) if @trace_flag
21+
end
22+
23+
def fail(message)
24+
log "#{@timestamp} : #{@application} : #{@environment} : #{message}"
25+
$stderr.puts "#{@timestamp} : #{@application} : #{@environment} : #{message}"
26+
exit 1
27+
end
28+
29+
def succeed(message)
30+
log(message)
31+
exit 0
32+
end
33+
34+
trace "Loading BDSM #{ENV["extension"]} #{ENV["action"]} Utility."
35+
@timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%S")
36+
# /TODO
37+
38+
#
39+
# Setup & Extract variables
40+
#
41+
@log_path = "#{ENV["HOME"]}/shared/backups/log"
42+
@log_file = "#{@log_path}/backup.log"
43+
44+
@user = (ENV["USER"]|| %x{printf $USER}).strip
45+
46+
@application = ARGV.shift unless ARGV[0].to_s.strip.empty?
47+
@application ||= ENV["project"]
48+
49+
@environment = ARGV.shift unless ARGV[0].to_s.strip.empty?
50+
@environment ||= (ENV["environment"] || ENV["RAILS_ENV"] || ENV["MERB_ENV"] || ENV["RACK_ENV"] || "production")
51+
52+
trace "user: #{@user}, application: #{@application}, environment: #{@environment}."
53+
fail "application must be specified as the first argument." if @application.nil? || @application.empty?
54+
55+
@target_path = "#{ENV["HOME"]}/shared/backups/#{@application}/#{@environment}"
56+
@config = YAML.load_file("#{ENV["HOME"]}/shared/config/database.yml")[@environment]
57+
@adapter = @config["adapter"].to_s.strip
58+
59+
trace "target_path: #{@target_path}, config: #{@config}, adapter: #{@adapter}."
60+
fail "adapter not specified in shared/config/database.yml" if @adapter.nil? || @adapter.empty?
61+
62+
@username = @config["username"] || @user
63+
@username = @user if @username.nil? || @username.empty?
64+
65+
@database = @config["database"].to_s.strip
66+
67+
trace "username: #{@username}, database: #{@database}"
68+
fail "database not specified in shared/config/database.yml" if @database.nil? || @database.empty?
69+
70+
@password = @config["password"].to_s.strip
71+
72+
trace "password: #{@password}"
73+
fail "password: not specified in shared/config/database.yml" if @password.nil? || @password.empty?
74+
75+
@hostname = @config["host"] || "localhost"
76+
@port = @config["port"] || "5432"
77+
78+
trace "hostname: #{@hostname}, port: #{@port}"
79+
trace "Ensuring that target path '#{@target_path}' exists."
80+
81+
FileUtils.mkdir_p(@target_path) unless File.exist?(@target_path)
82+
83+
#
84+
# Perform the backup
85+
#
86+
case @adapter
87+
when "mysql","mysql2"
88+
@dump = %x{which mysqldump}.strip
89+
90+
fail "mysqldump command not found" if @dump.empty?
91+
92+
@flags = "--add-drop-table --complete-insert --default-character-set=utf8 --quote-names --extended-insert"
93+
@filename = "#{@database}%#{@timestamp}.mysql"
94+
@command = %Q(#{@dump} #{@flags} --user=#{@username} -p"#{@password}" --quick #{@database} | bzip2 -c > #{@target_path}/#{@filename}.bz2)
95+
when "postgresql"
96+
97+
@dump = %x{which pg_dump}.strip
98+
99+
fail "pg_dump command not found" if @dump.empty?
100+
101+
@flags = "-b -c -E UTF8 -O -x --no-password"
102+
@filename = "#{@database}%#{@timestamp}.psql"
103+
credentials = "#{@hostname}:#{@port}:#{@database}:#{@username}:#{@password}"
104+
105+
# Only write credentials if they are not already in the file.
106+
pgpass_filename = "#{ENV["HOME"]}/.pgpass"
107+
File.open(pgpass_filename, "w+") do |pgpass|
108+
trace "Ensuring '#{pgpass_filename}' contains credentials '#{credentials}'"
109+
pgpass.write credentials unless pgpass.read.match(credentials)
110+
111+
trace "Ensuring '#{pgpass_filename}' has correct permissions 0600."
112+
pgpass.chmod 0600
113+
end
114+
115+
@command = "#{@dump} #{@flags}"
116+
@command << " -U#{@username} -h #{@hostname} #{@database} "
117+
@command << "| bzip2 -c - > #{@target_path}/#{@filename}.bz2"
118+
119+
when "mongodb"
120+
log "Backups for MongoDB has not yet been implemented"
121+
122+
when "sqlite3"
123+
log "Backups for sqlite3 has not yet been implemented"
124+
125+
when "riak"
126+
log "Backups for riak has not yet been implemented"
127+
128+
else
129+
fail "unknown adapter, supported adapters are currently mysql and postgresql"
130+
%x{rm -f ~/.pgpass}
131+
132+
end
133+
134+
log "Backing up #{@adapter} database."
135+
136+
trace "( #{@command} )"
137+
138+
if system(@command)
139+
succeed "Successfully backed up database to #{@filename}"
140+
else
141+
log "command: #{@command}."
142+
log "Log File: #{@filename}."
143+
%x{tail -10 #{@filename}}
144+
fail "Backup failed!"
145+
end

rails/actions/help

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
description "Extension for managing Rails (http://rubyonrails.org/)."
4+
5+
action "install" "Installs ${package_name} gem."
6+
action "setup" "Bootstrap the current user's directory for a rails application deployment."
7+
action "logtail" "Tail all logs in shared/log."
8+
action "console" "Start an irb rails console."
9+
action "dbconsole" "Start a database console."
10+
action "migrate" "Run migrations."
11+
12+
show_help
13+

rails/config/defaults

Whitespace-only changes.

rails/modules/ruby/dsl.rb

Whitespace-only changes.

rails/modules/ruby/initialize.rb

Whitespace-only changes.

rails/modules/shell/cli

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
3+
usage()
4+
{
5+
log "
6+
CLI Arguments:
7+
8+
-r|--repository - The URL for the project repository code.
9+
10+
-E|--environment - The environment (eg. production,staging,etc...)
11+
12+
-p|--project - The project name, defaults to current user name
13+
14+
-D|--database) - Database name
15+
16+
-s|--server - Application server (eg. unicorn, thin, etc...)
17+
18+
--help - This help text
19+
20+
--licence - Display extension license
21+
"
22+
}
23+
24+
number_of_args=${#extension_args[@]}
25+
26+
for ((index=0 ; index < $number_of_args ; index++))
27+
do
28+
token="${extension_args[$index]}"
29+
30+
case "$token" in
31+
-r|--repo|--repository)
32+
repository_url="${extension_args[$((++index))]}"
33+
;;
34+
-E|--environment|--env)
35+
environment="${extension_args[$((++index))]}"
36+
;;
37+
-p|--project)
38+
project="${extension_args[$((++index))]}"
39+
;;
40+
-D|--database|--db)
41+
database="${extension_args[$((++index))]}"
42+
;;
43+
-s|--server)
44+
server="${extension_args[$((++index))]}"
45+
;;
46+
--scm-flag|-F)
47+
scm_flags+=("${extension_args[$((++index))]}")
48+
;;
49+
--help)
50+
usage
51+
exit 0
52+
;;
53+
--trace)
54+
set -o xtrace
55+
;;
56+
--licence)
57+
display_extension_licence
58+
exit 0
59+
;;
60+
esac
61+
done
62+

0 commit comments

Comments
 (0)