arkisto/src/lib/openstack.cr

78 lines
2.1 KiB
Crystal

module Arkisto
class OpenStack
#
# openstack volume list
#
def self.volume_list
cmd = "openstack"
args = [
"volume", "list",
"--long",
"--format", "yaml"
]
stdout = IO::Memory.new
stderr = IO::Memory.new
status = Process.run(cmd, args: args, output: stdout, error: stderr)
if ! status.success?
raise RuntimeError.new("command failed for: #{cmd} #{args.join(" ")}") # FIXME: improve message
end
return Array(OSVolume::ListItemModel).from_yaml(stdout.to_s)
rescue ex : RuntimeError
puts "ERROR: #{ex.message}".colorize(:red)
exit(1)
end
#
# openstack volume snapshot list
#
def self.volume_snapshot_list
cmd = "openstack"
args =[
"volume", "snapshot", "list",
"--long",
"--format", "yaml"
]
stdout = IO::Memory.new
stderr = IO::Memory.new
status = Process.run(cmd, args: args, output: stdout, error: stderr)
if ! status.success?
raise RuntimeError.new("command failed for: #{cmd} #{args.join(" ")}") # FIXME: improve message
end
return Array(OSVolume::SnapshotListItemModel).from_yaml(stdout.to_s)
rescue ex : RuntimeError
puts "ERROR: #{ex.message}".colorize(:red)
exit(1)
end
#
# openstack volume snapshot create --volume VOLUME_ID --force SNAPSHOT_NAME -f yaml
#
def self.volume_snapshot_create(target_volume, snap_name)
cmd = "openstack"
args = ["volume", "snapshot", "create",
"--volume", target_volume.id,
"--force",
snap_name,
"--format", "yaml"]
stdout = IO::Memory.new
stderr = IO::Memory.new
status = Process.run(cmd, args: args, output: stdout, error: stderr)
if ! status.success?
raise RuntimeError.new("#{cmd} #{args.join(" ")}\n#{stderr.to_s}") # FIXME: improve message
end
return OSVolume::SnapshotCreateItemModel.from_yaml(stdout.to_s)
rescue ex : RuntimeError
puts "ERROR: #{ex.message}".colorize(:red)
exit(1)
end
end
end