]>
git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/API2/HA/Resources.pm
1 package PVE
::API2
::HA
::Resources
;
7 use PVE
::Tools
qw(extract_param);
10 use PVE
::HA
::Resources
;
11 use HTTP
::Status
qw(:constants);
12 use Storable
qw(dclone);
13 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::RPCEnvironment
;
19 use base
qw(PVE::RESTHandler);
21 # fixme: use cfs_read_file
23 my $resource_type_enum = PVE
::HA
::Resources-
>lookup_types();
25 my $api_copy_config = sub {
28 die "no such resource '$sid'\n" if !$cfg->{ids
}->{$sid};
30 my $scfg = dclone
($cfg->{ids
}->{$sid});
32 $scfg->{digest
} = $cfg->{digest
};
37 __PACKAGE__-
>register_method ({
41 description
=> "List HA resources.",
43 check
=> ['perm', '/', [ 'Sys.Audit' ]],
46 additionalProperties
=> 0,
49 description
=> "Only list resources of specific type",
51 enum
=> $resource_type_enum,
60 properties
=> { sid
=> { type
=> 'string'} },
62 links
=> [ { rel
=> 'child', href
=> "{sid}" } ],
67 my $cfg = PVE
::HA
::Config
::read_resources_config
();
68 my $groups = PVE
::HA
::Config
::read_group_config
();
71 foreach my $sid (keys %{$cfg->{ids
}}) {
72 my $scfg = &$api_copy_config($cfg, $sid);
73 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
74 if ($scfg->{group
} && !$groups->{ids
}->{$scfg->{group
}}) {
75 $scfg->{errors
}->{group
} = "group '$scfg->{group}' does not exist";
83 __PACKAGE__-
>register_method ({
88 check
=> ['perm', '/', [ 'Sys.Audit' ]],
90 description
=> "Read resource configuration.",
92 additionalProperties
=> 0,
94 sid
=> get_standard_option
('pve-ha-resource-or-vm-id',
95 { completion
=> \
&PVE
::HA
::Tools
::complete_sid
}),
102 my $cfg = PVE
::HA
::Config
::read_resources_config
();
104 my $sid = PVE
::HA
::Tools
::parse_sid
($param->{sid
});
106 return &$api_copy_config($cfg, $sid);
109 __PACKAGE__-
>register_method ({
115 check
=> ['perm', '/', [ 'Sys.Console' ]],
117 description
=> "Create a new HA resource.",
118 parameters
=> PVE
::HA
::Resources-
>createSchema(),
119 returns
=> { type
=> 'null' },
123 # create /etc/pve/ha directory
124 PVE
::Cluster
::check_cfs_quorum
();
125 mkdir("/etc/pve/ha");
127 my ($sid, $type, $name) = PVE
::HA
::Tools
::parse_sid
(extract_param
($param, 'sid'));
129 if (my $param_type = extract_param
($param, 'type')) {
130 # useless, but do it anyway
131 die "types does not match\n" if $param_type ne $type;
134 my $plugin = PVE
::HA
::Resources-
>lookup($type);
135 $plugin->verify_name($name);
137 $plugin->exists($name);
139 my $opts = $plugin->check_config($sid, $param, 1, 1);
141 PVE
::HA
::Config
::lock_ha_domain
(
144 my $cfg = PVE
::HA
::Config
::read_resources_config
();
146 if ($cfg->{ids
}->{$sid}) {
147 die "resource ID '$sid' already defined\n";
150 $cfg->{ids
}->{$sid} = $opts;
152 PVE
::HA
::Config
::write_resources_config
($cfg)
154 }, "create resource failed");
159 __PACKAGE__-
>register_method ({
164 description
=> "Update resource configuration.",
166 check
=> ['perm', '/', [ 'Sys.Console' ]],
168 parameters
=> PVE
::HA
::Resources-
>updateSchema(),
169 returns
=> { type
=> 'null' },
173 my $digest = extract_param
($param, 'digest');
174 my $delete = extract_param
($param, 'delete');
176 my ($sid, $type, $name) = PVE
::HA
::Tools
::parse_sid
(extract_param
($param, 'sid'));
178 if (my $param_type = extract_param
($param, 'type')) {
179 # useless, but do it anyway
180 die "types does not match\n" if $param_type ne $type;
183 PVE
::HA
::Config
::lock_ha_domain
(
186 my $cfg = PVE
::HA
::Config
::read_resources_config
();
188 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
190 my $scfg = $cfg->{ids
}->{$sid} ||
191 die "no such resource '$sid'\n";
193 my $plugin = PVE
::HA
::Resources-
>lookup($scfg->{type
});
194 my $opts = $plugin->check_config($sid, $param, 0, 1);
196 foreach my $k (%$opts) {
197 $scfg->{$k} = $opts->{$k};
201 my $options = $plugin->private()->{options
}->{$type};
202 foreach my $k (PVE
::Tools
::split_list
($delete)) {
203 my $d = $options->{$k} ||
204 die "no such option '$k'\n";
205 die "unable to delete required option '$k'\n"
207 die "unable to delete fixed option '$k'\n"
213 PVE
::HA
::Config
::write_resources_config
($cfg)
215 }, "update resource failed");
220 __PACKAGE__-
>register_method ({
225 description
=> "Delete resource configuration.",
227 check
=> ['perm', '/', [ 'Sys.Console' ]],
230 additionalProperties
=> 0,
232 sid
=> get_standard_option
('pve-ha-resource-or-vm-id',
233 { completion
=> \
&PVE
::HA
::Tools
::complete_sid
}),
236 returns
=> { type
=> 'null' },
240 my ($sid, $type, $name) = PVE
::HA
::Tools
::parse_sid
(extract_param
($param, 'sid'));
242 PVE
::HA
::Config
::service_is_ha_managed
($sid);
244 PVE
::HA
::Config
::lock_ha_domain
(
247 my $cfg = PVE
::HA
::Config
::read_resources_config
();
249 delete $cfg->{ids
}->{$sid};
251 PVE
::HA
::Config
::write_resources_config
($cfg)
253 }, "delete resource failed");
258 __PACKAGE__-
>register_method ({
261 path
=> '{sid}/migrate',
263 description
=> "Request resource migration (online) to another node.",
265 check
=> ['perm', '/', [ 'Sys.Console' ]],
268 additionalProperties
=> 0,
270 sid
=> get_standard_option
('pve-ha-resource-or-vm-id',
271 { completion
=> \
&PVE
::HA
::Tools
::complete_sid
}),
272 node
=> get_standard_option
('pve-node',
273 { completion
=> \
&PVE
::Cluster
::get_nodelist
}),
276 returns
=> { type
=> 'null' },
280 my ($sid, $type, $name) = PVE
::HA
::Tools
::parse_sid
(extract_param
($param, 'sid'));
282 PVE
::HA
::Config
::service_is_ha_managed
($sid);
284 PVE
::HA
::Config
::queue_crm_commands
("migrate $sid $param->{node}");
289 __PACKAGE__-
>register_method ({
292 path
=> '{sid}/relocate',
294 description
=> "Request resource relocatzion to another node. This stops the service on the old node, and restarts it on the target node.",
296 check
=> ['perm', '/', [ 'Sys.Console' ]],
299 additionalProperties
=> 0,
301 sid
=> get_standard_option
('pve-ha-resource-or-vm-id',
302 { completion
=> \
&PVE
::HA
::Tools
::complete_sid
}),
303 node
=> get_standard_option
('pve-node',
304 { completion
=> \
&PVE
::Cluster
::get_nodelist
}),
307 returns
=> { type
=> 'null' },
311 my ($sid, $type, $name) = PVE
::HA
::Tools
::parse_sid
(extract_param
($param, 'sid'));
313 PVE
::HA
::Config
::service_is_ha_managed
($sid);
315 PVE
::HA
::Config
::queue_crm_commands
("relocate $sid $param->{node}");