⬆️ ⬇️

CLI combine for Plesk



This article focuses on the Plesk hosting control panel and CLI interfaces, however, the information may be useful in general in terms of creating convenient CLI applications.



So it turns out that while administering the Plesk hosting control panel, despite the web interface, I still spend quite a lot of time in the console. In the end, if I want to have a look at the rights to the file inside the vasya.com domain, not in the same file manager go. If you take the battle server under a good load, then you want to look at some things even more from the console, instead of the web interface.



On the one hand, while administering Unix / Linux systems on a regular basis, you get used to using automit and aliases, and you start writing bash scripts for small routine tasks. On the other hand, if you look at Plesk, then it has a rather rich CLI interface, which is not found in every control panel. But the strange thing is to use it, to put it mildly, is not very convenient, and for many typical administrative tasks it is generally useless.



The desire to somehow influence the situation and make some improvements in the CLI matured long ago. Spending half the time in the console, you type and type the next commands. If you look closely at these commands, you can notice that these same commands are in the history file of other Plesk administrators. And the teams are very, very wordy:

')

# mysql -uadmin -p`cat /etc/psa/.psa.shadow` psa # /usr/local/psa/bin/domain ... # /usr/local/psa/bin/sw-engine-pleskrun /usr/local... # tail -f /var/log/sw-cp-server/error_log ... 


The previous points mentioned were the motivation to try to change something. In addition, the solution came to me at the beginning of the year that I came across a book about creating a command line application in Ruby (it was already somehow promoted in Habré). The book, by the way, I liked the fact that the water there was at a minimum, but mostly there were examples of how to make good console applications. I will not say that I learned a lot of new things, but the book systematized knowledge, laying out things on the shelves and serving as an additional incentive to apply this knowledge.



The last motivating factor was that I wanted to make a tool that would be useful in my daily work for myself.



This is how a console utility appeared with a very obvious name - " plesk ", which was designed to simplify the execution of a number of frequent operations in the CLI. The utility comes as standard Plesk 11.5.



But back to the theory. If you look at the Unix / Linux utilities, they can be divided into two classes: simple commands and command suites. When the number of options begins to exceed a reasonable limit and there is an opportunity to split the toolkit into separate areas, at this moment it becomes necessary to form the command suite. An example of a simple command is ls (there are many options, but the command is one), and examples of command suites are git or svn (there are many subcommands).



In the context of Plesk, one of the problems was as follows. There are more than a hundred different utilities in the /usr/local/psa/bin/ . I don't really want to type the prefix /usr/local/psa/bin/ each time. Do " cd /usr/local/psa/bin/ " too. Cramming /usr/local/psa/bin/ in PATH is an even less nice idea, because there will be chaos in PATH and it will be impossible to understand whether this is a standard utility or a utility from Plesk. Throwing out the old CLI interface and writing a completely new one over the coming weekend is utopian both in terms of time and in terms of backward compatibility. The logical answer to this problem is to make the command suite a wrapper over the standard interface. The utility /usr/local/psa/bin/domain will be called as " plesk bin domain ". In order not to memorize the names of the plesk utility subcommands and the available utilities from the bin, add the bash completion. To make it even more convenient, add bash completion for the commands / options of the utilities themselves from bin.



Previously, in order to create a client from the console, I did something like the following. Did call help and looked options. Very often I remember approximate names and I understand what I want, but not the exact spelling of a specific option:



 # /usr/local/psa/bin/client -h Usage: client command <login_name> .. Available commands: --create or -c <login_name> Creates a new customer account.. 


After that, I typed the command I needed (going back to the reference information along the way, since not all options can be immediately remembered):



 # /usr/local/psa/bin/client --create ... 


Now, instead, rather often press Tab;)



 # plesk bin cl<TAB> client client_pref cloning # plesk bin client --create<TAB> --create --create-gapps-account 


Nothing, as they say, supernatural, but I am now able to dial the team to create a client without looking in the help.



The next aspect is shortcuts for partly typed but long teams.



Instead of typing a rather long command to get into the console MySQL client:



 # mysql -uadmin -p`cat /etc/psa/.psa.shadow` psa 


Now, just dial:



 # plesk db 


Sometimes it is necessary to find out the version of Plesk on the machine. The architecture of the machine. The name of the distribution. And the version of micro-update, if installed, will not hurt to learn. To do this, you will have to type a lot of commands:



 # cat /etc/issue CentOS release 5.9 (Final).. # cat /usr/local/psa/version 11.5.22 CentOS 5 115130325.19 # cat /usr/local/psa/.revision 319415 # uname -a Linux hp-demo... # cat /root/.autoinstaller/microupdates.xml ... 


Now it’s enough to type one small team and get all the information at once:



 # plesk version Product version: 11.5.30 Update #6 Update date: 2013/07/19 07:42 Build date: 2013/07/11 12:00 Build target: Debian 6.0 Revision: 323071 Architecture: 32-bit Wrapper version: 1.0 


But simple shortcuts are few, so go ahead. Suppose we want to look at the contents of a particular table. Enough to dial:



 # plesk db show misc +-------------------------------------+----------------------------------------------------------------+ | param | val | +-------------------------------------+----------------------------------------------------------------+ | FullHostName | unknown | | actionlog_rot_num_periods | 30 | ... 


If the output exceeds the size of the screen, then paging will be used. Those who used svn log and git log will understand what difference there is. You can achieve this behavior, for example, using the command "| less --quit-if-one-screen --no-init".



The panel has various configuration files. You can also simplify their opening for editing:



 # plesk conf panel.ini 


And we are already in Vim and can make changes. This is achieved with the usual proc_open.



Few know that the tail command can monitor multiple files at the same time. Using this feature, a command was created to simultaneously monitor all log files to which the panel writes:



 # plesk log --all Log files: /usr/local/psa/var/log/maillog /var/log/sw-cp-server/.. ==> /usr/local/psa/var/log/maillog <== Jul 22 17:25:43 ay postfix/anvil[25376]: statistics: max connection count 1 for (smtp:1.164.98.84) at Jul 22 17:22:21 .. ==> /var/log/sw-cp-server/error_log <== ==> /usr/local/psa/admin/logs/httpsd_access_log <== ... 


A fresh look at old problems gives interesting results. In 370 lines of the wrapper code, quite a few ideas fit in in an effort to simplify certain moments.



Another important one is documentation.



If you call the utility without parameters, then a brief reference should be issued. However, the certificate should not exactly look like " Usage: command [options] " (some of Plesk’s regular utilities suffer from this). Still, some details will not hurt, namely, the listing of options, the list of subcommands, if any. For command suites, there must be a help subcommand and the ability to execute a command help subcommand. In essence, this is a substitute for a manual with the ability to filter information. And of course, true Unix-utility must have a man-page. Yes, now you can type " man plesk " :) In general, it is thanks to man that the user can learn the subtleties of the utility and get additional information, such as usage examples, combinations of options, etc. If you set out to create your own command suite, then do not forget about usage, help subcommand and man page. Although writing a certificate is one of the habits that programmers dislike.



Returning to the utility " plesk ". Any person has the opportunity to participate in its development. Plesk is developing a very small development team. On the other hand, administrators using Plesk are several orders of magnitude larger. Since the utility is designed primarily to simplify life in the CLI for administrators, it comes with open source, and the mirror repository is on GitHub - github.com/sibprogrammer/plesk-ctl If you have some great idea and you you can implement it, that is, the chance that this functionality will be in the upstream. To minimize dependencies, the utility was written in PHP, the language used for the web interface of the panel (even though I am an ardent supporter of Ruby and JS :)). On the other hand, PHP is quite popular, so the choice of this language can only contribute to the development of the utility.



PS For lovers of visual perception - a presentation on SlideShare .



Source: https://habr.com/ru/post/187092/



All Articles