add check vm

master
Долгий Артём 2023-03-11 22:32:11 +03:00
parent 89d762b3a9
commit e171887466
2 changed files with 29 additions and 0 deletions

View File

@ -10,6 +10,20 @@ with open('vars.yaml') as data:
vars_dict = yaml.safe_load(data) vars_dict = yaml.safe_load(data)
def check_vm(id):
"""
Проверка существования ВМ.
:param id: (int), идентификатор ВМ.
:return: None
"""
proc = subprocess.run(f'qm status {id}'.split(), capture_output=True, text=True)
if proc.stderr:
print(f'Вероятно ВМ с идентификатором - {id} существует')
print(proc.stderr)
exit(1)
def shell_run(cmd): def shell_run(cmd):
""" """
Выполнение shell команды. Выполнение shell команды.
@ -65,6 +79,12 @@ def create_vms(commands_dict):
if __name__ == "__main__": if __name__ == "__main__":
ids = [vars_dict['template']['id']]
for id in vars_dict['vms']['id']:
ids.append(id)
for id in ids:
check_vm(id)
cmds = gen_vm_commands(vars_dict) cmds = gen_vm_commands(vars_dict)
create_vms(cmds) create_vms(cmds)
# pprint(cmds) # pprint(cmds)

View File

@ -85,6 +85,14 @@ def deploy_template(list_commands):
print('Шаблон создан.') print('Шаблон создан.')
def check_vm(id):
proc = subprocess.run(f'qm status {id}'.split(), capture_output=True, text=True)
if proc.stderr:
print(f'Вероятно ВМ с идентификатором - {id} существует')
print(proc.stderr)
exit(1)
if __name__ == "__main__": if __name__ == "__main__":
# в зависимости от типа и нахождения хранилища # в зависимости от типа и нахождения хранилища
# путь к создаваемому в процессе импорта диску указывается по-разному # путь к создаваемому в процессе импорта диску указывается по-разному
@ -100,6 +108,7 @@ if __name__ == "__main__":
f'qm set {tmpl_id} --ciuser={tmpl_user} --cipassword={tmpl_passwd}', f'qm set {tmpl_id} --ciuser={tmpl_user} --cipassword={tmpl_passwd}',
f'qm template {tmpl_id}'] f'qm template {tmpl_id}']
check_vm(tmpl_id)
download_img(img_name, img_url) download_img(img_name, img_url)
deploy_template(commands) deploy_template(commands)
del_img(img_name) del_img(img_name)