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(project_root, machine = 'default') ⇒ Config

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



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
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/salted-rails/config.rb', line 42

def initialize(project_root, machine = 'default')
  @logger = Log4r::Logger.new("vagrant::salted-rails")
  @machine = machine
  @project_root = project_root
  @project_root += '/' unless @project_root =~ /\/$/
  @salt_root = nil
  # see salt/vagrant/top.sls for other roles
  @roles = %w{ app web db }
  @domain = nil
  @admin_password = nil
  @staging_password = nil
  @private_key_path = nil
  @mirror = nil
  @memory = nil

  # app in production mode: 80, 443
  # admin: 880
  # app in dev mode: 3000
  # gem server: 8808
  @ports = [ 80, 443, 880, 3000, 8808 ]
  @mapped_ports = { }
  @sync_vagrant = nil
  @box = nil
  @ca_path = nil
  @region = nil
  @forward_agent = true
  @files = [ '.ruby-version', '.java-version', '.php-version', 'config/database.yml', 'Gemfile', 'Gemfile.lock' ].select{ |f| File.exist?(@project_root + f) }
  @packages = nil
  @copy_from_home = [ ]

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

  @gems = { }
  if File.exists? @project_root + 'Gemfile'
    File.foreach(@project_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

  @roles << 'mysql' if @gems.include?('mysql') or @gems.include?('mysql2')

  @versions= { }

  @machines = [ ]
  @hostname = nil
end

Instance Attribute Details

#admin_passwordObject

Returns the value of attribute admin_password.



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

def admin_password
  @admin_password
end

#boxObject

Returns the value of attribute box.



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

def box
  @box
end

#ca_pathObject

Returns the value of attribute ca_path.



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

def ca_path
  @ca_path
end

#copy_from_homeObject

Returns the value of attribute copy_from_home.



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

def copy_from_home
  @copy_from_home
end

#databasesObject

Returns the value of attribute databases.



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

def databases
  @databases
end

#domainObject

Returns the value of attribute domain.



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

def domain
  @domain
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

#forward_agentObject

Returns the value of attribute forward_agent.



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

def forward_agent
  @forward_agent
end

#gemsObject

Returns the value of attribute gems.



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

def gems
  @gems
end

#hostnameObject

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#machineObject

Returns the value of attribute machine.



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

def machine
  @machine
end

#machinesObject

Returns the value of attribute machines.



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

def machines
  @machines
end

#mapped_portsObject

Returns the value of attribute mapped_ports.



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

def mapped_ports
  @mapped_ports
end

#memoryObject

Returns the value of attribute memory.



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

def memory
  @memory
end

#mirrorObject

Returns the value of attribute mirror.



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

def mirror
  @mirror
end

#packagesObject

Returns the value of attribute packages.



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

def packages
  @packages
end

#portsObject

Returns the value of attribute ports.



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

def ports
  @ports
end

#private_key_pathObject

Returns the value of attribute private_key_path.



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

def private_key_path
  @private_key_path
end

#project_rootObject

Returns the value of attribute project_root.



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

def project_root
  @project_root
end

#regionObject

Returns the value of attribute region.



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

def region
  @region
end

#rolesObject

Returns the value of attribute roles.



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

def roles
  @roles
end

#salt_rootObject

Returns the value of attribute salt_root.



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

def salt_root
  @salt_root
end

#staging_passwordObject

Returns the value of attribute staging_password.



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

def staging_password
  @staging_password
end

#sync_vagrantObject

Returns the value of attribute sync_vagrant.



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

def sync_vagrant
  @sync_vagrant
end

#versionsObject

Returns the value of attribute versions.



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

def versions
  @versions
end

Instance Method Details

#cloneObject

Clone



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/salted-rails/config.rb', line 122

def clone
  obj = self.dup
  obj.roles = @roles.dup
  obj.ports = @ports.dup
  obj.mapped_ports = @mapped_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



162
163
164
165
166
167
# File 'lib/salted-rails/config.rb', line 162

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)


104
105
106
107
108
109
110
111
# File 'lib/salted-rails/config.rb', line 104

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)


100
101
102
# File 'lib/salted-rails/config.rb', line 100

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

#normalizeObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/salted-rails/config.rb', line 169

def normalize
  @versions['mysql'] ||= '5.5' if @roles.include?('mysql')
  @versions['teamcity'] ||= '8.0.4' if @roles.include?('teamcity')
  @versions['rubymine'] ||= '5.4.3' if @roles.include?('rubymine')
  @roles << 'gui' if @roles.include?('rubymine') and not gui?

  %w{ ruby php java }.each do |lang|
    version = File.open(@project_root + ".#{lang}-version", 'r') do |f_in|
      f_in.gets.gsub(/\s/,'')
    end rescue nil
    @versions[lang] ||= version if version
  end
  unless @versions.include?('ruby')
    File.open(@project_root + '.rvmrc', 'r') do |f_in|
      while (line = f_in.gets) and not @versions.include('ruby')
        @versions['ruby'] = $1 if line =~ /^\s*environment_id=['"]([^"'@]+)/
      end
    end rescue nil
  end

  if @memory.nil?
    @memory = 512
    {
            'gui' => 1536,
            'teamcity' => 1536,
            'cruisecontrolrb' => 512
    }.each do |role, extra|
      @memory += extra if @roles.include?(role)
    end
  end

  if @domain.nil?
    if @hostname
      @domain = @hostname.sub(/^[^.]*\.?/, '')
    else
      @domain = sanitize_dns_name(File.basename(File.expand_path(@project_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

  @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)
  @box ||= 'preciseCloud32'
  @region ||= 'San Francisco 1'

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

  @sync_vagrant = (provider == 'virtualbox') if @sync_vagrant.nil?

  unless @roles.include?('secure') or @roles.include?('insecure')
    @roles << (provider == 'virtualbox' ? 'insecure' : 'secure')
  end
  @machines.each {|m| m.normalize}
end

#providerObject



113
114
115
116
117
118
119
# File 'lib/salted-rails/config.rb', line 113

def provider
  pat = File.join(@project_root, '.vagrant','machines',@machine,'*','id')
  prov = Dir.glob(pat).collect{|path| File.basename(File.dirname(path))}.first
  prov ||= ARGV.select{|a| a =~ /^--provider=/}.collect{|a| a.sub(/.*=/, '')}.first
  prov ||= ENV['VAGRANT_DEFAULT_PROVIDER'] || 'virtualbox'
  prov
end

#sanitize_dns_name(name) ⇒ Object



37
38
39
# File 'lib/salted-rails/config.rb', line 37

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

#to_hashObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/salted-rails/config.rb', line 134

def to_hash
  {
    'admin_password' => @admin_password,
    'databases' => @databases,
    'disable_vagrant_sync' => @disable_vagrant_sync,
    'domain' => @domain,
    'files' => @files,
    'forward_agent' => @forward_agent,
    'gems' => @gems,
    'hostname' => @hostname,
    'machine' => @machine,
    'mapped_ports' => @mapped_ports,
    'memory' => @memory,
    'mirror' => @mirror,
    'ports' => @ports,
    'region' => @region,
    'roles' => @roles,
    'provider' => provider,
    'staging_password' => @staging_password,
    'versions' => @versions
  }
end

#to_yamlObject



157
158
159
160
# File 'lib/salted-rails/config.rb', line 157

def to_yaml
  normalize
  self.to_hash.to_yaml
end