Compare commits
24 Commits
Author | SHA1 | Date |
---|---|---|
Denis Knauf | f780eccc6a | |
Denis Knauf | 5eb6cd52eb | |
Denis Knauf | d6e7d84afc | |
Denis Knauf | e2b38ca779 | |
Denis Knauf | 82b3d3cda2 | |
Denis Knauf | 6530c40749 | |
Denis Knauf | 83211a817f | |
Denis Knauf | 20db6bd9b2 | |
Denis Knauf | d558af77b6 | |
Denis Knauf | 93889f9dab | |
Denis Knauf | 4f6479a0d2 | |
Denis Knauf | 8994faad9e | |
Denis Knauf | efe4f41eda | |
Denis Knauf | ce7a95945c | |
Denis Knauf | b2d78971b0 | |
Denis Knauf | 45646994ff | |
Denis Knauf | 3d1a671134 | |
Denis Knauf | 4148b86e6e | |
Denis Knauf | 3ea52e1f72 | |
Denis Knauf | ca44934546 | |
Denis Knauf | 38ea2125ff | |
Denis Knauf | 890aa2a389 | |
Denis Knauf | db6e06c1ef | |
Denis Knauf | c7c9bf4800 |
|
@ -1,2 +1,4 @@
|
|||
*.sw[pomnqrst]
|
||||
*.gem
|
||||
Gemfile.lock
|
||||
.rake_tasks
|
||||
|
|
70
Gemfile.lock
70
Gemfile.lock
|
@ -1,70 +0,0 @@
|
|||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
pve (0.1.1)
|
||||
activesupport (>= 2)
|
||||
dencli (~> 0.3.1)
|
||||
ipaddress (~> 0.8.3)
|
||||
pmap (~> 1.1)
|
||||
rest-client (~> 2.1)
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
activesupport (6.1.3.1)
|
||||
concurrent-ruby (~> 1.0, >= 1.0.2)
|
||||
i18n (>= 1.6, < 2)
|
||||
minitest (>= 5.1)
|
||||
tzinfo (~> 2.0)
|
||||
zeitwerk (~> 2.3)
|
||||
concurrent-ruby (1.1.8)
|
||||
dencli (0.3.1)
|
||||
diff-lcs (1.4.4)
|
||||
domain_name (0.5.20190701)
|
||||
unf (>= 0.0.5, < 1.0.0)
|
||||
http-accept (1.7.0)
|
||||
http-cookie (1.0.3)
|
||||
domain_name (~> 0.5)
|
||||
i18n (1.8.10)
|
||||
concurrent-ruby (~> 1.0)
|
||||
ipaddress (0.8.3)
|
||||
mime-types (3.3.1)
|
||||
mime-types-data (~> 3.2015)
|
||||
mime-types-data (3.2021.0225)
|
||||
minitest (5.14.4)
|
||||
netrc (0.11.0)
|
||||
pmap (1.1.1)
|
||||
rest-client (2.1.0)
|
||||
http-accept (>= 1.7.0, < 2.0)
|
||||
http-cookie (>= 1.0.2, < 2.0)
|
||||
mime-types (>= 1.16, < 4.0)
|
||||
netrc (~> 0.8)
|
||||
rspec (3.10.0)
|
||||
rspec-core (~> 3.10.0)
|
||||
rspec-expectations (~> 3.10.0)
|
||||
rspec-mocks (~> 3.10.0)
|
||||
rspec-core (3.10.1)
|
||||
rspec-support (~> 3.10.0)
|
||||
rspec-expectations (3.10.1)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.10.0)
|
||||
rspec-mocks (3.10.2)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.10.0)
|
||||
rspec-support (3.10.2)
|
||||
tzinfo (2.0.4)
|
||||
concurrent-ruby (~> 1.0)
|
||||
unf (0.1.4)
|
||||
unf_ext
|
||||
unf_ext (0.0.7.7)
|
||||
zeitwerk (2.4.2)
|
||||
|
||||
PLATFORMS
|
||||
x86_64-linux
|
||||
|
||||
DEPENDENCIES
|
||||
pve!
|
||||
rspec (~> 3.2)
|
||||
|
||||
BUNDLED WITH
|
||||
2.2.15
|
|
@ -0,0 +1,661 @@
|
|||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU Affero General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
12
README.adoc
12
README.adoc
|
@ -14,6 +14,18 @@ You need to provide a config-file `/etc/pve/pvecli.yml`:
|
|||
connect:
|
||||
verify_tls: no if you do not use known CA-signed X509-Certificates
|
||||
|
||||
Featurs
|
||||
=======
|
||||
|
||||
The library provides an interface to interact with PVE-servers.
|
||||
Provided abstractions for:
|
||||
|
||||
* Node
|
||||
* LXC (create, delete, modify, use)
|
||||
* Qemu (create, delete, modify, use)
|
||||
* Storages (list, list content)
|
||||
* Appliances (list, download to storage)
|
||||
|
||||
pvecli
|
||||
======
|
||||
|
||||
|
|
497
lib/pve/cli.rb
497
lib/pve/cli.rb
|
@ -9,11 +9,87 @@ require_relative 'cli/ha'
|
|||
require_relative 'cli/task'
|
||||
require_relative 'cli/qm'
|
||||
require_relative 'cli/node'
|
||||
require_relative 'cli/storage'
|
||||
|
||||
class UsageError <RuntimeError
|
||||
class DenCli::Sub
|
||||
def provide_help name: nil, aliases: nil, min: nil
|
||||
base = self
|
||||
name = :help if name.nil?
|
||||
aliases = %w[-h --help] if aliases.nil?
|
||||
min = 1 if min.nil?
|
||||
#p name: name, aliases: aliases, min: min
|
||||
cmd( name, '', aliases: aliases, min: min) {|*args| help *args }
|
||||
end
|
||||
end
|
||||
|
||||
class PVE::Cli
|
||||
class RuntimeError <::RuntimeError
|
||||
end
|
||||
|
||||
class UsageError <RuntimeError
|
||||
end
|
||||
def UsageError msg
|
||||
raise UsageError, msg
|
||||
end
|
||||
|
||||
class OperationalError <RuntimeError
|
||||
end
|
||||
def OperationalError msg
|
||||
raise OperationalError, msg
|
||||
end
|
||||
|
||||
class ConditionViolation <RuntimeError
|
||||
end
|
||||
def ConditionViolation msg
|
||||
raise ConditionViolation, msg
|
||||
end
|
||||
|
||||
class Output
|
||||
LINERESET = "\r\e[J"
|
||||
RESET = "\e[0m"
|
||||
SUCCESS_ = "\e[32;1m✓"
|
||||
SUCCESS = "#{LINERESET}[%s] #{SUCCESS_}#{RESET} "
|
||||
FAILED_ = "\e[31;1m✗"
|
||||
FAILED = "#{LINERESET}[%s] #{FAILED_}#{RESET} "
|
||||
PROGRESS_ = "\e[33;1m%s"
|
||||
PROGRESS = "#{LINERESET}[%s] #{PROGRESS_}#{RESET} "
|
||||
INFO_ = "\e[33;1m•"
|
||||
INFO = "#{LINERESET}[%s] #{INFO_}#{RESET} "
|
||||
|
||||
attr_accessor :host
|
||||
|
||||
def initialize host
|
||||
@host, @spinners, @spin = host, "▖▘▝▗", 0
|
||||
end
|
||||
|
||||
def progress format, *args, **kwargs
|
||||
STDERR.printf PROGRESS, @host, @spinners[@spin = (@spin + 1) % 4]
|
||||
STDERR.printf( format, *args, **kwargs)
|
||||
end
|
||||
|
||||
def info format, *args, **kwargs
|
||||
STDERR.printf INFO, @host
|
||||
STDERR.printf( format, *args, **kwargs)
|
||||
STDERR.puts
|
||||
end
|
||||
|
||||
def failed format, *args, **kwargs
|
||||
STDERR.printf FAILED, @host
|
||||
STDERR.printf( format, *args, **kwargs)
|
||||
STDERR.puts
|
||||
end
|
||||
|
||||
def success format, *args, **kwargs
|
||||
STDERR.printf SUCCESS, @host
|
||||
STDERR.printf( format, *args, **kwargs)
|
||||
STDERR.puts
|
||||
end
|
||||
|
||||
def reset
|
||||
STDERR.printf LINERESET
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :cfg, :cli
|
||||
|
||||
def connect
|
||||
|
@ -27,10 +103,12 @@ class PVE::Cli
|
|||
def initialize config_file = nil
|
||||
config_file ||= '/etc/pve/pvecli.yml'
|
||||
@cfg =
|
||||
YAML.safe_load File.read( config_file), [], [], false,
|
||||
config_file, symbolize_names: true
|
||||
YAML.safe_load File.read( config_file), permitted_classes: [],
|
||||
permitted_symbols: [], aliases: false, filename: config_file,
|
||||
symbolize_names: true
|
||||
@cli = DenCli.new File.basename($0), "PVE CLI"
|
||||
@interactive = false
|
||||
@out = Output.new "<>"
|
||||
prepare
|
||||
end
|
||||
|
||||
|
@ -39,90 +117,103 @@ class PVE::Cli
|
|||
end
|
||||
|
||||
def enter host, *args
|
||||
@out.host = host.name
|
||||
r = host.enter *args
|
||||
STDERR.puts "! #{$?.exitstatus}" unless r
|
||||
operational_error "Exitstatus: #{$?.exitstatus}" unless r
|
||||
end
|
||||
|
||||
def wait_state host, state, timeout: nil, lock: nil
|
||||
spinners = %w[- / | \\]
|
||||
spin = 0
|
||||
@out.host = host.name
|
||||
r =
|
||||
host.wait state, lock: lock, timeout: timeout, secs: 0.2 do |state, lock|
|
||||
lock = " (locked: #{lock})" if lock
|
||||
STDERR.printf "\r[%s] %s Still %s%s...\e[J\n", host.name, spinners[spin = (spin + 1) % 4], state, lock
|
||||
@out.progress "Still %s%s...", state, lock ? " (locked: #{lock})" : ''
|
||||
end
|
||||
STDERR.printf "\r\e[J"
|
||||
exit 1 unless interactive? and r
|
||||
@out.reset
|
||||
exit 1 if interactive? and !r
|
||||
end
|
||||
|
||||
def wait task, secs: nil, text: nil
|
||||
def task_log task, logn, limit = 1024
|
||||
log = task.log start: logn, limit: limit
|
||||
log = [] if [{n: 1, t: 'no content'}] == log
|
||||
unless log.empty?
|
||||
@out.reset
|
||||
log.each {|l| puts l[:t] }
|
||||
logn = log.last[:n]
|
||||
end
|
||||
logn
|
||||
end
|
||||
|
||||
def wait task, secs: nil, text: nil, timeout: nil
|
||||
secs ||= 0.1
|
||||
spinners, spin, logn = "▖▘▝▗", 0, 0
|
||||
STDERR.puts task.upid
|
||||
host = task.host&.name
|
||||
logn = 0
|
||||
STDOUT.puts task.upid
|
||||
@out.host = host = task.host&.name || '<>'
|
||||
loop do
|
||||
s = task.status
|
||||
if s[:exitstatus]
|
||||
STDERR.printf "\r[%s] %s %s %s\e[J\n",
|
||||
host || s[:id],
|
||||
case s[:exitstatus]
|
||||
when "OK" then "\e[32;1m✓\e[0m"
|
||||
else "\e[31;1m✗\e[0m"
|
||||
end,
|
||||
text && "#{text}:",
|
||||
s[:status] == 'stopped' ? :finished : s[:status]
|
||||
return task
|
||||
logn = self.task_log task, logn
|
||||
if s.finished?
|
||||
loop do
|
||||
r = self.task_log task, logn
|
||||
break if 0 == logn - r
|
||||
logn = r
|
||||
end
|
||||
if s.successfull?
|
||||
@out.success "%s %s", text && "#{text}:", s.stopped? ? :finished : s.status
|
||||
else
|
||||
@out.failed "%s %s", text && "#{text}:", s.stopped? ? :finished : s.status
|
||||
end
|
||||
return s
|
||||
end
|
||||
log = task.log start: logn
|
||||
log = [] if [{n: 1, t: 'no content'}] == log
|
||||
unless log.empty?
|
||||
STDERR.printf "\r\e[J"
|
||||
log.each {|l| puts l[:t] }
|
||||
logn = log.last[:n]
|
||||
end
|
||||
STDERR.printf "\r[%s] \e[33;1m%s\e[0m %s...\e[J",
|
||||
host || s[:id],
|
||||
spinners[spin = (spin + 1) % 4],
|
||||
text || "Working"
|
||||
@out.progress text || "Working"
|
||||
sleep secs
|
||||
end
|
||||
end
|
||||
|
||||
def start host, node: nil, timeout: nil, fire: nil, secs: nil
|
||||
def migrate host, node, timeout: nil, fire: nil, secs: nil, online: nil
|
||||
timeout ||= 30
|
||||
if node
|
||||
task = host.migrate Proxmox::Node.find_by_name!( node)
|
||||
wait task, text: "Migrating"
|
||||
end
|
||||
task = host.migrate Proxmox::Node.find_by_name!( node), online: online || false
|
||||
wait task, text: "Migrating", timeout: timeout if fire
|
||||
end
|
||||
|
||||
def start host, node: nil, timeout: nil, fire: nil, secs: nil
|
||||
timeout ||= 60
|
||||
if host.running?
|
||||
STDERR.puts "Already running."
|
||||
ConditionViolation "Already running."
|
||||
return
|
||||
end
|
||||
task = host.start
|
||||
t = Time.now
|
||||
unless fire
|
||||
wait task, text: "Starting"
|
||||
wait task, text: "Starting", timeout: timeout
|
||||
wait_state host, :running, timeout: timeout-(Time.now-t) if host.ha.exist? and not host.running?
|
||||
end
|
||||
end
|
||||
|
||||
def stop host, timeout: nil, fire: nil, secs: nil
|
||||
timeout ||= 30
|
||||
if host.stopped?
|
||||
STDERR.puts "Already stopped."
|
||||
preset_error "Already stopped."
|
||||
return
|
||||
end
|
||||
task = host.stop
|
||||
t = Time.now
|
||||
unless fire
|
||||
wait task, text: "Stopping"
|
||||
wait task, text: "Stopping", timeout: timeout
|
||||
wait_state host, :stopped, timeout: timeout-(Time.now-t) if host.ha.exist? and not host.stopped?
|
||||
end
|
||||
end
|
||||
|
||||
def create klass, template, timeout: nil, fire: nil, secs: nil, start: nil, **options
|
||||
def create klass, template, timeout: nil, fire: nil, start: nil, **options
|
||||
options[:start] = fire && start
|
||||
task = klass.create template, **options
|
||||
return if fire
|
||||
wait task, text: "Creating"
|
||||
host = task.host.refresh!
|
||||
start host, timeout: timeout, secs: secs if start
|
||||
status = wait task, text: "Creating"
|
||||
if status.successfull?
|
||||
host = task.host.refresh!
|
||||
start host, timeout: timeout, secs: secs if start
|
||||
elsif not interactive?
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
def destroy ct, timeout: nil, fire: nil, secs: nil
|
||||
|
@ -132,6 +223,193 @@ class PVE::Cli
|
|||
end
|
||||
end
|
||||
|
||||
def rbd_device_map image_spec, &exe
|
||||
dev = IO.popen( [*%w[rbd device map], image_spec]) {|io| io.read }.chomp
|
||||
raise OperationalError, "Device mapping #{image_spec} localy failed." unless $?.success?
|
||||
begin
|
||||
yield dev
|
||||
ensure
|
||||
system *%w[rbd device unmap], image_spec
|
||||
raise OperationalError, "Device unmapping #{image_spec} failed." unless $?.success?
|
||||
end
|
||||
end
|
||||
|
||||
def rbd_devices_maps spec, *specs, &exe
|
||||
rbd_device_map spec do |dev|
|
||||
if specs.empty?
|
||||
yield dev
|
||||
else
|
||||
rbd_devices_maps *specs do |*devs|
|
||||
yield dev, *devs
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def mount dev, mp, &exe
|
||||
system 'mount', dev.to_s, mp.to_s
|
||||
begin yield
|
||||
ensure
|
||||
# it could be possible, that a process is working in fs, yet.
|
||||
sleep 1
|
||||
system 'umount', mp.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def ct_volume_move ct, disk, destination
|
||||
@out.host = ct.name
|
||||
host = Proxmox::Node.find_by_name! Socket.gethostname
|
||||
unless host.node == ct.node.node
|
||||
ConditionViolation( "CT is hosted on #{ct.node.node}, not local (#{Socket.gethostname})")
|
||||
end
|
||||
# Lock, we do not want changes, while we check/prepare,
|
||||
# these checks could be obsolete while moving.
|
||||
configupdate = {}
|
||||
ct.lock :migrate do
|
||||
disk =
|
||||
case disk
|
||||
when 'rootfs', /\Amp\d+\z/ then disk.to_sym
|
||||
else UsageError( "Unknown disk #{disk}")
|
||||
end
|
||||
ctcnf = ct.config
|
||||
UsageError( "CT has no disk #{disk}") if ctcnf[disk].nil?
|
||||
UsageError( "CT not stopped, yet.") unless ct.stopped?
|
||||
dest = host.storage.find {|x| destination == x.storage }
|
||||
ConditionViolation( "Destination storage #{dest.storage} disabled") unless dest.enabled?
|
||||
ConditionViolation( "Destination storage #{dest.storage} not active") unless dest.active?
|
||||
|
||||
unless /\A([^:]+):([^,]+)(,.+)?\z/ =~ ctcnf[disk]
|
||||
OperationalError( "disk-specification cannot be parsed: [#{disk}: #{ctcnf[disk]}]")
|
||||
end
|
||||
source, name, diskopts = $1, $2, $3.to_s
|
||||
src = host.storage.find {|x| source == x.storage }
|
||||
|
||||
ConditionViolation( "Source storage #{dest.storage} disabled") unless dest.enabled?
|
||||
ConditionViolation( "Source storage #{dest.storage} not active") unless dest.active?
|
||||
case src.type
|
||||
when 'rbd'
|
||||
else ConditionViolation( "Storage type #{src.type} not supported as source. (supported: rbd only, yet)")
|
||||
end
|
||||
#unless c = src.content.find {|x| x.name == name }
|
||||
# usage_error "Source storage #{src.storage} has no disk named #{name}"
|
||||
#end
|
||||
case dest.type
|
||||
when 'rbd'
|
||||
else ConditionViolation( "Storage type #{dest.type} not supported as destination. (supported: rbd only, yet)")
|
||||
end
|
||||
#if c = dest.content.find {|x| x.name == name }
|
||||
# usage_error "Destination storage #{dest.storage} has already a disk named #{name}"
|
||||
#end
|
||||
|
||||
src_image_spec, dest_image_spec = "#{source}/#{name}", "#{destination}/#{name}"
|
||||
mp_path = Pathname.new( "/var/lib/lxc/").join ct.vmid.to_s
|
||||
mp_path.mkdir unless mp_path.exist?
|
||||
src_mp, dest_mp = mp_path + src_image_spec.gsub('/','-'), mp_path + dest_image_spec.gsub('/','-')
|
||||
|
||||
src_mp.mkdir unless src_mp.exist?
|
||||
dest_mp.mkdir unless dest_mp.exist?
|
||||
# We check, if something is mounted already on our mountpoints.
|
||||
# We check it later, too, but later cleaning up would be impossible.
|
||||
# We do not trust only this checks, so we check later, too.
|
||||
ConditionViolation( "Something already mounted at #{src_mp}") if src_mp.mountpoint?
|
||||
ConditionViolation( "Something already mounted at #{dest_mp}") if dest_mp.mountpoint?
|
||||
|
||||
rbd_image_info = JSON.parse IO.popen( [*%w[rbd info --format json], src_image_spec]) {|io| io.read }
|
||||
OperationalError( "Couldn't determine size of #{src_image_spec}.") unless $?.success?
|
||||
|
||||
# checks and preparation done.
|
||||
size = rbd_image_info['size']/1024/1024
|
||||
|
||||
unless system *%w[rbd create -s], size.to_s, dest_image_spec
|
||||
OperationalError( "Creating disk #{dest_image_spec} failed.")
|
||||
end
|
||||
@out.progress "Map devices %s, %s \e[J", ct.name, dest_image_spec, src_image_spec
|
||||
rbd_devices_maps dest_image_spec, src_image_spec do |dest_dev, src_dev|
|
||||
@out.info "Maped source device %s => %s => %s", src_image_spec, src_dev, src_mp
|
||||
@out.info "Maped destination device %s => %s => %s", dest_image_spec, dest_dev, dest_mp
|
||||
@out.info "Formatting destination disk"
|
||||
unless system *%w[mkfs.xfs -mreflink=1 -bsize=4096 -ssize=4096], dest_dev
|
||||
OperationalError( "Formatting #{dest_image_spec} failed.")
|
||||
end
|
||||
OperationalError( "Something already mounted at #{src_mp}") if src_mp.mountpoint?
|
||||
@out.progress "Mounting source disk"
|
||||
mount src_dev, src_mp do
|
||||
OperationalError( "Something already mounted at #{src_mp}") if dest_mp.mountpoint?
|
||||
@out.progress "Mounting destination disk"
|
||||
mount dest_dev, dest_mp do
|
||||
#@out.info "rsyncing..."
|
||||
#system *%w[rsync -aHAX --info=progress2], "#{src_mp}/", "#{dest_mp}/"
|
||||
IO.popen %w[rsync -ahHAX --info=progress2] + ["#{src_mp}/", "#{dest_mp}/"] do |io|
|
||||
io.each_line("\r") {|l| @out.progress 'rsync|%s', l.chomp }
|
||||
end
|
||||
OperationalError( "rsync had an error. [#{$?.exitcode}]") unless $?.success?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
unusedfield = (0..20).map{|i| "unused#{i}" }.find {|n| ctcnf[n].nil? }
|
||||
configupdate = {unusedfield => "#{source}:#{name}", disk => "#{destination}:#{name}#{diskopts}"}
|
||||
end
|
||||
ct.cnfset **configupdate
|
||||
@out.success "disk moved."
|
||||
end
|
||||
|
||||
def node_opt node = nil
|
||||
node ? [Proxmox::Node.find_by_name!( node)] : Proxmox::Node.all
|
||||
end
|
||||
|
||||
def target_opt target = nil, &exe
|
||||
if target
|
||||
target = /\A#{target}\z/
|
||||
lambda {|n| exe.call n if n === target }
|
||||
else
|
||||
exe
|
||||
end
|
||||
end
|
||||
|
||||
def hosting_table target:, status:, sort:, tags:
|
||||
connect
|
||||
to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/% Tags], format: '<<<<<>>>>>><'
|
||||
target &&= /\A#{target}\z/i
|
||||
nottags = nil
|
||||
if tags
|
||||
tags = tags.split /,/
|
||||
nottags = tags.grep /\A-/
|
||||
tags -= nottags
|
||||
nottags.map! {|x| x[1..-1] }
|
||||
end
|
||||
status =
|
||||
case status
|
||||
when /\Asta(?:r(?:t(?:ed?)?)?)?\z/i, /\Aon(?:l(?:i(?:ne?)?)?)?\z/i, /\Ar(?:u(?:n(?:n(?:i(?:ng?)?)?)?)?)?\z/i, '1'
|
||||
%i[started online running]
|
||||
when /\Asto(?:p(?:p(?:ed?)?)?)?\z/i, /\Aof(?:f(?:l(?:i(?:ne?)?)?)?)?\z/i, '0'
|
||||
%i[stopped offline]
|
||||
when nil, '', /\Aa(ll?)?\z/i then nil
|
||||
else
|
||||
usage_error "Unknown state #{status}"
|
||||
end
|
||||
push =
|
||||
begin
|
||||
condition = []
|
||||
condition.push lambda {|n| n === target } if target
|
||||
if status
|
||||
condition.push lambda {|n| !n.respond_to?( :status) or status.include?( n.status) }
|
||||
end
|
||||
if tags
|
||||
condition.push lambda {|n|
|
||||
if n.respond_to?( :tags) and n.tags
|
||||
nt = n.tags
|
||||
[] == tags - nt and nottags == nottags - nt
|
||||
else false
|
||||
end
|
||||
}
|
||||
end
|
||||
lambda {|n| to.push tablized_virt( n) if condition.all? {|c| c.call n} }
|
||||
end
|
||||
yield push
|
||||
to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) }
|
||||
end
|
||||
|
||||
def help cl, *args
|
||||
STDERR.puts cl.help( *args)
|
||||
exit 1 unless interactive?
|
||||
|
@ -144,6 +422,36 @@ class PVE::Cli
|
|||
opt( :fire, "-f", "--[no-]fire", "Do not wait till running", default: false)
|
||||
end
|
||||
|
||||
def complete_lxc f
|
||||
Proxmox::LXC.all.
|
||||
flat_map {|x| [x.name, x.vmid.to_s] }.
|
||||
select {|x| f =~ x }
|
||||
end
|
||||
|
||||
def complete_qemu f
|
||||
Proxmox::Qemu.all.
|
||||
flat_map {|x| [x.name, x.vmid.to_s] }.
|
||||
select {|x| f =~ x }
|
||||
end
|
||||
|
||||
def complete_node f
|
||||
Proxmox::Qemu.all.
|
||||
map {|x| x.name }.
|
||||
select {|x| f =~ x }
|
||||
end
|
||||
|
||||
def completion_helper *pre, arg, &exe
|
||||
if pre.empty?
|
||||
connect
|
||||
xs = yield /\A#{Regexp.quote arg}/
|
||||
STDOUT.print "\a" if xs.empty?
|
||||
xs
|
||||
else
|
||||
STDOUT.print "\a"
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def prepare
|
||||
cli_node
|
||||
cli_ct
|
||||
|
@ -151,14 +459,101 @@ class PVE::Cli
|
|||
cli_task
|
||||
cli_ha
|
||||
cli_base
|
||||
cli_storage
|
||||
end
|
||||
|
||||
def call *argv
|
||||
cli.call *argv
|
||||
rescue RestClient::ExceptionWithResponse
|
||||
STDERR.puts "#$! - #{$!.response} (#{$!.class})" #, $!.backtrace.map {|b|" #{b}"}
|
||||
rescue UsageError, DenCli::UsageError
|
||||
STDERR.puts $!
|
||||
@out.failed "%s: %s - %s (%s)", $!.request, $!, $!.response, $!.class
|
||||
#STDERR.puts $!.backtrace.map {|b|" #{b}"}
|
||||
exit 1
|
||||
rescue DenCli::UsageError, RuntimeError
|
||||
@out.failed "%s", $!
|
||||
exit 1
|
||||
end
|
||||
|
||||
def per_argument arguments, print: nil, &exe
|
||||
arguments.each do |argument|
|
||||
@out.host = argument
|
||||
@out.info "\e[1;34m#{print}\e[0m", argument if print
|
||||
begin
|
||||
yield argument
|
||||
rescue RestClient::ExceptionWithResponse
|
||||
@out.failed "%s: %s - %s", $!.request, $!, JSON.parse( $!.response.body)
|
||||
rescue DenCli::UsageError, RuntimeError
|
||||
@out.failed "%s", $!
|
||||
rescue RestClient::BadRequest
|
||||
@out.failed "%p", $!.message
|
||||
rescue Interrupt
|
||||
@out.failed "Interrupted by user"
|
||||
rescue SystemExit
|
||||
@out.failed "Exitcode: %d", $!.status if 0 < $!.status
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def appliances node, regexp, system, applications
|
||||
system = applications = true if system.nil? and applications.nil?
|
||||
node = node ? Proxmox::Node.find_by_name!( node) : Proxmox::Node.all.first
|
||||
to = TablizedOutput.new %w<Section Package Version OS Template Description>, format: %w[> > > > > <]
|
||||
node.aplinfo.
|
||||
select {|a| 'system' == a.section ? system : applications}.
|
||||
each do |apl|
|
||||
to.push [
|
||||
apl.section,
|
||||
apl.package,
|
||||
apl.version,
|
||||
apl.os,
|
||||
apl.template,
|
||||
apl.description,
|
||||
]
|
||||
end
|
||||
to.print order: [1,2]
|
||||
end
|
||||
|
||||
COLORS = %w[black red green yellow blue magenta cyan white].each_with_index.to_h
|
||||
def fgcolor color
|
||||
color = color.to_s
|
||||
if /\Abright[-_]?(.*)\z/ =~ color
|
||||
c = COLORS[$1]
|
||||
c.nil? ? nil : "1;3#{c}"
|
||||
else
|
||||
c = COLORS[color]
|
||||
c.nil? ? nil : "3#{c}"
|
||||
end
|
||||
end
|
||||
|
||||
def tablized_virt v
|
||||
ha = v.respond_to?( :ha) ? v.ha : nil
|
||||
unknown = TablizedOutput::V.new 0, '-'
|
||||
node = v.node.is_a?(String) ? v.node : v.node.node
|
||||
if color = @cfg[:hosts]&.[](node.to_sym)&.[](:color)
|
||||
node = ColoredString.new node, fgcolor( color)
|
||||
end
|
||||
[
|
||||
case v.status
|
||||
when :running, :online then ColoredString.new v.status, "32"
|
||||
when :stopped, :offline then ColoredString.new v.status, "31"
|
||||
else v.status
|
||||
end,
|
||||
ha&.state || '·',
|
||||
case v.t
|
||||
when "nd" then ColoredString.new v.sid, "33"
|
||||
when "qm" then ColoredString.new v.sid, "35"
|
||||
when "ct" then ColoredString.new v.sid, "36"
|
||||
else v.sid
|
||||
end,
|
||||
v.name, node,
|
||||
v.respond_to?(:uptime) ? TablizedOutput::V.new( v.uptime, Measured.seconds( v.uptime)) : unknown,
|
||||
v.respond_to?(:cpu) ? TablizedOutput::Percentage.new( v.cpu) : unknown,
|
||||
v.respond_to?(:mem) ? TablizedOutput::V.new( v.mem, Measured.bytes( v.mem)) : unknown,
|
||||
v.respond_to?(:maxmem) ? TablizedOutput::Percentage.new( v.mem/v.maxmem.to_f) : unknown,
|
||||
v.respond_to?(:disk) ? TablizedOutput::V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown,
|
||||
if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i
|
||||
TablizedOutput::Percentage.new( v.disk.to_f/v.maxdisk.to_f)
|
||||
else unknown end,
|
||||
v.respond_to?(:tags) ? v.tags.join(', ') : '',
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,26 +15,26 @@ def cli_base
|
|||
each {|c| puts c }
|
||||
}
|
||||
|
||||
cli.cmd( :status, "Lists Nodes/VMs/CTs with status", &lambda {|target=nil, sort: 'n', node: nil|
|
||||
connect
|
||||
node &&= /\A#{node}\z/
|
||||
to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/%]
|
||||
push =
|
||||
if target
|
||||
target = /\A#{target}\z/
|
||||
lambda {|n| to.virt n if n === target }
|
||||
else
|
||||
lambda {|n| to.virt n }
|
||||
end
|
||||
nodes = Proxmox::Node.all
|
||||
nodes.
|
||||
select {|n| not node or n === node }.
|
||||
flat_map {|n| [ n.method(:lxc), n.method(:qemu) ] }.
|
||||
each {|m| m.call.each &push }
|
||||
to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) }
|
||||
cli.cmd( :status, "Lists Nodes/VMs/CTs with status", &lambda {|target=nil, sort:, node: nil, status: nil, tags: nil|
|
||||
hosting_table target: target, status: status, sort: sort, tags: tags do |push|
|
||||
node_opt( node).
|
||||
each( &push).lazy.
|
||||
flat_map {|n| [ Thread.new( n, &:lxc), Thread.new( n, &:qemu) ] }.
|
||||
each {|n| n.value.each &push }
|
||||
end
|
||||
}).
|
||||
opt( :sort, '-s', '--sort=COLUMNS', "Sort by COLUMNs eg hn for host and name ([s]tatus, h[a], [i]d, [n]ame (default), [h]ost, [u]ptime, [c]pu, [m]em, [d]isk)").
|
||||
opt( :node, '-n', '--node=NODE', "List only hosted by this NODE")
|
||||
opt( :sort, '-s', '--sort=COLUMNS', "Sort by COLUMNs eg hn for host and name ([s]tatus, h[a], [i]d, [n]ame (default), [h]ost, [u]ptime, [c]pu, [m]em, [d]isk)", default: 'n').
|
||||
opt( :node, '-n', '--node=NODE', "List only hosted by this NODE").
|
||||
opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)").
|
||||
opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for Machine")
|
||||
|
||||
def val2str v
|
||||
case v
|
||||
when true then 1
|
||||
when false then 0
|
||||
else v
|
||||
end
|
||||
end
|
||||
|
||||
def prepare_show_config cnf
|
||||
r = {}
|
||||
|
@ -46,18 +46,15 @@ def cli_base
|
|||
net.
|
||||
reject {|k, v| :card == k }.
|
||||
sort_by {|k, v| :name == k ? :AAAAAAAAA : k }.
|
||||
map {|k, v| case v when true then [k,1] when false then [k,0] else [k,v] end }.
|
||||
map {|k, v| "#{k}=#{v}" }
|
||||
r[net[:card].to_sym] = s.join(",")
|
||||
map {|k, v| "#{k}=#{val2str v}" }
|
||||
r[net[:card].to_sym] = s.join ','
|
||||
end
|
||||
when :sshkeys
|
||||
r[k] = CGI.unescape(v).gsub( /^/, " "*14).gsub /\A {14}|\n\z/, ''
|
||||
r[k] = CGI.unescape( v).gsub( /^/, " "*14).gsub /\A {14}|\n\z/, ''
|
||||
when :features
|
||||
r[k] = v.map {|k, v| "#{k}=#{val2str v}" }.join ','
|
||||
else
|
||||
case v
|
||||
when true then v = 1
|
||||
when false then v = 0
|
||||
end
|
||||
r[k] = v.to_s.gsub( /$^/, " "*14).gsub /\n\z/, ''
|
||||
r[k] = val2str( v).to_s.gsub( /$^/, " "*14).gsub /\n\z/, ''
|
||||
end
|
||||
end
|
||||
r
|
||||
|
@ -65,34 +62,29 @@ def cli_base
|
|||
|
||||
def show_config cnf, old = nil
|
||||
cnf = prepare_show_config cnf
|
||||
l = cnf.keys.map( &:length).max
|
||||
if old
|
||||
old = prepare_show_config old
|
||||
(cnf.keys+old.keys).uniq.sort.each do |k|
|
||||
v, o = cnf[k], old[k]
|
||||
if v == o
|
||||
puts "#{k}:#{' ' * (12-k.length)} #{v}"
|
||||
puts "#{k}:#{' ' * (l-k.length)} #{v}"
|
||||
else
|
||||
puts "\e[31m#{k}:#{' ' * (12-k.length)} #{o}\e[0m" unless o.nil?
|
||||
puts "\e[32m#{k}:#{' ' * (12-k.length)} #{v}\e[0m" unless v.nil?
|
||||
puts "\e[31m#{k}:#{' ' * (l-k.length)} #{o}\e[0m" unless o.nil?
|
||||
puts "\e[32m#{k}:#{' ' * (l-k.length)} #{v}\e[0m" unless v.nil?
|
||||
end
|
||||
end
|
||||
else
|
||||
cnf.sort_by{|k,v|k}.each do |k,v|
|
||||
puts "#{k}:#{' ' * (12-k.length)} #{v}"
|
||||
cnf.sort_by {|k,_| k }.each do |k,v|
|
||||
puts "#{k}:#{' ' * (l-k.length)} #{v}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cli.sub :config, "CT/VM Configuration", min: 2, aliases: %w[cnf] do |ccli|
|
||||
ccli.cmd 'help', '', aliases: [nil, '-h', '--help'], &lambda {|*args| help ccli, *args }
|
||||
ccli.cmd :help, '', aliases: [nil, '-h', '--help'], &lambda {|*args| help ccli, *args }
|
||||
|
||||
ccli.cmd :show, "Show Config of CT/VM", &lambda {|name_or_id|
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
show_config th.config
|
||||
}
|
||||
|
||||
ccli.cmd :set, "Set Configs for CT/VM", &lambda {|name_or_id, *args|
|
||||
ccli.cmd :set, "Set Configs for CT/VM", min: 3, &lambda {|name_or_id, *args|
|
||||
if %w[-h --help].include? name_or_id
|
||||
STDERR.puts "Usage: set -h|--help # Show help"
|
||||
STDERR.puts " set ct|vm --CNF1=VAL1 --CNF2=VAL2 ... # Set config-value. Empty value clears field."
|
||||
|
@ -101,17 +93,26 @@ def cli_base
|
|||
opts = {}
|
||||
until args.empty?
|
||||
case arg = args.shift
|
||||
when /\A--no-(\w+)\z/
|
||||
opts[$1.to_sym] = false
|
||||
when /\A--(\w+)=(.*)\z/
|
||||
opts[$1.to_sym] = $2
|
||||
when /\A--(\w+)!\z/, /\A--del-(\w+)\z/
|
||||
opts[$1.to_sym] = nil
|
||||
when /\A--(\w+)\z/
|
||||
opts[$1.to_sym] = args.shift
|
||||
n = $1.to_sym
|
||||
opts[n] =
|
||||
if args.empty? or /\A--/ == args.first
|
||||
true
|
||||
else opts[n] = args.shift
|
||||
end
|
||||
else
|
||||
raise UsageError, "Expection option to set. What do you mean with: #{arg}"
|
||||
end
|
||||
end
|
||||
opts.each do |k, v|
|
||||
opts[k] =
|
||||
case v = opts[k]
|
||||
case v
|
||||
when '' then nil
|
||||
else v
|
||||
end
|
||||
|
@ -136,34 +137,70 @@ def cli_base
|
|||
next unless opts.has_key? k
|
||||
opts[k] =
|
||||
case v = opts[k]
|
||||
when *%w[1 T TRUE t true True Y YES y yes Yes] then true
|
||||
when *%w[0 F FALSE f false False N NO n no No] then false
|
||||
when true, *%w[1 T TRUE t true True Y YES y yes Yes] then true
|
||||
when false, *%w[0 F FALSE f false False N NO n no No] then false
|
||||
when '', 'nil', nil then nil
|
||||
else raise UsageError, "Boolean expected, given: #{v.inspect}"
|
||||
end
|
||||
end
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or Node not found: #{name_or_id}" unless th
|
||||
old = th.config
|
||||
opts[:digest] ||= old[:digest]
|
||||
th.cnfset opts
|
||||
show_config th.config, old
|
||||
}
|
||||
|
||||
ccli.cmd :show, "Show Config of CT/VM", &lambda {|name_or_id|
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or Node not found: #{name_or_id}" unless th
|
||||
show_config th.config
|
||||
}
|
||||
end
|
||||
|
||||
cli.cmd :enter, "Enter Console of CT/Node", &lambda {|name_or_id|
|
||||
cli.cmd( :enter, "Enter Console of CT/Node", &lambda {|name_or_id|
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Node.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or Node not found: #{name_or_id}" unless th
|
||||
STDERR.puts "! #{$?.exitstatus}" unless th.enter
|
||||
}
|
||||
}).
|
||||
completion do |*pre, arg|
|
||||
completion_helper *pre, arg do |f|
|
||||
complete_lxc( f) + complete_node( f)
|
||||
end
|
||||
end
|
||||
|
||||
cli.cmd :run, "Starts CT/VM", aliases: %w[start star], &lambda {|name_or_id|
|
||||
cli.cmd( :run, "Starts CT/VM", aliases: %w[start star], &lambda {|*names_or_ids|
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or Node not found: #{name_or_id}" unless th
|
||||
start th
|
||||
}
|
||||
raise UsageError, "Nothing to start?" if names_or_ids.empty?
|
||||
per_argument names_or_ids, print: "\e[34;1mRunning CT/VM\e[0m %s:\n" do |name_or_id|
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or Node not found: #{name_or_id}" unless th
|
||||
start th
|
||||
end
|
||||
}).
|
||||
completion do |*pre, arg|
|
||||
completion_helper *pre, arg do |f|
|
||||
complete_lxc( f) + complete_qemu( f)
|
||||
end
|
||||
end
|
||||
|
||||
cli.cmd( :migrate, "Migrates (halted) CTs/VMs to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:|
|
||||
connect
|
||||
node = Proxmox::Node.find_by_name! target
|
||||
per_argument names_or_ids, print: "\e[1;34mCT/VM(s)\e[0m %s:\n" do |name_or_id|
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "CT/VM not found: #{name_or_id}" unless th
|
||||
unless th.stopped?
|
||||
raise UsageError, "VM #{name_or_id} is running. Shutdown or for VM-online-migration see `help qm migrate`" if Proxmox::Qemu === th
|
||||
raise UsageError, "CT #{name_or_id} is running. You have to shutdown it."
|
||||
end
|
||||
task = th.migrate node
|
||||
wait task, text: "Migrating", timeout: timeout unless fire
|
||||
end
|
||||
}).tap {|c| opts_wait c }
|
||||
|
||||
#cli.cmd :reboot, "Reboot CT/VM (not implemented, yet)", min: 6, &lambda {|name_or_id|
|
||||
# connect
|
||||
|
@ -172,16 +209,30 @@ def cli_base
|
|||
# reboot th
|
||||
#}
|
||||
|
||||
cli.cmd :stop, "Stops CT/VM", min: 4, &lambda {|name_or_id|
|
||||
cli.cmd( :stop, "Stops CT/VM", min: 4, &lambda {|*names_or_ids|
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or Node not found: #{name_or_id}" unless th
|
||||
stop th
|
||||
}
|
||||
per_argument names_or_ids, print: "\e[34;1mStopping CT/VM\e[0m %s:\n" do |name_or_id|
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or Node not found: #{name_or_id}" unless th
|
||||
stop th
|
||||
end
|
||||
}).
|
||||
completion do |*pre, arg|
|
||||
completion_helper *pre, arg do |f|
|
||||
complete_lxc( f) + complete_qemu( f)
|
||||
end
|
||||
end
|
||||
|
||||
cli.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help cli, *args }
|
||||
cli.cmd( :help, '', aliases: ['-h', '--help'], &lambda {|*args, full:|
|
||||
if full
|
||||
cli.help_full *args, output: STDERR
|
||||
else
|
||||
cli.help *args, output: STDERR
|
||||
end
|
||||
}).
|
||||
opt( :full, '-f', '--[no-]full', 'Includes all commands of all subcommands.', default: false)
|
||||
|
||||
cli.cmd 'cli', 'Opens interactive console', min: 3, aliases: [nil], &lambda {
|
||||
cli.cmd :cli, 'Opens interactive console', min: 3, aliases: [nil], &lambda {
|
||||
@interactive = true
|
||||
cli.interactive( File.basename($0,'.rb')).run
|
||||
}
|
||||
|
|
|
@ -3,46 +3,66 @@ def cli_ct
|
|||
cli.sub :ct, "Containers", aliases: %w[lx lxc] do |ct_cli|
|
||||
ct_cli.cmd :list, "List CT-IDs", aliases: ['ls'], &lambda {|node=nil|
|
||||
connect
|
||||
nodes = Proxmox::Node.all
|
||||
nodes = nodes.select {|n| node == n.name } if node
|
||||
nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all
|
||||
nodes.flat_map do |n|
|
||||
n.lxc.map {|c| c.vmid.to_i }
|
||||
end.sort.each {|c| puts c }
|
||||
}
|
||||
|
||||
ct_cli.cmd :status, "List CTs with status", aliases: [nil], &lambda {|node=nil|
|
||||
connect
|
||||
to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU Mem/MiB Disk/MiB]
|
||||
nodes = Proxmox::Node.all
|
||||
nodes = nodes.select {|n| node == n.name } if node
|
||||
nodes.each do |n|
|
||||
n.lxc.each &to.method( :virt)
|
||||
ct_cli.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil, tags: nil|
|
||||
hosting_table target: target, status: status, sort: sort, tags: tags do |push|
|
||||
node_opt( node).lazy.
|
||||
map {|n| Thread.new n, &:lxc }.
|
||||
each {|n| n.value.each &push }
|
||||
end
|
||||
to.print order: [3]
|
||||
}
|
||||
}).
|
||||
opt( :sort, '-s', '--sort=COLUMNS', "Sort by COLUMNs eg hn for host and name ([s]tatus, h[a], [i]d, [n]ame (default), [h]ost, [u]ptime, [c]pu, [m]em, [d]isk)", default: 'n').
|
||||
opt( :node, '-n', '--node=NODE', "List only hosted by this NODE").
|
||||
opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)").
|
||||
opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for CT")
|
||||
|
||||
ct_cli.cmd :enter, "Enter Console of CT", &lambda {|name_or_id|
|
||||
connect
|
||||
STDERR.puts "! #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).enter
|
||||
@out.host = name_or_id
|
||||
@out.failed "Exitstatus #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).enter
|
||||
}
|
||||
|
||||
ct_cli.cmd :exec, "Executes Command in CT", min: 4, &lambda {|name_or_id, *command|
|
||||
connect
|
||||
STDERR.puts "! #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command
|
||||
@out.host = name_or_id
|
||||
@out.failed "Exitstatus #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command
|
||||
}
|
||||
|
||||
ct_cli.cmd( :start, "Starts CT", min: 3, &lambda {|name_or_id, node: nil, fire:, timeout:, secs:|
|
||||
ct_cli.cmd( :migrate, "Migrates halted CT(s) to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:|
|
||||
connect
|
||||
ct = Proxmox::LXC.find! name_or_id
|
||||
start ct, node: node, fire: fire, timeout: timeout, secs: secs
|
||||
}).
|
||||
opt( :node, "-nNODE", "--node=NODE", "On NODE (default, as is, so without migration)").
|
||||
tap {|c| opts_wait c }
|
||||
node = Proxmox::Node.find_by_name! target
|
||||
per_argument names_or_ids, print: "Migrate CT:" do |name_or_id|
|
||||
ct = Proxmox::LXC.find! name_or_id
|
||||
task = ct.migrate node
|
||||
wait task, text: "Migrating", timeout: timeout unless fire
|
||||
end
|
||||
}).tap {|c| opts_wait c }
|
||||
|
||||
ct_cli.cmd( :stop, "Stops CT", min: 3, &lambda {|name_or_id, fire: nil, timeout:, secs:|
|
||||
ct_cli.cmd( :volume_move, "Moves volume to an other storage/pool and marks old volume as unused.", min: 11, aliases: %i[volmv mv], &lambda {|name_or_id, volume, destination|
|
||||
connect
|
||||
ct = Proxmox::LXC.find! name_or_id
|
||||
stop ct, fire: fire, timeout: timeout, secs: secs
|
||||
ct_volume_move ct, volume, destination
|
||||
})
|
||||
|
||||
ct_cli.cmd( :start, "Starts CT(s)", min: 4, &lambda {|*names_or_ids, node: nil, fire:, timeout:, secs:|
|
||||
connect
|
||||
per_argument names_or_ids, print: "Start CT:" do |name_or_id|
|
||||
ct = Proxmox::LXC.find! name_or_id
|
||||
start ct, node: node, fire: fire, timeout: timeout, secs: secs
|
||||
end
|
||||
}).tap {|c| opts_wait c }
|
||||
|
||||
ct_cli.cmd( :stop, "Stops CT(s)", min: 3, &lambda {|*names_or_ids, fire: nil, timeout:, secs:|
|
||||
connect
|
||||
per_argument names_or_ids, print: "Start CT:" do |name_or_id|
|
||||
ct = Proxmox::LXC.find! name_or_id
|
||||
stop ct, fire: fire, timeout: timeout, secs: secs
|
||||
end
|
||||
}).tap {|c| opts_wait c }
|
||||
|
||||
ct_cli.cmd( :wait, "Wait till CT is in state", &lambda {|name_or_id, state, timeout: nil, secs: nil|
|
||||
|
@ -53,7 +73,7 @@ def cli_ct
|
|||
opt( :timeout, "-tTIMEOUT", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil).
|
||||
opt( :secs, "-sSECONDS", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2)
|
||||
|
||||
ct_cli.cmd( :create, "Creates a new container", &lambda {|template, *options| #, fire:, timeout:, secs:, start:|
|
||||
ct_cli.cmd( :create, "Creates a new container", min: 2, &lambda {|template, *options| #, fire:, timeout:, secs:, start:|
|
||||
if %w[-h --help].include? template
|
||||
STDERR.puts "Usage: ct create TEMPLATE -h # Shows template-related options"
|
||||
STDERR.puts " ct create TEMPLATE [OPTIONS] # Creates a container"
|
||||
|
@ -99,10 +119,10 @@ EOU
|
|||
create Proxmox::LXC, template, **ctopts
|
||||
})
|
||||
|
||||
ct_cli.cmd( :config, 'Shows current config', &lambda {|name_or_id|
|
||||
ct_cli.cmd( :config, 'Shows current config', aliases: %w[cnf], min: 2, &lambda {|name_or_id|
|
||||
connect
|
||||
ct = Proxmox::LXC.find! name_or_id
|
||||
STDOUT.puts ct.config.to_json
|
||||
STDOUT.puts JSON.dump( ct.config)
|
||||
})
|
||||
|
||||
ct_cli.cmd( :resize, 'Resize a disk', &lambda {|name_or_id, disk, size|
|
||||
|
|
|
@ -9,22 +9,22 @@ def opts_ha cl
|
|||
end
|
||||
|
||||
def cli_ha
|
||||
cli.sub :ha, "Inspect High-Availability" do |hacli|
|
||||
cli.sub :ha, "Inspect High-Availability", min: 2 do |hacli|
|
||||
hacli.cmd( :create, "Create HA for CT/VM", &lambda {|name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:|
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
||||
ha = th.ha
|
||||
raise UsageError, "#{th.sid} is already High-Available" unless ha.active?
|
||||
raise UsageError, "#{th.sid} is already High-Available" if ha.active?
|
||||
ha.create group: group, comment: comment, max_relocate: max_relocate, max_restart: max_restart
|
||||
}).tap {|cl| opts_ha cl }
|
||||
|
||||
hacli.cmd :remove, "Remove CT/VM from HA", &lambda {|name_or_id|
|
||||
hacli.cmd :remove, "Remove CT/VM from HA", min: 5, &lambda {|name_or_id|
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
||||
ha = th.ha
|
||||
raise UsageError, "#{th.sid} is not High-Available" if ha.active?
|
||||
raise UsageError, "#{th.sid} is not High-Available" unless ha.active?
|
||||
ha.delete
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ def cli_ha
|
|||
ha = ha.create unless ha.active?
|
||||
ha.disabled! if force and ha.error?
|
||||
ha.started!
|
||||
}).opt( :force, "-f", "--force", "If CT/VM is in error-state, first disable HA, than try to start.")
|
||||
}).opt( :force, "-f", "--force", "If CT/VM is in error-state, first reset HA, than try to start.")
|
||||
|
||||
hacli.cmd :stopped, "CT/VM should be in state stopped. By starting CT/VM via pct/e state will be changed in HA, too.", min: 3, &lambda {|name_or_id|
|
||||
connect
|
||||
|
@ -63,7 +63,7 @@ def cli_ha
|
|||
ha.stopped!
|
||||
}
|
||||
|
||||
hacli.cmd :reset, "If state of CT/VM is failed, Proxmox will not start/stop it anyway. You have to reset state (state=disabled), first", &lambda {|name_or_id|
|
||||
hacli.cmd :reset, "If state of CT/VM is failed, Proxmox will not start/stop it anyway. You have to reset state (state=disabled), first", min: 3, aliases: [:rst], &lambda {|name_or_id|
|
||||
connect
|
||||
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
||||
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
||||
|
|
|
@ -30,7 +30,7 @@ def cli_node
|
|||
end
|
||||
|
||||
nod_cli.sub :task, "Inspect tasks" do |tcli|
|
||||
tcli.cmd :list, "List done tasks", aliases: [nil, 'ls'], &lambda {|node|
|
||||
tcli.cmd :list, "List done tasks", aliases: [:ls], &lambda {|node|
|
||||
connect
|
||||
Proxmox::Node.find_by_name!( node).
|
||||
tasks.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class PVE::Cli
|
||||
def cli_qm
|
||||
cli.sub :qm, "Virtual Machines", aliases: %w[v vm qemu], &lambda {|qm|
|
||||
qm.cmd :list, "List VM-IDs", aliases: ['ls'], &lambda {|node=nil|
|
||||
cli.sub :qm, "Virtual Machines", aliases: %w[v vm qemu], &lambda {|qm_cli|
|
||||
qm_cli.cmd :list, "List VM-IDs", aliases: ['ls'], &lambda {|node=nil|
|
||||
connect
|
||||
nodes = Proxmox::Node.all
|
||||
nodes = nodes.select {|n| node == n.name } if node
|
||||
|
@ -10,23 +10,72 @@ def cli_qm
|
|||
end.sort.each {|c| puts c }
|
||||
}
|
||||
|
||||
qm.cmd :status, "List VMs with status", aliases: [nil], &lambda {|node=nil|
|
||||
qm_cli.cmd( :migrate, "Migrates VM(s) to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:|#, online:, restart:|
|
||||
#if online and restart
|
||||
# raise UsageError, "You have to decide for one migration-mode: --restart or --online."
|
||||
#end
|
||||
#online = !restart or online
|
||||
connect
|
||||
to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU Mem/MiB Disk/MiB]
|
||||
nodes = Proxmox::Node.all
|
||||
nodes = nodes.select {|n| node == n.name } if node
|
||||
nodes.each do |n|
|
||||
n.qemu.each &to.method( :virt)
|
||||
node = Proxmox::Node.find_by_name! target
|
||||
per_argument names_or_ids, print: "\e[1;34mVM\e[0m %s:\n" do |name_or_id|
|
||||
qm = Proxmox::Qemu.find! name_or_id
|
||||
task = qm.migrate node #, online: online
|
||||
wait task, text: "Migrating", timeout: timeout unless fire
|
||||
end
|
||||
to.print order: [3]
|
||||
}
|
||||
}).
|
||||
#opt( :online, '-o', '--online', "Online-migration: Does not shutdown or interrupt running VM. Opposite of --online, Default", default: nil).
|
||||
#opt( :restart, '-r', '--restart', "Restart-migration: Does shutdown or interrupt running VM. Opposite of --restart", default: nil).
|
||||
tap {|c| opts_wait c }
|
||||
|
||||
qm.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command|
|
||||
qm_cli.cmd( :start, "Starts VM(s)", min: 4, &lambda {|*names_or_ids, node: nil, fire:, timeout:, secs:|
|
||||
connect
|
||||
per_argument names_or_ids, print: "\e[1;34mStart VM %s:\e[0m" do |name_or_id|
|
||||
qm = Proxmox::Qemu.find! name_or_id
|
||||
start qm, node: node, fire: fire, timeout: timeout, secs: secs
|
||||
end
|
||||
}).tap {|c| opts_wait c }
|
||||
|
||||
qm_cli.cmd( :stop, "Stops VM(s)", min: 3, &lambda {|*names_or_ids, fire: nil, timeout:, secs:|
|
||||
connect
|
||||
per_argument names_or_ids, print: "\e[1;34mStart VM %s:\e[0m" do |name_or_id|
|
||||
qm = Proxmox::Qemu.find! name_or_id
|
||||
stop qm, fire: fire, timeout: timeout, secs: secs
|
||||
end
|
||||
}).tap {|c| opts_wait c }
|
||||
|
||||
qm_cli.cmd( :wait, "Wait till VM is in state", &lambda {|name_or_id, state, timeout: nil, secs: nil|
|
||||
connect
|
||||
qm = Proxmox::Qemu.find! name_or_id
|
||||
wait qm, state, timeout: timeout, secs: secs
|
||||
}).
|
||||
opt( :timeout, "-tTIMEOUT", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil).
|
||||
opt( :secs, "-sSECONDS", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2)
|
||||
|
||||
qm_cli.cmd( :status, "Lists VMs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil, tags: nil|
|
||||
hosting_table target: target, status: status, sort: sort, tags: tags do |push|
|
||||
node_opt( node).lazy.
|
||||
flat_map {|n| [ Thread.new( n, &:qemu) ] }.
|
||||
each {|n| n.value.each &push }
|
||||
end
|
||||
}).
|
||||
opt( :sort, '-s', '--sort=COLUMNS', "Sort by COLUMNs eg hn for host and name ([s]tatus, h[a], [i]d, [n]ame (default), [h]ost, [u]ptime, [c]pu, [m]em, [d]isk)", default: 'n').
|
||||
opt( :node, '-n', '--node=NODE', "List only hosted by this NODE").
|
||||
opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)").
|
||||
opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for CT")
|
||||
|
||||
qm_cli.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command|
|
||||
connect
|
||||
STDERR.puts "! #{$?.exitstatus}" unless Proxmox::Qemu.find!( name_or_id).exec *command
|
||||
}
|
||||
|
||||
qm.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help qm, *args }
|
||||
qm_cli.cmd( :resize, 'Resize a disk', &lambda {|name_or_id, disk, size|
|
||||
connect
|
||||
qm = Proxmox::Qemu.find! name_or_id
|
||||
task = qm.resize disk, size
|
||||
wait task, text: "Resizing #{qm.sid} #{disk} to #{size}"
|
||||
})
|
||||
|
||||
qm_cli.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help qm_cli, *args }
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
class PVE::Cli
|
||||
def cli_storage
|
||||
cli.sub :storage, "Storages", min: 3 do |cli_sm|
|
||||
cli_sm.cmd :list, "List Storages", aliases: ['ls'], &lambda {|node=nil|
|
||||
connect
|
||||
nodes = node ? [Proxmox::Node.find_by_name!( node)] : Proxmox::Node.all
|
||||
nodes.flat_map do |n|
|
||||
n.lxc.map {|c| c.vmid.to_i }
|
||||
end.sort.each {|c| puts c }
|
||||
}
|
||||
|
||||
cli_sm.cmd :status, "List Storages with status", aliases: [nil], &lambda {|node=nil|
|
||||
connect
|
||||
to = TablizedOutput.new %w[A E S Storage Host Type]
|
||||
nodes = node ? [Proxmox::Node.find_by_name!( node)] : Proxmox::Node.all
|
||||
nodes.each do |n|
|
||||
n.storage.each do |v|
|
||||
to.push [
|
||||
case v.active
|
||||
when 1 then ColoredString.new 'Y', "32"
|
||||
when 0 then ColoredString.new 'n', "31"
|
||||
else v.active.to_s
|
||||
end,
|
||||
case v.enabled
|
||||
when 1 then ColoredString.new 'Y', "32"
|
||||
when 0 then ColoredString.new 'n', "31"
|
||||
else v.enabled.to_s
|
||||
end,
|
||||
1 == v.shared ? 's' : 'l', v.storage, v.node.node, v.type
|
||||
]
|
||||
end
|
||||
end
|
||||
to.print order: [4,5]
|
||||
}
|
||||
|
||||
cli_sm.sub :content, "Content of Storage", aliases: ['cnt'] do |cli_cnt|
|
||||
cli_cnt.cmd :list, "List Content", aliases: ['ls'], &lambda {|node=nil, storage|
|
||||
connect
|
||||
node = node ? Proxmox::Node.find_by_name!( node) : Proxmox::Node.all.first
|
||||
storage = node.storage.select {|sm| storage == sm.storage }.first
|
||||
storage.content.each {|c| puts c.to_s }
|
||||
}
|
||||
cli_cnt.cmd( :help, '', aliases: ['-h', '--help', nil]) {|*args| help cli_cnt, *args }
|
||||
end
|
||||
|
||||
cli_sm.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help cli_sm, *args }
|
||||
#cli_sm.provide_help
|
||||
end
|
||||
|
||||
cli.sub :apl, "Appliances - Downloadable container images", min: 3 do |cli_apl|
|
||||
|
||||
cli_apl.cmd( :content, "Table of all provided appliances", aliases: [nil], &lambda {|node:, regexp:, system:, applications:|
|
||||
connect
|
||||
appliances node, regexp, system, applications
|
||||
}).
|
||||
opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil).
|
||||
opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil).
|
||||
opt( :system, '-s', '--system', 'Only system templates', default: nil).
|
||||
opt( :applications, '-a', '--applications', 'Only applications (non system) templates', default: nil)
|
||||
|
||||
cli_apl.cmd( :system, "Table of provided systems", &lambda {|node:, regexp:|
|
||||
connect
|
||||
appliances node, regexp, true, nil
|
||||
}).
|
||||
opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil).
|
||||
opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil)
|
||||
|
||||
cli_apl.cmd( :applications, "Table of provided applications", &lambda {|node:, regexp:|
|
||||
connect
|
||||
appliances node, regexp, nil, true
|
||||
}).
|
||||
opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil).
|
||||
opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil)
|
||||
|
||||
cli_apl.cmd( :list, "List provided appliances", aliases: ['ls'], &lambda {|node=nil, regexp:|
|
||||
connect
|
||||
node = node ? Proxmox::Node.find_by_name!( node) : Proxmox::Node.all.first
|
||||
node.aplinfo.each do |apl|
|
||||
puts apl.template
|
||||
end
|
||||
}).
|
||||
opt( :regexp, '-r=REGEXP', '--regexp', 'Filters by name', default: nil)
|
||||
|
||||
cli_apl.cmd :download, "Download appliance", aliases: ['dl'], min: 2, &lambda {|template, node, storage=nil|
|
||||
storage ||= 'local'
|
||||
connect
|
||||
node = Proxmox::Node.find_by_name! node
|
||||
apl = node.aplinfo.find {|apl| apl.template == template }
|
||||
raise UsageError, "Appliance not found" unless apl
|
||||
task = apl.download storage
|
||||
wait task, text: "Download #{apl.template} on #{node.node} to #{storage}"
|
||||
}
|
||||
|
||||
cli_apl.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help cli_apl, *args }
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,4 +1,33 @@
|
|||
class PVE::Cli
|
||||
def task_table order: nil, &exe
|
||||
to = TablizedOutput.new %w[S Starttime Node SID Type UPID], format: '<<<<<<'
|
||||
hosted = {}
|
||||
Proxmox::Hosted.all.each {|h| hosted[h.vmid.to_i] = h }
|
||||
hosted.delete nil
|
||||
exe.call lambda {|t|
|
||||
u = t.upid
|
||||
v = u.id ? hosted[u.id.to_i] : t.node
|
||||
to.push [
|
||||
case t.status.state
|
||||
when :running then ColoredString.new '...', '30'
|
||||
when :success then ColoredString.new 'OK', '32'
|
||||
when :failed then ColoredString.new 'failed', '31'
|
||||
end,
|
||||
u.starttime.strftime( '%Y-%m-%d %H:%M:%S'),
|
||||
u.node,
|
||||
case v&.t
|
||||
when 'nd' then ColoredString.new v.sid, '33'
|
||||
when 'qm' then ColoredString.new v.sid, '35'
|
||||
when 'ct' then ColoredString.new v.sid, '36'
|
||||
when nil then u.id.inspect
|
||||
else v.sid
|
||||
end,
|
||||
u.dtype, u.upid
|
||||
]
|
||||
}
|
||||
to.print order: order
|
||||
end
|
||||
|
||||
def cli_task
|
||||
cli.sub :task, "Inspect tasks" do |tcli|
|
||||
tcli.cmd :list, "List done tasks", &lambda {|node=nil|
|
||||
|
@ -7,7 +36,7 @@ def cli_task
|
|||
nodes = nodes.select {|n| node == n.name } if node
|
||||
nodes.flat_map do |n|
|
||||
n.tasks.map &:upid
|
||||
end.sort.each {|upid| puts upid }
|
||||
end.sort_by(&:upid).each {|upid| puts upid }
|
||||
}
|
||||
|
||||
tcli.cmd :get, "Inspect a task", &lambda {|upid|
|
||||
|
@ -16,11 +45,45 @@ def cli_task
|
|||
n.tasks.each do |t|
|
||||
next unless t.upid == upid
|
||||
puts t.upid
|
||||
t.log.each {|l| puts l[:l] }
|
||||
t.log( start: 0, limit: 1024).each {|l| puts l[:t] }
|
||||
return
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
tcli.cmd( :status, "Lists tasks with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil|
|
||||
connect
|
||||
task_table order: [2] do |push|
|
||||
Proxmox::Node.all.each {|n| n.tasks.each &push }
|
||||
end
|
||||
})
|
||||
|
||||
tcli.cmd( :monitor, "Monitors running tasks", &lambda {|node: nil|
|
||||
connect
|
||||
nodes = Proxmox::Node.all
|
||||
nodes = nodes.select {|n| node == n.name } if node
|
||||
tasks = {}
|
||||
nodes.each {|n| n.tasks.each {|t| tasks[t.upid.upid] = true } }
|
||||
begin
|
||||
loop do
|
||||
task_table order: [2] do |push|
|
||||
begin
|
||||
nodes.
|
||||
flat_map {|n| n.tasks }.
|
||||
select {|t| tasks[t.upid.upid] != true or t.running? }.
|
||||
each( &push)
|
||||
STDERR.print "\e[2J\e[1;1H"
|
||||
rescue RestClient::InternalServerError
|
||||
end
|
||||
end
|
||||
sleep 1
|
||||
end
|
||||
rescue Interrupt
|
||||
STDERR.print "\e[2J\e[1;1H"
|
||||
end
|
||||
})
|
||||
|
||||
tcli.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help ct_cli, *args }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,17 +13,24 @@ end
|
|||
|
||||
|
||||
class Measured
|
||||
class V
|
||||
attr_reader :value, :length
|
||||
|
||||
def initialize( value, length = nil) @value, @length = value, length || value.length end
|
||||
def inspect() "#<Measured::V #{@value.inspect} (#{@length})>" end
|
||||
alias :to_s :value
|
||||
alias :to_str :value
|
||||
end
|
||||
|
||||
class <<self
|
||||
def bytes1 v
|
||||
v = v.to_f
|
||||
return "%d B" % v if 512 > v
|
||||
return units '%d ' % v , :B if 512 > v
|
||||
%w[KiB MiBy GiByt TiByte ExiByte PetiByte].each_with_index do |m|
|
||||
v /= 1024
|
||||
#return "%.1f %s" % [v, m] if 10 > v
|
||||
#return "%d %s" % [v, m] if 512 > v
|
||||
return "%.1f %s" % [v, m] if 512 > v
|
||||
return units '%.1f ' % v, m if 512 > v
|
||||
end
|
||||
"%d PetiByte" % v
|
||||
units '%d ' % v, :PetiByte
|
||||
end
|
||||
|
||||
def bytes2 v
|
||||
|
@ -40,21 +47,28 @@ class Measured
|
|||
end
|
||||
alias bytes bytes2
|
||||
|
||||
def units val, unit
|
||||
v = "#{val}\e[1;30m#{unit}\e[0m"
|
||||
V.new v, v.length - 11
|
||||
end
|
||||
|
||||
def seconds i
|
||||
i = i.to_i
|
||||
return '·' if 0 == i
|
||||
return "%d s" % i if 90 > i
|
||||
return V.new "\e[1;30m·\e[0m", 1 if 0 == i
|
||||
return units '%d ' % i, :s if 90 > i
|
||||
i /= 60
|
||||
return "%d mi" % i if 90 > i
|
||||
return units '%d ' % i, :mi if 90 > i
|
||||
i /= 60
|
||||
return "%d hou" % i if 36 > i
|
||||
return units '%d ' % i, :hou if 36 > i
|
||||
i /= 24
|
||||
return "%d days" % i if 14 > i
|
||||
return units '%d ' % i, :days if 14 > i
|
||||
j = i / 7
|
||||
return "%d weeks" % j if 25 > j
|
||||
i /= 365
|
||||
return "%.1f years" if 550 > i
|
||||
"%dy" % i
|
||||
return units '%d ' % j, :weeks if 8 > j
|
||||
j = i / 30
|
||||
return units '%d ' % j, :months if 11 > j
|
||||
i /= 365.0
|
||||
return units '%.2f ' % i, :years if 550 > i
|
||||
units '%d ' % i, :years
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -72,21 +86,19 @@ class ColoredString
|
|||
|
||||
def length() @string.length end
|
||||
alias size length
|
||||
#def to_str() self end
|
||||
def to_s() "\e[#{@color_codes}m#{@string}\e[0m" end
|
||||
alias to_str to_s
|
||||
#alias inspect to_str
|
||||
|
||||
include Comparable
|
||||
def <=>(o) @string <=> o.string end
|
||||
end
|
||||
|
||||
|
||||
|
||||
class TablizedOutput
|
||||
def initialize header, stdout: nil
|
||||
def initialize header, stdout: nil, format: nil
|
||||
@header = header.map &:to_s
|
||||
@columnc = header.size
|
||||
@format = format || ['>']*@columnc
|
||||
@maxs = header.map &:length
|
||||
@stdout ||= STDOUT
|
||||
@lines = []
|
||||
|
@ -100,7 +112,7 @@ class TablizedOutput
|
|||
class V < B
|
||||
attr_reader :v, :s
|
||||
def initialize( v, s=nil) @v, @s = v, s || "#{v}" end
|
||||
def to_s() @s end
|
||||
def to_s() @s.to_s end
|
||||
def length() @s.length end
|
||||
def inspect() "#<TO:V #{@v.inspect} #{@s.inspect}>" end
|
||||
end
|
||||
|
@ -112,10 +124,14 @@ class TablizedOutput
|
|||
def inspect() "#<TO:Percentage #{@v}%>" end
|
||||
|
||||
def to_s
|
||||
y = w - (v*w).round
|
||||
x = (100*v).round
|
||||
r = "%*s" % [w, 0==x ? '·' : x]
|
||||
"\e[0m#{r[0...y]}\e[1;4;#{0.75>v ? 32 : 31}m#{r[y..-1]}\e[0m"
|
||||
vw = v*w
|
||||
percent = (100*v).round
|
||||
vwi = vw.to_i
|
||||
rounded = (vw+0.5).to_i
|
||||
s = "%*s" % [w, 0==percent ? '·' : percent]
|
||||
pre = "\e[1;4;#{0.75>v ? 32 : 31}m"
|
||||
mid = (vw % 1) > 0.5 ? "\e[2m" : "\e[0m"
|
||||
"#{pre}#{s[0...vwi]}#{mid}#{s[vwi]}\e[0m#{s[(vwi+1)..-1]}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -136,7 +152,6 @@ class TablizedOutput
|
|||
end
|
||||
|
||||
def print order: nil
|
||||
format = "#{(["\e[%%sm%% %ds\e[0m"] * @columnc).join( ' ') % @maxs}\n"
|
||||
ls = @lines
|
||||
if order
|
||||
eval <<-EOC, binding, __FILE__, 1+__LINE__
|
||||
|
@ -146,36 +161,28 @@ class TablizedOutput
|
|||
}
|
||||
EOC
|
||||
end
|
||||
#ls = ls.sort_by {|e| p e; order.map &e.method(:[]) } if order
|
||||
@stdout.printf format, *@header.flat_map {|s|['',s]}
|
||||
ls.each {|l| @stdout.printf format, *l.flat_map {|s| s.is_a?(ColoredString) ? [s.color_codes, s.string] : ["", s.to_s] } }
|
||||
end
|
||||
|
||||
def virt v
|
||||
ha = v.respond_to?( :ha) ? v.ha : nil
|
||||
unknown = V.new 0, '-'
|
||||
push [
|
||||
case v.status
|
||||
when "running", "online" then ColoredString.new v.status, "32"
|
||||
when "stopped" then ColoredString.new v.status, "31"
|
||||
else v.status
|
||||
end,
|
||||
ha&.state || '·',
|
||||
case v.t
|
||||
when "nd" then ColoredString.new v.sid, "33"
|
||||
when "qm" then ColoredString.new v.sid, "35"
|
||||
when "ct" then ColoredString.new v.sid, "36"
|
||||
else v.sid
|
||||
end,
|
||||
v.name, v.node.is_a?(String) ? v.node : v.node.node,
|
||||
v.respond_to?(:uptime) ? V.new( v.uptime, Measured.seconds( v.uptime)) : unknown,
|
||||
v.respond_to?(:cpu) ? Percentage.new( v.cpu) : unknown,
|
||||
v.respond_to?(:mem) ? V.new( v.mem, Measured.bytes( v.mem)) : unknown,
|
||||
v.respond_to?(:maxmem) ? Percentage.new( v.mem/v.maxmem.to_f) : unknown,
|
||||
v.respond_to?(:disk) ? V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown,
|
||||
if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i
|
||||
Percentage.new( v.disk.to_f/v.maxdisk.to_f)
|
||||
else unknown end,
|
||||
]
|
||||
@stdout.puts \
|
||||
@header.each_with_index.map {|s, i|
|
||||
#"#{' ' * (@maxs[i] - s.length)}#{s}"
|
||||
pad = ' ' * (@maxs[i] - s.length)
|
||||
case @format[i]
|
||||
when '<'
|
||||
"#{s}#{pad}"
|
||||
else
|
||||
"#{pad}#{s}"
|
||||
end
|
||||
}.join( ' | ')
|
||||
ls.each_with_index do |l, i|
|
||||
@stdout.puts \
|
||||
l.each_with_index.map {|s, i|
|
||||
pad = ' ' * (@maxs[i] - s.length)
|
||||
case @format[i]
|
||||
when '<'
|
||||
"#{s}#{pad}"
|
||||
else
|
||||
"#{pad}#{s}"
|
||||
end
|
||||
}.join( "\e[3#{i.even? ? 6 : 3}m | \e[0m")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,6 +5,13 @@ require 'ipaddress'
|
|||
require 'shellwords'
|
||||
require 'active_support/all'
|
||||
|
||||
class RestClient::Exception
|
||||
attr_reader :request
|
||||
def to_s
|
||||
"#{request|"?"}: #{message}: #{response}"
|
||||
end
|
||||
end
|
||||
|
||||
module Proxmox
|
||||
class Exception < ::Exception
|
||||
end
|
||||
|
@ -12,6 +19,34 @@ module Proxmox
|
|||
end
|
||||
class AlreadyExists < Exception
|
||||
end
|
||||
class AlreadyLocked < Exception
|
||||
end
|
||||
class UnparsableUPID < Exception
|
||||
end
|
||||
|
||||
def self.cnfstr2hash str
|
||||
str.
|
||||
split( ',').
|
||||
map do |o|
|
||||
k, v = o.split( '=', 2)
|
||||
[k.to_sym, v]
|
||||
end.
|
||||
to_h
|
||||
end
|
||||
|
||||
def self.hash2cnfstr **opts
|
||||
opts.
|
||||
map do |k, v|
|
||||
v =
|
||||
case v
|
||||
when true then 1
|
||||
when false then 0
|
||||
else v.to_s
|
||||
end
|
||||
"#{k}=#{v}"
|
||||
end.
|
||||
join( ',')
|
||||
end
|
||||
|
||||
def self.connect username, password, realm: nil, verify_tls: nil, uri: nil
|
||||
uri ||= 'https://localhost:8006/api2/json'
|
||||
|
@ -51,7 +86,7 @@ module Proxmox
|
|||
end
|
||||
|
||||
def self.find_by_vmid vmid
|
||||
Proxmox::LXC.find_by_vmid( vmid) || Proxmox::Qemu.find_by_vmid( vmid) || Proxmox::Node.find_by_vmid( vmid)
|
||||
Proxmox::LXC.find_by_vmid( vmid) || Proxmox::Qemu.find_by_vmid( vmid)
|
||||
end
|
||||
|
||||
def self.find name_or_id
|
||||
|
@ -95,19 +130,37 @@ module Proxmox
|
|||
def rest_get path, **data, &exe
|
||||
data = data.delete_if {|k,v|v.nil?}
|
||||
path += "#{path.include?( ??) ? ?& : ??}#{data.map{|k,v|"#{CGI.escape k.to_s}=#{CGI.escape v.to_s}"}.join '&'}" unless data.empty?
|
||||
STDERR.puts "GET #{path} <= #{data}" if $DEBUG
|
||||
__response__ Proxmox.connection[path].get( __headers__( :'Content-Type' => 'application/json'))
|
||||
rescue RestClient::Exception
|
||||
$!.instance_variable_set :@request, "GET #{path}"
|
||||
raise
|
||||
end
|
||||
|
||||
def rest_put path, **data, &exe
|
||||
__response__ Proxmox.connection[path].put( __data__( data), __headers__( :'Content-Type' => 'application/json'))
|
||||
data = __data__( **data)
|
||||
STDERR.puts "PUT #{path} <= #{data}" if $DEBUG
|
||||
__response__ Proxmox.connection[path].put( data, __headers__( :'Content-Type' => 'application/json'))
|
||||
rescue RestClient::Exception
|
||||
$!.instance_variable_set :@request, "PUT #{path}"
|
||||
raise
|
||||
end
|
||||
|
||||
def rest_del path, &exe
|
||||
STDERR.puts "DELETE #{path}" if $DEBUG
|
||||
__response__ Proxmox.connection[path].delete( __headers__( :'Content-Type' => 'application/json'))
|
||||
rescue RestClient::Exception
|
||||
$!.instance_variable_set :@request, "DELETE #{path}"
|
||||
raise
|
||||
end
|
||||
|
||||
def rest_post path, **data, &exe
|
||||
__response__ Proxmox.connection[path].post( __data__( data), __headers__( :'Content-Type' => 'application/json'))
|
||||
data = __data__( data)
|
||||
STDERR.puts "POST #{path} <= #{data}" if $DEBUG
|
||||
__response__ Proxmox.connection[path].post( data, __headers__( :'Content-Type' => 'application/json'))
|
||||
rescue RestClient::Exception
|
||||
$!.instance_variable_set :@request, "POST #{path}"
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -123,10 +176,14 @@ module Proxmox
|
|||
n.send :__update__, data
|
||||
end
|
||||
private :__new__
|
||||
|
||||
def fetch predata
|
||||
__new__( predata).refresh!
|
||||
end
|
||||
end
|
||||
|
||||
def respond_to? method
|
||||
super or instance_variable_defined?( "@#{method}")
|
||||
def respond_to? method, also_private = false
|
||||
instance_variable_defined?( "@#{method}") or super(method, also_private)
|
||||
end
|
||||
|
||||
def method_missing method, *args, &exe
|
||||
|
@ -147,6 +204,7 @@ module Proxmox
|
|||
initialize
|
||||
self
|
||||
end
|
||||
private :__update__
|
||||
|
||||
def refresh!
|
||||
__update__ rest_get( @rest_prefix)
|
||||
|
@ -173,24 +231,48 @@ module Proxmox
|
|||
|
||||
attr_reader :name
|
||||
|
||||
def __update__ **data
|
||||
data[:status] &&= data[:status].to_sym
|
||||
super **data
|
||||
end
|
||||
private :__update__
|
||||
|
||||
def === t
|
||||
@name =~ t or @vmid.to_s =~ t or @sid =~ t
|
||||
end
|
||||
|
||||
def rest_prefix
|
||||
@rest_prefix ||= "/nodes/#{@node}"
|
||||
end
|
||||
|
||||
def initialize
|
||||
@rest_prefix = "/nodes/#{@node}"
|
||||
rest_prefix
|
||||
@sid = "nd:#{@node}"
|
||||
@name = @node
|
||||
end
|
||||
|
||||
def offline?() @status.nil? or 'offline' == @status end
|
||||
def online?() 'online' == @status end
|
||||
def offline?() @status.nil? or :offline == @status end
|
||||
def online?() :online == @status end
|
||||
|
||||
def lxc
|
||||
return [] if offline?
|
||||
rest_get( "#{@rest_prefix}/lxc").map {|d| LXC.send :__new__, d.merge( node: self, t: 'ct') }
|
||||
end
|
||||
|
||||
def aplinfo
|
||||
return [] if offline?
|
||||
rest_get( "#{@rest_prefix}/aplinfo").map do |d|
|
||||
AplInfo.send :__new__, d.merge( node: self, t: 'apl')
|
||||
end
|
||||
end
|
||||
|
||||
def storage
|
||||
return [] if offline?
|
||||
rest_get( "#{@rest_prefix}/storage").map do |d|
|
||||
Storage.send :__new__, d.merge( node: self, t: 'sm')
|
||||
end
|
||||
end
|
||||
|
||||
def qemu
|
||||
return [] if offline?
|
||||
rest_get( "#{@rest_prefix}/qemu").map {|d| Qemu.send :__new__, d.merge( node: self, t: 'qm') }
|
||||
|
@ -211,21 +293,106 @@ module Proxmox
|
|||
end
|
||||
|
||||
class Task < Base
|
||||
def initialize
|
||||
class Status < Base
|
||||
def rest_prefix
|
||||
@rest_prefix ||= '/nodes/%s/tasks/%s/status' % [@node.node, @upid]
|
||||
end
|
||||
|
||||
def refresh!
|
||||
d = rest_get @rest_prefix
|
||||
d[:starttime] &&= Time.at d[:starttime]
|
||||
__update__ d.merge( node: @node, upid: @upid, task: @task)
|
||||
end
|
||||
|
||||
def __update__ **data
|
||||
data = data.merge t: 'status'
|
||||
data[:status] = data[:status]&.to_sym
|
||||
data[:exitstatus] = data[:exitstatus]&.to_sym
|
||||
data[:state] =
|
||||
case data[:status]
|
||||
when :running then :running
|
||||
when :stopped
|
||||
case data[:exitstatus]
|
||||
when :OK then :success
|
||||
else :failed
|
||||
end
|
||||
end
|
||||
super **data
|
||||
end
|
||||
|
||||
def initialize
|
||||
rest_prefix
|
||||
@sid = upid
|
||||
end
|
||||
|
||||
def inspect
|
||||
h = instance_variables - %i[@node @task @sid @rest_prefix @upid @t]
|
||||
h.map! {|k| "#{k[1..-1]}=#{instance_variable_get(k).inspect}" }
|
||||
"#<#{self.class.name}|#{@upid} node=#{@node.node} #{h.join ' '}>"
|
||||
end
|
||||
|
||||
def running?() :running == @status end
|
||||
def finished?() :stopped == @status end
|
||||
alias stopped? finished?
|
||||
def successfull?() stopped? ? :OK == @exitstatus : nil end
|
||||
def failed?() stopped? ? :OK != @exitstatus : nil end
|
||||
end
|
||||
|
||||
def running?() status.running? end
|
||||
def finished?() status.finished? end
|
||||
alias stopped? finished?
|
||||
def successfull?() status.successfull? end
|
||||
def failed?() status.failed? end
|
||||
|
||||
class UPID
|
||||
RE =
|
||||
/^
|
||||
UPID :
|
||||
(?<node>[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?) :
|
||||
(?<pid>[0-9A-Fa-f]{8}) :
|
||||
(?<pstart>[0-9A-Fa-f]{8,9}) :
|
||||
(?<starttime>[0-9A-Fa-f]{8}) :
|
||||
(?<dtype>[^:\s]+) :
|
||||
(?<id>[^:\s]*) :
|
||||
(?<user>[^:\s]+) :
|
||||
$/x
|
||||
|
||||
attr_reader :upid, :node, :pid, :pstart, :starttime, :dtype, :id, :user
|
||||
alias to_s upid
|
||||
|
||||
def initialize upid
|
||||
m = RE.match upid
|
||||
raise UnparsableUPID, "UPID cannot be parsed: #{upid.inspect} (#{upid.class})" unless m
|
||||
@upid = upid
|
||||
@node = m[:node]
|
||||
@pid = m[:pid].to_i 16
|
||||
@pstart = m[:pstart].to_i 16
|
||||
@starttime = Time.at m[:starttime].to_i( 16)
|
||||
@dtype = m[:dtype]
|
||||
@id = case m[:id] when '' then nil else m[:id].to_i end
|
||||
@user = m[:user]
|
||||
end
|
||||
|
||||
def inspect
|
||||
"#<#{self.class.name} #{@upid}>"
|
||||
end
|
||||
end
|
||||
|
||||
def rest_prefix
|
||||
@rest_prefix = "/nodes/#{@node.node}/tasks/#{upid}"
|
||||
@sid = upid
|
||||
end
|
||||
|
||||
def initialize
|
||||
rest_prefix
|
||||
@sid, @upid = upid, UPID.new( upid)
|
||||
end
|
||||
|
||||
def inspect
|
||||
"#<#{self.class.name} #{upid}>"
|
||||
"#<#{self.class.name} #{@upid}>"
|
||||
end
|
||||
|
||||
#def finished?
|
||||
# rest_get( "/nodes/#{node}/tasks/")
|
||||
#end
|
||||
|
||||
def status
|
||||
rest_get( "#{@rest_prefix}/status")
|
||||
Status.fetch node: @node, task: self, upid: @upid
|
||||
end
|
||||
|
||||
def log start: nil, limit: nil
|
||||
|
@ -234,24 +401,25 @@ module Proxmox
|
|||
end
|
||||
|
||||
class Hosted < Base
|
||||
def refresh!
|
||||
node, t = @node, @t
|
||||
__update__ rest_get( "#{@rest_prefix}/status/current").merge( node: node, t: t)
|
||||
def self.all
|
||||
Node.all.flat_map {|n| n.qemu + n.lxc }
|
||||
end
|
||||
|
||||
def refresh!
|
||||
__update__ rest_get( "#{@rest_prefix}/status/current").merge( node: @node, t: @t)
|
||||
end
|
||||
|
||||
def __update__ **data
|
||||
data[:tags] = (data[:tags]||'').split ';'
|
||||
data[:status] &&= data[:status].to_sym
|
||||
super **data
|
||||
end
|
||||
private :__update__
|
||||
|
||||
def === t
|
||||
@name =~ t or @vmid.to_s =~ t or @sid =~ t
|
||||
end
|
||||
|
||||
def migrate node
|
||||
node =
|
||||
case node
|
||||
when Node then node
|
||||
else Node.find!( node.to_s)
|
||||
end
|
||||
Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/migrate", target: node.node)
|
||||
end
|
||||
|
||||
def start
|
||||
Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/status/start")
|
||||
end
|
||||
|
@ -298,23 +466,23 @@ module Proxmox
|
|||
map( &:to_s).
|
||||
grep( /\Anet\d+\z/).
|
||||
map do |k|
|
||||
nc = {card: k}
|
||||
cnf.delete( k.to_sym).
|
||||
split( ',').
|
||||
each do |f|
|
||||
k, v = f.split( '=', 2)
|
||||
nc[k.to_sym] = v
|
||||
end
|
||||
nc[:ip] &&= IPAddress::IPv4.new nc[:ip]
|
||||
nc[:gw] &&= IPAddress::IPv4.new nc[:gw]
|
||||
nc[:mtu] &&= nc[:mtu].to_i
|
||||
nc[:tag] &&= nc[:tag].to_i
|
||||
nc = Proxmox::cnfstr2hash cnf.delete( k.to_sym)
|
||||
nc[:card] = k
|
||||
nc[:ip] &&= IPAddress::IPv4.new nc[:ip]
|
||||
nc[:gw] &&= IPAddress::IPv4.new nc[:gw]
|
||||
nc[:mtu] &&= nc[:mtu].to_i
|
||||
nc[:tag] &&= nc[:tag].to_i
|
||||
nc[:firewall] &&= 1 == nc[:firewall].to_i
|
||||
nc
|
||||
end
|
||||
cnf[:features] &&=
|
||||
Proxmox::cnfstr2hash( cnf[:features]).
|
||||
tap {|f| f[:nesting] &&= '1' == f[:nesting] }
|
||||
cnf[:unprivileged] &&= 1 == cnf[:unprivileged]
|
||||
cnf[:memory] &&= cnf[:memory].to_i
|
||||
cnf[:cores] &&= cnf[:cores].to_i
|
||||
cnf[:memory] &&= cnf[:memory].to_i
|
||||
cnf[:cores] &&= cnf[:cores].to_i
|
||||
cnf[:tags] &&= cnf[:tags].split ';'
|
||||
cnf.update cnf.delete( :lxc).map{|k,v|[k.to_sym,v]}.to_h if cnf[:lxc]
|
||||
cnf
|
||||
end
|
||||
|
||||
|
@ -325,11 +493,36 @@ module Proxmox
|
|||
when true then r[k] = 1
|
||||
when false then r[k] = 0
|
||||
when nil then r[:delete].push k
|
||||
when Array
|
||||
case k
|
||||
when :tags
|
||||
r[k] = v.join ';'
|
||||
else r[k] = v
|
||||
end
|
||||
else r[k] = v
|
||||
end
|
||||
end
|
||||
r.delete :delete if r[:delete].empty?
|
||||
rest_put "#{@rest_prefix}/config", r
|
||||
rest_put "#{@rest_prefix}/config", **r
|
||||
end
|
||||
|
||||
# it is only allowed to lock local guests, because it is only possible to unlock local guests.
|
||||
def lock reason, &exe
|
||||
cnf = config
|
||||
raise Proxmox::AlreadyLocked, "Machine #{self} Already locked for: #{cnf[:lock].inspect}" unless cnf[:lock].nil?
|
||||
r = rest_put "#{@rest_prefix}/config", {lock: reason}
|
||||
if block_given?
|
||||
begin return yield
|
||||
ensure unlock
|
||||
end
|
||||
else r
|
||||
end
|
||||
end
|
||||
|
||||
def unlock
|
||||
system 'pct', 'unlock', @vmid.to_s
|
||||
raise Proxmox::UnlockFailed unless $?.success?
|
||||
#rest_put "#{@rest_prefix}/config", {delete: %w[lock], skiplock: 1}
|
||||
end
|
||||
|
||||
def resize disk, size
|
||||
|
@ -346,19 +539,15 @@ module Proxmox
|
|||
|
||||
def find_by_vmid vmid
|
||||
vmid = vmid.to_s
|
||||
Node.all.each do |n|
|
||||
n.qemu.each do |l|
|
||||
return l if l.vmid == vmid
|
||||
end
|
||||
all.each do |l|
|
||||
return l if l.vmid == vmid
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def find_by_name name
|
||||
Node.all.each do |n|
|
||||
n.qemu.each do |l|
|
||||
return l if l.name == name
|
||||
end
|
||||
all.each do |l|
|
||||
return l if l.name == name
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
@ -392,8 +581,12 @@ module Proxmox
|
|||
end
|
||||
end
|
||||
|
||||
def rest_prefix
|
||||
@rest_prefix ||= "/nodes/%s/qemu/%d" % [@node.node, @vmid]
|
||||
end
|
||||
|
||||
def initialize
|
||||
@rest_prefix = "/nodes/%s/qemu/%d" % [@node.node, @vmid]
|
||||
rest_prefix
|
||||
@sid = "qm:#{@vmid}"
|
||||
end
|
||||
|
||||
|
@ -404,6 +597,20 @@ module Proxmox
|
|||
def exec *args
|
||||
node.exec 'qm', 'guest', 'exec', vmid, '--', *args
|
||||
end
|
||||
|
||||
def resize disk, size
|
||||
upid = rest_put "#{@rest_prefix}/resize", disk: disk, size: size
|
||||
Task.send :__new__, node: @node, host: self, upid: upid
|
||||
end
|
||||
|
||||
def migrate target, online: nil
|
||||
target =
|
||||
case target
|
||||
when Node then target
|
||||
else Node.find!( target.to_s)
|
||||
end
|
||||
Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/migrate", target: target.node, online: ! !online)
|
||||
end
|
||||
end
|
||||
|
||||
class LXC < Hosted
|
||||
|
@ -494,6 +701,7 @@ module Proxmox
|
|||
#}.map {|k,v| "#{k}=#{v}" }.join( ','),
|
||||
swap: tmplt.swap,
|
||||
unprivileged: tmplt.unprivileged,
|
||||
features: tmplt.features ? Proxmox::hash2cnfstr( tmplt.features) : nil,
|
||||
}.delete_if {|k,v| v.nil? }
|
||||
|
||||
temp = LXC.send :__new__, node: node, vmid: options[:vmid], name: name, hostname: options[:hostname]
|
||||
|
@ -502,8 +710,12 @@ module Proxmox
|
|||
end
|
||||
end
|
||||
|
||||
def rest_prefix
|
||||
@rest_prefix ||= "/nodes/%s/lxc/%d" % [@node.node, @vmid]
|
||||
end
|
||||
|
||||
def initialize
|
||||
@rest_prefix = "/nodes/%s/lxc/%d" % [@node.node, @vmid]
|
||||
rest_prefix
|
||||
@sid = "ct:#{@vmid}"
|
||||
end
|
||||
|
||||
|
@ -518,11 +730,24 @@ module Proxmox
|
|||
def enter
|
||||
node.enter 'pct', 'enter', vmid
|
||||
end
|
||||
|
||||
def migrate target
|
||||
target =
|
||||
case target
|
||||
when Node then target
|
||||
else Node.find!( target.to_s)
|
||||
end
|
||||
Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/migrate", target: target.node)
|
||||
end
|
||||
end
|
||||
|
||||
class HA < Base
|
||||
def rest_prefix
|
||||
@rest_prefix ||= "/cluster/ha/resources/#{virt.sid}"
|
||||
end
|
||||
|
||||
def initialize
|
||||
@rest_prefix = "/cluster/ha/resources/#{virt.sid}"
|
||||
rest_prefix
|
||||
end
|
||||
|
||||
class <<self
|
||||
|
@ -563,7 +788,8 @@ module Proxmox
|
|||
end
|
||||
|
||||
def state= state
|
||||
rest_put "#{@rest_prefix}", state: state.to_s
|
||||
r = rest_put "#{@rest_prefix}", state: state.to_s
|
||||
pp r: r
|
||||
refresh!
|
||||
end
|
||||
|
||||
|
@ -582,20 +808,53 @@ module Proxmox
|
|||
self
|
||||
end
|
||||
|
||||
def started?
|
||||
'started' == self.state
|
||||
def started?() 'started' == self.state end
|
||||
def stopped?() 'stopped' == self.state end
|
||||
def error?() 'error' == self.state end
|
||||
def active?() ! ! digest end
|
||||
def exist?() ! ! digest end
|
||||
end
|
||||
|
||||
class Storage < Base
|
||||
def rest_prefix
|
||||
@rest_prefix ||= "/nodes/#{@node.node}/storage/#{@storage}"
|
||||
end
|
||||
|
||||
def stopped?
|
||||
'stopped' == self.state
|
||||
def content
|
||||
rest_get( "#{@rest_prefix}/content").map do |c|
|
||||
Content.send :__new__, c.merge( node: @node, storage: self, t: 'smc')
|
||||
end
|
||||
end
|
||||
|
||||
def error?
|
||||
'error' == self.state
|
||||
def initialize() rest_prefix end
|
||||
def to_s() "#{@node.node}:#{@storage}" end
|
||||
def active?() 1 == @active end
|
||||
def enabled?() 1 == @enabled end
|
||||
|
||||
class Content < Base
|
||||
def rest_prefix
|
||||
@rest_prefix ||= "/nodes/#{@node.node}/storage/#{@storage}/content/#{@content}"
|
||||
end
|
||||
|
||||
def initialize() rest_prefix end
|
||||
def to_s() "#{node.node} #{volid}" end
|
||||
end
|
||||
end
|
||||
|
||||
class AplInfo < Base
|
||||
def rest_prefix
|
||||
@rest_prefix ||= "/nodes/#{@node.node}/aplinfo"
|
||||
end
|
||||
|
||||
def active?
|
||||
! ! digest
|
||||
def initialize() rest_prefix end
|
||||
def name() @template end
|
||||
def system?() 'system' == @section end
|
||||
def debian?() %r[\Adebian-] =~ @os end
|
||||
def lxc?() 'lxc' == @type end
|
||||
|
||||
def download storage
|
||||
upid = rest_post "#{@rest_prefix}", template: @template, storage: storage.to_s
|
||||
Task.send :__new__, node: @node, host: self, upid: upid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -20,7 +20,18 @@ module PVE::CTTemplate
|
|||
def hostname() options.hostname || name end
|
||||
def memory() options.memory || 1024 end
|
||||
def swap() options.swap || 0 end
|
||||
def unprivileged() options.unprivileged() || 1 end
|
||||
def unprivileged() options.unprivileged || 1 end
|
||||
|
||||
def features
|
||||
r =
|
||||
if options.features
|
||||
Proxmox::cnfstr2hash( options.features).
|
||||
tap {|f| f[:nesting] &&= 1 == f[:nesting] }
|
||||
else {}
|
||||
end
|
||||
r[:nesting] = options.nesting.nil? ? true : options.nesting
|
||||
r
|
||||
end
|
||||
|
||||
def ssh_public_keys
|
||||
options[:'ssh-public-keys'] ||
|
||||
|
@ -82,6 +93,8 @@ module PVE::CTTemplate
|
|||
ipv6: [:string, false, "IPv6-Address with net-size or auto."],
|
||||
gateway6: [:string, false, "IPv6-Address of gateway."],
|
||||
storage: [:string, false, "Device will be create on this Storage (default: local"],
|
||||
features: [:string, false, "Features"],
|
||||
nesting: [:boolean, false, "Support nested containers (->Features)"],
|
||||
}
|
||||
end
|
||||
end
|
||||
|
@ -122,12 +135,14 @@ module PVE::CTTemplate
|
|||
gateway6: [:string, false, "IPv6-Address of gateway."],
|
||||
storage: [:string, false, "Device will be create on this Storage (default: root)"],
|
||||
ostemplate: [:string, false, "OS-Template eg. local:vztmpl/superlinux-1.2-amd64.tar.xz"],
|
||||
features: [:string, false, "Features"],
|
||||
nesting: [:boolean, false, "Support nested containers (->Features)"],
|
||||
}
|
||||
end
|
||||
|
||||
def node() options.node || 'svc1' end
|
||||
def ostype() options.ostype || 'debian' end
|
||||
def memory() options.memory || 2048 end
|
||||
def node() options.node || 'svc1' end
|
||||
def ostype() options.ostype || 'debian' end
|
||||
def memory() options.memory || 2048 end
|
||||
def storage() options.storage || 'root' end
|
||||
|
||||
def network_id
|
||||
|
@ -186,7 +201,8 @@ module PVE::CTTemplate
|
|||
options.ostemplate ||
|
||||
case ostype
|
||||
when 'debian'
|
||||
'local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz'
|
||||
# TODO: how to determine which template?
|
||||
'local:vztmpl/debian-11-standard_11.0-1_amd64.tar.gz'
|
||||
else
|
||||
raise ArgumentError, "OS-Template for ostype #{ostype} not found or ostemplate not provided."
|
||||
end
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module PVE
|
||||
VERSION = '0.1.3'
|
||||
VERSION = '0.3.3'
|
||||
end
|
||||
|
|
|
@ -12,14 +12,16 @@ Gem::Specification.new do |spec|
|
|||
spec.homepage = "https://git.denkn.at/deac/pve"
|
||||
# Ruby3 interpret **opts in another way than before.
|
||||
# 2.7.0 should work with both(?) behaviours.
|
||||
# PVE based on debian, so ruby 2.5 is default.
|
||||
spec.required_ruby_version = Gem::Requirement.new "~> 2.5.0"
|
||||
# PVE-7 based on debian-11, so ruby 2.7 is default.
|
||||
# No support for ruby 2.5 anymore (PVE-6 based on debian-10)
|
||||
# Old behaviour will be droped.
|
||||
spec.required_ruby_version = Gem::Requirement.new "~> 2.7"
|
||||
|
||||
spec.metadata["homepage_uri"] = spec.homepage
|
||||
spec.metadata["source_code_uri"] = spec.homepage
|
||||
spec.metadata["changelog_uri"] = spec.homepage
|
||||
|
||||
spec.add_dependency 'dencli', '~> 0.3.1'
|
||||
spec.add_dependency 'dencli', '~> 0.5.3'
|
||||
spec.add_dependency 'rest-client', '~> 2.1'
|
||||
spec.add_dependency 'ipaddress', '~> 0.8.3'
|
||||
spec.add_dependency 'activesupport', '>= 2'
|
||||
|
|
Loading…
Reference in New Issue