Module: Gadfly

Defined in:
lib/gadfly.rb

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

INIT_DIR =
'/etc/init'

Class Method Summary collapse

Class Method Details

.install_upstart_script(script_name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gadfly.rb', line 10

def self.install_upstart_script(script_name)
  script_name = script_name.gsub('.rb', '')

  raise('script not found') unless File.exists?("#{script_name}.rb")
  raise('upstart not installed') unless Helpers.command?('start') && Helpers.command?('stop')

  start_script = <<-eos
#!/bin/bash

set -u
set -e

INIT_RBENV='export PATH=$PATH:$HOME/.rbenv/bin; eval "$(rbenv init -)"'

CMD="$INIT_RBENV; cd '#{Dir.pwd}'; bundle exec ruby #{script_name}.rb"

su - #{ENV['USER']} -c "$CMD";
  eos

  conf = <<-eos
description "#{script_name}"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [016]

console log
exec #{Dir.pwd}/start_script
  eos

  `bundle`

  File.open('start_script', 'w') { |f| f.write(start_script) }
  `chmod +x start_script`

  File.open("#{script_name}.conf", 'w') { |f| f.write(conf) }

  [ 'mkdir -p /var/log/upstart',
    "mv #{script_name}.conf #{INIT_DIR}",
    "chown root:root #{INIT_DIR}/#{script_name}.conf",
    "stop #{script_name}",
    "start #{script_name}",
  ].each { |c| system "sudo #{c}" }
end