summaryrefslogtreecommitdiff
path: root/openstack/usr/share/openstack/modules/glance
blob: f62942f958c08ca8f285d496c4d20b7038f08eb3 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python
# -*- coding: utf-8 -*-

DOCUMENTATION = '''
---
module: glance
short_description: Manage OpenStack virtual machine images
description:
    - Upload virtual machine images to OpenStack Image Service (glance)
requirements: [ python-glanceclient ]
options:
  name:
    description:
      - name of the image
    required: true
  format:
    description:
      - disk format
    choices: [ami, ari, aki, vhd, vmdk, raw, qcow2, vdi, iso]
    required: true
  is_public:
    description:
      - if true, image is public
    choices: [true, false]
    aliases: [public]
    required: false
    default: false
  file:
    description:
      - path to the file that contains the image
    required: true
    aliases: [path]
  auth_url:
    description:
      - URL to Identity service (keystone) catalog endpoint
    required: true
  region:
    description:
      - OpenStack region name
    required: false
    aliases: [region_name]
  username:
    description:
      - user name to authenticate against Identity service
    aliases: [user, user_name, login_user]
  password:
    description:
      - password to authenticate against Identity service
    aliases: [pass, login_password]
  tenant_name:
    description:
      - name of the tenant
  endpoint_type:
    description:
      - endpoint URL type
    choices: [publicURL, internalURL]
    required: false
    default: publicURL

examples:
  - code: 'glance: name=cirros file=/tmp/cirros.img format=qcow2 is_public=true auth_url=http://192.168.206.130:5000/v2.0/ username=admin tenant_name=demo password=secrete region=RegionOne endpoint_type=publicURL '
'''


try:
    from glanceclient import Client
    from keystoneclient.v2_0 import client as ksclient
except ImportError:
    glanceclient_found = False
else:
    glanceclient_found = True


def get_token_and_endpoint(auth_url, username, password, tenant_name,
                           region_name, endpoint_type):

    keystone = ksclient.Client(username=username,
                               password=password,
                               tenant_name=tenant_name,
                               auth_url=auth_url,
                               region_name=region_name)
    glance_endpoint = keystone.service_catalog.url_for(
        service_type="image",
        endpoint_type=endpoint_type)
    return (keystone.auth_token, glance_endpoint)


def authenticate(auth_url, username, password, tenant_name, region,
                 endpoint_type, version='1'):
    """Return a keystone client object"""

    (token, endpoint) = get_token_and_endpoint(auth_url, username, password,
                                               tenant_name, region,
                                               endpoint_type)

    return Client(version, endpoint=endpoint, token=token)


def get_images(glance, name):
    """ Retrieve all images with a certain name """
    images = [x for x in glance.images.list() if x.name == name]
    return images


def create_image(glance, name, path, disk_format, is_public, check_mode):
    """ Create a new image from a file on the path.

        Return a pair. First element indicates whether a change occurred,
        second one is the ID of the iamge """

    # If the image(s) already exists, we're done
    images = get_images(glance, name)
    if len(images) > 0:
        return (False, images[0].id)

    if check_mode:
        return (True, None)

    image = glance.images.create(name=name, disk_format=disk_format,
                                 container_format='bare',
                                 is_public=is_public)
    image.update(data=open(path, 'rb'))
    return (True, image.id)


def main():

    module = AnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            file=dict(required=True, aliases=['path']),
            auth_url=dict(required=True),
            region=dict(required=False, aliases=['region_name']),
            username=dict(required=True, aliases=['user',
                                                  'user_name',
                                                  'login_user']),
            password=dict(required=True, aliases=['pass', 'login_password']),
            tenant_name=dict(required=True, aliases=['tenant']),
            disk_format=dict(required=True,
                        choices=['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw',
                                 'qcow2', 'vdi', 'iso'],
                        aliases=['disk-format', 'format']),
            is_public=dict(required=False,
                           default=False,
                           aliases=['public']),
            endpoint_type=dict(required=False,
                               choices=['publicURL', 'internalURL'],
                               default='publicURL')
        ),
        supports_check_mode=True
    )

    name = module.params['name']
    path = module.params['file']
    auth_url = module.params['auth_url']
    region = module.params['region']
    username = module.params['username']
    password = module.params['password']
    tenant_name = module.params['tenant_name']
    disk_format = module.params['disk_format']
    is_public = module.params['is_public']
    endpoint_type = module.params['endpoint_type']
    check_mode = module.check_mode

    glance = authenticate(auth_url, username, password, tenant_name, region,
                          endpoint_type)

    (changed, id) = create_image(glance, name, path, disk_format, is_public,
                                 check_mode)

    module.exit_json(changed=changed, name=name, id=id)

# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
if __name__ == '__main__':
    main()