Class: OsCtl::Cli::Self

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

Constant Summary collapse

SHUTDOWN_MARKER =
'/run/osctl/shutdown'

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods included from Assets

#print_assets

Methods inherited from Command

#cli_opt, #format_output, #initialize, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, #require_args!, run

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

This class inherits a constructor from OsCtl::Cli::Command

Instance Method Details

#activateObject



60
61
62
# File 'lib/osctl/cli/self.rb', line 60

def activate
  osctld_fmt(:self_activate, system: opts[:system], lxcfs: opts[:lxcfs])
end

#assetsObject



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

def assets
  print_assets(:self_assets)
end

#do_pingObject (protected)



117
118
119
120
121
122
123
124
125
126
# File 'lib/osctl/cli/self.rb', line 117

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
58
# 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
        return if do_ping

      rescue GLI::CustomExit
        raise if secs > 0 && i >= secs
      end

      sleep(1)
    end

  else
    puts 'pong' if do_ping
  end
end

#shutdownObject



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
108
109
110
111
112
113
114
# File 'lib/osctl/cli/self.rb', line 64

def shutdown
  unless opts[:force]
    STDOUT.write(
      'Do you really wish to stop all containers and export all pools? '+
      '[y/N]: '
    )

    if !%w(y yes).include?(STDIN.readline.strip.downcase)
      puts 'Aborting'
      return
    end
  end

  # Ensure osctld will shutdown even if it crashes/restarts
  File.open(SHUTDOWN_MARKER, 'w', 0000){}

  begin
    osctld_fmt(:self_shutdown)
    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 & 0100 == 0100
        warn ' ok'
        return
      end
    rescue Errno::ENOENT
      warn 'Shutdown mark does not exist, osctld will not shutdown'
      return
    end

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

    sleep(1)
  end

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