GCEΒΆ

 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
#!/usr/bin/env python3
# This file is part of pycloudlib. See LICENSE file for license information.
"""Basic examples of various lifecycle with an GCE instance."""

import logging
import os

import pycloudlib


def demo():
    """Show example of using the GCE library.

    Connects to GCE and finds the latest daily image. Then runs
    through a number of examples.
    """
    gce = pycloudlib.GCE(
        tag='examples',
        credentials_path='MY-GCE-CREDENTIALS-PATH',
        project='PROJECT-ID',
        region='us-west2',
        zone='a'
    )
    daily = gce.daily_image('bionic')

    pub_key_path = "gce-pubkey"
    priv_key_path = "gce-privkey"
    pub_key, priv_key = gce.create_key_pair()

    with open(pub_key_path, "w") as f:
        f.write(pub_key)

    with open(priv_key_path, "w") as f:
        f.write(priv_key)

    os.chmod(pub_key_path, 0o600)
    os.chmod(priv_key_path, 0o600)

    gce.use_key(
        public_key_path=pub_key_path,
        private_key_path=priv_key_path
    )

    inst = gce.launch(daily)
    print(inst.execute("lsb_release -a"))


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    demo()