Class: Rrunit::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/rrunit/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
13
14
# File 'lib/rrunit/service.rb', line 8

def initialize(command)
  @command = command
  @name = File.basename(command)
  @environment = Hash.new
  @exported_environment = Hash.new
  @args = Array.new
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/rrunit/service.rb', line 6

def command
  @command
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rrunit/service.rb', line 5

def name
  @name
end

Instance Method Details

#args(*args) ⇒ Object



16
17
18
19
# File 'lib/rrunit/service.rb', line 16

def args(*args)
  @args = args unless args.empty?
  args.empty? ? @args : self
end

#env_var(value) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rrunit/service.rb', line 36

def env_var(value)
  case
  when value.is_a?(String)
    "\"#{value}\""
  else
    value.to_s
  end
end

#environment(env = nil) ⇒ Object



26
27
28
29
# File 'lib/rrunit/service.rb', line 26

def environment(env=nil)
  @environment=env if env.is_a?(Hash)
  env.nil? ? @environment : self
end

#exported_environment(env = nil) ⇒ Object



31
32
33
34
# File 'lib/rrunit/service.rb', line 31

def exported_environment(env=nil)
  @exported_environment=env if env.is_a?(Hash)
  env.nil? ? @exported_environment : self
end

#log_dir(log_dir = nil) ⇒ Object



21
22
23
24
# File 'lib/rrunit/service.rb', line 21

def log_dir(log_dir=nil)
  @log_dir = log_dir unless log_dir.nil?
  log_dir.nil? ? @log_dir : self
end

#log_serviceObject



60
61
62
63
64
65
66
67
68
# File 'lib/rrunit/service.rb', line 60

def log_service
  if @log_dir
    sv = Array.new
    cmd = "exec svlogd -tt #{@log_dir}"
    sv << "#!/bin/bash"
    sv << cmd
    sv.join("\n")
  end
end

#rmObject



88
89
90
# File 'lib/rrunit/service.rb', line 88

def rm
  FileUtils.rm_r @output_directory
end

#serviceObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rrunit/service.rb', line 45

def service
  sv = Array.new
  cmd = "exec #{command}"
  cmd << " #{@args.join(' ')}" unless @args.empty?
  sv << "#!/bin/bash"
  @exported_environment.each do |key, value|
    sv << "export #{key.to_s.upcase}=#{env_var(value)}"
  end
  @environment.each do |key, value|
    sv << "#{key.to_s.upcase}=#{env_var(value)}"
  end
  sv << cmd
  sv.join("\n")
end

#write(path) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rrunit/service.rb', line 70

def write(path)
  @output_directory = path
  FileUtils.mkdir_p @output_directory
  File.open("#{@output_directory}/run", 'w') do |f|
    f << service
    f << "\n"
  end
  FileUtils.chmod 0755, "#{@output_directory}/run"
  if @log_dir
    FileUtils.mkdir_p "#{@output_directory}/log"
    File.open("#{@output_directory}/log/run", 'w') do |f|
      f << log_service
      f << "\n"
    end
    FileUtils.chmod 0755, "#{@output_directory}/log/run"
  end
end