File: local-http-test.py

package info (click to toggle)
python-pylxd 2.2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 820 kB
  • sloc: python: 7,258; sh: 104; makefile: 21
file content (51 lines) | stat: -rwxr-xr-x 1,363 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3

import datetime
import pylxd
import requests
import time

from requests.packages.urllib3.exceptions import InsecureRequestWarning


requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


def log(s):
    now = datetime.datetime.utcnow()
    print("{} - {}".format(now, s))


def create_and_update(client):
    log("Creating...")
    base = client.containers.create({
        'name': 'ubuntu-1604',
        'source': {
            'type': 'image',
            'protocol': 'simplestreams',
            'server': 'https://images.linuxcontainers.org',
            'alias': 'ubuntu/xenial/amd64'
        }
    }, wait=True)
    log("starting...")
    base.start(wait=True)
    while len(base.state().network['eth0']['addresses']) < 2:
        time.sleep(1)
    commands = [
        ['apt-get', 'update'],
        ['apt-get', 'install', 'openssh-server', 'sudo', 'man', '-y']
    ]
    for command in commands:
        log("command: {}".format(command))
        result = base.execute(command)
        log("result: {}".format(result.exit_code))
        log("stdout: {}".format(result.stdout))
        log("stderr: {}".format(result.stderr))


if __name__ == '__main__':
    client = pylxd.Client("https://127.0.0.1:8443/", verify=False)
    log("Authenticating...")
    client.authenticate('password')

    create_and_update(client)