ashleyhood / php-lxd

A PHP library for interacting with the LXD REST API
MIT License
28 stars 15 forks source link

Cloud-init #29

Closed ostap-mykhaylyak closed 3 years ago

ostap-mykhaylyak commented 3 years ago

Hello,

I searched the documentation but found no reference to using Cloud-init:

  user.user-data: |
    #cloud-config
    package_upgrade: true
    packages:
      - package1
      - package2
ashleyhood commented 3 years ago

Hi @ostap-mykhaylyak ,

There is no specific Cloud-init support but since the user.user-data is added to a profile, you could try passing in a string with the Cloud-init data in the config array:

$userData = <<<USERDATA
|
    #cloud-config
    package_upgrade: true
    packages:
      - package1
      - package2
USERDATA;

$lxd->profiles->create(
    'test-profile',
    'My test profile',
    [
        "limits.memory" => "2GB",
        "user.user-data" => $userData,
    ],
    [
        "kvm" => [
            "type" => "unix-char",
            "path" => "/dev/kvm",
        ],
    ]
);

NOTE: this is untested code

ostap-mykhaylyak commented 3 years ago

Working code:

$userData = <<<USERDATA
#cloud-config
package_upgrade: true
packages:
 - nginx
runcmd:
 - service nginx restart
USERDATA;

But I have a problem with aliases, for example (without 'server'):

$options = ['alias' => 'ubuntu/bionic/amd64'];

(ubuntu: repository) does not work, while the images of

'server' => 'https://images.linuxcontainers.org:8443'

they do not contain cloud-init

ashleyhood commented 3 years ago

If you are using https://images.linuxcontainers.org then you will need to use the aliases with cloud:

$options = ['alias' => 'ubuntu/bionic/cloud/amd64'];

The other option is to change to the cloud-images server:

$options = [
    'server' => 'https://cloud-images.ubuntu.com/releases',
    'alias' => 'bionic/amd64',
];
ostap-mykhaylyak commented 3 years ago
$options = [
   'server' => 'https://cloud-images.ubuntu.com/releases',
   'alias' => 'bionic/amd64'
];
{"error":"Failed to fetch https://cloud-images.ubuntu.com/releases/1.0: 404 Not Found","error_code":400,"type":"error"}
$options = [
   'server' => 'https://images.linuxcontainers.org:8443',
   'alias' => 'ubuntu/bionic/cloud/amd64'
];
{"error":"Failed to fetch https://uk.images.linuxcontainers.org:8443/1.0/images/ubuntu/bionic/cloud/amd64: 404 Not Found (truncated...)

Inside my old script i use this: https://github.com/ostap-mykhaylyak/lxd-socket/blob/master/example/create.php

$parameters = '{"name": "bionic", "source": {"type": "image", "protocol": "simplestreams", "server": "https://cloud-images.ubuntu.com/daily", "alias": "18.04"}}';
ashleyhood commented 3 years ago

Try passing in an empty string for the apiVersion:

require "vendor/autoload.php";

use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

$config = [
    'verify' => false,
    'cert' => [
        __DIR__.'/client.pem',
        ''
    ]
];

$guzzle = new GuzzleClient($config);
$adapter = new GuzzleAdapter($guzzle);
$apiVersion = '';
$url = 'https://cloud-images.ubuntu.com/releases';

$lxd = new \Opensaucesystems\Lxd\Client($adapter, $apiVersion, $url);
ostap-mykhaylyak commented 3 years ago

I think:

$url = 'https://cloud-images.ubuntu.com/releases';

change:

$lxd->setUrl('https://MY_SERVER_IP:8443');

https://github.com/ashleyhood/php-lxd/blob/e7cd44473e94809d9a083251dd224362aa7e9382/src/Client.php#L71 so i try with only:

$apiVersion = '';

but nothing, same error:

{"error":"Failed to fetch https://cloud-images.ubuntu.com/daily/1.0: 404 Not Found","error_code":400,"type":"error"}
ashleyhood commented 3 years ago

@ostap-mykhaylyak so sorry about my previous comment. It had nothing to do with your issue :disappointed:

This is what worked for me:


require "vendor/autoload.php";

$config = [
    'verify' => false,
    'cert' => [
        __DIR__.'/client.pem',
        ''
    ]
];

$lxd = new \Opensaucesystems\Lxd\Client(\Http\Adapter\Guzzle7\Client::createWithConfig($config));

$options = [
    'server' => 'https://cloud-images.ubuntu.com/releases',
    'alias' => 'bionic/amd64',
    'protocol' => 'simplestreams',
];

// Alternatively:
// $options = [
//     'server' => 'https://images.linuxcontainers.org',
//     'alias' => 'ubuntu/bionic/cloud/amd64',
//     'protocol' => 'simplestreams',
// ];

$lxd->containers->create('my_container', $options);

You must specify the protocol as it will default to lxd.

ashleyhood commented 3 years ago

@ostap-mykhaylyak I will close this issue now. If you are still unable to load a cloud-init image, feel free to reopen this issue.