Class: SaltedRails::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/salted-rails/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rails_root, machine = 'default') ⇒ Config

pass vm.ui for the logger if you want debugging info



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/salted-rails/config.rb', line 39

def initialize(rails_root, machine = 'default')
  @logger = Log4r::Logger.new("vagrant::salted-rails")
  @machine = machine
  @rails_root = rails_root
  @rails_root += '/' unless @rails_root =~ /\/$/
  @salt_root = nil
  # see salt/vagrant/top.sls for other roles
  @roles = %w{ app web db }
  @domain = nil
  @private_key_path = nil
  @mirror = nil
  @memory = nil
  @ports = [ 80, 443, 880, 3000 ]
  @disable_vagrant_sync = true
  @ca_path = nil
  @region = nil
  @forward_agent = true
  @files = [ '.ruby-version', '.java-version', '.php-version', '.rvmrc', 'config/database.yml', 'Gemfile', 'Gemfile.lock' ].select{ |f| File.exist?(@rails_root + f) }
  @copy_from_home = [ ]

  ENV['REMOTE_MACHINE'] = 'true'
  database_file = @rails_root + 'config/database.yml'
  @databases = YAML.load(ERB.new(IO.read(database_file)).result) rescue { }
  ENV['REMOTE_MACHINE'] = nil

  @gems = { }
  if File.exists? @rails_root + 'Gemfile'
    File.foreach(@rails_root + 'Gemfile') do |line|
      if line =~ /^\s*gem\s*['"]([^'"]+)['"][,\s]*(['"]([^'"]+)['"])?/
        gem = $1
        version = $3.to_s
        version = true if version == '' or version !~ /\d/
        @gems[gem] = version
      end
    end
  end
  @databases.each do |key, details|
    @gems[details['adapter']] = true
  end

  @ruby_version = nil
  @java_version = nil
  @php_version = nil

  @machines = [ ]
  @hostname = nil
end

Instance Attribute Details

#ca_pathObject

Returns the value of attribute ca_path.



21
22
23
# File 'lib/salted-rails/config.rb', line 21

def ca_path
  @ca_path
end

#copy_from_homeObject

Returns the value of attribute copy_from_home.



32
33
34
# File 'lib/salted-rails/config.rb', line 32

def copy_from_home
  @copy_from_home
end

#databasesObject

Returns the value of attribute databases.



25
26
27
# File 'lib/salted-rails/config.rb', line 25

def databases
  @databases
end

#disable_vagrant_syncObject

Returns the value of attribute disable_vagrant_sync.



20
21
22
# File 'lib/salted-rails/config.rb', line 20

def disable_vagrant_sync
  @disable_vagrant_sync
end

#domainObject

Returns the value of attribute domain.



13
14
15
# File 'lib/salted-rails/config.rb', line 13

def domain
  @domain
end

#filesObject

Returns the value of attribute files.



24
25
26
# File 'lib/salted-rails/config.rb', line 24

def files
  @files
end

#forward_agentObject

Returns the value of attribute forward_agent.



23
24
25
# File 'lib/salted-rails/config.rb', line 23

def forward_agent
  @forward_agent
end

#gemsObject

Returns the value of attribute gems.



27
28
29
# File 'lib/salted-rails/config.rb', line 27

def gems
  @gems
end

#hostnameObject

Returns the value of attribute hostname.



12
13
14
# File 'lib/salted-rails/config.rb', line 12

def hostname
  @hostname
end

#java_versionObject

Returns the value of attribute java_version.



29
30
31
# File 'lib/salted-rails/config.rb', line 29

def java_version
  @java_version
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/salted-rails/config.rb', line 17

def logger
  @logger
end

#machineObject

Returns the value of attribute machine.



11
12
13
# File 'lib/salted-rails/config.rb', line 11

def machine
  @machine
end

#machinesObject

Returns the value of attribute machines.



31
32
33
# File 'lib/salted-rails/config.rb', line 31

def machines
  @machines
end

#memoryObject

Returns the value of attribute memory.



18
19
20
# File 'lib/salted-rails/config.rb', line 18

def memory
  @memory
end

#mirrorObject

Returns the value of attribute mirror.



16
17
18
# File 'lib/salted-rails/config.rb', line 16

def mirror
  @mirror
end

#php_versionObject

Returns the value of attribute php_version.



30
31
32
# File 'lib/salted-rails/config.rb', line 30

def php_version
  @php_version
end

#portsObject

Returns the value of attribute ports.



19
20
21
# File 'lib/salted-rails/config.rb', line 19

def ports
  @ports
end

#private_key_pathObject

Returns the value of attribute private_key_path.



10
11
12
# File 'lib/salted-rails/config.rb', line 10

def private_key_path
  @private_key_path
end

#rails_rootObject

Returns the value of attribute rails_root.



14
15
16
# File 'lib/salted-rails/config.rb', line 14

def rails_root
  @rails_root
end

#regionObject

Returns the value of attribute region.



22
23
24
# File 'lib/salted-rails/config.rb', line 22

def region
  @region
end

#rolesObject

Returns the value of attribute roles.



26
27
28
# File 'lib/salted-rails/config.rb', line 26

def roles
  @roles
end

#ruby_versionObject

Returns the value of attribute ruby_version.



28
29
30
# File 'lib/salted-rails/config.rb', line 28

def ruby_version
  @ruby_version
end

#salt_rootObject

Returns the value of attribute salt_root.



15
16
17
# File 'lib/salted-rails/config.rb', line 15

def salt_root
  @salt_root
end

Instance Method Details

#cloneObject

Clone



168
169
170
171
172
173
174
175
176
177
# File 'lib/salted-rails/config.rb', line 168

def clone
  obj = self.dup
  obj.roles = @roles.dup
  obj.ports = @ports.dup
  obj.files = @files.dup
  obj.copy_from_home = @copy_from_home.dup
  obj.databases = @databases.dup
  obj.gems = @gems.dup
  obj
end

#configure_vagrant(config) ⇒ Object



204
205
206
207
208
209
# File 'lib/salted-rails/config.rb', line 204

def configure_vagrant(config)
  require 'salted-rails/vagrant_helper'
  normalize
  helper = SaltedRails::VagrantHelper.new(self)
  helper.configure_vagrant(config)
end

#define(machine) {|obj| ... } ⇒ Object

Yields:

  • (obj)


158
159
160
161
162
163
164
165
# File 'lib/salted-rails/config.rb', line 158

def define(machine, &block)
  obj = self.clone
  obj.machine = machine
  obj.machines = [ ]
  obj.logger = @logger
  @machines << obj
  yield(obj) if block_given?
end

#gui?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/salted-rails/config.rb', line 87

def gui?
  @roles.include? 'gui'
end

#normalizeObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/salted-rails/config.rb', line 91

def normalize
  unless @memory
    @memory = 512
    {
            'gui' => 1536,
            'teamcity' => 1536,
            'cruisecontrolrb' => 512
    }.each do |role, extra|
      @memory += extra if @roles.include?(role)
    end
  end
  unless @domain
    if @hostname
      @domain = @hostname.sub(/^[^.]*\.?/, '')
    else
      @domain = sanitize_dns_name(File.basename(File.expand_path(@rails_root).sub(/\/$/, '').sub(/\/(app|site|web|www|website)\d*$/, ''))) + '.test'
      @domain = 'railsapp.test' if @domain == '.test'
    end
  end
  if @hostname.nil? or @hostname == ''
    if @machine == 'default'
      @hostname = @domain
    else
      @hostname = sanitize_dns_name(@machine.to_s)
      if @hostname == ''
        @hostname = @domain
      else
        @hostname <<= '.' + @domain
      end
    end
  end
  @ruby_version ||= File.open(@rails_root + '.ruby-version', 'r') do |f_in|
    f_in.gets.gsub(/\s/,'')
  end rescue nil
  @ruby_version ||= File.open(@rails_root + '.rvmrc', 'r') do |f_in|
    while !ruby_version && (line = f_in.gets) 
      ruby_version = $1 if line =~ /^\s*environment_id=['"]([^"'@]+)/
    end
  end rescue nil
  @ruby_version ||= '1.9.3'

  @java_version ||= File.open(@rails_root + '.java-version', 'r') do |f_in|
    f_in.gets.gsub(/\s/,'')
  end rescue nil

  @php_version ||= File.open(@rails_root + '.php-version', 'r') do |f_in|
    f_in.gets.gsub(/\s/,'')
  end rescue nil

  @private_key_path ||= '~/.ssh/id_rsa'
  @mirror ||= 'auto'
  @salt_root ||= File.dirname(__FILE__) + '/../../'
  @ca_path ||= '/etc/ssl/certs/ca-certificates.crt'
  @ca_path = nil unless File.exist?(@ca_path)
  @region ||= 'San Francisco 1'

  @machines.each {|m| m.normalize}

  {
          'teamcity' => 8111,
          'cruisecontrolrb' => 3333
  }.each do |role, port|
    @ports << port if @roles.include?(role) and not @ports.include?(port)
  end

end

#sanitize_dns_name(name) ⇒ Object



34
35
36
# File 'lib/salted-rails/config.rb', line 34

def sanitize_dns_name(name)
  dns_name = name.downcase.gsub(/[^-0-9a-z]+/,'-').sub(/^-+/, '').sub(/-+$/, '')
end

#to_hashObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/salted-rails/config.rb', line 179

def to_hash
  {
    'domain' => @domain,
    'mirror' => @mirror,
    'machine' => @machine,
    'hostname' => @hostname,
    'memory' => @memory,
    'disable_vagrant_sync' => @disable_vagrant_sync,
    'region' => @region,
    'forward_agent' => @forward_agent,
    'files' => @files,
    'databases' => @databases,
    'gems' => @gems,
    'ruby_version' => @ruby_version,
    'java_version' => @java_version,
    'php_version' => @php_version,
    'roles' => @roles
  }
end

#to_yamlObject



199
200
201
202
# File 'lib/salted-rails/config.rb', line 199

def to_yaml
  normalize
  self.to_hash.to_yaml
end