|
| 1 | +# PHP OVH CLI |
| 2 | + |
| 3 | +A simple [OVH API](https://api.ovh.com/console/) client for managing OVH infrastructure, written with PHP. |
| 4 | + |
| 5 | +## Features |
| 6 | +**General** |
| 7 | +- Configuration wizard |
| 8 | +- Cache support |
| 9 | +- PHAR package |
| 10 | +- Easy to extend with new commands! |
| 11 | +- Safe mode (dry-run) |
| 12 | + |
| 13 | +**API** |
| 14 | +- Application management |
| 15 | + |
| 16 | +**Dedicated Servers** |
| 17 | +- Search by name/reverse (using RegExps) |
| 18 | +- Retrieve information |
| 19 | +- Configure boot mode |
| 20 | +- Enable/disable OVH monitoring |
| 21 | +- Access KVM console (requires Java-WS) |
| 22 | +- Perform IPMI reset |
| 23 | +- Request hardware reboot |
| 24 | + |
| 25 | +**vRack** |
| 26 | +- List associated servers |
| 27 | +- Assign servers to vRack |
| 28 | + |
| 29 | +**DNS** |
| 30 | +- Reverse DNS management |
| 31 | +- Automatically resolve reverse hostname to OVH `nsXXX` hostname |
| 32 | + |
| 33 | + |
| 34 | +## Requirements |
| 35 | + |
| 36 | +* PHP >= 7.0 |
| 37 | +* PHP gmp extension (used by [rlanvin/php-ip](https://github.com/rlanvin/php-ip)) |
| 38 | +* [Composer](https://getcomposer.org) |
| 39 | + |
| 40 | +## Install |
| 41 | + |
| 42 | +```text |
| 43 | +$ git clone https://github.com/miklinux/php-ovh-cli.git |
| 44 | +$ cd php-ovh-cli |
| 45 | +$ composer update |
| 46 | +``` |
| 47 | + |
| 48 | +## Configuration |
| 49 | +To interact with OVH API you must already have an account at OVH. |
| 50 | +The script will take care of requesting an API token for you. |
| 51 | + |
| 52 | +```text |
| 53 | +$ ./ovh-cli api:setup |
| 54 | +Would you like me to open the browser and create a new OVH API token? [N]: |
| 55 | +``` |
| 56 | +If you answer *YES*, the script will attempt to open the browser to a page where you can generate an OVH API token, pre-populating some fields. |
| 57 | +In case you would like to do that manually here's the link: https://api.ovh.com/createToken |
| 58 | + |
| 59 | +```text |
| 60 | +Application key [xxx]: |
| 61 | +Application secret [xxx]: |
| 62 | +Consumer key [xxx]: |
| 63 | +Endpoint [ovh-eu]: |
| 64 | +JavaWS binary [/usr/bin/javaws]: |
| 65 | +Cache TTL in seconds [3600]: |
| 66 | +
|
| 67 | +Configuration file written: /home/user/.ovh-cli.config.json |
| 68 | +``` |
| 69 | + |
| 70 | +Once the script is configured you can test if it's working properly: |
| 71 | +```text |
| 72 | +$ ./ovh-cli api:test |
| 73 | +Hi John Doe, OVH API are working properly ;) |
| 74 | +``` |
| 75 | + |
| 76 | +## Usage |
| 77 | +Once the script is correctly configured, you can interact with OVH API using this script. |
| 78 | +Please refer to its help for all the information you need. |
| 79 | + |
| 80 | +```text |
| 81 | +$ ./ovh-cli --help |
| 82 | +``` |
| 83 | + |
| 84 | +**NOTE:** If you're concerned about damaging your systems by improper use of this tool, use the `--dry-run` or `-t` command line switch. |
| 85 | +This will prevent the execution of potentially harmful **PUT/POST/DELETE** requests, so only **GET**s will be executed. |
| 86 | + |
| 87 | +## Development |
| 88 | +This script has been developed to be easily extended, so if you want to contribute and/or add your own commands, |
| 89 | +you can do it without particular efforts. This project has been structured in this way: |
| 90 | + |
| 91 | +| Path | Description | |
| 92 | +|-----------------|----------------------------------------------------------------------| |
| 93 | +| ovh-cli | Simple executable | |
| 94 | +| cli.php | Main script (to be used with PHAR) | |
| 95 | +| create-phar.php | Package the script into an executable: `ovh-cli` | |
| 96 | +| src/ | Project directory | |
| 97 | +| src/Cli.php | Contains all the CLI decorators (colors, prompts, formatting, ...) | |
| 98 | +| src/Command.php | Class `OvhCli\Command`: contains all common command logic | |
| 99 | +| src/Config.php | Class `OvhCli\Config`: manages configuration file | |
| 100 | +| src/Ovh.php | Class `OvhCli\Ovh`: contains wrappers to OVH API calls | |
| 101 | +| src/Command/ | Directory containing all the available commands | |
| 102 | + |
| 103 | +### Creating new command |
| 104 | +See: http://getopt-php.github.io/getopt-php/ |
| 105 | + |
| 106 | +In this example we will create a new `hello:world` command. |
| 107 | +``` |
| 108 | +$ cd src/Command |
| 109 | +$ mkdir Hello |
| 110 | +$ touch Hello/World.php |
| 111 | +``` |
| 112 | + |
| 113 | +**World.php** |
| 114 | +```php |
| 115 | +<?php |
| 116 | + |
| 117 | +// The namespace should match the directory structure |
| 118 | +namespace OvhCli\Command\Hello; |
| 119 | + |
| 120 | +use GetOpt\GetOpt; |
| 121 | +use GetOpt\Operand; |
| 122 | +use GetOpt\Option; |
| 123 | +use GetOpt\Argument; |
| 124 | +use OvhCli\Cli; |
| 125 | + |
| 126 | +class World extends \OvhCli\Command |
| 127 | +{ |
| 128 | + // Command description, used for help |
| 129 | + public $shortDescription = "This is my hello world command"; |
| 130 | + // Optional. Add some usage examples. |
| 131 | + public $usageExamples = [ |
| 132 | + '-a' => 'Do action A', |
| 133 | + '-b' => 'Do action B', |
| 134 | + ]; |
| 135 | + |
| 136 | + // Here you will define command options/operands |
| 137 | + public function __construct() { |
| 138 | + parent::__construct($this->getName(), [$this, 'handle']); |
| 139 | + $this->addOptions([ |
| 140 | + Option::create('a', 'action-a', GetOpt::NO_ARGUMENT) |
| 141 | + ->setDescription('Perform action A'), |
| 142 | + Option::create('b', 'action-b', GetOpt::NO_ARGUMENT) |
| 143 | + ->setDescription('Perform action B'), |
| 144 | + ]); |
| 145 | + } |
| 146 | + |
| 147 | + // This function will contain the main logic of your command |
| 148 | + public function handle(GetOpt $getopt) { |
| 149 | + $a = $getopt->getOption('action-a'); |
| 150 | + $b = $getopt->getOption('action-b'); |
| 151 | + |
| 152 | + if ($a) { |
| 153 | + print "This is action A\n"; |
| 154 | + } elseif($b) { |
| 155 | + print "This is action A\n"; |
| 156 | + } else { |
| 157 | + print "No action specified. Use --help!\n"; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +You can then find your new command in the help (output omitted for brevity): |
| 164 | +``` |
| 165 | +$ ./ovh-cli --help |
| 166 | +Usage: ./ovh-cli <command> [options] [operands] |
| 167 | +
|
| 168 | +Options: |
| 169 | + -?, --help Show this help and quit |
| 170 | +
|
| 171 | +Commands: |
| 172 | + hello:world This is my hello world command |
| 173 | +``` |
| 174 | + |
| 175 | +Get your new command help: |
| 176 | +``` |
| 177 | +$ ./ovh-cli hello:world --help |
| 178 | +Usage: ./ovh-cli hello:world [options] [operands] |
| 179 | +
|
| 180 | +This is my hello world command |
| 181 | +
|
| 182 | +Examples: |
| 183 | + hello:world -a Do action A |
| 184 | + hello:world -b Do action B |
| 185 | +
|
| 186 | +Options: |
| 187 | + -?, --help Show this help and quit |
| 188 | + -a, --action-a Perform action A |
| 189 | + -b, --action-b Perform action B |
| 190 | +``` |
| 191 | + |
| 192 | +And then, simply execute it! |
| 193 | +```text |
| 194 | +$ ./ovh-cli hello:world |
| 195 | +No action specified. Use --help! |
| 196 | +$ ./ovh-cli hello:world -a |
| 197 | +This is action A |
| 198 | +$ ./ovh-cli hello:world -b |
| 199 | +This is action A |
| 200 | +``` |
0 commit comments