Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
sskarimov | e537eadda6 | 5 days ago |
sskarimov | 5f3df12742 | 2 months ago |
@ -1,36 +0,0 @@
|
|||||||
---
|
|
||||||
#
|
|
||||||
# DECORT kvmvm module example
|
|
||||||
#
|
|
||||||
- hosts: ansible_master
|
|
||||||
tasks:
|
|
||||||
- name: create a VM named cloud-init_example
|
|
||||||
decort_kvmvm:
|
|
||||||
name: affinity_example
|
|
||||||
annotation: "VM managed by decort_kvmvm module"
|
|
||||||
authenticator: oauth2
|
|
||||||
app_id: "" # Application id from SSO Digital Energy
|
|
||||||
app_secret: "" # API key from SSO Digital Energy
|
|
||||||
controller_url: "" #"https://mr4.digitalenergy.online"
|
|
||||||
rg_id: # Resource group id
|
|
||||||
cpu: 2
|
|
||||||
ram: 2048
|
|
||||||
boot_disk: 10
|
|
||||||
image_name: "DECS Ubuntu 18.04 v1.2.3" # Name of OS image
|
|
||||||
networks:
|
|
||||||
- type: VINS
|
|
||||||
id: # VINS id
|
|
||||||
tags: "Ansible cloud init example"
|
|
||||||
aff_lable: "Affinity lable"
|
|
||||||
tag:
|
|
||||||
- key: bd
|
|
||||||
value: main
|
|
||||||
aff_rule:
|
|
||||||
- key: app
|
|
||||||
value: main
|
|
||||||
topology: compute
|
|
||||||
policy: REQUIRED
|
|
||||||
mode: EQ
|
|
||||||
state: present
|
|
||||||
delegate_to: localhost
|
|
||||||
register: simple_vm
|
|
@ -1,36 +0,0 @@
|
|||||||
---
|
|
||||||
#
|
|
||||||
# DECORT kvmvm module example
|
|
||||||
#
|
|
||||||
- hosts: ansible_master
|
|
||||||
tasks:
|
|
||||||
- name: create a VM named cloud-init_example
|
|
||||||
decort_kvmvm:
|
|
||||||
name: anti-affinity_example
|
|
||||||
annotation: "VM managed by decort_kvmvm module"
|
|
||||||
authenticator: oauth2
|
|
||||||
app_id: "" # Application id from SSO Digital Energy
|
|
||||||
app_secret: "" # API key from SSO Digital Energy
|
|
||||||
controller_url: "" #"https://mr4.digitalenergy.online"
|
|
||||||
rg_id: # Resource group id
|
|
||||||
cpu: 2
|
|
||||||
ram: 2048
|
|
||||||
boot_disk: 10
|
|
||||||
image_name: "DECS Ubuntu 18.04 v1.2.3" #Name of OS image
|
|
||||||
networks:
|
|
||||||
- type: VINS
|
|
||||||
id: #VINS id
|
|
||||||
tags: "Ansible cloud init example"
|
|
||||||
aff_lable: "Anti affinity lable"
|
|
||||||
tag:
|
|
||||||
- key: bd
|
|
||||||
value: main
|
|
||||||
aaff_rule:
|
|
||||||
- key: app
|
|
||||||
value: main
|
|
||||||
topology: compute
|
|
||||||
policy: REQUIRED
|
|
||||||
mode: ANY
|
|
||||||
state: present
|
|
||||||
delegate_to: localhost
|
|
||||||
register: simple_vm
|
|
@ -1,38 +0,0 @@
|
|||||||
#
|
|
||||||
# DECORT kvmvm module example
|
|
||||||
#
|
|
||||||
- hosts: ansible_master
|
|
||||||
tasks:
|
|
||||||
- name: create a VM named cloud-init_example
|
|
||||||
decort_kvmvm:
|
|
||||||
annotation: "VM managed by decort_kvmvm module"
|
|
||||||
authenticator: oauth2
|
|
||||||
app_id: "" # Application id from SSO Digital Energy
|
|
||||||
app_secret: "" # API key from SSO Digital Energy
|
|
||||||
controller_url: "" #"https://mr4.digitalenergy.online"
|
|
||||||
name: cloud-init_example
|
|
||||||
cpu: 2
|
|
||||||
ram: 2048
|
|
||||||
boot_disk: 10
|
|
||||||
image_name: "DECS Ubuntu 18.04 v1.2.3" #Name of OS image
|
|
||||||
networks:
|
|
||||||
- type: VINS
|
|
||||||
id: #VINS id
|
|
||||||
tags: "Ansible cloud init example"
|
|
||||||
state: present
|
|
||||||
rg_id: #Resource group id
|
|
||||||
ci_user_data:
|
|
||||||
packages:
|
|
||||||
- apache2
|
|
||||||
write_files:
|
|
||||||
- content: |
|
|
||||||
<div>
|
|
||||||
Hello World!
|
|
||||||
</div>
|
|
||||||
owner: user:user
|
|
||||||
path: /var/www/html/index.html
|
|
||||||
hostname: test-apache
|
|
||||||
ssh_keys:
|
|
||||||
- rsa_public: ssh-rsa AAAAOasDmLxnD= user@pc
|
|
||||||
delegate_to: localhost
|
|
||||||
register: simple_vm
|
|
@ -0,0 +1,157 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
DOCUMENTATION = r'''
|
||||||
|
---
|
||||||
|
module: decort_snapshot
|
||||||
|
|
||||||
|
description: See L(Module Documentation,https://repository.basistech.ru/BASIS/decort-ansible/wiki/Home).
|
||||||
|
'''
|
||||||
|
|
||||||
|
import time
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.decort_utils import DecortController
|
||||||
|
|
||||||
|
|
||||||
|
class DecortSnapshot(DecortController):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(AnsibleModule(**self.amodule_init_args))
|
||||||
|
self.check_amodule_args()
|
||||||
|
|
||||||
|
self.aparams_label = self.aparams['label']
|
||||||
|
self.aparams_vm_id = self.aparams['vm_id']
|
||||||
|
|
||||||
|
vm_id, vm_facts, _ = self._compute_get_by_id(
|
||||||
|
comp_id=self.aparams_vm_id,
|
||||||
|
)
|
||||||
|
if not vm_id:
|
||||||
|
self.message(f'VM {self.aparams_vm_id} not found')
|
||||||
|
self.exit(fail=True)
|
||||||
|
|
||||||
|
self.vm_name = vm_facts['name']
|
||||||
|
self.vm_snapshots = vm_facts['snapSets']
|
||||||
|
self.vm_snapshot_labels = [
|
||||||
|
snapshot['label'] for snapshot in self.vm_snapshots
|
||||||
|
]
|
||||||
|
|
||||||
|
if (
|
||||||
|
self.aparams_label is not None
|
||||||
|
and self.aparams_label not in self.vm_snapshot_labels
|
||||||
|
and self.aparams['state'] is None
|
||||||
|
):
|
||||||
|
self.message(
|
||||||
|
f'Snapshot {self.aparams_label} '
|
||||||
|
f'not found for VM {self.aparams_vm_id}'
|
||||||
|
)
|
||||||
|
self.exit(fail=True)
|
||||||
|
|
||||||
|
self.new_snapshot_label = None
|
||||||
|
if self.aparams['state'] == 'present':
|
||||||
|
if self.aparams_label is None:
|
||||||
|
self.new_snapshot_label = (
|
||||||
|
f'{self.vm_name}_{self.sec_to_dt_str(time.time())}'
|
||||||
|
)
|
||||||
|
elif self.aparams_label not in self.vm_snapshot_labels:
|
||||||
|
self.new_snapshot_label = self.aparams_label
|
||||||
|
|
||||||
|
@property
|
||||||
|
def amodule_init_args(self) -> dict:
|
||||||
|
return self.pack_amodule_init_args(
|
||||||
|
argument_spec=dict(
|
||||||
|
label=dict(
|
||||||
|
type='str',
|
||||||
|
),
|
||||||
|
state=dict(
|
||||||
|
type='str',
|
||||||
|
choices=(
|
||||||
|
'absent',
|
||||||
|
'present',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
usage=dict(
|
||||||
|
type='bool',
|
||||||
|
default=False,
|
||||||
|
),
|
||||||
|
vm_id=dict(
|
||||||
|
type='int',
|
||||||
|
required=True,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
supports_check_mode=True,
|
||||||
|
required_if=[
|
||||||
|
('state', 'absent', ('label',)),
|
||||||
|
],
|
||||||
|
required_one_of=[
|
||||||
|
('label', 'state'),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
def check_amodule_args(self):
|
||||||
|
check_error = False
|
||||||
|
if (
|
||||||
|
self.aparams['state'] == 'absent'
|
||||||
|
and self.aparams['usage']
|
||||||
|
):
|
||||||
|
self.message(
|
||||||
|
'Parameter "usage" is not supported when deleting snapshot'
|
||||||
|
)
|
||||||
|
check_error = True
|
||||||
|
|
||||||
|
if check_error:
|
||||||
|
self.exit(fail=True)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.get_info(first_run=True)
|
||||||
|
self.change()
|
||||||
|
self.exit()
|
||||||
|
|
||||||
|
def get_info(self, first_run: bool = False):
|
||||||
|
if not first_run:
|
||||||
|
self.vm_snapshots = self.snapshot_list(
|
||||||
|
compute_id=self.aparams_vm_id,
|
||||||
|
)
|
||||||
|
label = self.new_snapshot_label or self.aparams_label
|
||||||
|
for snapshot in self.vm_snapshots:
|
||||||
|
if snapshot['label'] == label:
|
||||||
|
self.facts = snapshot
|
||||||
|
if self.aparams['usage']:
|
||||||
|
self.facts['stored'] = self.get_snapshot_usage()
|
||||||
|
self.facts['vm_id'] = self.aparams_vm_id
|
||||||
|
break
|
||||||
|
|
||||||
|
def change(self):
|
||||||
|
match self.aparams['state']:
|
||||||
|
case 'present':
|
||||||
|
if self.new_snapshot_label:
|
||||||
|
self.create()
|
||||||
|
case 'absent':
|
||||||
|
if self.aparams_label in self.vm_snapshot_labels:
|
||||||
|
self.delete()
|
||||||
|
|
||||||
|
def create(self):
|
||||||
|
self.snapshot_create(
|
||||||
|
compute_id=self.aparams_vm_id,
|
||||||
|
label=self.new_snapshot_label,
|
||||||
|
)
|
||||||
|
self.get_info()
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
self.snapshot_delete(
|
||||||
|
compute_id=self.aparams_vm_id,
|
||||||
|
label=self.aparams_label,
|
||||||
|
)
|
||||||
|
self.facts = {}
|
||||||
|
|
||||||
|
def get_snapshot_usage(self) -> int:
|
||||||
|
label = self.new_snapshot_label or self.aparams_label
|
||||||
|
common_snapshots_usage_info, _ = self.snapshot_usage(
|
||||||
|
compute_id=self.aparams_vm_id,
|
||||||
|
label=label,
|
||||||
|
)
|
||||||
|
return common_snapshots_usage_info['stored']
|
||||||
|
|
||||||
|
def main():
|
||||||
|
DecortSnapshot().run()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue