Paymenter的 CreateServer有些问题,比如IP这里,会卡住。
public function createServer($user, $parmas, $order, $product, $configurableOptions)
{
$node = isset($configurableOptions['node']) ? $configurableOptions['node'] : $parmas['node'];
$storage = isset($configurableOptions['storage']) ? $configurableOptions['storage'] : $parmas['storage'];
$pool = isset($configurableOptions['pool']) ? $configurableOptions['pool'] : $parmas['pool'];
$cores = isset($configurableOptions['cores']) ? $configurableOptions['cores'] : $parmas['cores'];
$memory = isset($configurableOptions['memory']) ? $configurableOptions['memory'] : $parmas['memory'];
$disk = isset($configurableOptions['disk']) ? $configurableOptions['disk'] : $parmas['disk'];
$swap = isset($configurableOptions['swap']) ? $configurableOptions['swap'] : $parmas['swap'];
$network_limit = isset($configurableOptions['network_limit']) ? $configurableOptions['network_limit'] : ($parmas['network_limit'] ?? null);
$cpu = isset($configurableOptions['cpu']) ? $configurableOptions['cpu'] : ($parmas['cpu'] ?? null);
$vmid = $this->getRequest('/cluster/nextid')->json()['data'];
// Assign it to the orderProduct for further use
ExtensionHelper::setOrderProductConfig('vmid', $vmid, $product->id);
$postData = [];
$currentConfig = $product->product->settings;
$postData = [];
$vmType = $currentConfig->where('name', 'type')->first()->value;
if ($currentConfig->where('name', 'clone')->first()->value == '1') {
$postData = [
'newid' => $vmid,
'target' => $node,
'full' => 1,
];
isset($parmas['pool']) && $postData['pool'] = $parmas['pool'];
$response = $this->postRequest('/nodes/' . $node . '/' . $vmType . '/' . $parmas['vmId'] . '/clone', $postData);
if (!$response->json()) throw new Exception('Unable to clone server');
// Update hardware
$postData = [
'cores' => $cores,
'memory' => $memory,
'cipassword' => $parmas['config']['password'],
];
$response = $this->putRequest('/nodes/' . $node . '/' . $vmType . '/' . $vmid . '/config', $postData);
if (!$response->json()) throw new Exception('Unable to update hardware');
// Get disk
$disk = $this->getRequest('/nodes/' . $node . '/' . $vmType . '/' . $vmid . '/config')->json()['data'];
$disk = explode('order=', $disk['boot'])[1];
$disk = explode(',', $disk)[0];
$postData = [
'disk' => $disk,
'size' => $parmas['disk'] . 'G',
];
$response = $this->putRequest('/nodes/' . $node . '/' . $vmType . '/' . $vmid . '/resize', $postData);
return true;
} else if ($vmType == 'lxc') {
$postData = [
'vmid' => $vmid,
'node' => $node,
'storage' => $storage,
'cores' => $cores,
'memory' => $memory,
'onboot' => 1,
'ostemplate' => $parmas['template'],
'ostype' => $parmas['ostypelxc'],
'description' => $parmas['config']['hostname'],
'hostname' => $parmas['config']['hostname'],
'password' => $parmas['config']['password'],
'swap' => $swap ?? 512,
'unprivileged' => isset($parmas['unprivileged']) ? 1 : 0,
'net0' => 'name=test' . ',bridge=' . $parmas['bridge'] . ',' . (isset($parmas['firewall']) ? 'firewall=1' : 'firewall=0') . (isset($network_limit) ? ',rate=' . $network_limit : ''),
];
$ips = isset($configurableOptions['ips']) ? $configurableOptions['ips'] : ($parmas['ips'] ?? null);
if (isset($ips)) {
$ips = explode(',', $ips);
// Get all ips which are not used
$usedIps = OrderProduct::where('product_id', $product->product->id)->where('status', '!=', 'terminated')->with('config')->get();
$usedIps = $usedIps->map(function ($orderProduct) {
return $orderProduct->config->where('key', 'ips')->first()->value ?? false;
})->toArray();
$ips = array_diff($ips, $usedIps);
if (empty($ips)) {
throw new Exception('No more IPs available');
} // Only one
// 获取第一个可用 IP
$ips = array_values($ips); // 重建索引
$ips = $ips[0];
ExtensionHelper::setOrderProductConfig('ips', $ips, $product->id);
$postData['net0'] .= ',ip=' . $ips . '/24';
}
$gateway = isset($configurableOptions['gateway']) ? $configurableOptions['gateway'] : ($parmas['gateway'] ?? null);
isset($gateway) ? $postData['net0'] .= ',gw=' . $gateway : null;
isset($pool) ? $postData['pool'] = $pool : null;
$response = $this->postRequest('/nodes/' . $node . '/lxc', $postData);
} else {
$socket = isset($configurableOptions['sockets']) ? $configurableOptions['sockets'] : ($parmas['sockets'] ?? null);
$vcpu = isset($configurableOptions['vcpu']) ? $configurableOptions['vcpu'] : ($parmas['vcpu'] ?? null);
$postData = [
'vmid' => $vmid,
'node' => $node,
'storage' => $storage,
'cores' => $cores,
'memory' => $memory,
'onboot' => 1,
'sockets' => $socket ?? 1,
'agent' => 1,
'ostype' => $parmas['ostype'],
'name' => $parmas['config']['hostname'],
'description' => $parmas['config']['hostname'],
$parmas['storageType'] . '0' => $parmas['storage'] . ':' . $disk . ',format=' . $parmas['storageFormat'],
'net0' => $parmas['model'] . ',bridge=' . $parmas['bridge'] . ',' . (isset($parmas['firewall']) ? 'firewall=1' : 'firewall=0'),
];
isset($pool) ? $postData['pool'] = $pool : null;
isset($parmas['cloudinit']) ? $postData[$parmas['storageType'] . '0'] = $parmas['storage'] . ':cloudinit' . ',format=' . $parmas['storageFormat'] : null;
isset($cpu) ? $postData['cpu'] = $cpu : null;
isset($vcpu) ? $postData['vcpus'] = $vcpu : null;
if (isset($parmas['os']) && $parmas['os'] == 'iso') {
$postData['ide2'] = $parmas['iso'] . ',media=cdrom';
}
$response = $this->postRequest('/nodes/' . $node . '/qemu', $postData);
}
if (!$response->json()) throw new Exception('Unable to create server' . $response->body());
return true;
}
这是更新后的代码。