Class: Smartguard::Applications::Smartkiosk

Inherits:
Smartguard::Application show all
Defined in:
lib/smartguard/applications/smartkiosk.rb,
lib/smartguard/applications/smartkiosk/web.rb,
lib/smartguard/applications/smartkiosk/sidekiq.rb,
lib/smartguard/applications/smartkiosk/scheduler.rb,
lib/smartguard/applications/smartkiosk/smartware.rb

Defined Under Namespace

Classes: Scheduler, Sidekiq, Smartware, Web

Instance Attribute Summary

Attributes inherited from Smartguard::Application

#active_path, #current_path, #releases_path, #shared_path

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Smartkiosk

Returns a new instance of Smartkiosk.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/smartguard/applications/smartkiosk.rb', line 4

def initialize(*args)
  super

  if Smartguard.environment == :production
    @head_path = @base_path.join('head')
  else
    @head_path = @base_path
  end

  @services = {
    smartware: Smartware.new(wrap_path),
    sidekiq: Sidekiq.new(wrap_path),
    web: Web.new(wrap_path),
    scheduler: Scheduler.new(wrap_path),
  }
end

Instance Method Details

#rebootObject



45
46
47
48
# File 'lib/smartguard/applications/smartkiosk.rb', line 45

def reboot
  stop_services
  ::Process.spawn 'reboot'
end

#reboot_asyncObject



50
51
52
53
# File 'lib/smartguard/applications/smartkiosk.rb', line 50

def reboot_async
  Thread.new{ self.reboot }
  true
end

#restart(path = nil) ⇒ Object



35
36
37
38
# File 'lib/smartguard/applications/smartkiosk.rb', line 35

def restart(path=nil)
  stop_services(path)
  start_services(path)
end

#restart_async(path = nil) ⇒ Object



40
41
42
43
# File 'lib/smartguard/applications/smartkiosk.rb', line 40

def restart_async(path=nil)
  Thread.new{ self.restart(path) }
  true
end

#servicesObject



21
22
23
# File 'lib/smartguard/applications/smartkiosk.rb', line 21

def services
  @services.keys
end

#start_services(path = nil, &block) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/smartguard/applications/smartkiosk.rb', line 114

def start_services(path=nil, &block)
  @services.each do |key, service|
    service.path = wrap_path path

    if !service.active?
      if !service.start && block_given?
        Logging.logger.debug "Startup of #{key} failed: running safety block"
        yield
        return false
      end
    end
  end
end

#statusObject



25
26
27
28
29
30
31
32
33
# File 'lib/smartguard/applications/smartkiosk.rb', line 25

def status
  data = {}

  @services.each do |key, service|
    data[key] = [ service.pid, service.active? ]
  end

  data
end

#stop_services(path = nil) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/smartguard/applications/smartkiosk.rb', line 128

def stop_services(path=nil)
  @services.each do |key, service|
    service.path = wrap_path path

    service.stop if service.active?
  end
end

#switch_release(release) ⇒ Object



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
99
100
101
102
103
104
105
106
107
# File 'lib/smartguard/applications/smartkiosk.rb', line 55

def switch_release(release)
  if Smartguard.environment != :production
    raise "Release switching is only supported in the production environment."
  end

  release = release.is_a?(Symbol) ? @releases_path.join(release.to_s)
                                  : @base_path.join(release)

  raise "Release doesn't exist: #{release.to_s}" unless File.exist? release

  FileUtils.mkdir_p "#{release}/tmp/pids"
  FileUtils.mkdir_p "#{release}/log"

  FileUtils.cd(release) do
    Logging.logger.info "Symlinking"
    FileUtils.rm_rf "config/services"
    FileUtils.rm_rf "public/uploads"
    FileUtils.ln_s "#{@shared_path.join 'config'}", "config/services"
    FileUtils.ln_s "#{@shared_path.join 'uploads'}", "public/uploads"

    FileUtils.rm_f "config/database.yml"
    FileUtils.ln_s "services/database.yml", "config/database.yml"

    Logging.logger.info "Installing release gems"
    if !system("bundle", "install")
      raise "bundle failed"
    end

    Logging.logger.info "Migrating database"
    if !system("bundle", "exec", "rake", "db:migrate", "--trace", "RACK_ENV=production")
      raise "migration failed"
    end
  end

  self.stop_services

  Logging.logger.info "Switching symlink from `#{@active_path}` to `#{release}`"
  File.delete @current_path if File.symlink? @current_path
  FileUtils.ln_s(release, @current_path)

  @active_path = release if self.start_services do
    Logging.logger.warn "New release `#{release}` did not start!"
    self.stop_services

    Logging.logger.info "Switching symlink back to `#{@active_path}`"
    File.delete @current_path
    FileUtils.ln_s @active_path, @current_path

    self.start_services

    raise "startup failed"
  end
end

#switch_release_async(release) ⇒ Object



109
110
111
112
# File 'lib/smartguard/applications/smartkiosk.rb', line 109

def switch_release_async(release)
  Thread.new{ switch_release(release) }
  true
end