PHP code example of th0rn0 / laravel-rancher

1. Go to this page and download the library: Download th0rn0/laravel-rancher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

th0rn0 / laravel-rancher example snippets


'providers' => [
    // ... other providers
    Benmag\Rancher\RancherServiceProvider::class,
]



namespace App\Http\Controllers;

use Rancher;
use App\Http\Controllers\Controller;

class HostController extends Controller
{
    public function index()
    {
        return response()->json(Rancher::host()->all());
    }
}

Rancher::host()->get("1h1");

Rancher::host()->activate("1h1");

Rancher::host()->evacuate("1h1");

Rancher::host()->remove("1h1");
 
Rancher::container()->all();
 
Rancher::container()->get("1i140");

use Rancher;
use Benmag\Rancher\Factories\Entity\Container;

$newContainer = new Container([
    'name' => 'HelloWorld',
    'description' => 'New container created via Laravel Rancher',
    'imageUuid' => "docker:ubuntu:14.04.3",
]);

Rancher::container()->create($newContainer);

use Rancher;
use Benmag\Rancher\Factories\Entity\Stack;

$newStack = new Stack([
    'name' => "my-stack",
    'description' => 'An example stack',
    'dockerCompose' => "odoo:\n  image: odoo\n  ports:\n    - \"8069:8069\"\n  links:\n    - db\ndb:\n  image: postgres\n  environment:\n    - POSTGRES_USER=odoo\n    - POSTGRES_PASSWORD=odoo\n",
    'rancherCompose' => ".catalog:\n  name: \"Odoo\"\n  version: \"0.1-educaas\"\n  description: \"ERP management powered by Odoo\"\n  uuid: odoo-0\n  questions:\n\nodoo:\n",
    'externalId' => "catalog://community:odoo:0",
    'startOnCreate' => true
]);

Rancher::stack()->project('1a8')->create($newStack);

use Rancher;
use Benmag\Rancher\Factories\Entity\Stack;

$updatedStack = new Stack([
    'name' => "my-updated-stack",
    'description' => 'An updated stack',
]);

Rancher::stack()->update("1st312", $updatedStack);

Rancher::stack()->activateServices("1st312");

Rancher::stack()->deactivateServices("1st312");

Rancher::stack()->delete("1st312");

use Rancher;
use Benmag\Rancher\Factories\Entity\Project;

$project = new Project([
    'name' => 'Hello World',
]);

Rancher::project()->create($project);

Rancher::project()->activate("1a8");

Rancher::project()->deactivate("1a8");

Rancher::project()->delete("1a8");

use Rancher; 
use Benmag\Rancher\Factories\Entity\Service;

$newService = new Service([
    'name' => 'newService',
    'stackId' => '1st312',
    'launchConfig' => [
        'stdinOpen' => true,
        'imageUuid' => 'docker:ubuntu:14.04.3'
    ],
]);

Rancher::service()->project("1a8")->create($newService);

use Rancher;
use Benmag\Rancher\Factories\Entity\Service;

$updatedService = new Service([
    "description" => "I was updated",
    "name" => "db",
    "scale" => 1
]);

Rancher::service()->project("1a8")->update("1s23", $updatedService);

use Rancher;

$serviceLink = ['name' => 'redis', 'serviceId' => '1s25'];

Rancher::service()->addServiceLink("1s24", $serviceLink);

use Rancher;

$serviceLinks = [
    ['name' => 'db', 'serviceId' => '1s23']
];

Rancher::service()->setServiceLinks("1s24", $serviceLinks);

use Rancher;

$remove = ['name' => 'db', 'serviceId' => '1s23'];

Rancher::service()->removeServiceLink("1s24", $remove);

use Rancher;

$serviceUpgrade = [
    'inServiceStrategy' => [
        'batchSize' => 1,
        'intervalMillis' => 2000,
        'startFirst' => false,
        'launchConfig' => [
            'imageUuid' => 'docker:postgres',
            'startOnCreate' => true,
        ]
    ]
];

Rancher::service()->upgrade("1s23", $serviceUpgrade);

Rancher::service()->finishUpgrade("1s23");

Rancher::service()->cancelUpgrade("1s23");

Rancher::service()->rollback("1s23");

Rancher::service()->cancelRollback("1s23");

use Rancher; 
use Benmag\Rancher\Factories\Entity\LoadBalancerService;

$newLb = new LoadBalancerService([
    'name' => 'lb',
    'stackId' => '1st316',
    'launchConfig' => [
        'ports' => [
            "80",
            "8080"
        ],
        'imageUuid' => 'docker:rancher/lb-service-haproxy:v0.7.5'
    ],
    'lbConfig' => [
        'portRules' => [
            [
                'hostname' => '',
                'serviceId' => "1s583",
                'sourcePort' => 8080,
                'targetPort' => 80,
                'type' => 'portRule'
            ]
        ]
    ]
]);

Rancher::loadBalancerService()->project('1a8')->create($newLb);

use Rancher;
use Benmag\Rancher\Factories\Entity\LoadBalancerService;

$updatedLb = new LoadBalancerService([
    'name' => 'lb-updated',
    'stackId' => '1st316',
    'launchConfig' => [
        'ports' => [
            "80",
        ],
        'imageUuid' => 'docker:rancher/lb-service-haproxy:v0.7.5'
    ],
    'lbConfig' => [
        'portRules' => [
            [
                'hostname' => '',
                'protocol' => 'http',
                'serviceId' => "1s583",
                'sourcePort' => 80,
                'targetPort' => 80,
                'type' => 'portRule'
            ]
        ]
    ],
]);

Rancher::loadBalancerService()->project("1a8")->update("1s26", $updatedLb);

use Rancher;

$serviceLink = [
    'serviceId' => '1s10', 
    'ports' => [
        "hello.world.domain.com=80"
    ]
];

Rancher::loadBalancerService()->addServiceLink("1s27", $serviceLink);

use Rancher;

$serviceLinks = [
    [
        'serviceId' => '1s23',
        'ports' => [
            "hello.world.example.com=80",
        ]
    ]
];

Rancher::loadBalancerService()->setServiceLinks("1s24", $serviceLinks);

use Rancher;

$remove = ['serviceId' => '1s23'];

Rancher::loadBalancerService()->removeServiceLink("1s24", $remove);

use Rancher; 
use Benmag\Rancher\Factories\Entity\Registry;

$registry = new Registry([
    'serverAddress' => "registry.example.com",
]);

$registryResponse = Rancher::registry()->project("1a8")->create($registry));

use Rancher; 
use Benmag\Rancher\Factories\Entity\RegistryCredential;

$cred = new RancherRegistryCredential([
    'registryId' => "1sp120",
    'email' => "[email protected]",
    'publicValue' => "username",
    'secretValue' => "password"
]);

Rancher::registryCredential()->create($cred);

$params = [
    'name' => 'Hello World', // name = "Hello World"
    'name_ne' => 'Hello World', // name != "Hello World"
    'name_notnull' => null, // name is not null
    'description_null' => null, // description is null
    'description_notlike' => 'Hello World', // description NOT LIKE '%Hello World%'
    'description_like' => 'Hello World', // description LIKE '%Hello World%'
    'name_prefix' => 'Hello', // name LIKE 'Hello%'
];

Rancher::stack()->filter($params)->get();


Rancher::service()->with(['instances'])->get("1s724");

Rancher::service()->fields(['uuid'])->get("1s724");

Rancher::host()->scope('1a3302')->all();

Rancher::services()
            ->scope('1a3302')
            ->with(['instances'])
            ->filter(['environmentId' => "1e512"])
            ->fields(['uuid'])
            ->get()

try {
    
    Rancher::host()->deactivate("1h1");
    
} catch(ClientException $e) {
    $response = $e->getResponse();
    $responseBodyAsString = $response->getBody()->getContents();
    echo $responseBodyAsString;
}
 php
Rancher::host()->all();