2021-02-11 14:34:40 +01:00
|
|
|
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-09 11:36:33 +02:00
|
|
|
@lcm_states [
|
|
|
|
{:LCM_INIT , 0},
|
|
|
|
{:PROLOG , 1},
|
|
|
|
{:BOOT , 2},
|
|
|
|
{:RUNNING , 3},
|
|
|
|
{:MIGRATE , 4},
|
|
|
|
{:SAVE_STOP , 5},
|
|
|
|
{:SAVE_SUSPEND , 6},
|
|
|
|
{:SAVE_MIGRATE , 7},
|
|
|
|
{:PROLOG_MIGRATE , 8},
|
|
|
|
{:PROLOG_RESUME , 9},
|
|
|
|
{:EPILOG_STOP , 10},
|
|
|
|
{:EPILOG , 11},
|
|
|
|
{:SHUTDOWN , 12},
|
|
|
|
{:CLEANUP_RESUBMIT , 15},
|
|
|
|
{:UNKNOWN , 16},
|
|
|
|
{:HOTPLUG , 17},
|
|
|
|
{:SHUTDOWN_POWEROFF , 18},
|
|
|
|
{:BOOT_UNKNOWN , 19},
|
|
|
|
{:BOOT_POWEROFF , 20},
|
|
|
|
{:BOOT_SUSPENDED , 21},
|
|
|
|
{:BOOT_STOPPED , 22},
|
|
|
|
{:CLEANUP_DELETE , 23},
|
|
|
|
{:HOTPLUG_SNAPSHOT , 24},
|
|
|
|
{:HOTPLUG_NIC , 25},
|
|
|
|
{:HOTPLUG_SAVEAS , 26},
|
|
|
|
{:HOTPLUG_SAVEAS_POWEROFF , 27},
|
|
|
|
{:HOTPLUG_SAVEAS_SUSPENDED , 28},
|
|
|
|
{:SHUTDOWN_UNDEPLOY , 29},
|
|
|
|
{:EPILOG_UNDEPLOY , 30},
|
|
|
|
{:PROLOG_UNDEPLOY , 31},
|
|
|
|
{:BOOT_UNDEPLOY , 32},
|
|
|
|
{:HOTPLUG_PROLOG_POWEROFF , 33},
|
|
|
|
{:HOTPLUG_EPILOG_POWEROFF , 34},
|
|
|
|
{:BOOT_MIGRATE , 35},
|
|
|
|
{:BOOT_FAILURE , 36},
|
|
|
|
{:BOOT_MIGRATE_FAILURE , 37},
|
|
|
|
{:PROLOG_MIGRATE_FAILURE , 38},
|
|
|
|
{:PROLOG_FAILURE , 39},
|
|
|
|
{:EPILOG_FAILURE , 40},
|
|
|
|
{:EPILOG_STOP_FAILURE , 41},
|
|
|
|
{:EPILOG_UNDEPLOY_FAILURE , 42},
|
|
|
|
{:PROLOG_MIGRATE_POWEROFF , 43},
|
|
|
|
{:PROLOG_MIGRATE_POWEROFF_FAILURE, 44},
|
|
|
|
{:PROLOG_MIGRATE_SUSPEND , 45},
|
|
|
|
{:PROLOG_MIGRATE_SUSPEND_FAILURE , 46},
|
|
|
|
{:BOOT_UNDEPLOY_FAILURE , 47},
|
|
|
|
{:BOOT_STOPPED_FAILURE , 48},
|
|
|
|
{:PROLOG_RESUME_FAILURE , 49},
|
|
|
|
{:PROLOG_UNDEPLOY_FAILURE , 50},
|
|
|
|
{:DISK_SNAPSHOT_POWEROFF , 51},
|
|
|
|
{:DISK_SNAPSHOT_REVERT_POWEROFF , 52},
|
|
|
|
{:DISK_SNAPSHOT_DELETE_POWEROFF , 53},
|
|
|
|
{:DISK_SNAPSHOT_SUSPENDED , 54},
|
|
|
|
{:DISK_SNAPSHOT_REVERT_SUSPENDED , 55},
|
|
|
|
{:DISK_SNAPSHOT_DELETE_SUSPENDED , 56},
|
|
|
|
{:DISK_SNAPSHOT , 57},
|
|
|
|
{:DISK_SNAPSHOT_DELETE , 59},
|
|
|
|
{:PROLOG_MIGRATE_UNKNOWN , 60},
|
|
|
|
{:PROLOG_MIGRATE_UNKNOWN_FAILURE , 61},
|
|
|
|
]
|
|
|
|
|
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},
|
|
|
|
]
|
|
|
|
|
2021-04-09 11:36:33 +02:00
|
|
|
defp find(table, state) when is_integer(state) do
|
|
|
|
case Enum.find(table, fn {_atom, value} -> value == state end) do
|
2021-04-07 17:23:48 +02:00
|
|
|
{atom, _value} -> atom
|
|
|
|
nil -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-09 11:36:33 +02:00
|
|
|
def state_for(state) when is_atom(state), do: @states |> Keyword.get(state)
|
|
|
|
def state_for(state) when is_integer(state), do: find(@states, state)
|
|
|
|
def lcm_state_for(state) when is_integer(state), do: find(@lcm_states, state)
|
|
|
|
|
2021-04-07 17:23:48 +02:00
|
|
|
def event_for(event) when is_integer(event) do
|
|
|
|
case Enum.find(@events, fn {_atom, value} -> value == event end) do
|
2021-02-11 14:34:40 +01:00
|
|
|
{atom, _value} -> atom
|
|
|
|
nil -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(id) do
|
2021-04-09 11:36:33 +02:00
|
|
|
fn location ->
|
|
|
|
case ONE.query(location, "one.vm.info", [id]) do
|
|
|
|
{:ok, raw} ->
|
|
|
|
data = raw
|
|
|
|
|> Schema.scan("vm")
|
|
|
|
|> Schema.map_record
|
|
|
|
{:ok, data}
|
|
|
|
{:error, err} -> {:error, err}
|
|
|
|
end
|
2021-02-11 14:34:40 +01:00
|
|
|
end
|
|
|
|
end
|
2021-04-07 17:23:48 +02:00
|
|
|
|
|
|
|
def execute(vm_id, action) when action in @actions do
|
2021-04-09 11:36:33 +02:00
|
|
|
fn location ->
|
|
|
|
case ONE.query(location, "one.vm.action", [action, vm_id]) do
|
|
|
|
{:ok, _raw} -> :ok
|
|
|
|
{:error, err} -> {:error, err}
|
|
|
|
end
|
2021-04-07 17:23:48 +02:00
|
|
|
end
|
|
|
|
end
|
2021-02-11 14:34:40 +01:00
|
|
|
end
|