capricorn 0.2.05 → 0.2.06

Sign up to get free protection for your applications and to get access to all the features.
data/lib/capricorn.rb CHANGED
@@ -30,6 +30,7 @@ module Capricorn
30
30
  autoload :PleskActor, (base+'/capricorn/actors/plesk_actor')
31
31
  autoload :ApacheActor, (base+'/capricorn/actors/apache_actor')
32
32
  autoload :Sqlite3Actor, (base+'/capricorn/actors/sqlite3_actor')
33
+ autoload :HostFileActor, (base+'/capricorn/actors/host_file_actor')
33
34
  autoload :PassengerActor, (base+'/capricorn/actors/passenger_actor')
34
35
  end
35
36
 
@@ -0,0 +1,75 @@
1
+
2
+ module Capricorn
3
+ module Actors # :nodoc:
4
+ class HostFileActor < Capricorn::Actor
5
+
6
+ on_install_satellite :add_host_name
7
+ on_uninstall_satellite :remove_host_name
8
+
9
+ # addthe fost form the host file
10
+ def add_host_name
11
+ content = load_file
12
+ content[:capricorn]['127.0.0.1'].push satellite.domain
13
+ dump_file(content)
14
+ end
15
+
16
+ # remove the fost form the host file
17
+ def remove_host_name
18
+ content = load_file
19
+ content[:capricorn]['127.0.0.1'].delete satellite.domain
20
+ dump_file(content)
21
+ end
22
+
23
+ SEPERATOR = '### the content below this line is managed by capricorn'
24
+
25
+ private
26
+
27
+ def load_file
28
+ content = File.read(system.host_file_path)
29
+ content_parts = content.split(Capricorn::Actors::HostFileActor::SEPERATOR)
30
+
31
+ user_hosts = content_parts[0] || ''
32
+ host_lines = content_parts[1] || ''
33
+ host_lines = host_lines.split(/\n/)
34
+ host_lines = host_lines.inject({}) do |m, host_line|
35
+ next if host_line.nil?
36
+ host_line = host_line.strip
37
+ next if host_line[0,1] == '#'
38
+ next if host_line == ''
39
+
40
+ host_line = host_line.split(/\s+/)
41
+ ip = host_line.shift
42
+ m[ip] ||= []
43
+ m[ip].concat(host_line)
44
+
45
+ m
46
+ end
47
+
48
+ {
49
+ :user => user_hosts,
50
+ :capricorn => host_lines
51
+ }
52
+ end
53
+
54
+ def dump_file(content)
55
+ File.open(system.host_file_path, 'w+') do |f|
56
+ f.puts content[:user].strip
57
+ f.puts Capricorn::Actors::HostFileActor::SEPERATOR
58
+ content[:capricorn].each do |ip, hosts|
59
+ f.puts "#{ip} #{hosts.join(' ')}"
60
+ end
61
+ end
62
+ end
63
+
64
+ module Config
65
+
66
+ # path to the hosts file.
67
+ def host_file_path(&block)
68
+ option(:host_file_path, block) { |v| v or '/etc/hosts' }
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+ end
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capricorn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.05
4
+ version: 0.2.06
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Menke
@@ -31,6 +31,7 @@ files:
31
31
  - lib/capricorn/actor.rb
32
32
  - lib/capricorn/actors/apache_actor.rb
33
33
  - lib/capricorn/actors/base_actor.rb
34
+ - lib/capricorn/actors/host_file_actor.rb
34
35
  - lib/capricorn/actors/mysql_actor.rb
35
36
  - lib/capricorn/actors/passenger_actor.rb
36
37
  - lib/capricorn/actors/plesk_actor.rb