Class: OsCtld::Daemon

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Assets::Definition
Defined in:
lib/osctld/daemon.rb

Defined Under Namespace

Classes: ClientHandler

Constant Summary collapse

SOCKET =
File.join(RunState::RUNDIR, 'osctld.sock')
@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assets::Definition

#define_assets

Class Method Details

.getObject



57
58
59
# File 'lib/osctld/daemon.rb', line 57

def get
  @@instance ||= new
end

Instance Method Details

#assetsObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/osctld/daemon.rb', line 130

def assets
  define_assets do |add|
    RunState.assets(add)

    add.socket(
      SOCKET,
      desc: 'Management socket',
      user: 0,
      group: 0,
      mode: 0600
    )
  end
end

#begin_shutdownObject



175
176
177
# File 'lib/osctld/daemon.rb', line 175

def begin_shutdown
  File.open(RunState::SHUTDOWN_MARKER, 'w', 0000){}
end

#confirm_shutdownObject



179
180
181
182
183
184
185
# File 'lib/osctld/daemon.rb', line 179

def confirm_shutdown
  unless File.exist?(RunState::SHUTDOWN_MARKER)
    File.open(RunState::SHUTDOWN_MARKER, 'w', 0100){}
  end

  File.chmod(0100, RunState::SHUTDOWN_MARKER)
end

#join_serverObject



156
157
158
# File 'lib/osctld/daemon.rb', line 156

def join_server
  @server_thread.join
end

#log_typeObject



191
192
193
# File 'lib/osctld/daemon.rb', line 191

def log_type
  'daemon'
end

#serveObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/osctld/daemon.rb', line 144

def serve
  @server_thread = Thread.new do
    log(:info, "Listening on control socket at #{SOCKET}")

    socket = UNIXServer.new(SOCKET)
    File.chmod(0600, SOCKET)

    @server = Generic::Server.new(socket, Daemon::ClientHandler)
    @server.start
  end
end

#setupObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/osctld/daemon.rb', line 86

def setup
  # Setup /run/osctl
  RunState.create

  SystemLimits.instance

  # Increase allowed number of open files
  PrLimits.set(Process.pid, PrLimits::NOFILE, 16384, 16384)

  # Setup shared AppArmor files
  AppArmor.setup

  # Send/Receive hooks and server
  SendReceive.setup

  # Setup network interfaces
  NetInterface.setup

  # Start accepting client commands
  serve

  # Load data pools
  if KernelParams.import_pools?
    autostart = KernelParams.autostart_cts?

    Commands::Pool::Import.run(all: true, autostart: autostart)

    unless autostart
      log(:info, 'Container autostart disabled by kernel parameter')
    end
  else
    log(:info, 'Pool autoimport disabled by kernel parameter')
  end

  # Resume shutdown
  if shutdown?
    log(:info, 'Resuming shutdown')
    Commands::Self::Shutdown.run
  end

  # Wait for the server to finish
  join_server
end

#shutdown?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/osctld/daemon.rb', line 187

def shutdown?
  File.exist?(RunState::SHUTDOWN_MARKER)
end

#stopObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/osctld/daemon.rb', line 160

def stop
  log(:info, 'Shutting down')
  Eventd.stop
  @server.stop if @server
  File.unlink(SOCKET) if File.exist?(SOCKET)
  UserControl.stop
  SendReceive.stop
  DB::Pools.get.each { |pool| pool.stop }
  ThreadReaper.stop
  Monitor::Master.stop
  LockRegistry.stop
  log(:info, 'Shutdown successful')
  exit(false)
end