[pve-devel] [PATCH 7/9] Move LXC start and stop from API2 to LXC.pm for reuse.
Wolfgang Link
w.link at proxmox.com
Fri Jul 17 09:26:49 CEST 2015
Signed-off-by: Wolfgang Link <w.link at proxmox.com>
---
src/PVE/API2/LXC.pm | 98 ++-----------------------------------------------
src/PVE/LXC.pm | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 107 insertions(+), 95 deletions(-)
diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index daf68ca..4e50b78 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -20,20 +20,6 @@ use base qw(PVE::RESTHandler);
use Data::Dumper; # fixme: remove
-my $get_container_storage = sub {
- my ($stcfg, $vmid, $lxc_conf) = @_;
-
- if (my $volid = $lxc_conf->{'pve.volid'}) {
- my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
- return wantarray ? ($sid, $volname) : $sid;
- } else {
- my $path = $lxc_conf->{'lxc.rootfs'};
- my ($vtype, $volid) = PVE::Storage::path_to_volume_id($stcfg, $path);
- my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid;
- return wantarray ? ($sid, $volname, $path) : $sid;
- }
-};
-
my $check_ct_modify_config_perm = sub {
my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
@@ -553,7 +539,7 @@ __PACKAGE__->register_method({
my $stcfg = PVE::Cluster::cfs_read_file("storage.cfg");
- my ($sid, undef, $path) = &$get_container_storage($stcfg, $param->{vmid}, $lxc_conf);
+ my ($sid, undef, $path) = PVE::LXC::get_container_storage($stcfg, $param->{vmid}, $lxc_conf);
$conf->{storage} = $sid || $path;
return $conf;
@@ -903,48 +889,7 @@ __PACKAGE__->register_method({
my $vmid = extract_param($param, 'vmid');
- die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
-
- if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
-
- my $hacmd = sub {
- my $upid = shift;
-
- my $service = "ct:$vmid";
-
- my $cmd = ['ha-manager', 'enable', $service];
-
- print "Executing HA start for CT $vmid\n";
-
- PVE::Tools::run_command($cmd);
-
- return;
- };
-
- return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
-
- } else {
-
- my $realcmd = sub {
- my $upid = shift;
-
- syslog('info', "starting CT $vmid: $upid\n");
-
- my $conf = PVE::LXC::load_config($vmid);
- my $stcfg = cfs_read_file("storage.cfg");
- if (my $sid = &$get_container_storage($stcfg, $vmid, $conf)) {
- PVE::Storage::activate_storage($stcfg, $sid);
- }
-
- my $cmd = ['lxc-start', '-n', $vmid];
-
- run_command($cmd);
-
- return;
- };
-
- return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
- }
+ PVE::LXC::lxc_start($vmid, $authuser, $rpcenv);
}});
__PACKAGE__->register_method({
@@ -977,43 +922,8 @@ __PACKAGE__->register_method({
my $node = extract_param($param, 'node');
my $vmid = extract_param($param, 'vmid');
-
- die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
-
- if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
-
- my $hacmd = sub {
- my $upid = shift;
-
- my $service = "ct:$vmid";
-
- my $cmd = ['ha-manager', 'disable', $service];
-
- print "Executing HA stop for CT $vmid\n";
-
- PVE::Tools::run_command($cmd);
-
- return;
- };
-
- return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
-
- } else {
-
- my $realcmd = sub {
- my $upid = shift;
-
- syslog('info', "stoping CT $vmid: $upid\n");
-
- my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
-
- run_command($cmd);
-
- return;
- };
-
- return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
- }
+
+ PVE::LXC::lxc_stop($vmid, $authuser, $rpcenv);
}});
__PACKAGE__->register_method({
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 668d0d2..f04db22 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -12,7 +12,7 @@ use PVE::Storage;
use PVE::SafeSyslog;
use PVE::INotify;
use PVE::JSONSchema qw(get_standard_option);
-use PVE::Tools qw($IPV6RE $IPV4RE);
+use PVE::Tools qw($IPV6RE $IPV4RE run_command);
use PVE::Network;
use Data::Dumper;
@@ -1618,4 +1618,106 @@ sub snapshot_rollback {
die "Not implemented\n";
}
+
+sub lxc_stop {
+ my ($vmid, $authuser, $rpcenv) = @_;
+
+ die "CT $vmid not running\n" if !check_running($vmid);
+
+ if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
+
+ my $hacmd = sub {
+ my $upid = shift;
+
+ my $service = "ct:$vmid";
+
+ my $cmd = ['ha-manager', 'disable', $service];
+
+ print "Executing HA stop for CT $vmid\n";
+
+ PVE::Tools::run_command($cmd);
+
+ return;
+ };
+
+ return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
+
+ } else {
+
+ my $realcmd = sub {
+ my $upid = shift;
+
+ syslog('info', "stoping CT $vmid: $upid\n");
+
+ my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
+
+ run_command($cmd);
+
+ return;
+ };
+
+ return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
+ }
+}
+
+sub lxc_start {
+ my ($vmid, $authuser, $rpcenv) = @_;
+
+ die "CT $vmid already running\n" if check_running($vmid);
+
+ if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
+
+ my $hacmd = sub {
+ my $upid = shift;
+
+ my $service = "ct:$vmid";
+
+ my $cmd = ['ha-manager', 'enable', $service];
+
+ print "Executing HA start for CT $vmid\n";
+
+ PVE::Tools::run_command($cmd);
+
+ return;
+ };
+
+ return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
+
+ } else {
+
+ my $realcmd = sub {
+ my $upid = shift;
+
+ syslog('info', "starting CT $vmid: $upid\n");
+
+ my $conf = PVE::LXC::load_config($vmid);
+ my $stcfg = cfs_read_file("storage.cfg");
+ if (my $sid = get_container_storage($stcfg, $vmid, $conf)) {
+ PVE::Storage::activate_storage($stcfg, $sid);
+ }
+
+ my $cmd = ['lxc-start', '-n', $vmid];
+
+ run_command($cmd);
+
+ return;
+ };
+
+ return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
+ }
+}
+
+sub get_container_storage {
+ my ($stcfg, $vmid, $lxc_conf) = @_;
+
+ if (my $volid = $lxc_conf->{'pve.volid'}) {
+ my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
+ return wantarray ? ($sid, $volname) : $sid;
+ } else {
+ my $path = $lxc_conf->{'lxc.rootfs'};
+ my ($vtype, $volid) = PVE::Storage::path_to_volume_id($stcfg, $path);
+ my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid;
+ return wantarray ? ($sid, $volname, $path) : $sid;
+ }
+}
1;
--
2.1.4
More information about the pve-devel
mailing list