Skip to content

Commit

Permalink
moves user folder structure to user model
Browse files Browse the repository at this point in the history
  • Loading branch information
MSchmidt committed Jun 3, 2010
2 parents f8a9eef + 131972c commit fe2f954
Show file tree
Hide file tree
Showing 22 changed files with 233 additions and 210 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ doc/api
doc/app doc/app
db/*.sqlite3 db/*.sqlite3
config/database.yml config/database.yml
config/gccp.yml
config/initializers/session_store.rb config/initializers/session_store.rb
config/initializers/gccp.rb
*.*~ *.*~
*~ *~
nbproject nbproject
Expand Down
37 changes: 0 additions & 37 deletions app/controllers/domains_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,6 @@
class DomainsController < ApplicationController class DomainsController < ApplicationController


before_filter :is_admin, :except => [:index, :show, :edit, :update] before_filter :is_admin, :except => [:index, :show, :edit, :update]
before_filter :check_for_unsaved


# GET /domains # GET /domains
# GET /domains.xml # GET /domains.xml
Expand Down Expand Up @@ -61,7 +60,6 @@ def edit
# ADMIN only # ADMIN only
def create def create
@domain = Domain.new(params[:domain]) @domain = Domain.new(params[:domain])
@domain.saved_by = current_user


respond_to do |format| respond_to do |format|
if @domain.save if @domain.save
Expand Down Expand Up @@ -113,39 +111,4 @@ def destroy
format.xml { head :ok } format.xml { head :ok }
end end
end end

def folder_structure
folders = Domain.get_user_www_dir_structure

respond_to do |format|
format.html { redirect_to(domains_url) }
format.js { render :json => folders }
format.xml do
xml = Builder::XmlMarkup.new(:indent => 2)
xml.ul { build_xml_branch(folders, xml) }
render :xml => xml.target!
end
end
end

protected
def check_for_unsaved
@unsaved_domains = Domain.all(:conditions => { :saved => false, :saved_by => current_user })
@unsaved_domains_count = @unsaved_domains.count
end

def build_xml_branch(branch, xml)
branch.keys.sort.each do |directory|
if branch[directory].empty?
xml.li(directory)
else
xml.li(directory, :class => 'has_sub')
xml.li(:class => "is_sub #{directory}-sub") do
xml.ul do
build_xml_branch(branch[directory], xml)
end
end
end
end
end
end end
48 changes: 40 additions & 8 deletions app/controllers/users_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ def create
@user = User.new(params[:user]) @user = User.new(params[:user])


respond_to do |format| respond_to do |format|
if @user.save if @user.save
#Delayed::Job.enqueue User.find(@user.id) #@user.send_later(:write_config, @user.password)
@user.send_later(:write_config, @user.password)
flash[:notice] = "User was successfully created. Please save the password: #{@user.password}" flash[:notice] = "User was successfully created. Please save the password: #{@user.password}"
format.html { redirect_to(users_url) } format.html { redirect_to(users_url) }
format.xml { render :xml => @user, :status => :created, :location => @user } format.xml { render :xml => @user, :status => :created, :location => @user }
Expand All @@ -55,7 +54,8 @@ def update


respond_to do |format| respond_to do |format|
if @user.update_attributes(params[:user]) if @user.update_attributes(params[:user])
if params[:user][:password] && params[:user][:password].any? #@user.send_later(:update_config, @user.password, oldname)
unless params[:user][:password].blank?
flash[:notice] = "User was successfully updated. Please save the new password: #{@user.password}" flash[:notice] = "User was successfully updated. Please save the new password: #{@user.password}"
else else
flash[:notice] = "User was successfully updated." flash[:notice] = "User was successfully updated."
Expand All @@ -77,7 +77,8 @@ def update_password
@user = current_user @user = current_user


respond_to do |format| respond_to do |format|
if @user.valid_password?(params[:old_password]) && (params[:user][:password] == params[:user][:password_confirmation]) && params[:user][:password].any? && @user.update_attribute(:password, params[:user][:password]) if @user.valid_password?(params[:old_password]) && (params[:user][:password] == params[:user][:password_confirmation]) && params[:user][:password] && @user.update_attribute(:password, params[:user][:password])
@user.send_later(:update_config, @user.password)
flash[:notice] = 'User Password was successfully changed.' flash[:notice] = 'User Password was successfully changed.'
format.html { redirect_to(user_root_url) } format.html { redirect_to(user_root_url) }
format.xml { head :ok } format.xml { head :ok }
Expand All @@ -94,7 +95,7 @@ def update_db_password


respond_to do |format| respond_to do |format|
if @user.update_attribute(:dbpassword, params[:user][:dbpassword]) if @user.update_attribute(:dbpassword, params[:user][:dbpassword])
ConnectedDatabase.send_later(:change_user_password, :name => @user.name, :password => @user.dbpassword) ConnectedDatabase.send_later(:change_user_password, :name => @user.username, :password => @user.dbpassword)
flash[:notice] = 'User DB Password was successfully changed.' flash[:notice] = 'User DB Password was successfully changed.'
format.html { redirect_to(databases_url) } format.html { redirect_to(databases_url) }
format.xml { head :ok } format.xml { head :ok }
Expand All @@ -107,11 +108,42 @@ def update_db_password


def destroy def destroy
@user = User.find(params[:id]) @user = User.find(params[:id])
@user.send_later(:destroy_config, @user.name) if @user.destroy @user.destroy

respond_to do |format| respond_to do |format|
format.html { redirect_to(users_url) } format.html { redirect_to(users_url) }
format.xml { head :ok } format.xml { head :ok }
end end
end end

def folder_structure
folders = User.get_user_www_dir_structure

respond_to do |format|
format.html { redirect_to(domains_url) }
format.js { render :json => folders }
format.xml do
xml = Builder::XmlMarkup.new(:indent => 2)
xml.ul { build_xml_branch(folders, xml) }
render :xml => xml.target!
end
end
end

protected

def build_xml_branch(branch, xml)
branch.keys.sort.each do |directory|
if branch[directory].empty?
xml.li(directory)
else
xml.li(directory, :class => 'has_sub')
xml.li(:class => "is_sub #{directory}-sub") do
xml.ul do
build_xml_branch(branch[directory], xml)
end
end
end
end
end
end end
19 changes: 13 additions & 6 deletions app/models/connected_database.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,38 +2,45 @@ class ConnectedDatabase < ActiveRecord::Base
establish_connection :connected_database establish_connection :connected_database


def self.create_database(options={}) def self.create_database(options={})
if options[:name] unless options[:name].blank?
connection.execute "CREATE DATABASE `#{options[:name]}`;" connection.execute "CREATE DATABASE `#{options[:name]}`;"
end end
end end


def self.create_user(options={}) def self.create_user(options={})
if options[:name] && options[:password] unless options[:name].blank? && options[:password].blank?
connection.execute "CREATE USER '#{options[:name]}'@'localhost' IDENTIFIED BY '#{options[:password]}';" connection.execute "CREATE USER '#{options[:name]}'@'localhost' IDENTIFIED BY '#{options[:password]}';"
end end
end end


def self.grant_permission(options={}) def self.grant_permission(options={})
if options[:dbname] && options[:user] unless options[:dbname].blank? && options[:user].blank?
connection.execute "GRANT SELECT , INSERT , UPDATE , DELETE ON `#{options[:dbname]}`.* TO '#{options[:user]}'@'localhost';" connection.execute "GRANT SELECT , INSERT , UPDATE , DELETE ON `#{options[:dbname]}`.* TO '#{options[:user]}'@'localhost';"
end end
end end


def self.destroy_database(options={}) def self.destroy_database(options={})
if options[:name] unless options[:name].blank?
connection.execute "DROP DATABASE `#{options[:name]}`;" connection.execute "DROP DATABASE `#{options[:name]}`;"
end end
end end


def self.destroy_user(options={}) def self.destroy_user(options={})
if options[:name] unless options[:name].blank?
connection.execute "DROP USER '#{options[:name]}'@'localhost';" connection.execute "DROP USER '#{options[:name]}'@'localhost';"
end end
end end


def self.change_user_password(options={}) def self.change_user_password(options={})
if options[:name] && options[:password] unless options[:name].blank? && options[:password].blank?
connection.execute "SET PASSWORD FOR '#{options[:name]}'@'localhost' = PASSWORD( '#{options[:password]}' );" connection.execute "SET PASSWORD FOR '#{options[:name]}'@'localhost' = PASSWORD( '#{options[:password]}' );"
end end
end end

def self.rename_user(options={})
unless options[:name].blank? && options[:oldname].blank?
res = connection.execute("SELECT COUNT(*) FROM user WHERE User = '#{options[:name]}';")
connection.execute "RENAME USER '#{options[:oldname]}'@'localhost' TO '#{options[:name]}'@'localhost';" if res.fetch_row.to_i != 0
end
end
end end
2 changes: 1 addition & 1 deletion app/models/database.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def perform


def write_config def write_config
dbname = self.name dbname = self.name
username = self.user.name username = self.user.username
userpwd = self.user.dbpassword userpwd = self.user.dbpassword


ConnectedDatabase::create_database(:name => dbname) ConnectedDatabase::create_database(:name => dbname)
Expand Down
79 changes: 23 additions & 56 deletions app/models/domain.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,59 +5,42 @@ class Domain < ActiveRecord::Base
validates_uniqueness_of :fqdn validates_uniqueness_of :fqdn
validates_length_of :fqdn, :minimum => 4 validates_length_of :fqdn, :minimum => 4
validates_presence_of :user_id validates_presence_of :user_id
validates_presence_of :saved_by


def perform def perform
write_config write_config
end end


def write_config def write_config
begin servername = self.fqdn
servername = self.fqdn serveralias = "www." + servername
serveralias = "www." + servername email = self.user.email
email = self.user.email path = File.join(self.user.userpath, self.mount_point)
folder = self.mount_point php = self.php
php = self.php rails = self.rails
rails = self.rails


vhost_template = ERB.new(read_template('apache2_vhost.conf')).result(binding) vhost_template = ERB.new(read_template('apache2_vhost.conf')).result(binding)
index_template = ERB.new(read_template('index.html')).result(binding) index_template = ERB.new(read_template('index.html')).result(binding)


Dir.chdir(VHOST_TARGET_DIR) #definend in config/initializers/gccp.rb File.open(File.join(VHOST_TARGET_DIR,VHOST_PREFIX + servername), "w") do |f|

f.write(vhost_template)
File.open('gccp_' + servername, "w") do |f| end
f.write(vhost_template)
end
system("a2ensite gccp_#{servername}")
system("/etc/init.d/apache2 reload")


Dir.mkdir(WWW_DIR + folder) Dir.mkdir(path) unless File.exists?(path)
Dir.chdir(WWW_DIR + folder)


if rails indexfile = File.join(path, "index.html")
#adduser deploy indexfile = File.join(path, "index.php") if php
#gem install mongrel_cluster
#a2enmod rewrite
#a2enmod proxy
#a2enmod proxy_http
#a2enmod proxy_balancer
system("chown deploy:deploy #{WWW_DIR + folder}")
system("mkdir -p current/public")
system("echo 'hello world' > current/public/index.html")
Dir.mkdir("/etc/mongrel_cluster")
system("chown deploy:deploy /etc/mongrel_cluster")
end

indexfile = "index.html"
indexfile = "index.php" if php


unless File.exists?(indexfile)
File.open(indexfile, "w") do |f| File.open(indexfile, "w") do |f|
f.write(index_template) f.write(index_template)
end end
end

system("a2ensite #{VHOST_PREFIX+servername}")
system("/etc/init.d/apache2 reload")


rescue Errno::ENOENT rescue Errno::ENOENT
puts "No such directory" puts "No such directory"
end
end end


def read_template(file) def read_template(file)
Expand All @@ -71,11 +54,11 @@ def destroy_config
servername = self.fqdn servername = self.fqdn
folder = self.mount_point folder = self.mount_point


system("a2dissite gccp_#{servername}") system("a2dissite #{VHOST_PREFIX+servername}")


Dir.chdir(VHOST_TARGET_DIR) #definend in config/initializers/gccp.rb vhost_path = File.join(VHOST_TARGET_DIR, VHOST_PREFIX + servername)
File.delete('gccp_' + servername) if File.exist?('gccp_'+servername) File.delete(vhost_path) if File.exist?(vhost_path)
system("rm -rf #{WWW_DIR + folder}") #system("rm -rf " + File.join(WWW_DIR, self.mount_point))


system("/etc/init.d/apache2 reload") system("/etc/init.d/apache2 reload")


Expand All @@ -84,20 +67,4 @@ def destroy_config
end end
end end
end end

def self.get_user_www_dir_structure
folders = Hash.new{ |h,k| h[k] = Hash.new &h.default_proc }

#Dir.chdir(WWW_DIR)
Dir.glob(File.join("**", "**")) do |path|
if File.directory?(path)
sub = folders
path.split('/').each do |dir|
sub = sub[dir]
end
end
end

return folders
end
end end
Loading

0 comments on commit fe2f954

Please sign in to comment.