Class: Devkitkat::Service

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

Constant Summary collapse

ScriptError =
Class.new(StandardError)
DIVISIONS =
%w[src script data cache log example dockerfile].freeze
SERVICE_PROPERTIES =
%w[repo host port]
SCRIPT_TEMPLATE =
<<-EOS
#!/bin/bash
set -e

# TODO: Define scripts
EOS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, command) ⇒ Service

Returns a new instance of Service.



23
24
25
# File 'lib/devkitkat/service.rb', line 23

def initialize(name, config, command)
  @name, @config, @command = name, config, command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/devkitkat/service.rb', line 7

def command
  @command
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/devkitkat/service.rb', line 7

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/devkitkat/service.rb', line 7

def name
  @name
end

Instance Method Details

#container_nameObject



69
70
71
# File 'lib/devkitkat/service.rb', line 69

def container_name
  "#{config.application}-#{name}"
end

#executeObject



27
28
29
30
31
32
33
34
35
# File 'lib/devkitkat/service.rb', line 27

def execute
  execute!

  true
rescue ScriptError => e
  puts "Failure: #{e}".colorize(:red)

  false
end

#execute!Object



37
38
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
# File 'lib/devkitkat/service.rb', line 37

def execute!
  executor.prepare

  inject_global_variables
  inject_public_variables
  inject_private_variables
  setup_logger

  FileUtils.rm_f(log_path)
  FileUtils.mkdir_p(log_dir)

  method = script.tr('-', '_')

  if File.exist?(script_path)
    executor.write(%Q{echo "This script is a custom script provided by you."})
    executor.write(script_path)
  elsif respond_to?(method, true)
    executor.write(%Q{echo "This script is a predefined script provided by devkitkat."})
    send(method)
  end

  executor.commit.tap do |result|
    raise ScriptError, process_error_message($?) unless result
  end
ensure
  executor.cleanup
end

#log_pathObject



65
66
67
# File 'lib/devkitkat/service.rb', line 65

def log_path
  File.join(log_dir, "#{script}.log")
end

#service_dirObject



89
90
91
# File 'lib/devkitkat/service.rb', line 89

def service_dir
  File.join(kit_root, 'services', name)
end

#shared_script_dirObject



93
94
95
# File 'lib/devkitkat/service.rb', line 93

def shared_script_dir
  File.join(script_dir, 'shared')
end