diff --git a/functions/LICENSE b/functions/LICENSE new file mode 100644 index 00000000..f288702d --- /dev/null +++ b/functions/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is 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. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + 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. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + 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 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. Use with the GNU Affero General Public License. + + 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 Affero 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 special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU 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 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 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 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. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + 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 GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/functions/backup.sh b/functions/backup.sh new file mode 100644 index 00000000..f0c2baf6 --- /dev/null +++ b/functions/backup.sh @@ -0,0 +1,310 @@ +#!/bin/bash + + +backup(){ +echo_backup+=("🄱 🄰 🄲 🄺 🅄 🄿 🅂") +echo_backup+=("Number of backups was set to $number_of_backups") +date=$(date '+%Y_%m_%d_%H_%M_%S') +[[ "$verbose" == "true" ]] && cli -c 'app kubernetes backup_chart_releases backup_name=''"'HeavyScript_"$date"'"' &> /dev/null && echo_backup+=(HeavyScript_"$date") +[[ -z "$verbose" ]] && echo_backup+=("\nNew Backup Name:") && cli -c 'app kubernetes backup_chart_releases backup_name=''"'HeavyScript_"$date"'"' | tail -n 1 &> /dev/null && echo_backup+=(HeavyScript_"$date") +mapfile -t list_backups < <(cli -c 'app kubernetes list_backups' | grep -E "HeavyScript_|TrueTool_" | sort -t '_' -Vr -k2,7 | awk -F '|' '{print $2}'| tr -d " \t\r") +if [[ ${#list_backups[@]} -gt "$number_of_backups" ]]; then + echo_backup+=("\nDeleted the oldest backup(s) for exceeding limit:") + overflow=$(( ${#list_backups[@]} - "$number_of_backups" )) + mapfile -t list_overflow < <(cli -c 'app kubernetes list_backups' | grep -E "HeavyScript_|TrueTool_" | sort -t '_' -V -k2,7 | awk -F '|' '{print $2}'| tr -d " \t\r" | head -n "$overflow") + for i in "${list_overflow[@]}" + do + cli -c 'app kubernetes delete_backup backup_name=''"'"$i"'"' &> /dev/null || echo_backup+=("Failed to delete $i") + echo_backup+=("$i") + done +fi + +#Dump the echo_array, ensures all output is in a neat order. +for i in "${echo_backup[@]}" +do + echo -e "$i" +done +echo +echo +} +export -f backup + + +deleteBackup(){ +clear -x && echo "pulling all restore points.." +# shellcheck disable=SC2178 +list_backups=$(cli -c 'app kubernetes list_backups' | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl -s ") " | column -t) +# shellcheck disable=SC2128 +if [[ -z "$list_backups" ]]; then + echo "No restore points available" + exit +fi + +#Select a restore point +while true +do + clear -x + title + echo -e "Choose a Restore Point to Delete\nThese may be out of order if they are not HeavyScript backups" + echo "$list_backups" + echo + echo "0) Exit" + read -rt 240 -p "Please type a number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + restore_point=$(echo "$list_backups" | grep ^"$selection)" | awk '{print $2}') + if [[ $selection == 0 ]]; then + echo "Exiting.." + exit + elif [[ -z "$selection" ]]; then + echo "Your selection cannot be empty" + sleep 3 + continue + elif [[ -z "$restore_point" ]]; then + echo "Invalid Selection: $selection, was not an option" + sleep 3 + continue + fi + break # Break out of the loop if all of the If statement checks above are untrue +done + +#Confirm deletion +while true +do + clear -x + echo -e "WARNING:\nYou CANNOT go back after deleting your restore point" + echo -e "\n\nYou have chosen:\n$restore_point\n\n" + read -rt 120 -p "Would you like to proceed with deletion? (y/N): " yesno || { echo -e "\nFailed to make a selection in time" ; exit; } + case $yesno in + [Yy] | [Yy][Ee][Ss]) + echo -e "\nDeleting $restore_point" + cli -c 'app kubernetes delete_backup backup_name=''"'"$restore_point"'"' &>/dev/null || { echo "Failed to delete backup.."; exit; } + echo "Sucessfully deleted" + break + ;; + [Nn] | [Nn][Oo]) + echo "Exiting" + exit + ;; + *) + echo "That was not an option, try again" + sleep 3 + continue + ;; + esac +done + +#Check if there are more backups to delete +while true +do + read -rt 120 -p "Delete more backups? (y/N): " yesno || { echo -e "\nFailed to make a selection in time" ; exit; } + case $yesno in + [Yy] | [Yy][Ee][Ss]) + break + ;; + [Nn] | [Nn][Oo]) + exit + ;; + *) + echo "$yesno was not an option, try again" + sleep 2 + continue + ;; + + esac + +done +} +export -f deleteBackup + + +restore(){ +clear -x && echo "pulling restore points.." +list_backups=$(cli -c 'app kubernetes list_backups' | grep -E "HeavyScript_|TrueTool_" | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl -s ") " | column -t) +if [[ -z "$list_backups" ]]; then + echo "No HeavyScript restore points available" + exit +fi + +#Select a restore point +while true +do + clear -x + title + echo "Choose a Restore Point" + echo "$list_backups" + echo + echo "0) Exit" + read -rt 240 -p "Please type a number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + restore_point=$(echo "$list_backups" | grep ^"$selection)" | awk '{print $2}') + if [[ $selection == 0 ]]; then + echo "Exiting.." + exit + elif [[ -z "$selection" ]]; then + echo "Your selection cannot be empty" + sleep 3 + continue + elif [[ -z "$restore_point" ]]; then + echo "Invalid Selection: $selection, was not an option" + sleep 3 + continue + fi + break +done + + +## Check to see if empty PVC data is present in any of the applications ## + +# Find all pv_info.json files two subfolders deep with the restore point name +pool=$(cli -c 'app kubernetes config' | grep -E "pool\s\|" | awk -F '|' '{print $3}' | tr -d " \t\n\r") +files=$(find "$(find /mnt/"$pool"/ix-applications/backups -maxdepth 0 )" -name pv_info.json | grep "$restore_point"); + +# Iterate over the list of files +for file in $files; do + # Check if the file only contains {} subfolders + contents=$(cat "$file") + if [[ "$contents" == '{}' ]]; then + # Print the file if it meets the criterion + file=$(echo "$file" | awk -F '/' '{print $7}') + borked_array+=("${file}") + fi +done + + +# Grab applications that are supposed to have PVC data +mapfile -t apps_with_pvc < <(k3s kubectl get pvc -A | sort -u | awk '{print $1 "\t" $2 "\t" $4}' | sed "s/^0/ /" | awk '{print $1}' | cut -c 4-) + + +# Iterate over the list of applications with empty PVC data +# Unset the application if it is not supposed to have PVC data +index=0 +for app in "${borked_array[@]}"; do + if ! printf '%s\0' "${apps_with_pvc[@]}" | grep -iFxqz "${app}" ; then + unset "borked_array[$index]" + else + borked=True + fi + ((index++)) +done + + + +# If there is still empty PVC data, exit +if [[ $borked == True ]]; then + echo "Warning!:" + echo "The following applications have empty PVC data:" + for app in "${borked_array[@]}"; do + echo -e "$app" + done + echo "We have no choice but to exit" + echo "If you were to restore, you would lose all of your application data" + echo "If you are on Bluefin version: 22.12.0, and have not yet ran the patch, you will need to run it" + echo "Afterwards you will be able to create backups and restore them" + echo "This is a known ix-systems bug, and has nothing to do with HeavyScript" + exit +fi + + + +## Check the restore point, and ensure it is the same version as the current system ## +# Boot Query +boot_query=$(cli -m csv -c 'system bootenv query created,realname') + +# Get the date of system version and when it was updated +current_version=$(cli -m csv -c 'system version' | awk -F '-' '{print $3}') +when_updated=$(echo "$boot_query" | grep "$current_version",\ +| awk -F ',' '{print $2}' | sed 's/[T|-]/_/g' | sed 's/:/_/g' | awk -F '_' '{print $1 $2 $3 $4 $5}') + +# Get the date of the chosen restore point +restore_point_date=$(echo "$restore_point" | awk -F '_' '{print $2 $3 $4 $5 $6}' | tr -d "_") + +# Grab previous version +previous_version=$(echo "$boot_query" | sort -nr | grep -A 1 "$current_version," | tail -n 1) + +# Compare the dates +while (("$restore_point_date" < "$when_updated" )) +do + clear -x + echo "The restore point you have chosen is from an older version of Truenas Scale" + echo "This is not recommended, as it may cause issues with the system" + echo "Either that, or your systems date is incorrect.." + echo + echo "Current SCALE Information:" + echo "Version: $current_version" + echo "When Updated: $(echo "$restore_point" | awk -F '_' '{print $2 "-" $3 "-" $4}')" + echo + echo "Restore Point SCALE Information:" + echo "Version: $(echo "$previous_version" | awk -F ',' '{print $1}')" + echo "When Updated: $(echo "$previous_version" | awk -F ',' '{print $2}' | awk -F 'T' '{print $1}')" + echo + read -rt 120 -p "Would you like to proceed? (y/N): " yesno || { echo -e "\nFailed to make a selection in time" ; exit; } + case $yesno in + [Yy] | [Yy][Ee][Ss]) + echo "Proceeding.." + sleep 3 + break + ;; + [Nn] | [Nn][Oo]) + echo "Exiting" + exit + ;; + *) + echo "That was not an option, try again" + sleep 3 + continue + ;; + esac +done + + +#Confirm restore +while true +do + clear -x + echo -e "WARNING:\nThis is NOT guranteed to work\nThis is ONLY supposed to be used as a LAST RESORT\nConsider rolling back your applications instead if possible" + echo -e "\n\nYou have chosen:\n$restore_point\n\n" + read -rt 120 -p "Would you like to proceed with restore? (y/N): " yesno || { echo -e "\nFailed to make a selection in time" ; exit; } + case $yesno in + [Yy] | [Yy][Ee][Ss]) + pool=$(cli -c 'app kubernetes config' | grep -E "pool\s\|" | awk -F '|' '{print $3}' | tr -d " \t\n\r") + + # Set mountpoints to legacy prior to restore, ensures correct properties for the are set + echo -e "\nSetting correct ZFS properties for application volumes.." + for pvc in $(zfs list -t filesystem -r "$pool"/ix-applications/releases -o name -H | grep "volumes/pvc") + do + if zfs set mountpoint=legacy "$pvc"; then + echo "Success for - \"$pvc\"" + else + echo "Error: Setting properties for \"$pvc\", failed.." + fi + done + + # Ensure readonly is turned off + if ! zfs set readonly=off "$pool"/ix-applications;then + echo -e "Error: Failed to set ZFS ReadOnly to \"off\"" + echo -e "After the restore, attempt to run the following command manually:" + echo "zfs set readonly=off $pool/ix-applications" + fi + + echo "Finished setting properties.." + + # Beginning snapshot restore + echo -e "\nStarting restore, this will take a LONG time." + if ! cli -c 'app kubernetes restore_backup backup_name=''"'"$restore_point"'"'; then + echo "Restore failed, exiting.." + exit 1 + fi + exit + ;; + [Nn] | [Nn][Oo]) + echo "Exiting" + exit + ;; + *) + echo "That was not an option, try again" + sleep 3 + continue + ;; + esac +done + +} +export -f restore diff --git a/functions/cmd_to_container.sh b/functions/cmd_to_container.sh new file mode 100644 index 00000000..a9a4132e --- /dev/null +++ b/functions/cmd_to_container.sh @@ -0,0 +1,119 @@ +#!/bin/bash + + +cmd_to_container(){ +app_name=$(k3s crictl pods -s ready --namespace ix | sed -E 's/[[:space:]]([0-9]*|About)[a-z0-9 ]{5,12}ago[[:space:]]//' | sed '1d' | awk '{print $4}' | cut -c4- | sort -u | nl -s ") " | column -t) +while true +do + clear -x + title + echo "Command to Container Menu" + echo "-------------------------" + echo "$app_name" + echo + echo "0) Exit" + read -rt 120 -p "Please type a number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + if [[ $selection == 0 ]]; then + echo "Exiting.." + exit + elif ! echo -e "$app_name" | grep -qs ^"$selection)" ; then + echo "Error: \"$selection\" was not an option.. Try again" + sleep 3 + continue + else + break + fi +done +rm cont_file 2> /dev/null +app_name=$(echo -e "$app_name" | grep ^"$selection)" | awk '{print $2}') +mapfile -t pod_id < <(k3s crictl pods -s ready --namespace ix | grep -v "[[:space:]]svclb-" | grep -E "[[:space:]]ix-${app_name}[[:space:]]" | awk '{print $1}') +search=$(k3s crictl ps -a -s running | sed -E 's/[[:space:]]([0-9]*|About)[a-z0-9 ]{5,12}ago[[:space:]]//') +for pod in "${pod_id[@]}" +do + echo "$search" | grep "$pod" >> cont_file +done +mapfile -t containers < <(sort -u cont_file 2> /dev/null) +case "${#containers[@]}" in + 0) + echo -e "No containers available\nAre you sure the application in running?" + exit + ;; + 1) + container=$(grep "${pod_id[0]}" cont_file | awk '{print $4}') + container_id=$(grep -E "[[:space:]]${container}[[:space:]]" cont_file | awk '{print $1}') + ;; + *) + while true + do + clear -x + title + echo "Available Containers" + echo "--------------------" + cont_search=$( + for i in "${containers[@]}" + do + echo "$i" | awk '{print $4}' + done | nl -s ") " | column -t + ) + echo "$cont_search" + echo + echo "0) Exit" + read -rt 120 -p "Choose a container by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + if [[ $selection == 0 ]]; then + echo "Exiting.." + exit + elif ! echo -e "$cont_search" | grep -qs ^"$selection)" ; then + echo "Error: \"$selection\" was not an option.. Try again" + sleep 3 + continue + else + break + fi + done + container=$(echo "$cont_search" | grep ^"$selection)" | awk '{print $2}') + container_id=$(grep -E "[[:space:]]${container}[[:space:]]" cont_file | awk '{print $1}') + ;; +esac +while true +do + clear -x + title + echo "App Name: $app_name" + echo "Container: $container" + echo + echo "1) Run a single command" + echo "2) Open Shell" + echo + echo "0) Exit" + read -rt 120 -p "Please choose an option: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + case $selection in + 0) + echo "Exiting.." + exit + ;; + 1) + clear -x + title + read -rt 500 -p "What command do you want to run?: " command || { echo -e "\nFailed to make a selection in time" ; exit; } + # shellcheck disable=SC2086 + # Quoting $command as suggested, causes the k3s command to fail + k3s crictl exec -it "$container_id" $command + break + ;; + 2) + clear -x + title + if ! k3s crictl exec -it "$container_id" /bin/bash 2>/dev/null; then + k3s crictl exec -it "$container_id" /bin/sh 2>/dev/null || echo "This container does not accept shell access, try a different one." + fi + break + ;; + *) + echo "That was not an option.. Try again" + sleep 3 + ;; + esac +done +rm cont_file 2> /dev/null +} +export -f cmd_to_container diff --git a/functions/dns.sh b/functions/dns.sh new file mode 100644 index 00000000..e628c12a --- /dev/null +++ b/functions/dns.sh @@ -0,0 +1,27 @@ +#!/bin/bash + + +dns(){ +clear -x +echo "Generating DNS Names.." + +# Pulling pod names +k3s crictl pods --namespace ix -s Ready | sed -E 's/[[:space:]]([0-9]*|About)[a-z0-9 ]{5,12}ago[[:space:]]//' | grep -v 'svclb-' | sed '1d' >> dns_file +mapfile -t ix_name_array < <(< dns_file awk '{print $4}' | sort -u ) + +# Pulling all ports +all_ports=$(k3s kubectl get service -A) + +clear -x +count=0 +for i in "${ix_name_array[@]}" +do + [[ count -le 0 ]] && echo -e "\n" && ((count++)) + full_app_name=$(grep -E "\s$i\s" "dns_file" | awk '{print $3}' | sed 's/-[^-]*-[^-]*$//' | sed 's/-0//' | head -n 1) + app_name=$(echo "$i" | cut -c 4-) + port=$(echo "$all_ports" | grep -E "\s$full_app_name\s" | awk '{print $6}' | grep -Eo "^[[:digit:]]+{1}") + echo -e "$app_name $full_app_name.$i.svc.cluster.local $port" +done | nl -s ") " -b t | sed '0,/\s\s\s/{s/\s\s\s/- ---- -------- ----/}'| column -t -N "#,Name,DNS_Name,Port" +rm dns_file +} +export -f dns diff --git a/functions/menu.sh b/functions/menu.sh new file mode 100644 index 00000000..8f808c57 --- /dev/null +++ b/functions/menu.sh @@ -0,0 +1,96 @@ +#!/bin/bash + + +menu(){ +clear -x +title +echo "Available Utilities" +echo "-------------------" +echo "1) Help" +echo "2) List DNS Names" +echo "3) Mount and Unmount PVC storage" +echo "4) Backup Options" +echo "5) Update HeavyScript" +echo "6) Update Applications" +echo "7) Command to Container" +echo "8) Patch 22.12.0" +echo "9) Patch 22.12.0 (2)" +echo +echo "0) Exit" +read -rt 120 -p "Please select an option by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + +case $selection in + 0) + echo "Exiting.." + exit + ;; + 1) + help + ;; + 2) + dns + ;; + 3) + mount + ;; + 4) + while [[ $backup_selection != true ]] + do + clear -x + title + echo "Backup Menu" + echo "-----------" + echo "1) Create Backup" + echo "2) Delete Backup" + echo "3) Restore Backup" + echo + echo "0) Exit" + read -rt 120 -p "Please select an option by number: " backup_selection || { echo -e "\nFailed to make a selection in time" ; exit; } + case $backup_selection in + 0) + echo "Exiting.." + exit + ;; + 1) + read -rt 120 -p "What is the maximun number of backups you would like?: " number_of_backups || { echo -e "\nFailed to make a selection in time" ; exit; } + ! [[ $number_of_backups =~ ^[0-9]+$ ]] && echo -e "Error: The input must be an interger\n\"""$number_of_backups""\" is not an interger" >&2 && exit + [[ "$number_of_backups" -le 0 ]] && echo "Error: Number of backups is required to be at least 1" && exit + backup_selection=true + ;; + 2) + backup_selection=true + deleteBackup + ;; + 3) + backup_selection=true + restore + ;; + *) + echo "\"$selection\" was not an option, please try agian" && sleep 3 && continue + ;; + esac + done + ;; + + 5) + self_update + ;; + 6) + script_create + ;; + 7) + cmd_to_container + ;; + 8) + patch_2212_backups + ;; + 9) + patch_2212_backups2 + ;; + *) + echo "\"$selection\" was not an option, please try agian" && sleep 3 && menu + ;; +esac +echo +} +export -f menu diff --git a/functions/misc.sh b/functions/misc.sh new file mode 100644 index 00000000..dd1ae239 --- /dev/null +++ b/functions/misc.sh @@ -0,0 +1,232 @@ +#!/bin/bash + + +sync(){ +echo_sync+=("🅂 🅈 🄽 🄲") +cli -c 'app catalog sync_all' &> /dev/null && echo_sync+=("Catalog sync complete") + +#Dump the echo_array, ensures all output is in a neat order. +for i in "${echo_sync[@]}" +do + echo -e "$i" +done +echo +echo +} +export -f sync + + +prune(){ +echo -e "🄿 🅁 🅄 🄽 🄴" +version="$(cli -c 'system version' | awk -F '-' '{print $3}' | awk -F '.' '{print $1 $2}' | tr -d " \t\r\.")" +if (( "$version" >= 2212 )); then + if ! cli -c 'app container config prune prune_options={"remove_unused_images": true, "remove_stopped_containers": true}' | head -n -4; then + echo "Failed to Prune Docker Images" + fi +else + if ! docker image prune -af | grep "^Total"; then + echo "Failed to Prune Docker Images" + fi +fi +} +export -f prune + + +title(){ +echo ' _ _ _____ _ _ ' +echo '| | | | / ___| (_) | | ' +echo '| |_| | ___ __ ___ ___ _\ `--. ___ _ __ _ _ __ | |_' +echo "| _ |/ _ \/ _\` \ \ / / | | |\`--. \/ __| '__| | '_ \| __|" +echo '| | | | __/ (_| |\ V /| |_| /\__/ / (__| | | | |_) | |_ ' +echo '\_| |_/\___|\__,_| \_/ \__, \____/ \___|_| |_| .__/ \__|' +echo ' __/ | | | ' +echo ' |___/ |_| ' +echo "$hs_version" +echo +} +export -f title + + +help(){ +[[ $help == "true" ]] && clear -x + +echo "Access the HeavyScript Menu" +echo "---------------------------" +echo "bash heavy_script.sh" +echo +echo "Utilities" +echo "---------" +echo "--mount | Initiates mounting feature, choose between unmounting and mounting PVC data" +echo "--restore | Opens a menu to restore a \"heavy_script\" backup that was taken on your \"ix-applications\" dataset" +echo "--delete-backup | Opens a menu to delete backups on your system" +echo "--dns | list all of your applications DNS names and their web ports" +echo "--cmd | Open a shell for one of your applications" +echo +echo "Update Types" +echo "------------" +echo "-U | Update all applications, ignores versions" +echo "-U 5 | Same as above, but updates 5 applications at one time" +echo "-u | Update all applications, does not update Major releases" +echo "-u 5 | Same as above, but updates 5 applications at one time" +echo +echo "Update Options" +echo "--------------" +echo "-r | Roll-back applications if they fail to update" +echo "-i | Add application to ignore list, one by one, see example below." +echo "-S | Shutdown applications prior to updating" +echo "-v | verbose output" +echo "-t 500| The amount of time HS will wait for an application to be ACTIVE. Defaults to 500 seconds" +echo +echo "Additional Options" +echo "------------------" +echo "-b 14 | Back-up your ix-applications dataset, specify a number after -b" +echo "-s | sync catalog" +echo "-p | Prune unused/old docker images" +echo "--ignore-img | Ignore container image updates" +echo "--self-update | Updates HeavyScript prior to running any other commands" +echo +echo "Examples" +echo "--------" +echo "bash heavy_script.sh" +echo "bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -t 600 -vrsUp --self-update" +echo "bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -t 600 -vrsp -U 10 --self-update" +echo "bash /mnt/tank/scripts/heavy_script.sh -t 150 --mount" +echo "bash /mnt/tank/scripts/heavy_script.sh --dns" +echo "bash heavy_script.sh --restore" +echo "bash /mnt/tank/scripts/heavy_script.sh --delete-backup" +echo +exit +} +export -f help + + + +patch_2212_backups(){ +clear -x +#Check TrueNAS version, skip if not 22.12.0 +if ! [ "$(cli -m csv -c 'system version' | awk -F '-' '{print $3}')" == "22.12.0" ]; then + echo "This patch does not apply to your version of TrueNAS" + return +fi + + +#Description +echo "This patch will fix the issue with backups not restoring properly" +echo "Due to Ix-Systems not saving PVC in backups, this patch will fix that" +echo "Otherwise backups will not restore properly" +echo "You only need to run this patch once, it will not run again" +echo + + +#Download patch +echo "Downloading Backup Patch" +if ! wget -q https://github.com/truecharts/truetool/raw/main/hotpatch/2212/HP1.patch; then + echo "Failed to download Backup Patch" + exit +else + echo "Downloaded Backup Patch" +fi + +echo + +# Apply patch +echo "Applying Backup Patch" +if patch -N --reject-file=/dev/null -s -p0 -d /usr/lib/python3/dist-packages/middlewared/ < HP1.patch &>/dev/null; then + echo "Backup Patch applied" + rm -rf HP1.patch +else + echo "Backup Patch already applied" + rm -rf HP1.patch + exit +fi + +echo + +#Restart middlewared +while true +do + echo "We need to restart middlewared to finish the patch" + echo "This will cause a short downtime for some minor services approximately 10-30 seconds" + echo "Applications should not be affected" + read -rt 120 -p "Would you like to proceed? (y/N): " yesno || { echo -e "\nFailed to make a selection in time" ; exit; } + case $yesno in + [Yy] | [Yy][Ee][Ss]) + echo "Restarting middlewared" + service middlewared restart & + wait $! + echo "Restarted middlewared" + echo "You are set, there is no need to run this patch again" + break + ;; + [Nn] | [Nn][Oo]) + echo "Exiting" + echo "Please restart middlewared manually" + echo "You can do: service middlewared restart" + exit + ;; + *) + echo "That was not an option, try again" + sleep 3 + continue + ;; + esac +done +} + + +patch_2212_backups2(){ +clear -x +#Check TrueNAS version, skip if not 22.12.0 +if ! [ "$(cli -m csv -c 'system version' | awk -F '-' '{print $3}')" == "22.12.0" ]; then + echo "This patch does not apply to your version of TrueNAS" + return +fi + + +#Description +echo "This patch will fix the issue certain applicattions breaking backups" +echo "You only need to run this patch once, it will not run again" +echo + + +# Apply patch +echo "Applying Backup Patch" +if patch -N --reject-file=/dev/null -s -p0 /usr/lib/python3/dist-packages/middlewared/plugins/kubernetes_linux/backup.py < patches/backups.patch &>/dev/null; then + echo "Backup Patch applied" +else + echo "Backup Patch already applied" + exit +fi + +echo + +#Restart middlewared +while true +do + echo "We need to restart middlewared to finish the patch" + echo "This will cause a short downtime for some minor services approximately 10-30 seconds" + echo "Applications should not be affected" + read -rt 120 -p "Would you like to proceed? (y/N): " yesno || { echo -e "\nFailed to make a selection in time" ; exit; } + case $yesno in + [Yy] | [Yy][Ee][Ss]) + echo "Restarting middlewared" + service middlewared restart & + wait $! + echo "Restarted middlewared" + echo "You are set, there is no need to run this patch again" + break + ;; + [Nn] | [Nn][Oo]) + echo "Exiting" + echo "Please restart middlewared manually" + echo "You can do: service middlewared restart" + exit + ;; + *) + echo "That was not an option, try again" + sleep 3 + continue + ;; + esac +done +} diff --git a/functions/mount.sh b/functions/mount.sh new file mode 100644 index 00000000..371c98d7 --- /dev/null +++ b/functions/mount.sh @@ -0,0 +1,114 @@ +#!/bin/bash + + +mount(){ +pool=$(cli -c 'app kubernetes config' | grep -E "pool\s\|" | awk -F '|' '{print $3}' | tr -d " \t\n\r") +while true +do + clear -x + title + echo "PVC Mount Menu" + echo "--------------" + echo "1) Mount" + echo "2) Unmount All" + echo + echo "0) Exit" + read -rt 120 -p "Please type a number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + case $selection in + 0) + echo "Exiting.." + exit + ;; + 1) + call=$(k3s kubectl get pvc -A | sort -u | awk '{print $1 "\t" $2 "\t" $4}' | sed "s/^0/ /") + mount_list=$(echo "$call" | sed 1d | nl -s ") ") + mount_title=$(echo "$call" | head -n 1) + list=$(echo -e "# $mount_title\n$mount_list" | column -t) + while true + do + clear -x + title + echo "$list" + echo + echo "0) Exit" + read -rt 120 -p "Please type a number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; } + + #Check for valid selection. If no issues, continue + [[ $selection == 0 ]] && echo "Exiting.." && exit + app=$(echo -e "$list" | grep ^"$selection)" | awk '{print $2}' | cut -c 4- ) + [[ -z "$app" ]] && echo "Invalid Selection: $selection, was not an option" && sleep 3 && continue + pvc=$(echo -e "$list" | grep ^"$selection)") + + #Stop applicaiton if not stopped + status=$(cli -m csv -c 'app chart_release query name,status' | grep "^$app," | awk -F ',' '{print $2}'| tr -d " \t\n\r") + if [[ "$status" != "STOPPED" ]]; then + echo -e "\nStopping $app prior to mount" + if ! cli -c 'app chart_release scale release_name='\""$app"\"\ 'scale_options={"replica_count": 0}' &> /dev/null; then + echo "Failed to stop $app" + exit 1 + else + echo "Stopped" + fi + else + echo -e "\n$app is already stopped" + fi + + #Grab data then output and mount + data_name=$(echo "$pvc" | awk '{print $3}') + volume_name=$(echo "$pvc" | awk '{print $4}') + full_path=$(zfs list -t filesystem -r "$pool"/ix-applications/releases/"$app"/volumes -o name -H | grep "$volume_name") + if ! zfs set mountpoint=/heavyscript/"$data_name" "$full_path" ; then + echo "Error: Failed to mount $app" + exit 1 + else + echo -e "\nMounted\n$data_name" + fi + echo -e "\nUnmount with:\nzfs set mountpoint=legacy $full_path && rmdir /mnt/heavyscript/$data_name\n\nOr use the Unmount All option\n" + + #Ask if user would like to mount something else + while true + do + echo + read -rt 120 -p "Would you like to mount anything else? (y/N): " yesno || { echo -e "\nFailed to make a selection in time" ; exit; } + case $yesno in + [Yy] | [Yy][Ee][Ss]) + clear -x + title + break + ;; + [Nn] | [Nn][Oo]) + exit + ;; + *) + echo "Invalid selection \"$yesno\" was not an option" + sleep 3 + continue + ;; + esac + done + done + ;; + 2) + mapfile -t unmount_array < <(basename -a /mnt/heavyscript/* | sed "s/*//") + [[ -z ${unmount_array[*]} ]] && echo "Theres nothing to unmount" && sleep 3 && continue + for i in "${unmount_array[@]}" + do + main=$(k3s kubectl get pvc -A | grep -E "\s$i\s" | awk '{print $1, $2, $4}') + app=$(echo "$main" | awk '{print $1}' | cut -c 4-) + pvc=$(echo "$main" | awk '{print $3}') + full_path=$(find /mnt/"$pool"/ix-applications/releases/"$app"/volumes/ -maxdepth 0 | cut -c 6-) + zfs set mountpoint=legacy "$full_path""$pvc" + echo "$i unmounted" && rmdir /mnt/heavyscript/"$i" || echo "failed to unmount $i" + done + rmdir /mnt/heavyscript + sleep 3 + ;; + *) + echo "Invalid selection, \"$selection\" was not an option" + sleep 3 + continue + ;; + esac +done +} +export -f mount diff --git a/functions/script_create.sh b/functions/script_create.sh new file mode 100644 index 00000000..3f1c26e4 --- /dev/null +++ b/functions/script_create.sh @@ -0,0 +1,180 @@ +#!/bin/bash + + +script_create(){ +while true +do + clear -x + title + echo "Choose Your Update Type" + echo "-----------------------" + echo "1) -U | Update all applications, ignores versions" + echo "2) -u | Update all applications, does not update Major releases" + echo + echo "0) Exit" + echo + read -rt 120 -p "Type the Number or Flag: " current_selection || { echo -e "\nFailed to make a selection in time" ; exit; } + case $current_selection in + 0 | [Ee][Xx][Ii][Tt]) + echo "Exiting.." + exit + ;; + 1 | -U) + while true + do + echo -e "\nHow many applications do you want updating at the same time?" + read -rt 120 -p "Please type an integer greater than 0: " up_async || { echo -e "\nFailed to make a selection in time" ; exit; } + if [[ $up_async == 0 ]]; then + echo "Error: \"$up_async\" is less than 1" + echo "NOT adding it to the list" + sleep 3 + continue + elif ! [[ $up_async =~ ^[0-9]+$ ]]; then + echo "Error: \"$up_async\" is invalid, it needs to be an integer" + echo "NOT adding it to the list" + sleep 3 + continue + else + update_selection+=("-U" "$up_async") + break + fi + done + break + ;; + 2 | -u) + while true + do + echo -e "\nHow many applications do you want updating at the same time?" + read -rt 120 -p "Please type an integer greater than 0: " up_async || { echo -e "\nFailed to make a selection in time" ; exit; } + if [[ $up_async == 0 ]]; then + echo "Error: \"$up_async\" is less than 1" + echo "NOT adding it to the list" + sleep 3 + continue + elif ! [[ $up_async =~ ^[0-9]+$ ]]; then + echo "Error: \"$up_async\" is invalid, it needs to be an integer" + echo "NOT adding it to the list" + sleep 3 + continue + else + update_selection+=("-u" "$up_async") + break + fi + done + break + ;; + *) + echo "$current_selection was not an option, try again" && sleep 3 + continue + ;; + esac +done +while true +do + clear -x + title + echo "Update Options" + echo "--------------" + echo "1) -r | Roll-back applications if they fail to update" + echo "2) -i | Add application to ignore list" + echo "3) -S | Shutdown applications prior to updating" + echo "4) -v | verbose output" + echo "5) -t | Set a custom timeout in seconds when checking if either an App or Mountpoint correctly Started, Stopped or (un)Mounted. Defaults to 500 seconds" + echo + echo "Additional Options" + echo "------------------" + echo "6) -b | Back-up your ix-applications dataset" + echo "7) -s | sync catalog" + echo "8) -p | Prune unused/old docker images" + echo "9) --ignore-img | Ignore container image updates" + echo "10) --self-update | Updates HeavyScript prior to running any other commands" + echo + echo "99) Remove Update Options, Restart" + echo "00) Done making selections, proceed with update" + echo + echo "0) Exit" + echo + echo "Current Choices" + echo "---------------" + echo "bash heavy_script.sh ${update_selection[*]}" + echo + read -rt 600 -p "Type the Number or Flag: " current_selection || { echo -e "\nFailed to make a selection in time" ; exit; } + case $current_selection in + 0 | [Ee][Xx][Ii][Tt]) + echo "Exiting.." + exit + ;; + 00) + clear -x + echo "Running \"bash heavy_script.sh ${update_selection[*]}\"" + echo + exec bash "$script_name" "${update_selection[@]}" + exit + ;; + 1 | -r) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "-r" && echo -e "\"-r\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + update_selection+=("-r") + ;; + 2 | -i) + read -rt 120 -p "What is the name of the application we should ignore?: " up_ignore || { echo -e "\nFailed to make a selection in time" ; exit; } + ! [[ $up_ignore =~ ^[a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9])?$ ]] && echo -e "Error: \"$up_ignore\" is not a possible option for an application name" && sleep 3 && continue + update_selection+=("-i" "$up_ignore") + ;; + 3 | -S) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "-S" && echo -e "\"-S\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + update_selection+=("-S") + ;; + 4 | -v) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "-v" && echo -e "\"-v\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + update_selection+=("-v") + ;; + 5 | -t) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "-t" && echo -e "\"-t\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + echo "What do you want your timeout to be?" + read -rt 120 -p "Please type an integer: " up_timeout || { echo -e "\nFailed to make a selection in time" ; exit; } + ! [[ $up_timeout =~ ^[0-9]+$ ]] && echo -e "Error: \"$up_timeout\" is invalid, it needs to be an integer\nNOT adding it to the list" && sleep 3 && continue + update_selection+=("-t" "$up_timeout") + ;; + 6 | -b) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "-b" && echo -e "\"-b\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + echo "Up to how many backups should we keep?" + read -rt 120 -p "Please type an integer: " up_backups || { echo -e "\nFailed to make a selection in time" ; exit; } + ! [[ $up_backups =~ ^[0-9]+$ ]] && echo -e "Error: \"$up_backups\" is invalid, it needs to be an integer\nNOT adding it to the list" && sleep 3 && continue + [[ $up_backups == 0 ]] && echo -e "Error: Number of backups cannot be 0\nNOT adding it to the list" && sleep 3 && continue + update_selection+=("-b" "$up_backups") + ;; + 7 | -s) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "-s" && echo -e "\"-s\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + update_selection+=("-s") + ;; + 8 | -p) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "-p" && echo -e "\"-p\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + update_selection+=("-p") + ;; + 9 | --ignore-img ) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "--ignore-img" && echo -e "\"--ignore-img\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + update_selection+=("--ignore-img") + ;; + 10 | --self-update ) + printf '%s\0' "${update_selection[@]}" | grep -Fxqz -- "--self-update" && echo -e "\"--self-update\" is already on here, skipping" && sleep 3 && continue #If option is already on there, skip it + update_selection+=("--self-update") + ;; + 99) + count=2 + echo "restarting" + for i in "${update_selection[@]:2}" + do + unset "update_selection[$count]" + echo "$i removed" + ((count++)) + done + sleep 3 + continue + ;; + *) + echo "\"$current_selection\" was not an option, try again" && sleep 3 && continue + ;; + esac +done +} +export -f script_create diff --git a/functions/self_update.sh b/functions/self_update.sh new file mode 100644 index 00000000..32795d01 --- /dev/null +++ b/functions/self_update.sh @@ -0,0 +1,37 @@ +#!/bin/bash + + +args=("$@") +self_update() { + +git fetch --tags &>/dev/null +git reset --hard &>/dev/null +latest_ver=$(git describe --tags "$(git rev-list --tags --max-count=1)") +echo "🅂 🄴 🄻 🄵" +echo "🅄 🄿 🄳 🄰 🅃 🄴" +if [[ "$hs_version" != "$latest_ver" ]] ; then + echo "Found a new version of HeavyScript, updating myself..." + git checkout "$latest_ver" &>/dev/null + count=0 + for i in "${args[@]}" + do + [[ "$i" == "--self-update" ]] && unset "args[$count]" && break + ((count++)) + done + echo "Updating from: $hs_version" + echo "Updating To: $latest_ver" + echo "Changelog:" + curl --silent "https://api.github.com/repos/HeavyBullets8/heavy_script/releases/latest" | jq -r .body + echo + [[ -z ${args[*]} ]] && echo -e "No more arguments, exiting..\n\n" && exit + echo -e "Running the new version...\n\n" + sleep 5 + exec bash "$script_name" "${args[@]}" + # Now exit this old instance + exit +else + echo "HeavyScript is already the latest version:" + echo -e "$hs_version\n\n" +fi +} +export -f self_update diff --git a/functions/update_apps.sh b/functions/update_apps.sh new file mode 100644 index 00000000..6ad0d59f --- /dev/null +++ b/functions/update_apps.sh @@ -0,0 +1,363 @@ +#!/bin/bash + + +commander(){ +mapfile -t array < <(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,container_images_update_available,status' | tr -d " \t\r" | grep -E ",true($|,)" | sort) +echo -e "🅄 🄿 🄳 🄰 🅃 🄴 🅂" +[[ -z ${array[*]} ]] && echo "There are no updates available" && echo -e "\n" && return 0 || echo "Update(s) Available: ${#array[@]}" +echo "Asynchronous Updates: $update_limit" +[[ -z $timeout ]] && echo "Default Timeout: 500" && timeout=500 || echo "Custom Timeout: $timeout" +[[ "$timeout" -le 120 ]] && echo "Warning: Your timeout is set low and may lead to premature rollbacks or skips" +[[ $ignore_image_update == "true" ]] && echo "Image Updates: Disabled" || echo "Image Updates: Enabled" +pool=$(cli -c 'app kubernetes config' | grep -E "dataset\s\|" | awk -F '|' '{print $3}' | awk -F '/' '{print $1}' | tr -d " \t\n\r") + +index=0 +for app in "${array[@]}" +do + app_name=$(echo "$app" | awk -F ',' '{print $1}') #print out first catagory, name. + old_app_ver=$(echo "$app" | awk -F ',' '{print $4}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #previous/current Application MAJOR Version + new_app_ver=$(echo "$app" | awk -F ',' '{print $5}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #new Application MAJOR Version + old_chart_ver=$(echo "$app" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # Old Chart MAJOR version + new_chart_ver=$(echo "$app" | awk -F ',' '{print $5}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # New Chart MAJOR version + diff_app=$(diff <(echo "$old_app_ver") <(echo "$new_app_ver")) #caluclating difference in major app versions + diff_chart=$(diff <(echo "$old_chart_ver") <(echo "$new_chart_ver")) #caluclating difference in Chart versions + old_full_ver=$(echo "$app" | awk -F ',' '{print $4}') #Upgraded From + new_full_ver=$(echo "$app" | awk -F ',' '{print $5}') #Upraded To + + #Skip application if its on ignore list + if printf '%s\0' "${ignore[@]}" | grep -iFxqz "${app_name}" ; then + echo -e "\n$app_name\nIgnored, skipping" + unset "array[$index]" + #Skip appliaction if major update and not ignoreing major versions + elif [[ "$diff_app" != "$diff_chart" && $update_apps == "true" ]] ; then + echo -e "\n$app_name\nMajor Release, update manually" + unset "array[$index]" + # Skip update if application previously failed on this exact update version + elif grep -qs "^$app_name," failed 2>/dev/null; then + failed_ver=$(grep "^$app_name," failed | awk -F ',' '{print $2}') + if [[ "$failed_ver" == "$new_full_ver" ]] ; then + echo -e "\n$app_name\nSkipping previously failed version:\n$new_full_ver" + unset "array[$index]" + else + sed -i /"$app_name",/d failed + fi + #Skip Image updates if ignore image updates is set to true + elif [[ $old_full_ver == "$new_full_ver" && $ignore_image_update == "true" ]]; then + echo -e "\n$app_name\nImage update, skipping.." + unset "array[$index]" + fi + ((index++)) +done +array=("${array[@]}") +[[ ${#array[@]} == 0 ]] && echo && echo && return + + +index=0 +while_count=0 +rm deploying 2>/dev/null +rm finished 2>/dev/null +while [[ ${#processes[@]} != 0 || $(wc -l finished 2>/dev/null | awk '{ print $1 }') -lt "${#array[@]}" ]] +do + if while_status=$(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,container_images_update_available,status' 2>/dev/null) ; then + ((while_count++)) + [[ -z $while_status ]] && continue || echo -e "$while_count\n$while_status" > all_app_status + mapfile -t deploying_check < <(grep ",DEPLOYING," all_app_status) + for i in "${deploying_check[@]}" + do + app_name=$(echo "$i" | awk -F ',' '{print $1}') + [[ ! -e deploying ]] && touch deploying + if ! grep -qs "$app_name,DEPLOYING" deploying; then + echo "$app_name,DEPLOYING" >> deploying + fi + done + else + echo "Middlewared timed out. Consider setting a lower number for async applications" + continue + fi + count=0 + for proc in "${processes[@]}" + do + kill -0 "$proc" &> /dev/null || unset "processes[$count]" + ((count++)) + done + processes=("${processes[@]}") + if [[ $index -lt ${#array[@]} && "${#processes[@]}" -lt "$update_limit" ]]; then + pre_process "${array[$index]}" & + processes+=($!) + ((index++)) + else + sleep 3 + fi +done +rm deploying 2>/dev/null +rm finished 2>/dev/null +echo +echo +} +export -f commander + + +pre_process(){ +app_name=$(echo "${array[$index]}" | awk -F ',' '{print $1}') #print out first catagory, name. +startstatus=$(echo "${array[$index]}" | awk -F ',' '{print $2}') #status of the app: STOPPED / DEPLOYING / ACTIVE +old_full_ver=$(echo "${array[$index]}" | awk -F ',' '{print $4}') #Upgraded From +new_full_ver=$(echo "${array[$index]}" | awk -F ',' '{print $5}') #Upraded To +rollback_version=$(echo "${array[$index]}" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}') + + +# Check if app is external services, append outcome to external_services file +[[ ! -e external_services ]] && touch external_services +if ! grep -qs "^$app_name," external_services ; then + if ! grep -qs "/external-service" /mnt/"$pool"/ix-applications/releases/"$app_name"/charts/"$(find /mnt/"$pool"/ix-applications/releases/"$app_name"/charts/ -maxdepth 1 -type d -printf '%P\n' | sort -r | head -n 1)"/Chart.yaml; then + echo "$app_name,false" >> external_services + else + echo "$app_name,true" >> external_services + fi +fi + +# If application is deploying prior to updating, attempt to wait for it to finish +if [[ "$startstatus" == "DEPLOYING" ]]; then + SECONDS=0 + while [[ "$status" == "DEPLOYING" ]] + do + status=$(grep "^$app_name," all_app_status | awk -F ',' '{print $2}') + if [[ "$SECONDS" -ge "$timeout" ]]; then + echo_array+=("Application is stuck Deploying, Skipping to avoid damage") + echo_array + return + fi + sleep 5 + done +fi + +# If user is using -S, stop app prior to updating +echo_array+=("\n$app_name") +if [[ $stop_before_update == "true" && "$startstatus" != "STOPPED" ]]; then # Check to see if user is using -S or not + [[ "$verbose" == "true" ]] && echo_array+=("Stopping prior to update..") + if stop_app ; then + echo_array+=("Stopped") + else + echo_array+=("Error: Failed to stop $app_name") + echo_array + return 1 + fi +fi + +# Send app through update function +[[ "$verbose" == "true" ]] && echo_array+=("Updating..") +if update_app ;then + if [[ $old_full_ver == "$new_full_ver" ]]; then + echo_array+=("Updated Container Image") + else + echo_array+=("Updated\n$old_full_ver\n$new_full_ver") + fi +else + echo_array+=("Failed to update\nManual intervention may be required") + echo_array + return +fi + +# If app is external services, do not send for post processing +if grep -qs "^$app_name,true" external_services ; then + echo_array + return +# If app is container image update, dont send for post processing +elif [[ $old_full_ver == "$new_full_ver" ]]; then + echo_array + return +else + post_process +fi +} +export -f pre_process + + +post_process(){ +SECONDS=0 +count=0 +if [[ $rollback == "true" || "$startstatus" == "STOPPED" ]]; then + while true + do + + # If app reports ACTIVE right away, assume its a false positive and wait for it to change, or trust it after 5 updates to all_app_status + status=$(grep "^$app_name," all_app_status | awk -F ',' '{print $2}') + if [[ $count -lt 1 && $status == "ACTIVE" && "$(grep "^$app_name," deploying 2>/dev/null | awk -F ',' '{print $2}')" != "DEPLOYING" ]]; then # If status shows up as Active or Stopped on the first check, verify that. Otherwise it may be a false report.. + [[ "$verbose" == "true" ]] && echo_array+=("Verifying $status..") + before_loop=$(head -n 1 all_app_status) + current_loop=0 + until [[ "$status" != "ACTIVE" || $current_loop -gt 4 ]] # Wait for a specific change to app status, or 3 refreshes of the file to go by. + do + status=$(grep "^$app_name," all_app_status | awk -F ',' '{print $2}') + sleep 1 + if ! echo -e "$(head -n 1 all_app_status)" | grep -qs ^"$before_loop" ; then + before_loop=$(head -n 1 all_app_status) + ((current_loop++)) + fi + done + fi + (( count++ )) + + if [[ "$status" == "ACTIVE" ]]; then + if [[ "$startstatus" == "STOPPED" ]]; then + [[ "$verbose" == "true" ]] && echo_array+=("Returing to STOPPED state..") + if stop_app ; then + echo_array+=("Stopped") + else + echo_array+=("Error: Failed to stop $app_name") + echo_array + return 1 + fi + break + else + echo_array+=("Active") + break + fi + elif [[ "$SECONDS" -ge "$timeout" ]]; then + if [[ $rollback == "true" ]]; then + if [[ "$failed" != "true" ]]; then + echo "$app_name,$new_full_ver" >> failed + echo_array+=("Error: Run Time($SECONDS) for $app_name has exceeded Timeout($timeout)") + echo_array+=("If this is a slow starting application, set a higher timeout with -t") + echo_array+=("If this applicaion is always DEPLOYING, you can disable all probes under the Healthcheck Probes Liveness section in the edit configuration") + echo_array+=("Reverting update..") + if rollback_app ; then + echo_array+=("Rolled Back") + else + echo_array+=("Error: Failed to rollback $app_name\nAbandoning") + echo_array + return 1 + fi + failed="true" + SECONDS=0 + count=0 + continue #run back post_process function if the app was stopped prior to update + else + echo_array+=("Error: Run Time($SECONDS) for $app_name has exceeded Timeout($timeout)") + echo_array+=("The application failed to be ACTIVE even after a rollback") + echo_array+=("Manual intervention is required\nStopping, then Abandoning") + if stop_app ; then + echo_array+=("Stopped") + else + echo_array+=("Error: Failed to stop $app_name") + echo_array + return 1 + fi + break + fi + else + echo "$app_name,$new_full_ver" >> failed + echo_array+=("Error: Run Time($SECONDS) for $app_name has exceeded Timeout($timeout)") + echo_array+=("If this is a slow starting application, set a higher timeout with -t") + echo_array+=("If this applicaion is always DEPLOYING, you can disable all probes under the Healthcheck Probes Liveness section in the edit configuration") + echo_array+=("Manual intervention is required\nStopping, then Abandoning") + if stop_app ; then + echo_array+=("Stopped") + else + echo_array+=("Error: Failed to stop $app_name") + echo_array + return 1 + fi + break + fi + else + [[ "$verbose" == "true" ]] && echo_array+=("Waiting $((timeout-SECONDS)) more seconds for $app_name to be ACTIVE") + sleep 5 + continue + fi + done +fi +echo_array +} +export -f post_process + + +rollback_app(){ +count=0 +app_update_avail=$(grep "^$app_name," all_app_status | awk -F ',' '{print $3}') +while [[ $app_update_avail == "false" ]] +do + app_update_avail=$(grep "^$app_name," all_app_status | awk -F ',' '{print $3}') + if [[ $count -gt 2 ]]; then # If failed to rollback app 3 times, return failure to parent shell + return 1 + elif ! cli -c "app chart_release rollback release_name=\"$app_name\" rollback_options={\"item_version\": \"$rollback_version\"}" &> /dev/null ; then + before_loop=$(head -n 1 all_app_status) + ((count++)) + until [[ $(head -n 1 all_app_status) != "$before_loop" ]] # Upon failure, wait for status update before continuing + do + sleep 1 + done + else + break + fi +done +} + + +update_app(){ +current_loop=0 +while true +do + update_avail=$(grep "^$app_name," all_app_status | awk -F ',' '{print $3","$6}') + if [[ $update_avail =~ "true" ]]; then + if ! cli -c 'app chart_release upgrade release_name=''"'"$app_name"'"' &> /dev/null ; then + before_loop=$(head -n 1 all_app_status) + current_loop=0 + until [[ "$(grep "^$app_name," all_app_status | awk -F ',' '{print $3","$6}')" != "$update_avail" ]] # Wait for a specific change to app status, or 3 refreshes of the file to go by. + do + if [[ $current_loop -gt 2 ]]; then + cli -c 'app chart_release upgrade release_name=''"'"$app_name"'"' &> /dev/null || return 1 # After waiting, attempt an update once more, if fails, return error code + elif ! echo -e "$(head -n 1 all_app_status)" | grep -qs ^"$before_loop" ; then # The file has been updated, but nothing changed specifically for the app. + before_loop=$(head -n 1 all_app_status) + ((current_loop++)) + fi + sleep 1 + done + fi + break + elif [[ ! $update_avail =~ "true" ]]; then + break + else + sleep 3 + fi +done +} +export -f update_app + + +stop_app(){ +count=0 +while [[ "$status" != "STOPPED" ]] +do + status=$( grep "^$app_name," all_app_status | awk -F ',' '{print $2}') + if [[ $count -gt 2 ]]; then # If failed to stop app 3 times, return failure to parent shell + return 1 + elif ! cli -c 'app chart_release scale release_name='\""$app_name"\"\ 'scale_options={"replica_count": 0}' &> /dev/null ; then + before_loop=$(head -n 1 all_app_status) + ((count++)) + until [[ $(head -n 1 all_app_status) != "$before_loop" ]] # Upon failure, wait for status update before continuing + do + sleep 1 + done + else + break + fi +done +} +export -f stop_app + + +echo_array(){ +#Dump the echo_array, ensures all output is in a neat order. +for i in "${echo_array[@]}" +do + echo -e "$i" +done +final_check +} +export -f echo_array + + +final_check(){ + [[ ! -e finished ]] && touch finished + echo "$app_name,finished" >> finished +} diff --git a/heavy_script.sh b/heavy_script.sh new file mode 100644 index 00000000..065a4c76 --- /dev/null +++ b/heavy_script.sh @@ -0,0 +1,161 @@ +#!/bin/bash + + +# cd to script, this ensures the script can find the source scripts below, even when ran from a seperate directory +script=$(readlink -f "$0") +script_path=$(dirname "$script") +script_name="heavy_script.sh" +cd "$script_path" || { echo "Error: Failed to change to script directory" ; exit ; } + +#Version +hs_version=$(git describe --tags) + +source functions/backup.sh +source functions/dns.sh +source functions/menu.sh +source functions/misc.sh +source functions/mount.sh +source functions/self_update.sh +source functions/update_apps.sh +source functions/cmd_to_container.sh +source functions/script_create.sh + + +#If no argument is passed, kill the script. +[[ -z "$*" || "-" == "$*" || "--" == "$*" ]] && menu + + +# Parse script options +while getopts ":si:rb:t:uUpSRv-:" opt +do + case $opt in + -) + case "${OPTARG}" in + help) + help="true" + ;; + self-update) + self_update="true" + ;; + dns) + dns="true" + ;; + cmd) + cmd="true" + ;; + restore) + restore="true" + ;; + mount) + mount="true" + ;; + delete-backup) + deleteBackup="true" + ;; + ignore-img) + ignore_image_update="true" + ;; + *) + echo -e "Invalid Option \"--$OPTARG\"\n" + help + ;; + esac + ;; + :) + echo -e "Option: \"-$OPTARG\" requires an argument\n" + help + ;; + b) + number_of_backups=$OPTARG + ! [[ $OPTARG =~ ^[0-9]+$ ]] && echo -e "Error: -b needs to be assigned an interger\n\"""$number_of_backups""\" is not an interger" >&2 && exit + [[ "$number_of_backups" -le 0 ]] && echo "Error: Number of backups is required to be at least 1" && exit + ;; + r) + rollback="true" + ;; + i) + if ! [[ $OPTARG =~ ^[a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9])?$ ]]; then # Using case insensitive version of the regex used by Truenas Scale + echo -e "Error: \"$OPTARG\" is not a possible option for an application name" + exit + else + ignore+=("$OPTARG") + fi + ;; + t) + timeout=$OPTARG + ! [[ $timeout =~ ^[0-9]+$ ]] && echo -e "Error: -t needs to be assigned an interger\n\"""$timeout""\" is not an interger" >&2 && exit + ;; + s) + sync="true" + ;; + U) + update_all_apps="true" + # Check next positional parameter + eval nextopt=${!OPTIND} + # existing or starting with dash? + if [[ -n $nextopt && $nextopt != -* ]] ; then + OPTIND=$((OPTIND + 1)) + update_limit="$nextopt" + else + update_limit=1 + fi + ;; + u) + update_apps="true" + # Check next positional parameter + eval nextopt=${!OPTIND} + # existing or starting with dash? + if [[ -n $nextopt && $nextopt != -* ]] ; then + OPTIND=$((OPTIND + 1)) + update_limit="$nextopt" + else + update_limit=1 + fi + ;; + S) + stop_before_update="true" + ;; + p) + prune="true" + ;; + v) + verbose="true" + ;; + *) + echo -e "Invalid Option \"-$OPTARG\"\n" + help + ;; + esac +done + + +#exit if incompatable functions are called +[[ "$update_all_apps" == "true" && "$update_apps" == "true" ]] && echo -e "-U and -u cannot BOTH be called" && exit + +#Continue to call functions in specific order +[[ "$self_update" == "true" ]] && self_update +[[ "$help" == "true" ]] && help +[[ "$cmd" == "true" ]] && cmd_to_container && exit +[[ "$deleteBackup" == "true" ]] && deleteBackup && exit +[[ "$dns" == "true" ]] && dns && exit +[[ "$restore" == "true" ]] && restore && exit +[[ "$mount" == "true" ]] && mount && exit +if [[ "$number_of_backups" -gt 1 && "$sync" == "true" ]]; then # Run backup and sync at the same time + echo "🅃 🄰 🅂 🄺 🅂 :" + echo -e "-Backing up ix-applications Dataset\n-Syncing catalog(s)" + echo -e "This can take a LONG time, Please Wait For Both Output..\n\n" + backup & + sync & + wait +elif [[ "$number_of_backups" -gt 1 && -z "$sync" ]]; then # If only backup is true, run it + echo "🅃 🄰 🅂 🄺 :" + echo -e "-Backing up \"ix-applications\" Dataset\nPlease Wait..\n\n" + backup +elif [[ "$sync" == "true" && -z "$number_of_backups" ]]; then # If only sync is true, run it + echo "🅃 🄰 🅂 🄺 :" + echo -e "Syncing Catalog(s)\nThis Takes a LONG Time, Please Wait..\n\n" + sync +fi +[[ "$update_all_apps" == "true" || "$update_apps" == "true" ]] && commander +[[ "$prune" == "true" ]] && prune +exit 0 diff --git a/includes/backup.sh b/includes/backup.sh old mode 100755 new mode 100644 diff --git a/includes/chores.sh b/includes/chores.sh old mode 100755 new mode 100644 diff --git a/includes/colors.sh b/includes/colors.sh old mode 100755 new mode 100644 diff --git a/includes/dns.sh b/includes/dns.sh old mode 100755 new mode 100644 diff --git a/includes/help.sh b/includes/help.sh old mode 100755 new mode 100644 diff --git a/includes/mount.sh b/includes/mount.sh old mode 100755 new mode 100644 diff --git a/includes/no_args.sh b/includes/no_args.sh old mode 100755 new mode 100644 diff --git a/includes/title.sh b/includes/title.sh old mode 100755 new mode 100644 diff --git a/includes/update.sh b/includes/update.sh old mode 100755 new mode 100644 diff --git a/includes/update_self.sh b/includes/update_self.sh old mode 100755 new mode 100644 diff --git a/truetool.sh b/truetool.sh old mode 100755 new mode 100644