1+ """
2+ This is a MAAS-specific sample of plug-in code for a 'During Resource Provisioning' Action.
3+ Please see the Plugin Actions documentation for more examples of plug-in code:
4+ https://docs.cloudbolt.io/articles/#!cloudbolt-latest-docs/cloudbolt-plug-ins
5+ """
6+ import json
7+ import uuid
8+
9+ from common .methods import set_progress
10+ from infrastructure .models import Server
11+ from resourcehandlers .maas .models import MaasResourceHandler
12+
13+
14+ def run (job , * args , ** kwargs ):
15+ set_progress ("This will show up in the job details page in the CB UI, and in the job log" )
16+ mount_point = "{{ filesystem_mount_point }}"
17+
18+ # machine is a MAAS-specific object, only available within the MaasWrapper
19+ machine = kwargs .get ('machine' , None )
20+
21+ #server_id is the ID of the Cloudbolt Server being provisioned
22+ server_id = kwargs .get ('server_id' , None )
23+ cb_server = Server .objects .get (pk = server_id )
24+
25+ # maas_machine_type = True when create virtual machine else False
26+ if cb_server .maas_machine_type :
27+ set_progress ("Provisioning a RAID is not valid for the virtual machine on a MAAS LXD host" )
28+ return "SUCCESS" , "The MaaS during-provisioning plugin has completed." , ""
29+
30+ if machine :
31+ maas_id = machine ['system_id' ]
32+ set_progress ("Configuring RAID on MAAS machine {}" .format (maas_id ))
33+ else :
34+ return "FAILURE" , "" , "Missing required MAAS machine info."
35+
36+ wrapper = kwargs .get ('wrapper' , None )
37+ if wrapper :
38+ vm = wrapper .get_vm ('unused' , maas_id )
39+ machine = wrapper .driver .get_machine (maas_id )
40+ else :
41+ return "FAILURE" , "" , "Invalid (None) wrapper passed to plugin"
42+
43+ fstype = "ext4"
44+ # remove any existing RAID configurations
45+ raids = wrapper .maas_api_request ("GET" , "nodes/{}/raids/" .format (maas_id ))
46+
47+ # We need to move to machine in ready state to delete the existig raid, you can't delete raid if machine in allocated state
48+ rel = wrapper .maas_api_request ("POST" , "machines/{}/?op=release" .format (maas_id ), data = {"comment" : "making in ready state" })
49+
50+ if raids :
51+ for raid in raids :
52+ set_progress ("Deleting RAID volume: {}" .format (raid ['name' ]))
53+ wrapper .maas_api_request ("DELETE" , "nodes/{}/raid/{}/" .format (maas_id , raid ['id' ]))
54+
55+ # get unused physical disks
56+ all_disks = wrapper .maas_api_request ("GET" , "nodes/{}/blockdevices/" .format (maas_id ))
57+ disks = [str (disk ['id' ]) for disk in all_disks if disk ['used_for' ] == 'Unused' and disk ['type' ] == 'physical' ]
58+ set_progress ("MaaS machine has {} associated disks, of which {} are unused physical disks." .format (len (all_disks ), len (disks )))
59+
60+ if len (disks )>= 2 :
61+ set_progress ("Creating a RAID-1 array md0 for the machine using all unused disks: {}" .format (',' .join ([disk ['name' ] for disk in all_disks if disk ['used_for' ] == 'Unused' and disk ['type' ] == 'physical' ])))
62+ data = {
63+ "name" : "md0" ,
64+ "uuid" : uuid .uuid4 (),
65+ "level" : 'raid-1' ,
66+ "block_devices" : disks ,
67+ "partitions" : [],
68+ "spare_devices" : [],
69+ "spare_partitions" : [],
70+ }
71+
72+ resp = wrapper .maas_api_request ("POST" , "nodes/{}/raids/" .format (maas_id ), data = data )
73+ block_device_id = resp ["virtual_device" ]["id" ]
74+
75+ set_progress ("Formatting filesystem: block_device_id={}, fstype={}" .format (block_device_id , fstype ))
76+ formatdata = {
77+ 'fstype' : fstype ,
78+ 'uuid' : uuid .uuid4 ()
79+ }
80+ formatres = wrapper .maas_api_request ("POST" , 'nodes/{}/blockdevices/{}/?op=format' .format (maas_id , block_device_id ), data = formatdata )
81+
82+ set_progress ("Configuring mount point: block_device_id={}, mount_point={}" .format (block_device_id , mount_point ))
83+ mountdata = {
84+ "mount_point" : mount_point ,
85+ "mount_options" : "" ,
86+ }
87+ mountres = wrapper .maas_api_request ("POST" , 'nodes/{}/blockdevices/{}/?op=mount' .format (maas_id , block_device_id ), data = mountdata )
88+ allocated_machines = wrapper .maas_api_request ("POST" , "machines/?op=allocate" , data = {'system_id' : maas_id })
89+ set_progress ('Reallocation of MaaS Machine: {}' .format (maas_id ))
90+
91+ return "SUCCESS" , "Finished configuring RAID for MaaS machine" , ""
92+ else :
93+ return "FAILURE" , "" , "Failed to create RAID during-provisioning because only {} disk, It should be have more than 2 disks" .format (disks )
0 commit comments