Class: OsCtl::Cli::Self

Inherits:
Command
  • Object
show all
Includes:
Assets
Defined in:
lib/osctl/cli/self.rb

Constant Summary collapse

SHUTDOWN_MARKER =
'/run/osctl/shutdown'.freeze

Instance Method Summary collapse

Methods included from Assets

#print_assets

Methods inherited from Command

#cli_opt, #format_output, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, run

Instance Method Details

#activateObject



75
76
77
# File 'lib/osctl/cli/self.rb', line 75

def activate
  osctld_fmt(:self_activate, cmd_opts: { system: opts[:system] })
end

#assetsObject



10
11
12
# File 'lib/osctl/cli/self.rb', line 10

def assets
  print_assets(:self_assets)
end

#do_pingObject (protected)



141
142
143
144
145
146
147
148
149
# File 'lib/osctl/cli/self.rb', line 141

def do_ping
  return true if osctld_call(:self_ping) == 'pong'

  raise GLI::CustomExit.new('unexpected response', 3)
rescue Errno::ENOENT
  raise GLI::CustomExit.new('unable to connect', 2)
rescue OsCtl::Client::Error
  raise GLI::CustomExit.new('invalid response', 3)
end

#healthcheckObject



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
# File 'lib/osctl/cli/self.rb', line 14

def healthcheck
  entities = osctld_call(
    :self_healthcheck,
    all: opts[:all],
    pools: opts[:all] || args.empty? ? nil : args
  )

  if gopts[:json]
    puts entities.to_json
    return
  end

  if entities.empty?
    puts 'No errors detected.'
    return
  end

  entities.each do |ent|
    puts "#{ent[:type]} #{ent[:pool] || '-'} #{ent[:id] || '-'}"

    ent[:assets].each do |asset|
      puts "\t#{asset[:type]} #{asset[:path]}: #{asset[:errors].join('; ')}"
    end
  end
end

#pingObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/osctl/cli/self.rb', line 40

def ping
  if args[0]
    secs = args[0].to_i

    (0..Float::INFINITY).each do |i|
      begin
        break if do_ping
      rescue GLI::CustomExit
        raise if secs > 0 && i >= secs
      end

      sleep(1)
    end

  elsif do_ping
    puts 'pong'
  end
end

#scriptObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/osctl/cli/self.rb', line 59

def script
  require_args!('file', strict: false)

  ARGV.shift # script
  ARGV.shift # <file>

  begin
    script_path = File.realpath(args[0])
  rescue Errno::ENOENT
    warn "Script not found at #{script_path}"
    exit(false)
  end

  load(script_path)
end

#shutdownObject



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/osctl/cli/self.rb', line 79

def shutdown
  if opts[:abort]
    osctld_fmt(:self_abort_shutdown)
    return
  end

  unless opts[:force]
    $stdout.write(
      'Do you really wish to stop all containers and export all pools? ' \
      '[y/N]: '
    )

    unless %w[y yes].include?($stdin.readline.strip.downcase)
      puts 'Aborting'
      return
    end
  end

  # Ensure osctld will shutdown even if it crashes/restarts
  File.new(SHUTDOWN_MARKER, 'w', 0o000).close

  begin
    osctld_fmt(:self_shutdown, cmd_opts: {
      wall: opts[:wall],
      message: opts[:message]
    })
    return
  rescue OsCtl::Client::Error => e
    warn "Lost connection to osctld: #{e.message}"
  rescue Errno::ENOENT
    warn 'Unable to connect to osctld: socket not found'
  end

  $stdout.write('Waiting for osctld to prepare for shutdown...')
  $stdout.flush

  (0..3600).each do |i|
    begin
      st = File.stat(SHUTDOWN_MARKER)
      if st.mode & 0o100 == 0o100
        warn ' ok'
        return # rubocop:disable Lint/NonLocalExitFromIterator
      end
    rescue Errno::ENOENT
      warn 'Shutdown mark does not exist, osctld will not shutdown'
      return # rubocop:disable Lint/NonLocalExitFromIterator
    end

    if i % 5 == 0
      $stdout.write('.')
      $stdout.flush
    end

    sleep(1)
  end

  warn
  warn 'Waited for an hour, going to shutdown'
end