-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm_start.py
More file actions
76 lines (63 loc) · 2.97 KB
/
vm_start.py
File metadata and controls
76 lines (63 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from argparse import ArgumentParser
from pyVmomi import vim
from pyVim.task import WaitForTask
import socket
from data_retriever.dto import result_message, output
from data_retriever.vm_ware_connection import VMwareConnection
def vm_start(vm: vim.VirtualMachine, name: str) -> dict:
"""
Start a VM
Args:
vm (vim.VirtualMachine): The `VirtualMachine` object representing the VM to start
name (str): The name of the VM to start for logging
Returns:
dict: A dictionary formatted for json dump containing the result message. See result_message() function in dto.py
"""
try:
if not vm:
return result_message("VM not found", 404)
if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
return result_message("VM is already on", 403)
task = vm.PowerOn()
WaitForTask(task)
return result_message(f"VM '{name}' has been successfully started", 200)
except (vim.fault.NoCompatibleHost, vim.fault.InvalidHostState, OSError, socket.error):
return result_message("Host is unreachable", 404)
except vim.fault.TaskInProgress:
return result_message(f"VM '{name}' is busy", 403)
except (vim.fault.InvalidPowerState, vim.fault.VimFault):
return result_message(f"VM '{name}' can't be started", 403)
except Exception as err:
return result_message(str(err), 400)
def complete_vm_start(moid: str, ip: str, user: str, password: str, port: int) -> dict:
"""
Start a VM
Args:
moid (str): The Managed Object ID of the VM to start
ip (str): The ip of the VCenter to connect to
user (str): The username of the VCenter to connect to
password (str): The password of the VCenter to connect to
port (int): The port to use to connect to the VCenter
Returns:
dict: A dictionary formatted for json dump containing the result message. See result_message() function in dto.py
"""
conn = VMwareConnection()
try:
conn.connect(ip, user, password, port=port)
vm = conn.get_vm(moid)
return vm_start(vm, moid)
except vim.fault.InvalidLogin:
return result_message("Invalid credentials", 401)
except Exception as err:
return result_message(str(err), 400)
finally:
conn.disconnect()
if __name__ == "__main__":
parser = ArgumentParser(description="Allume une VM sur un serveur ESXi")
parser.add_argument("--moid", required=True, help="Le Managed Object ID de la VM")
parser.add_argument("--ip", required=True, help="Adresse IP du vCenter")
parser.add_argument("--user", required=True, help="Nom d'utilisateur du vCenter")
parser.add_argument("--password", required=True, help="Mot de passe de l'utilisateur du vCenter")
parser.add_argument("--port", type=int, default=443, help="Port du vCenter (optionnel, 443 par défaut)")
args = parser.parse_args()
output(complete_vm_start(args.moid, args.ip, args.user, args.password, args.port))