capricorn 0.2.00

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/app_generators/engine/engine_generator.rb +39 -0
  2. data/app_generators/engine/templates/Gmfile +20 -0
  3. data/app_generators/engine/templates/MIT-LICENSE.txt +20 -0
  4. data/app_generators/engine/templates/README.rdoc +7 -0
  5. data/app_generators/engine/templates/config/routes.rb +2 -0
  6. data/app_generators/engine/templates/gitignore +3 -0
  7. data/app_generators/engine/templates/init.rb +1 -0
  8. data/app_generators/engine/templates/lib/engine.rb +1 -0
  9. data/app_generators/engine/templates/rails/init.rb +1 -0
  10. data/app_generators/engine/templates/tasks/engine_tasks.rake +4 -0
  11. data/bin/capricorn +20 -0
  12. data/lib/capricorn.rb +100 -0
  13. data/lib/capricorn/actor.rb +23 -0
  14. data/lib/capricorn/actor/actions.rb +76 -0
  15. data/lib/capricorn/actors/apache_actor.rb +56 -0
  16. data/lib/capricorn/actors/base_actor.rb +276 -0
  17. data/lib/capricorn/actors/mysql_actor.rb +20 -0
  18. data/lib/capricorn/actors/passenger_actor.rb +19 -0
  19. data/lib/capricorn/actors/plesk_actor.rb +210 -0
  20. data/lib/capricorn/actors/sqlite3_actor.rb +44 -0
  21. data/lib/capricorn/app_runner.rb +119 -0
  22. data/lib/capricorn/apps/dev.rb +15 -0
  23. data/lib/capricorn/apps/engines.rb +33 -0
  24. data/lib/capricorn/apps/jobs.rb +35 -0
  25. data/lib/capricorn/apps/satellite.rb +32 -0
  26. data/lib/capricorn/apps/server.rb +67 -0
  27. data/lib/capricorn/client.rb +48 -0
  28. data/lib/capricorn/client/auth_token.rb +98 -0
  29. data/lib/capricorn/daemon.rb +71 -0
  30. data/lib/capricorn/exception_handler.rb +79 -0
  31. data/lib/capricorn/extentions/rubygems_plugin.rb +27 -0
  32. data/lib/capricorn/extentions/thor_extentions.rb +32 -0
  33. data/lib/capricorn/job_queue.rb +199 -0
  34. data/lib/capricorn/satellite.rb +50 -0
  35. data/lib/capricorn/satellite/actions.rb +35 -0
  36. data/lib/capricorn/satellite/dependency_loader.rb +78 -0
  37. data/lib/capricorn/satellite/persistence.rb +50 -0
  38. data/lib/capricorn/server.rb +122 -0
  39. data/lib/capricorn/server/daemon.rb +88 -0
  40. data/lib/capricorn/server/proxy.rb +25 -0
  41. data/lib/capricorn/server/security.rb +113 -0
  42. data/lib/capricorn/system.rb +184 -0
  43. data/lib/capricorn/system/config.rb +49 -0
  44. data/lib/capricorn/system/helper.rb +21 -0
  45. data/lib/capricorn/system/options.rb +79 -0
  46. data/lib/capricorn/system/process_user.rb +73 -0
  47. data/lib/capricorn/system/satellites.rb +44 -0
  48. data/lib/capricorn/system/shell.rb +80 -0
  49. data/lib/rubygems_plugin.rb +1 -0
  50. data/spec/actor/actions_spec.rb +13 -0
  51. data/spec/spec_helper.rb +1 -0
  52. metadata +108 -0
@@ -0,0 +1,49 @@
1
+
2
+ module Capricorn
3
+ class System
4
+ module Config
5
+
6
+ def use_development!
7
+ environment { 'development' }
8
+ end
9
+
10
+ def development?
11
+ environment == 'development'
12
+ end
13
+
14
+ def use_production!
15
+ environment { 'production' }
16
+ end
17
+
18
+ def production?
19
+ environment == 'production'
20
+ end
21
+
22
+ def environment(&block)
23
+ option(:environment, block) { |s,v| v or 'production' }
24
+ end
25
+
26
+ def use_ssl!
27
+ option(:use_ssl, lambda { true })
28
+ end
29
+
30
+ def use_ssl?
31
+ option(:use_ssl, nil)
32
+ end
33
+
34
+ def bind(hostname=nil, port=nil)
35
+ server_hostname { hostname }
36
+ server_port { port }
37
+ end
38
+
39
+ def server_hostname(&block)
40
+ option(:server_hostname, block) { |v| v or 'localhost' }
41
+ end
42
+
43
+ def server_port(&block)
44
+ option(:server_port, block) { |v| v or 5000 }
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+
2
+ module Capricorn
3
+ class System
4
+ module Helper
5
+
6
+ def use(actor)
7
+ actor_klass = (Capricorn::Actors.const_get(actor) rescue nil)
8
+ raise "Actor not found! (#{actor})" unless actor_klass
9
+
10
+ actor_helper = (actor_klass.const_get('Helper') rescue nil)
11
+ extend actor_helper if actor_helper
12
+
13
+ actor_config = (actor_klass.const_get('Config') rescue nil)
14
+ extend actor_config if actor_config
15
+
16
+ @actors.push(actor_klass)
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,79 @@
1
+
2
+ module Capricorn
3
+ class System
4
+ module Options
5
+
6
+ def option(name, proc, &handler)
7
+ if proc
8
+ @option_descriptors.push({
9
+ :name => name.to_sym,
10
+ :proc => proc,
11
+ :handler => handler
12
+ })
13
+ else
14
+ if handler && !@options.key?(name.to_sym)
15
+ @options[name.to_sym] = handler.call(nil)
16
+ end
17
+ @options[name.to_sym]
18
+ end
19
+ end
20
+
21
+ def satellite_option(name, proc, &handler)
22
+ if proc
23
+ @satellite_option_descriptors.push({
24
+ :name => name.to_sym,
25
+ :proc => proc,
26
+ :handler => handler
27
+ })
28
+ else
29
+ if handler && !@satellite_options.key?(name.to_sym)
30
+ @satellite_options[name.to_sym] = handler.call(@current_satellite, nil)
31
+ end
32
+ @satellite_options[name.to_sym]
33
+ end
34
+ end
35
+
36
+ def set_satellite_option(name, value)
37
+ @satellite_options[name.to_sym] = value
38
+ end
39
+
40
+ def resolve_options_with(satellite)
41
+ @current_satellite = satellite
42
+ resolve_options!
43
+
44
+ result = yield
45
+
46
+ @current_satellite = nil
47
+ resolve_options!
48
+
49
+ result
50
+ end
51
+
52
+ def resolve_options!
53
+ resolve_options_for_system!
54
+ resolve_options_for_satellite!
55
+ end
56
+
57
+ def resolve_options_for_system!
58
+ @options = {}
59
+ @option_descriptors.each do |option|
60
+ value = option[:proc].call if option[:proc]
61
+ value = option[:handler].call(value) if option[:handler]
62
+ @options[option[:name]] = value
63
+ end
64
+ end
65
+
66
+ def resolve_options_for_satellite!
67
+ @satellite_options = {}
68
+ if @current_satellite
69
+ @satellite_option_descriptors.each do |option|
70
+ value = option[:proc].call(@current_satellite) if option[:proc]
71
+ value = option[:handler].call(@current_satellite, value) if option[:handler]
72
+ @satellite_options[option[:name]] = value
73
+ end
74
+ end
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,73 @@
1
+
2
+ module Capricorn
3
+ class System
4
+ module ProcessUser
5
+
6
+ # get the uid for a user name
7
+ def get_uid(username)
8
+ return username if Numeric === username
9
+ Etc.getpwnam(username).uid
10
+ end
11
+
12
+ # get the gid for a group name
13
+ def get_gid(groupname)
14
+ return groupname if Numeric === groupname
15
+ Etc.getgrnam(groupname).gid
16
+ end
17
+
18
+ # get the user name for a uid (or the current process user)
19
+ def get_user_name(uid=Process.uid)
20
+ return uid if String === uid
21
+ Etc.getpwuid(uid).name
22
+ end
23
+
24
+ # get the group name for a gid (or the current process group)
25
+ def get_group_name(gid=Process.gid)
26
+ return gid if String === gid
27
+ Etc.getgrgid(gid).name
28
+ end
29
+
30
+ # is this process running as this user?
31
+ def is_user(username)
32
+ uid = get_uid(username)
33
+ Process.euid == uid and Process.uid == uid
34
+ end
35
+
36
+ # is this process running as this group?
37
+ def is_group(groupname)
38
+ gid = get_gid(groupname)
39
+ Process.egid == gid and Process.gid == gid
40
+ end
41
+
42
+ # switch this user to the specified user and group
43
+ def switch_to_user(username, groupname=nil)
44
+ different_uid = (Process.euid != get_uid(username))
45
+ different_gid = (Process.egid != get_gid(groupname)) if groupname
46
+
47
+ if groupname and different_gid
48
+ Process.gid = Process.egid = get_gid(groupname)
49
+ end
50
+
51
+ if different_uid
52
+ Process.uid = Process.euid = get_uid(username)
53
+ end
54
+ end
55
+
56
+ # run the passed block as the specified user and group
57
+ def as_user(username, groupname=nil, &block)
58
+ euid = Process.euid
59
+ egid = Process.egid
60
+
61
+ value = nil
62
+ begin
63
+ switch_to_user(username, groupname)
64
+ value = block.call
65
+ ensure
66
+ switch_to_user(euid, egid)
67
+ end
68
+ value
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,44 @@
1
+
2
+ module Capricorn
3
+ class System
4
+ module Satellites
5
+
6
+ def satellites_hash
7
+ @satellites || load_satellites
8
+ end
9
+
10
+ def satellites
11
+ satellites_hash.values
12
+ end
13
+
14
+ def find_satellite(domain)
15
+ satellites_hash[domain]
16
+ end
17
+
18
+ def save_satellite!(satellite)
19
+ satellites_hash[satellite.domain] = satellite
20
+ satellite.dump_file(self.path('satellites', "#{satellite.domain}.yml"))
21
+ end
22
+
23
+ def destroy_satellite!(satellite)
24
+ satellites_hash.delete(satellite.domain)
25
+ FileUtils.rm_f(self.path('satellites', "#{satellite.domain}.yml"))
26
+ end
27
+
28
+ private
29
+
30
+ def load_satellites
31
+ @satellites = {}
32
+
33
+ FileUtils.mkdir_p(self.path('satellites'))
34
+ Dir.glob(self.path('satellites', '*.yml')).each do |yml|
35
+ satellite = Capricorn::Satellite.load_file(yml)
36
+ @satellites[satellite.domain] = satellite
37
+ end
38
+
39
+ @satellites
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,80 @@
1
+
2
+ module Capricorn
3
+ class System
4
+ module Shell
5
+
6
+ def run(cmd)
7
+ as_user 'root', 'wheel' do
8
+ Capricorn.log "[#{get_user_name}]> "+cmd
9
+ cmd = "bash -l -c #{cmd.inspect}"
10
+ output = %x[#{cmd} 2>&1]
11
+ Capricorn.log output if development?
12
+ output
13
+ end
14
+ end
15
+
16
+ def user_run(username, cmd)
17
+ as_user 'root', 'wheel' do
18
+ Capricorn.log "[#{username}]> "+cmd
19
+ cmd = "su #{username} -l -c #{cmd.inspect}"
20
+ output = %x[#{cmd} 2>&1]
21
+ Capricorn.log output if development?
22
+ output
23
+ end
24
+ end
25
+
26
+ def popen(cmd, *args, &block)
27
+ as_user 'root', 'wheel' do
28
+ Capricorn.log cmd
29
+ cmd = "bash -l -c #{cmd.inspect}"
30
+ IO.popen(cmd, *args, &block)
31
+ end
32
+ end
33
+
34
+ def user_popen(username, cmd, *args, &block)
35
+ as_user 'root', 'wheel' do
36
+ Capricorn.log cmd
37
+ cmd = "su #{username} -l -c #{cmd.inspect}"
38
+ IO.popen(cmd, *args, &block)
39
+ end
40
+ end
41
+
42
+ def find_bin(*names)
43
+ names = names.flatten.compact.uniq
44
+ unless names.empty?
45
+ names = names.join(' ')
46
+ popen("which #{names}", 'r') do |f|
47
+
48
+ until f.eof?
49
+ path = f.readline
50
+ next unless path
51
+ path = path.strip
52
+ next if path =~ /(Last login)|(You have new)|(Using ruby)/
53
+ return path
54
+ end
55
+
56
+ end
57
+ end
58
+ end
59
+
60
+ def user_find_bin(username, *names)
61
+ names = names.flatten.compact.uniq
62
+ unless names.empty?
63
+ names = names.join(' ')
64
+ user_popen(username, "which #{names}", 'r') do |f|
65
+
66
+ until f.eof?
67
+ path = f.readline
68
+ next unless path
69
+ path = path.strip
70
+ next if path =~ /(Last login)|(You have new)|(Using ruby)/
71
+ return path
72
+ end
73
+
74
+ end
75
+ end
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1 @@
1
+ require 'capricorn/extentions/rubygems_plugin'
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe 'Actor::Actions' do
4
+
5
+ before do
6
+ @actor = Class.new('Actor')
7
+ end
8
+
9
+ it "should description" do
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__)+'/../lib/capricorn'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capricorn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.00
5
+ platform: ruby
6
+ authors:
7
+ - Simon Menke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-20 00:00:00 +02:00
13
+ default_executable: capricorn
14
+ dependencies: []
15
+
16
+ description: Manage satellites
17
+ email: simon.menke@gmail.com
18
+ engine_dependencies: {}
19
+
20
+ executables:
21
+ - capricorn
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - spec/actor/actions_spec.rb
28
+ - spec/spec_helper.rb
29
+ - bin/capricorn
30
+ - lib/capricorn/actor/actions.rb
31
+ - lib/capricorn/actor.rb
32
+ - lib/capricorn/actors/apache_actor.rb
33
+ - lib/capricorn/actors/base_actor.rb
34
+ - lib/capricorn/actors/mysql_actor.rb
35
+ - lib/capricorn/actors/passenger_actor.rb
36
+ - lib/capricorn/actors/plesk_actor.rb
37
+ - lib/capricorn/actors/sqlite3_actor.rb
38
+ - lib/capricorn/app_runner.rb
39
+ - lib/capricorn/apps/dev.rb
40
+ - lib/capricorn/apps/engines.rb
41
+ - lib/capricorn/apps/jobs.rb
42
+ - lib/capricorn/apps/satellite.rb
43
+ - lib/capricorn/apps/server.rb
44
+ - lib/capricorn/client/auth_token.rb
45
+ - lib/capricorn/client.rb
46
+ - lib/capricorn/daemon.rb
47
+ - lib/capricorn/exception_handler.rb
48
+ - lib/capricorn/extentions/rubygems_plugin.rb
49
+ - lib/capricorn/extentions/thor_extentions.rb
50
+ - lib/capricorn/job_queue.rb
51
+ - lib/capricorn/satellite/actions.rb
52
+ - lib/capricorn/satellite/dependency_loader.rb
53
+ - lib/capricorn/satellite/persistence.rb
54
+ - lib/capricorn/satellite.rb
55
+ - lib/capricorn/server/daemon.rb
56
+ - lib/capricorn/server/proxy.rb
57
+ - lib/capricorn/server/security.rb
58
+ - lib/capricorn/server.rb
59
+ - lib/capricorn/system/config.rb
60
+ - lib/capricorn/system/helper.rb
61
+ - lib/capricorn/system/options.rb
62
+ - lib/capricorn/system/process_user.rb
63
+ - lib/capricorn/system/satellites.rb
64
+ - lib/capricorn/system/shell.rb
65
+ - lib/capricorn/system.rb
66
+ - lib/capricorn.rb
67
+ - lib/rubygems_plugin.rb
68
+ - app_generators/engine/engine_generator.rb
69
+ - app_generators/engine/templates/Gmfile
70
+ - app_generators/engine/templates/MIT-LICENSE.txt
71
+ - app_generators/engine/templates/README.rdoc
72
+ - app_generators/engine/templates/config/routes.rb
73
+ - app_generators/engine/templates/gitignore
74
+ - app_generators/engine/templates/init.rb
75
+ - app_generators/engine/templates/lib/engine.rb
76
+ - app_generators/engine/templates/rails/init.rb
77
+ - app_generators/engine/templates/tasks/engine_tasks.rake
78
+ has_rdoc: true
79
+ homepage: http://github.com/simonmenke/capricorn
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ version:
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.5
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Manage satellites
106
+ test_files:
107
+ - spec/actor/actions_spec.rb
108
+ - spec/spec_helper.rb