meta/lib/recycledcloud/opennebula/vm.ex

142 lines
3.9 KiB
Elixir
Raw Normal View History

defmodule RecycledCloud.OpenNebula.VM do
@moduledoc """
OpenNebula VM: http://docs.opennebula.io/5.12/integration/system_interfaces/api.html#one-vm-info
"""
alias RecycledCloud.OpenNebula, as: ONE
alias RecycledCloud.OpenNebula.Schema
@states [
{:any, -2},
{:any_except_done, -1},
{:init, 0},
{:pending, 1},
{:hold, 2},
{:active, 3},
{:stopped, 4},
{:suspended, 5},
{:done, 6},
{:failure, 7},
{:poweroff, 8},
{:undeployed, 9},
{:cloning, 10},
{:cloning_failure, 11}
]
2021-04-07 17:23:48 +02:00
@actions [
"terminate-hard",
"terminate",
"undeploy-hard",
"undeploy",
"poweroff-hard",
"poweroff",
"reboot-hard",
"reboot",
"hold",
"release",
"stop",
"suspend",
"resume",
"resched",
"unresched"
]
@events [
{:NONE_ACTION , 0},
{:MIGRATE_ACTION , 1},
{:LIVE_MIGRATE_ACTION , 2},
{:SHUTDOWN_ACTION , 3},
{:SHUTDOWN_HARD_ACTION , 4},
{:UNDEPLOY_ACTION , 5},
{:UNDEPLOY_HARD_ACTION , 6},
{:HOLD_ACTION , 7},
{:RELEASE_ACTION , 8},
{:STOP_ACTION , 9},
{:SUSPEND_ACTION , 10},
{:RESUME_ACTION , 11},
{:BOOT_ACTION , 12},
{:DELETE_ACTION , 13},
{:DELETE_RECREATE_ACTION , 14},
{:REBOOT_ACTION , 15},
{:REBOOT_HARD_ACTION , 16},
{:RESCHED_ACTION , 17},
{:UNRESCHED_ACTION , 18},
{:POWEROFF_ACTION , 19},
{:POWEROFF_HARD_ACTION , 20},
{:DISK_ATTACH_ACTION , 21},
{:DISK_DETACH_ACTION , 22},
{:NIC_ATTACH_ACTION , 23},
{:NIC_DETACH_ACTION , 24},
{:DISK_SNAPSHOT_CREATE_ACTION , 25},
{:DISK_SNAPSHOT_DELETE_ACTION , 26},
{:TERMINATE_ACTION , 27},
{:TERMINATE_HARD_ACTION , 28},
{:DISK_RESIZE_ACTION , 29},
{:DEPLOY_ACTION , 30},
{:CHOWN_ACTION , 31},
{:CHMOD_ACTION , 32},
{:UPDATECONF_ACTION , 33},
{:RENAME_ACTION , 34},
{:RESIZE_ACTION , 35},
{:UPDATE_ACTION , 36},
{:SNAPSHOT_CREATE_ACTION , 37},
{:SNAPSHOT_DELETE_ACTION , 38},
{:SNAPSHOT_REVERT_ACTION , 39},
{:DISK_SAVEAS_ACTION , 40},
{:DISK_SNAPSHOT_REVERT_ACTION , 41},
{:RECOVER_ACTION , 42},
{:RETRY_ACTION , 43},
{:MONITOR_ACTION , 44},
{:DISK_SNAPSHOT_RENAME_ACTION , 45},
{:ALIAS_ATTACH_ACTION , 46},
{:ALIAS_DETACH_ACTION , 47},
]
def state_for(state) when is_atom(state) do
@states |> Keyword.get(state)
end
def state_for(state) when is_integer(state) do
2021-04-07 17:23:48 +02:00
case Enum.find(@states, fn {_atom, value} -> value == state end) do
{atom, _value} -> atom
nil -> nil
end
end
def event_for(event) when is_integer(event) do
case Enum.find(@events, fn {_atom, value} -> value == event end) do
{atom, _value} -> atom
nil -> nil
end
end
def get(id) do
case ONE.query("one.vm.info", [id]) do
{:ok, raw} ->
data = raw
|> Schema.scan("vm")
|> Schema.map_record
{:ok, data}
{:error, err} -> {:error, err}
end
end
2021-04-07 17:23:48 +02:00
def get_by_username(username) do
{:ok, %{VM: vms}} = RecycledCloud.OpenNebula.VMPool.get(%{
filter_flag: :all,
range_start: :infinite,
range_end: :infinite,
state_filter: state_for(:any_except_done),
kv_filter: ""
})
Enum.filter(vms, fn vm -> List.to_string(Map.get(vm, :UNAME)) == username end)
end
def execute(vm_id, action) when action in @actions do
case ONE.query("one.vm.action", [action, vm_id]) do
{:ok, _raw} -> :ok
{:error, err} -> {:error, err}
end
end
end