app_maint 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module AppMaint
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,10 @@
1
+ Capistrano::Configuration.instance.load do
2
+ namespace :fix_permissions do
3
+ desc "Fix wrong permissions for apps directory"
4
+ task :setup, roles: :web do
5
+ run "#{sudo} chown -R #{user}:admin /home/#{user}/apps"
6
+ end
7
+
8
+ after "deploy:setup", "fix_permissions:setup"
9
+ end
10
+ end
data/lib/recipes/base.rb CHANGED
@@ -1,43 +1,101 @@
1
+ def template(from, to)
2
+ erb = File.read(File.expand_path("../templates/#{from}", __FILE__))
3
+ put ERB.new(erb).result(binding), to
4
+ end
5
+
1
6
  Capistrano::Configuration.instance.load do
2
7
  namespace :base do
3
8
  desc "Prepare system for deployment"
4
9
  task :setup do
5
10
  init_deploy_user
6
- install
11
+ install_packages
12
+ install_ruby
13
+ pam_ssh_agent_auth
7
14
  end
8
-
15
+
9
16
  desc "Init environment for deployment user"
10
17
  task :init_deploy_user do
11
18
  set :deploy_user, "#{user}"
12
19
  with_user "#{sudo_user}" do
13
20
  if capture( "#{sudo} cat /etc/passwd | grep #{deploy_user} | wc -l" ).to_i == 0
14
21
  run "#{sudo} addgroup admin"
15
- run "#{sudo} useradd deployer -m -s /bin/bash -g admin"
22
+ run "#{sudo} useradd #{deploy_user} -m -s /bin/bash -g admin"
16
23
  upload "#{Dir.home}/.ssh/id_rsa.pub", "/tmp/id_rsa.pub"
17
24
  run "#{sudo} mkdir -p /home/#{deploy_user}/.ssh"
18
25
  run "echo \"cat /tmp/id_rsa.pub >> /home/#{deploy_user}/.ssh/authorized_keys\" | sudo -s"
19
26
  run "rm /tmp/id_rsa.pub"
27
+ run "#{sudo} chown -R #{deploy_user}:admin /home/#{deploy_user}"
20
28
  end
21
29
  end
22
30
  end
23
31
 
24
- desc "Install minimum prerequisites for app_maint"
25
- task :install do
32
+ desc "Installs the necessary OS packages"
33
+ task :install_packages do
34
+ with_user "#{sudo_user}" do
35
+ run "#{sudo} apt-get -y update 1>/dev/null"
36
+ run "#{sudo} apt-get -y install python-software-properties 1>/dev/null"
37
+ run "#{sudo} apt-get -y install curl git-core 1>/dev/null"
38
+ run "#{sudo} apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev 1>/dev/null"
39
+ run "#{sudo} apt-get -y install libpam0g-dev checkinstall 1>/dev/null"
40
+ end
41
+ end
42
+
43
+ desc "Installs up-to-date ruby version from sources"
44
+ task :install_ruby do
45
+ version = (defined? ruby_version) ? ruby_version : '1.9.3'
46
+ patch = (defined? ruby_patch) ? ruby_patch : 'p362'
26
47
  with_user "#{sudo_user}" do
27
- if capture( "which ruby | wc -l" ).to_i == 0
28
- run "#{sudo} apt-get -y update"
29
- run "#{sudo} apt-get -y install python-software-properties"
30
- run "#{sudo} apt-get -y install curl git-core"
31
- run "#{sudo} apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev"
32
- run "wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.gz"
33
- run "tar -xvzf ruby-1.9.3-p194.tar.gz"
34
- run "cd ruby-1.9.3-p194/ && ./configure --prefix=/usr/local && make && #{sudo} make install"
48
+ if capture( "dpkg --list ruby-#{version}#{patch} 2>&1 | grep 'No packages found' | wc -l" ).to_i == 1
49
+ run [
50
+ "wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-#{version}-#{patch}.tar.gz --quiet",
51
+ "tar -xzf ruby-#{version}-#{patch}.tar.gz",
52
+ "cd ruby-#{version}-#{patch}/",
53
+ "./configure --prefix=/usr/local 1>/dev/null",
54
+ "make 1>/dev/null 2>&1"
55
+ ].join( '&&' )
56
+ run [
57
+ "cd ruby-#{version}-#{patch}/",
58
+ "#{sudo} checkinstall --default --pkgname=ruby-#{version}#{patch} --pkgversion=#{version}#{patch} --nodoc 1>/dev/null 2>&1"
59
+ ].join( '&&' )
35
60
  run "#{sudo} gem install bundler --no-ri --no-rdoc"
36
- run "#{sudo} gem install chef ruby-shadow --no-ri --no-rdoc"
61
+ run "#{sudo} gem install chef ruby-shadow right_aws --no-ri --no-rdoc"
37
62
  run "#{sudo} gem install server_maint --no-ri --no-rdoc"
38
63
  end
39
64
  end
40
65
  end
66
+
67
+ desc "Make shared keys and sudo work together"
68
+ task :pam_ssh_agent_auth do
69
+ version = (defined? pam_ssh_agent_auth_version) ? pam_ssh_agent_auth : '0.9.4'
70
+ with_user "#{sudo_user}" do
71
+ if capture( "dpkg --list pam-ssh-agent-auth-#{version} 2>&1 | grep 'No packages found' | wc -l" ).to_i == 1
72
+ run [
73
+ "wget http://downloads.sourceforge.net/project/pamsshagentauth/pam_ssh_agent_auth/v#{version}/pam_ssh_agent_auth-#{version}.tar.bz2 --quiet",
74
+ "tar -xjf pam_ssh_agent_auth-#{version}.tar.bz2",
75
+ "cd pam_ssh_agent_auth-#{version}",
76
+ "./configure --libexecdir=/lib/security --with-mantype=man 1>/dev/null",
77
+ "make 1>/dev/null 2>&1"
78
+ ].join( '&&' )
79
+ run [
80
+ "cd pam_ssh_agent_auth-#{version}",
81
+ "#{sudo} checkinstall --default --pkgname=pam_ssh_agent_auth-#{version} --nodoc 1>/dev/null 2>&1"
82
+ ].join( '&&' )
83
+ pam_config = [
84
+ '#%PAM-1.0',
85
+ 'auth sufficient pam_ssh_agent_auth.so file=%h/.ssh/authorized_keys',
86
+ '@include common-account',
87
+ 'session required pam_permit.so',
88
+ 'session required pam_limits.so'
89
+ ].join( "\n" )
90
+ put(
91
+ pam_config,
92
+ "/tmp/pam_config"
93
+ )
94
+ run "#{sudo} mv /tmp/pam_config /etc/pam.d/sudo"
95
+ end
96
+ end
97
+ end
98
+
41
99
  before "deploy:setup", "base:setup"
42
100
  end
43
101
  end
data/lib/recipes/chef.rb CHANGED
@@ -4,29 +4,36 @@ Capistrano::Configuration.instance.load do
4
4
  namespace :chef do
5
5
  desc "Manages all system dependencies for this application"
6
6
  task :manages, roles: :web do
7
- run "mkdir -p /home/#{user}/apps/#{application}/shared/log"
8
- chef_cookbook_path =
7
+ run "mkdir -p /home/#{user}/chef/#{application}/log"
8
+ chef_cookbook_path =
9
9
  capture( %q{ruby -e 'require "server_maint"; puts ServerMaint::get_cookbook_path'} ).chomp
10
- app_cookbook_path = "/home/#{user}/apps/#{application}/shared/cookbooks"
10
+ app_cookbook_path = "/home/#{user}/chef/#{application}/cookbooks"
11
11
  chef_config = [
12
12
  %Q(cookbook_path ["#{chef_cookbook_path}", File.expand_path("../cookbooks", __FILE__)]),
13
13
  %Q(json_attribs File.expand_path("../node.json", __FILE__))
14
14
  ].join( "\n" )
15
15
  put(
16
16
  chef_config,
17
- "/home/#{user}/apps/#{application}/shared/solo.rb"
17
+ "/home/#{user}/chef/#{application}/solo.rb"
18
18
  )
19
- node_config = { "run_list" => ["recipe[main]"] }.to_json
20
- put node_config, "/home/#{user}/apps/#{application}/shared/node.json"
19
+ node_config = {
20
+ "user" => "#{user}",
21
+ "application" => "#{application}",
22
+ "server_name" => "#{host_name}",
23
+ "run_list" => ["recipe[main]"]
24
+ }
25
+ node_config.update( app_node_config ) if defined? app_node_config
26
+ node_config.update( stage_node_config ) if defined? stage_node_config
27
+ put node_config.to_json, "/home/#{user}/chef/#{application}/node.json"
21
28
  upload(
22
29
  "#{cookbooks}",
23
- "/home/#{user}/apps/#{application}/shared/cookbooks",
30
+ "/home/#{user}/chef/#{application}",
24
31
  {:recursive => true, :via => :scp}
25
32
  )
26
33
  run [
27
- "chef-solo",
28
- "-c /home/#{user}/apps/#{application}/shared/solo.rb",
29
- "-L /home/#{user}/apps/#{application}/shared/log/chef.log",
34
+ "#{sudo} chef-solo",
35
+ "-c /home/#{user}/chef/#{application}/solo.rb",
36
+ "-L /home/#{user}/chef/#{application}/log/chef.log",
30
37
  "1>/dev/null"
31
38
  ].join( ' ' )
32
39
  end
data/lib/recipes/git.rb CHANGED
@@ -4,8 +4,12 @@ Capistrano::Configuration.instance.load do
4
4
  task :setup, roles: :web do
5
5
  run "mkdir -p /home/deployer/repos/#{application}.git"
6
6
  run "cd /home/deployer/repos/#{application}.git && git --bare init"
7
- run_locally "git remote add #{remote} ssh://#{user}@#{host_name}/home/deployer/repos/#{application}.git"
7
+ run_locally "git init"
8
+ if `git config --get remote.#{remote}.url` == ''
9
+ run_locally "git remote add #{remote} ssh://#{user}@#{host_name}/home/deployer/repos/#{application}.git"
10
+ end
8
11
  end
12
+
9
13
  after "deploy:setup", "git:setup"
10
14
  end
11
15
  end
@@ -0,0 +1,8 @@
1
+ working_directory "<%= current_path %>"
2
+ pid "<%= shared_path %>/pids/unicorn.pid"
3
+ stderr_path "<%= shared_path %>/log/unicorn.log"
4
+ stdout_path "<%= shared_path %>/log/unicorn.log"
5
+
6
+ listen "/tmp/unicorn.<%= application %>.sock"
7
+ worker_processes <%= unicorn_workers %>
8
+ timeout 30
@@ -0,0 +1,84 @@
1
+ #!/bin/sh
2
+ ### BEGIN INIT INFO
3
+ # Provides: unicorn
4
+ # Required-Start: $remote_fs $syslog
5
+ # Required-Stop: $remote_fs $syslog
6
+ # Default-Start: 2 3 4 5
7
+ # Default-Stop: 0 1 6
8
+ # Short-Description: Manage unicorn server
9
+ # Description: Start, stop, restart unicorn server for a specific application.
10
+ ### END INIT INFO
11
+ set -e
12
+
13
+ # Feel free to change any of the following variables for your app:
14
+ TIMEOUT=${TIMEOUT-60}
15
+ APP_ROOT=<%= current_path %>
16
+ PID=<%= shared_path %>/pids/unicorn.pid
17
+ CMD="cd <%= current_path %>; bundle exec unicorn -D -c <%= shared_path %>/config/unicorn.rb -E production"
18
+ AS_USER=<%= user %>
19
+ set -u
20
+
21
+ OLD_PIN="$PID.oldbin"
22
+
23
+ sig () {
24
+ test -s "$PID" && kill -$1 `cat $PID`
25
+ }
26
+
27
+ oldsig () {
28
+ test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
29
+ }
30
+
31
+ run () {
32
+ if [ "$(id -un)" = "$AS_USER" ]; then
33
+ eval $1
34
+ else
35
+ su -c "$1" - $AS_USER
36
+ fi
37
+ }
38
+
39
+ case "$1" in
40
+ start)
41
+ sig 0 && echo >&2 "Already running" && exit 0
42
+ run "$CMD"
43
+ ;;
44
+ stop)
45
+ sig QUIT && exit 0
46
+ echo >&2 "Not running"
47
+ ;;
48
+ force-stop)
49
+ sig TERM && exit 0
50
+ echo >&2 "Not running"
51
+ ;;
52
+ restart|reload)
53
+ sig HUP && echo reloaded OK && exit 0
54
+ echo >&2 "Couldn't reload, starting '$CMD' instead"
55
+ run "$CMD"
56
+ ;;
57
+ upgrade)
58
+ if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
59
+ then
60
+ n=$TIMEOUT
61
+ while test -s $OLD_PIN && test $n -ge 0
62
+ do
63
+ printf '.' && sleep 1 && n=$(( $n - 1 ))
64
+ done
65
+ echo
66
+
67
+ if test $n -lt 0 && test -s $OLD_PIN
68
+ then
69
+ echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
70
+ exit 1
71
+ fi
72
+ exit 0
73
+ fi
74
+ echo >&2 "Couldn't upgrade, starting '$CMD' instead"
75
+ run "$CMD"
76
+ ;;
77
+ reopen-logs)
78
+ sig USR1
79
+ ;;
80
+ *)
81
+ echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
82
+ exit 1
83
+ ;;
84
+ esac
@@ -0,0 +1,22 @@
1
+ Capistrano::Configuration.instance.load do
2
+ namespace :unicorn do
3
+ desc "Setup Unicorn initializer and app configuration"
4
+ task :setup, roles: :app do
5
+ run "mkdir -p #{shared_path}/config"
6
+ template "unicorn.rb.erb", "#{shared_path}/config/unicorn.rb"
7
+ template "unicorn_init.erb", "/tmp/unicorn_init"
8
+ run "chmod +x /tmp/unicorn_init"
9
+ run "#{sudo} mv /tmp/unicorn_init /etc/init.d/unicorn_#{application}"
10
+ run "#{sudo} update-rc.d -f unicorn_#{application} defaults 1>/dev/null"
11
+ end
12
+ after "deploy:setup", "unicorn:setup"
13
+
14
+ %w[start stop restart].each do |command|
15
+ desc "#{command} unicorn"
16
+ task command, roles: :app do
17
+ run "service unicorn_#{application} #{command}"
18
+ end
19
+ after "deploy:#{command}", "unicorn:#{command}"
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_maint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-23 00:00:00.000000000 Z
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -89,9 +89,13 @@ files:
89
89
  - app_maint.gemspec
90
90
  - lib/app_maint.rb
91
91
  - lib/app_maint/version.rb
92
+ - lib/recipes/_fix_permissons.rb
92
93
  - lib/recipes/base.rb
93
94
  - lib/recipes/chef.rb
94
95
  - lib/recipes/git.rb
96
+ - lib/recipes/templates/unicorn.rb.erb
97
+ - lib/recipes/templates/unicorn_init.erb
98
+ - lib/recipes/unicorn.rb
95
99
  homepage: http://github.com/fdoebbelin/app_maint
96
100
  licenses: []
97
101
  post_install_message:
@@ -106,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
110
  version: '0'
107
111
  segments:
108
112
  - 0
109
- hash: 202345415463774109
113
+ hash: -3945106699859961047
110
114
  required_rubygems_version: !ruby/object:Gem::Requirement
111
115
  none: false
112
116
  requirements:
@@ -115,10 +119,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
119
  version: '0'
116
120
  segments:
117
121
  - 0
118
- hash: 202345415463774109
122
+ hash: -3945106699859961047
119
123
  requirements: []
120
124
  rubyforge_project:
121
- rubygems_version: 1.8.23
125
+ rubygems_version: 1.8.24
122
126
  signing_key:
123
127
  specification_version: 3
124
128
  summary: Application maintenance with capistrano