Newer
Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
if($server_id > 0) {
$tmp = $app->db->queryOneRecord("SELECT ip_address_id, server_id, ip_address FROM openvz_ip WHERE reserved = 'n' AND vm_id = 0 AND server_id = $server_id LIMIT 0,1");
} else {
$tmp = $app->db->queryOneRecord("SELECT ip_address_id, server_id, ip_address FROM openvz_ip WHERE reserved = 'n' AND vm_id = 0 LIMIT 0,1");
}
if(count($tmp) > 0) {
return $tmp;
} else {
$this->server->fault('no_free_ip', 'There is no free IP available.');
}
}
//* Add a openvz ip record
public function openvz_ip_add($session_id, $client_id, $params)
{
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
return $this->insertQuery('../vm/form/openvz_ip.tform.php',$client_id,$params);
}
//* Update openvz ip record
public function openvz_ip_update($session_id, $client_id, $ip_id, $params)
{
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$affected_rows = $this->updateQuery('../vm/form/openvz_ip.tform.php',$client_id,$ip_id,$params);
return $affected_rows;
}
//* Delete openvz ip record
public function openvz_ip_delete($session_id, $ip_id)
{
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$affected_rows = $this->deleteQuery('../vm/form/openvz_ip.tform.php',$ip_id);
return $affected_rows;
}
//* Get OpenVZ vm details
public function openvz_vm_get($session_id, $vm_id)
{
global $app;
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$app->uses('remoting_lib');
$app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
return $app->remoting_lib->getDataRecord($vm_id);
}
//* Get OpenVZ list
public function openvz_vm_get_by_client($session_id, $client_id)
{
global $app;
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
if (!empty($client_id)) {
$client_id = intval($client_id);
$tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = $client_id");
$sql = "SELECT * FROM openvz_vm WHERE sys_groupid = ".intval($tmp['groupid']);
$result = $app->db->queryAllRecords($sql);
return $result;
}
return false;
}
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
//* Add a openvz vm record
public function openvz_vm_add($session_id, $client_id, $params)
{
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
return $this->insertQuery('../vm/form/openvz_vm.tform.php',$client_id,$params);
}
//* Add a openvz vm record from template
public function openvz_vm_add_from_template($session_id, $client_id, $ostemplate_id, $template_id, $override_params = array())
{
global $app;
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$template_id = intval($template_id);
$ostemplate_id = intval($ostemplate_id);
//* Verify parameters
if($template_id == 0) {
$this->server->fault('template_id_error', 'Template ID must be > 0.');
return false;
}
if($ostemplate_id == 0) {
$this->server->fault('ostemplate_id_error', 'OSTemplate ID must be > 0.');
return false;
}
// Verify if template and ostemplate exist
$tmp = $app->db->queryOneRecord("SELECT template_id FROM openvz_template WHERE template_id = $template_id");
if(!is_array($tmp)) {
$this->server->fault('template_id_error', 'Template does not exist.');
return false;
}
$tmp = $app->db->queryOneRecord("SELECT ostemplate_id FROM openvz_ostemplate WHERE ostemplate_id = $ostemplate_id");
if(!is_array($tmp)) {
$this->server->fault('ostemplate_id_error', 'OSTemplate does not exist.');
return false;
}
//* Get the template
$vtpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = $template_id");
//* Get the IP address and server_id
if($override_params['server_id'] > 0) {
$vmip = $app->db->queryOneRecord("SELECT ip_address_id, server_id, ip_address FROM openvz_ip WHERE reserved = 'n' AND vm_id = 0 AND server_id = ".$override_params['server_id']." LIMIT 0,1");
} else {
$vmip = $app->db->queryOneRecord("SELECT ip_address_id, server_id, ip_address FROM openvz_ip WHERE reserved = 'n' AND vm_id = 0 LIMIT 0,1");
}
if(!is_array($vmip)) {
$this->server->fault('vm_ip_error', 'Unable to get a free VM IP.');
return false;
}
//* Build the $params array
$params = array();
$params['server_id'] = $vmip['server_id'];
$params['ostemplate_id'] = $ostemplate_id;
$params['template_id'] = $template_id;
$params['ip_address'] = $vmip['ip_address'];
$params['hostname'] = (isset($override_params['hostname']))?$override_params['hostname']:$vtpl['hostname'];
$params['vm_password'] = (isset($override_params['vm_password']))?$override_params['vm_password']:$app->auth->get_random_password(10);
$params['start_boot'] = (isset($override_params['start_boot']))?$override_params['start_boot']:'y';
$params['active'] = (isset($override_params['active']))?$override_params['active']:'y';
$params['active_until_date'] = (isset($override_params['active_until_date']))?$override_params['active_until_date']:'0000-00-00';
$params['description'] = (isset($override_params['description']))?$override_params['description']:'';
//* The next params get filled with pseudo values, as the get replaced
//* by the openvz event plugin anyway with values from the template
$params['veid'] = 1;
$params['diskspace'] = 1;
$params['ram'] = 1;
$params['ram_burst'] = 1;
$params['cpu_units'] = 1;
$params['cpu_num'] = 1;
$params['cpu_limit'] = 1;
$params['io_priority'] = 1;
$params['nameserver'] = '8.8.8.8 8.8.4.4';
$params['create_dns'] = 'n';
$params['capability'] = '';
return $this->insertQuery('../vm/form/openvz_vm.tform.php',$client_id,$params,'vm:openvz_vm:on_after_insert');
}
//* Update openvz vm record
public function openvz_vm_update($session_id, $client_id, $vm_id, $params)
{
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$affected_rows = $this->updateQuery('../vm/form/openvz_vm.tform.php',$client_id,$vm_id,$params,'vm:openvz_vm:on_after_update');
return $affected_rows;
}
//* Delete openvz vm record
public function openvz_vm_delete($session_id, $vm_id)
{
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$affected_rows = $this->deleteQuery('../vm/form/openvz_vm.tform.php',$vm_id,'vm:openvz_vm:on_after_delete');
return $affected_rows;
}
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
//* Start VM
public function openvz_vm_start($session_id, $vm_id)
{
global $app;
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$app->uses('remoting_lib');
$app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
$vm = $app->remoting_lib->getDataRecord($vm_id);
if(!is_array($vm)) {
$this->server->fault('action_pending', 'No VM with this ID available.');
return false;
}
if($vm['active'] == 'n') {
$this->server->fault('action_pending', 'VM is not in active state.');
return false;
}
$action = 'openvz_start_vm';
$tmp = $app->db->queryOneRecord("SELECT count(action_id) as actions FROM sys_remoteaction
WHERE server_id = '".$vm['server_id']."'
AND action_type = '$action'
AND action_param = '".$vm['veid']."'
AND action_state = 'pending'");
if($tmp['actions'] > 0) {
$this->server->fault('action_pending', 'There is already a action pending for this VM.');
return false;
} else {
$sql = "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
"VALUES (".
(int)$vm['server_id'] . ", ".
time() . ", ".
"'".$action."', ".
$vm['veid'].", ".
"'pending', ".
"''".
")";
$app->db->query($sql);
}
}
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
//* Stop VM
public function openvz_vm_stop($session_id, $vm_id)
{
global $app;
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$app->uses('remoting_lib');
$app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
$vm = $app->remoting_lib->getDataRecord($vm_id);
if(!is_array($vm)) {
$this->server->fault('action_pending', 'No VM with this ID available.');
return false;
}
if($vm['active'] == 'n') {
$this->server->fault('action_pending', 'VM is not in active state.');
return false;
}
$action = 'openvz_stop_vm';
$tmp = $app->db->queryOneRecord("SELECT count(action_id) as actions FROM sys_remoteaction
WHERE server_id = '".$vm['server_id']."'
AND action_type = '$action'
AND action_param = '".$vm['veid']."'
AND action_state = 'pending'");
if($tmp['actions'] > 0) {
$this->server->fault('action_pending', 'There is already a action pending for this VM.');
return false;
} else {
$sql = "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
"VALUES (".
(int)$vm['server_id'] . ", ".
time() . ", ".
"'".$action."', ".
$vm['veid'].", ".
"'pending', ".
"''".
")";
$app->db->query($sql);
}
}
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
//* Restart VM
public function openvz_vm_restart($session_id, $vm_id)
{
global $app;
if(!$this->checkPerm($session_id, 'vm_openvz')) {
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
return false;
}
$app->uses('remoting_lib');
$app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
$vm = $app->remoting_lib->getDataRecord($vm_id);
if(!is_array($vm)) {
$this->server->fault('action_pending', 'No VM with this ID available.');
return false;
}
if($vm['active'] == 'n') {
$this->server->fault('action_pending', 'VM is not in active state.');
return false;
}
$action = 'openvz_restart_vm';
$tmp = $app->db->queryOneRecord("SELECT count(action_id) as actions FROM sys_remoteaction
WHERE server_id = '".$vm['server_id']."'
AND action_type = '$action'
AND action_param = '".$vm['veid']."'
AND action_state = 'pending'");
if($tmp['actions'] > 0) {
$this->server->fault('action_pending', 'There is already a action pending for this VM.');
return false;
} else {
$sql = "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
"VALUES (".
(int)$vm['server_id'] . ", ".
time() . ", ".
"'".$action."', ".
$vm['veid'].", ".
"'pending', ".
"''".
")";
$app->db->query($sql);
}
}