Module: VagrantPlugins::Solidus::SiteHelpers

Included in:
Provisioner, VagrantPlugins::Solidus::Site::Subcommand
Defined in:
lib/vagrant-solidus/site_helpers.rb

Constant Summary collapse

BASE_PORT =
8081
BASE_UTILS_PORT =
35730
SITE_TEMPLATE_GIT_URL =
"https://github.com/solidusjs/solidus-site-template.git"
SITE_TEMPLATE_GIT_TAG =
"v1.0.0"
SITE_STATUS_WATCHER_POLLING_FREQUENCY =
1
PROVISION_ID =
20140502
DEFAULT_NODE_VERSION =
'stable'
DEFAULT_NPM_VERSION =
'^2.0.0'

Instance Method Summary collapse

Instance Method Details

#build_siteObject



210
211
212
# File 'lib/vagrant-solidus/site_helpers.rb', line 210

def build_site
  guest_exec(:log_on_error, "cd #{@site_guest_path} && #{node_command} npm #{site_commands_arguments} run build")
end

#clone_site_template(site_template_git_url) ⇒ Object

Site Template



288
289
290
291
292
293
294
295
296
# File 'lib/vagrant-solidus/site_helpers.rb', line 288

def clone_site_template(site_template_git_url)
  FileUtils.rm_rf(SITE_TEMPLATE_HOST_PATH)
  if site_template_git_url
    fail("Site template could not be cloned") unless host_exec(:log_on_error, "git", "clone", site_template_git_url, SITE_TEMPLATE_HOST_PATH)
  else
    fail("Site template could not be cloned") unless host_exec(:log_on_error, "git", "clone", "--branch", SITE_TEMPLATE_GIT_TAG, SITE_TEMPLATE_GIT_URL, SITE_TEMPLATE_HOST_PATH)
  end
  wait_until_guest_directory_exists(SITE_TEMPLATE_GUEST_PATH)
end

#create_site_from_template(site_template_guest_path) ⇒ Object



298
299
300
301
302
# File 'lib/vagrant-solidus/site_helpers.rb', line 298

def create_site_from_template(site_template_guest_path)
  site_template_guest_path ||= SITE_TEMPLATE_GUEST_PATH
  fail("Site could not be created") unless guest_exec(:log_on_error, "mkdir -p #{@site_guest_path}")
  fail("Site could not be created") unless guest_exec(:log_on_error, "cd #{@site_guest_path} && grunt-init --default=1 #{site_template_guest_path}")
end

#directory_exists?(directory) ⇒ Boolean

Returns:

  • (Boolean)


380
381
382
# File 'lib/vagrant-solidus/site_helpers.rb', line 380

def directory_exists?(directory)
  File.directory?(directory) && !(Dir.entries(directory) - %w{. ..}).empty?
end

#fail(error) ⇒ Object

Misc



369
370
371
372
# File 'lib/vagrant-solidus/site_helpers.rb', line 369

def fail(error)
  @env.ui.error(error)
  abort
end

#find_port(current_port, all_ports, used_ports) ⇒ Object



93
94
95
# File 'lib/vagrant-solidus/site_helpers.rb', line 93

def find_port(current_port, all_ports, used_ports)
  all_ports.include?(current_port) ? current_port : (all_ports - used_ports).first
end

#follow_site_logObject

Site Log



112
113
114
115
116
117
118
119
120
# File 'lib/vagrant-solidus/site_helpers.rb', line 112

def follow_site_log
  command = "tail -f -n 0 #{@site_log_file_guest_path}"
  begin
    guest_exec(:log_all, command)
  rescue Interrupt
    # Don't forget to stop tail in the vm
    guest_exec(nil, "kill -s SIGINT `pgrep -f \"#{command}\"`")
  end
end

#guest_exec(log_type, command, opts = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/vagrant-solidus/site_helpers.rb', line 24

def guest_exec(log_type, command, opts = {})
  with_log(log_type) do
    @last_exit_code = @machine.communicate.execute(command, {error_check: false}.merge(opts), &method(:log_callback))
    @last_exit_code == 0
  end
end

#help_command_line_option(opts) ⇒ Object



317
318
319
320
321
322
# File 'lib/vagrant-solidus/site_helpers.rb', line 317

def help_command_line_option(opts)
  opts.on("-h", "--help", "Print this help") do
    safe_puts(opts.help)
    abort
  end
end

#host_exec(log_type, *args) ⇒ Object

System Calls



17
18
19
20
21
22
# File 'lib/vagrant-solidus/site_helpers.rb', line 17

def host_exec(log_type, *args)
  with_log(log_type) do
    @last_exit_code = Vagrant::Util::Subprocess.execute(*args, {notify: [:stdout, :stderr]}, &method(:log_callback)).exit_code
    @last_exit_code == 0
  end
end

#install_pow_siteObject



263
264
265
# File 'lib/vagrant-solidus/site_helpers.rb', line 263

def install_pow_site
  File.write(File.expand_path("~/.pow/#{@site_name}"), @site_port)
end

#install_site_dependenciesObject

Site dependencies



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/vagrant-solidus/site_helpers.rb', line 130

def install_site_dependencies
  # Ruby gems
  if File.exists?(File.join(@site_host_path, 'Gemfile'))
    return unless guest_exec(:log_on_error, "cd #{@site_guest_path} && bundle install")
  else
    # Until all sites use bundler...
    return unless guest_exec(:log_on_error, "gem install sass")
  end

  # Bower packages
  if File.exists?(File.join(@site_host_path, 'bower.json'))
    return unless guest_exec(:log_on_error, "#{node_command} npm install bower -g")
    return unless guest_exec(:log_on_error, "cd #{@site_guest_path} && #{node_command} bower install")
  end

  return true
end

#install_site_nodeObject

Node.js



152
153
154
# File 'lib/vagrant-solidus/site_helpers.rb', line 152

def install_site_node
  guest_exec(:log_on_error, "nvm install #{node_version} && #{node_command} npm install npm@'#{npm_version}' -g")
end

#install_site_node_packagesObject



156
157
158
# File 'lib/vagrant-solidus/site_helpers.rb', line 156

def install_site_node_packages
  guest_exec(:log_on_error, "cd #{@site_guest_path} && #{node_command} npm install")
end

#install_site_serviceObject

Upstart



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/vagrant-solidus/site_helpers.rb', line 184

def install_site_service
  command = "exec su - vagrant -c 'cd #{@site_guest_path} &&"
  logging = ">> #{@site_log_file_guest_path} 2>&1'"

  conf = ""
  return unless guest_exec(:log_on_error, "echo \"#{conf}\" > /etc/init/#{site_service_name}.conf", sudo: true)

  conf = "start on starting #{site_service_name}
          stop on stopping #{site_service_name}
          #{command} #{node_command} npm #{site_commands_arguments} run watch #{logging}"
  return unless guest_exec(:log_on_error, "echo \"#{conf}\" > /etc/init/#{site_watcher_service_name}.conf", sudo: true)

  conf = "start on starting #{site_service_name}
          stop on stopping #{site_service_name}
          #{command} #{node_command} ./node_modules/.bin/solidus start --dev --loglevel=3 #{site_commands_arguments} #{logging}"
  return unless guest_exec(:log_on_error, "echo \"#{conf}\" > /etc/init/#{solidus_server_service_name}.conf", sudo: true)

  return true
end

#ip_addressObject



394
395
396
# File 'lib/vagrant-solidus/site_helpers.rb', line 394

def ip_address
  @ip_address ||= Socket.ip_address_list.detect(&:ipv4_private?).ip_address
end

#load_siteObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vagrant-solidus/site_helpers.rb', line 70

def load_site
  @site_host_path           = File.join(ROOT_HOST_PATH, @site_name)
  @site_guest_path          = File.join(ROOT_GUEST_PATH, @site_name)
  @site_log_file_path       = ".vagrant-solidus/log/#{@site_name}.log"
  @site_log_file_guest_path = File.join(ROOT_GUEST_PATH, @site_log_file_path)
  @package                  = JSON.load(File.new(File.join(@site_host_path, 'package.json'))) rescue {}

  if config = sites[@site_name]
    @site_port            = config['port']
    @site_livereload_port = config['livereload-port']
    @site_log_server_port = config['log-server-port']
  end
end

#log(type, data) ⇒ Object



51
52
53
# File 'lib/vagrant-solidus/site_helpers.rb', line 51

def log(type, data)
  @env.ui.info(data, prefix: false, new_line: false, channel: type == :stdout ? :out : :error)
end

#log_callback(*args) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/vagrant-solidus/site_helpers.rb', line 42

def log_callback(*args)
  case @log_type
  when :log_all
    log(*args)
  when :log_on_error
    @log_buffer << args
  end
end

#log_site_log_tail(lines) ⇒ Object



122
123
124
# File 'lib/vagrant-solidus/site_helpers.rb', line 122

def log_site_log_tail(lines)
  guest_exec(:log_all, "tail -n #{lines} #{@site_log_file_guest_path}")
end

#log_site_urlsObject



384
385
386
387
388
389
390
391
392
# File 'lib/vagrant-solidus/site_helpers.rb', line 384

def log_site_urls
  @env.ui.info("  Local URL:")
  @env.ui.info("    http://#{@site_name}.dev") if pow_installed?
  @env.ui.info("    http://lvh.me:#{@site_port}")
  @env.ui.info("    http://localhost:#{@site_port}")
  @env.ui.info("  Network URL:")
  @env.ui.info("    http://#{@site_name}.#{ip_address}.xip.io") if pow_installed?
  @env.ui.info("    http://#{ip_address}.xip.io:#{@site_port}")
end

#node_commandObject



168
169
170
# File 'lib/vagrant-solidus/site_helpers.rb', line 168

def node_command
  "nvm exec #{node_version}"
end

#node_versionObject



160
161
162
163
164
165
166
# File 'lib/vagrant-solidus/site_helpers.rb', line 160

def node_version
  unless @node_version
    @node_version = @package['engines']['node'] if @package['engines']
    @node_version = DEFAULT_NODE_VERSION if !@node_version || @node_version.empty?
  end
  @node_version
end

#npm_versionObject



172
173
174
175
176
177
178
# File 'lib/vagrant-solidus/site_helpers.rb', line 172

def npm_version
  unless @npm_version
    @npm_version = @package['engines']['npm'] if @package['engines']
    @npm_version = DEFAULT_NPM_VERSION if !@npm_version || @npm_version.empty?
  end
  @npm_version
end

#pow_installed?Boolean

Pow

Returns:

  • (Boolean)


259
260
261
# File 'lib/vagrant-solidus/site_helpers.rb', line 259

def pow_installed?
  File.directory?(File.expand_path("~/.pow"))
end

#provisioned!Object



361
362
363
# File 'lib/vagrant-solidus/site_helpers.rb', line 361

def provisioned!
  "mkdir -p ~/.vagrant-solidus && echo #{PROVISION_ID} > ~/.vagrant-solidus/provision"
end

#provisioned?Boolean

Provision

Returns:

  • (Boolean)


357
358
359
# File 'lib/vagrant-solidus/site_helpers.rb', line 357

def provisioned?
  guest_exec(nil, "echo #{PROVISION_ID} | diff - ~/.vagrant-solidus/provision")
end

#quiet_command_line_option(opts) ⇒ Object



334
335
336
337
338
# File 'lib/vagrant-solidus/site_helpers.rb', line 334

def quiet_command_line_option(opts)
  opts.on("-q", "--quiet", "Quiet mode. Don't output anything.") do
    @quiet = true
  end
end

#save_siteObject



101
102
103
104
105
106
# File 'lib/vagrant-solidus/site_helpers.rb', line 101

def save_site
  config = {'port' => @site_port, 'livereload-port' => @site_livereload_port, 'log-server-port' => @site_log_server_port}
  File.open(SITES_CONFIGS_FILE_HOST_PATH, 'w') do |file|
    file.write(JSON.pretty_generate(sites.merge(@site_name => config)))
  end
end

#set_site_portsObject



84
85
86
87
88
89
90
91
# File 'lib/vagrant-solidus/site_helpers.rb', line 84

def set_site_ports
  all  = @machine.config.solidus
  used = sites.values
  return unless @site_port            = find_port(@site_port,            all.site_ports,       used.map {|c| c['port']})
  return unless @site_livereload_port = find_port(@site_livereload_port, all.livereload_ports, used.map {|c| c['livereload-port']})
  return unless @site_log_server_port = find_port(@site_log_server_port, all.log_server_ports, used.map {|c| c['log-server-port']})
  return true
end

#site_commands_argumentsObject



251
252
253
# File 'lib/vagrant-solidus/site_helpers.rb', line 251

def site_commands_arguments
  "--port=#{@site_port} --livereloadport=#{@site_livereload_port} --logserverport=#{@site_log_server_port}"
end

#site_name_command_line_option(opts) ⇒ Object

Command Line Options



308
309
310
311
312
313
314
315
# File 'lib/vagrant-solidus/site_helpers.rb', line 308

def site_name_command_line_option(opts)
  @site_name = Pathname.pwd.relative_path_from(ROOT_HOST_PATH).to_s.split(File::SEPARATOR).first

  opts.on("-s", "--site <site>", "Site to use, instead of the current directory.") do |site_name|
    raise Vagrant::Errors::CLIInvalidUsage, help: opts.help.chomp if !site_name || site_name.empty?
    @site_name = site_name.chomp('/')
  end
end

#site_responding?Boolean

Returns:

  • (Boolean)


231
232
233
234
235
236
237
# File 'lib/vagrant-solidus/site_helpers.rb', line 231

def site_responding?
  loop do
    return false unless site_started?
    return true if guest_exec(nil, "curl --head localhost:#{@site_port}/status")
    sleep 0.5
  end
end

#site_service_nameObject



239
240
241
# File 'lib/vagrant-solidus/site_helpers.rb', line 239

def site_service_name
  "site-#{@site_name}"
end

#site_start_command_line_options(opts) ⇒ Object



324
325
326
327
328
329
330
331
332
# File 'lib/vagrant-solidus/site_helpers.rb', line 324

def site_start_command_line_options(opts)
  site_name_command_line_option(opts)
  opts.on("-f", "--fast", "Fast mode. Don't install the site dependencies first.") do
    @fast = true
  end
  opts.on("-d", "--deaf", "Don't automatically launch the `watch` command in background (file events will be much slower).") do
    @deaf = true
  end
end

#site_started?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/vagrant-solidus/site_helpers.rb', line 227

def site_started?
  guest_exec(nil, "status #{site_service_name} | grep 'start/running'", sudo: true)
end

#site_template_command_line_options(opts) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/vagrant-solidus/site_helpers.rb', line 340

def site_template_command_line_options(opts)
  opts.on("-g", "--template-git-url <URL>", "URL of the Solidus site template Git repository", "Default: #{SITE_TEMPLATE_GIT_URL}, #{SITE_TEMPLATE_GIT_TAG} tag") do |url|
    raise Vagrant::Errors::CLIInvalidUsage, help: opts.help.chomp if !url || url.empty?
    @site_template_git_url = url
  end

  opts.on("-p", "--template-path <path>", "Path of the Solidus site template to use, instead of the Git repository", "Must be relative to the Vagrantfile's directory") do |path|
    raise Vagrant::Errors::CLIInvalidUsage, help: opts.help.chomp if !path || path.empty?
    @site_template_host_path  = File.join(ROOT_HOST_PATH, path)
    @site_template_guest_path = File.join(ROOT_GUEST_PATH, path)
  end
end

#site_watcher_service_nameObject



243
244
245
# File 'lib/vagrant-solidus/site_helpers.rb', line 243

def site_watcher_service_name
  "#{site_service_name}-assets-watcher"
end

#sitesObject

Sites Management



66
67
68
# File 'lib/vagrant-solidus/site_helpers.rb', line 66

def sites
  @@sites ||= File.exists?(SITES_CONFIGS_FILE_HOST_PATH) ? JSON.load(File.new(SITES_CONFIGS_FILE_HOST_PATH)) : {}
end

#solidus_server_service_nameObject



247
248
249
# File 'lib/vagrant-solidus/site_helpers.rb', line 247

def solidus_server_service_name
  "#{site_service_name}-server"
end

#start_site_serviceObject



214
215
216
# File 'lib/vagrant-solidus/site_helpers.rb', line 214

def start_site_service
  guest_exec(:log_on_error, "start #{site_service_name}", sudo: true)
end

#start_site_watcherObject

Site Watcher



275
276
277
278
# File 'lib/vagrant-solidus/site_helpers.rb', line 275

def start_site_watcher
  command = "vagrant site watch -s #{@site_name} -q"
  Process.detach(Process.spawn(command, chdir: ROOT_HOST_PATH))
end

#stop_siteObject



222
223
224
225
# File 'lib/vagrant-solidus/site_helpers.rb', line 222

def stop_site
  stop_site_service
  wait_for_site_watcher_to_stop
end

#stop_site_serviceObject



218
219
220
# File 'lib/vagrant-solidus/site_helpers.rb', line 218

def stop_site_service
  guest_exec(nil, "stop #{site_service_name}", sudo: true)
end

#uninstall_pow_siteObject



267
268
269
# File 'lib/vagrant-solidus/site_helpers.rb', line 267

def uninstall_pow_site
  File.delete(File.expand_path("~/.pow/#{@site_name}")) if File.exists?(File.expand_path("~/.pow/#{@site_name}"))
end

#uninstall_site_serviceObject



204
205
206
207
208
# File 'lib/vagrant-solidus/site_helpers.rb', line 204

def uninstall_site_service
  guest_exec(nil, "rm /etc/init/#{site_service_name}.conf", sudo: true)
  guest_exec(nil, "rm /etc/init/#{site_watcher_service_name}.conf", sudo: true)
  guest_exec(nil, "rm /etc/init/#{solidus_server_service_name}.conf", sudo: true)
end

#validate_siteObject



97
98
99
# File 'lib/vagrant-solidus/site_helpers.rb', line 97

def validate_site
  !@package['dependencies']['solidus'].empty? rescue false
end

#wait_for_site_watcher_to_stopObject



280
281
282
# File 'lib/vagrant-solidus/site_helpers.rb', line 280

def wait_for_site_watcher_to_stop
  sleep(SITE_STATUS_WATCHER_POLLING_FREQUENCY)
end

#wait_until_guest_directory_exists(directory) ⇒ Object



374
375
376
377
378
# File 'lib/vagrant-solidus/site_helpers.rb', line 374

def wait_until_guest_directory_exists(directory)
  until guest_exec(nil, "cd #{directory}") do
    sleep 0.5
  end
end

#with_log(log_type) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/vagrant-solidus/site_helpers.rb', line 31

def with_log(log_type)
  @log_type   = log_type
  @log_buffer = []
  success = yield
  @log_buffer.each {|args| log(*args)} if @log_type == :log_on_error && !success
  return success
ensure
  @log_type   = nil
  @log_buffer = nil
end

#with_mutexObject



55
56
57
58
59
60
# File 'lib/vagrant-solidus/site_helpers.rb', line 55

def with_mutex
  @mutex ||= Mutex.new
  @mutex.synchronize do
    yield
  end
end