|
| 1 | +open Lwt.Infix |
| 2 | +open Testarossa |
| 3 | +open Cmd_types |
| 4 | +open Context |
| 5 | +open Xen_api_lwt_unix |
| 6 | + |
| 7 | +let ensure_vm_off ctx vifr = |
| 8 | + let self = vifr.API.vIF_VM in |
| 9 | + rpc ctx @@ VM.get_power_state ~self |
| 10 | + >>= function |
| 11 | + | `Halted -> Lwt.return_unit |
| 12 | + | _ -> |
| 13 | + rpc ctx @@ VM.shutdown ~vm:self |
| 14 | + |
| 15 | +let ensure_vm_on ctx (vm, vmr) = |
| 16 | + match vmr.API.vM_power_state with |
| 17 | + | `Halted -> rpc ctx @@ VM.start ~start_paused:false ~force:false ~vm |
| 18 | + | _ -> Lwt.return_unit |
| 19 | + |
| 20 | +let ensure_vhosts_on conf = |
| 21 | + match conf.physical with |
| 22 | + | None -> Lwt.return_unit |
| 23 | + | Some host -> |
| 24 | + Context.with_login ~uname:conf.uname ~pwd:conf.pwd host (fun phys -> |
| 25 | + Context.step phys "ensure vhosts are powered on" @@ fun ctx -> |
| 26 | + Rollback.list_vms ctx |
| 27 | + >>= Lwt_list.iter_p (ensure_vm_on ctx) |
| 28 | + ) |
| 29 | + |
| 30 | +let ensure_vhost_nics conf = |
| 31 | + match conf.physical with |
| 32 | + | None -> Lwt.return_unit |
| 33 | + | Some host -> |
| 34 | + Context.with_login ~uname:conf.uname ~pwd:conf.pwd host (fun phys -> |
| 35 | + Context.step phys "ensure vhost has enough NICs" @@ fun ctx -> |
| 36 | + Rollback.list_vms ctx |
| 37 | + >>= Lwt_list.iter_p (fun (vm, vmr) -> |
| 38 | + match vmr.API.vM_VIFs with |
| 39 | + | [] -> Lwt.fail_with "No NICs" |
| 40 | + | [ one ] -> |
| 41 | + rpc ctx @@ VIF.get_network ~self:one |
| 42 | + >>= fun network -> |
| 43 | + rpc ctx @@ VIF.create ~device:"1" ~network ~vM:vm ~mAC:"" |
| 44 | + ~mTU:1500L ~other_config:[] ~qos_algorithm_type:"" |
| 45 | + ~qos_algorithm_params:[] ~locking_mode:`network_default |
| 46 | + ~ipv4_allowed:[] ~ipv6_allowed:[] |
| 47 | + >>= fun vif -> |
| 48 | + debug (fun m -> m "VIF %s created" (Ref.string_of vif)); |
| 49 | + (* can't hotplug due to pure HVM mode *) |
| 50 | + rpc ctx @@ VM.hard_reboot ~vm |
| 51 | + | _ -> Lwt.return_unit |
| 52 | + ) |
| 53 | + ) |
| 54 | + |
| 55 | +let ensure_bonding_on_master conf = |
| 56 | + let master = List.hd conf.hosts |> Ipaddr.V4.to_string in |
| 57 | + Context.with_login ~uname:conf.uname ~pwd:conf.pwd master (fun t -> |
| 58 | + step t "Create bond on master if needed" @@ fun ctx -> |
| 59 | + rpc ctx @@ Pool.get_all |
| 60 | + >>= fun pools -> |
| 61 | + rpc ctx @@ Pool.get_master ~self:(List.hd pools) |
| 62 | + >>= fun host -> |
| 63 | + rpc ctx @@ PIF.scan ~host |
| 64 | + >>= fun () -> |
| 65 | + rpc ctx @@ Bond.get_all |
| 66 | + >>= function |
| 67 | + | [] -> |
| 68 | + info (fun m -> m "No bond, creating one"); |
| 69 | + rpc ctx @@ Host.get_management_interface ~host |
| 70 | + >>= fun pif -> |
| 71 | + rpc ctx @@ PIF.get_record ~self:pif |
| 72 | + >>= fun pifr -> |
| 73 | + rpc ctx @@ Host.get_PIFs ~self:host |
| 74 | + >>= fun members -> |
| 75 | + rpc ctx @@ Network.create ~name_label:"bonded network" |
| 76 | + ~name_description:"" ~mTU:1500L ~other_config:[] |
| 77 | + ~bridge:"" ~managed:true ~tags:[] |
| 78 | + >>= fun network -> |
| 79 | + rpc ctx @@ Bond.create ~network ~members ~mAC:"" ~mode:`balanceslb ~properties:[] |
| 80 | + >>= fun bond -> |
| 81 | + debug (fun m -> m "Created bond %s" (Ref.string_of bond)); |
| 82 | + Lwt.return_unit |
| 83 | + | _ -> Lwt.return_unit |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +let do_prepare conf = |
| 88 | + let master = List.hd conf.hosts |> Ipaddr.V4.to_string in |
| 89 | + Context.with_login ~uname:conf.uname ~pwd:conf.pwd master (fun t -> |
| 90 | + License.maybe_apply_license_pool t conf [Features.HA; Features.Corosync] |
| 91 | + >>= fun () -> |
| 92 | + Test_sr.enable_clustering t |
| 93 | + >>= fun _cluster -> |
| 94 | + (match (conf.iscsi, conf.iqn) with |
| 95 | + | Some iscsi, Some iqn -> |
| 96 | + Test_sr.get_gfs2_sr t ~iscsi ~iqn ?scsiid:conf.scsiid () >>= fun _gfs2 -> Lwt.return_unit |
| 97 | + | _ -> Lwt.return_unit) |
| 98 | + >>= fun () -> |
| 99 | + Test_sr.make_pool ~uname:conf.uname ~pwd:conf.pwd conf conf.hosts) |
| 100 | + |
| 101 | +let run conf = |
| 102 | + ensure_vhost_nics conf |
| 103 | + >>= fun () -> |
| 104 | + ensure_vhosts_on conf |
| 105 | + >>= fun () -> |
| 106 | + (* Test_sr.destroy_pools ~uname:conf.uname ~pwd:conf.pwd conf.hosts : |
| 107 | + >>= fun () ->*) |
| 108 | + ensure_bonding_on_master conf |
| 109 | + >>= fun ()-> |
| 110 | + do_prepare conf |
0 commit comments