summaryrefslogtreecommitdiffstats
path: root/roles/openshift_node/library/openshift_register_node.py
blob: 4b306db9fa6cd7855cab0fea1fb20b8639c81db4 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: expandtab:tabstop=4:shiftwidth=4

import os
import multiprocessing
import socket
from subprocess import check_output, Popen

DOCUMENTATION = '''
---
module: openshift_register_node
short_description: This module registers an openshift-node with an openshift-master
author: Jason DeTiberus
requirements: [ openshift-node ]
notes: Node resources can be specified using either the resources option or the following options: cpu, memory
options:
    name:
        description:
            - id for this node (usually the node fqdn)
        required: true
    hostIP:
        description:
            - ip address for this node
        required: false
    cpu:
        description:
            - number of CPUs for this node
        required: false
        default: number of logical CPUs detected
    memory:
        description:
            - Memory available for this node in bytes
        required: false
        default: 80% MemTotal
    resources:
        description:
            - A json string representing Node resources
        required: false
'''
EXAMPLES = '''
# Minimal node registration
- openshift_register_node: name=ose3.node.example.com

# Node registration with all options (using cpu and memory options)
- openshift_register_node:
    name: ose3.node.example.com
    hostIP: 192.168.1.1
    apiVersion: v1beta1
    cpu: 1
    memory: 1073741824

# Node registration with all options (using resources option)
- openshift_register_node:
    name: ose3.node.example.com
    hostIP: 192.168.1.1
    apiVersion: v1beta1
    resources:
        capacity:
            cpu: 1
            memory: 1073741824
'''

def main():
    module = AnsibleModule(
        argument_spec      = dict(
            name           = dict(required = True),
            hostIP         = dict(),
            apiVersion     = dict(),
            cpu            = dict(),
            memory         = dict(),
            resources      = dict(),
            client_config  = dict(),
            client_cluster = dict(default = 'master'),
            client_context = dict(default = 'master'),
            client_user    = dict(default = 'admin')
        ),
        mutually_exclusive = [
            ['resources', 'cpu'],
            ['resources', 'memory']
        ],
        supports_check_mode=True
    )

    user_has_client_config = os.path.exists(os.path.expanduser('~/.kube/.kubeconfig'))
    if not (user_has_client_config or module.params['client_config']):
        module.fail_json(msg="Could not locate client configuration, "
                         "client_config must be specified if "
                         "~/.kube/.kubeconfig is not present")

    client_opts = []
    if module.params['client_config']:
        client_opts.append("--kubeconfig=%s" % module.params['client_config'])

    try:
        output = check_output(["/usr/bin/openshift", "ex", "config", "view",
                               "-o", "json"] + client_opts,
                stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        module.fail_json(msg="Failed to get client configuration",
                command=e.cmd, returncode=e.returncode, output=e.output)

    config = json.loads(output)
    if not (bool(config['clusters']) or bool(config['contexts']) or
            bool(config['current-context']) or bool(config['users'])):
        module.fail_json(msg="Client config missing required values",
                         output=output)

    client_context = module.params['client_context']
    if client_context:
        if client_context not in config['contexts']:
            module.fail_json(msg="Context %s not found in client config" %
                             client_context)
        if not config['current-context'] or config['current-context'] != client_context:
            client_opts.append("--context=%s" % client_context)

    client_user = module.params['client_user']
    if client_user:
        if client_user not in config['users']:
            module.fail_json(msg="User %s not found in client config" %
                             client_user)
        if client_user != config['contexts'][client_context]['user']:
            client_opts.append("--user=%s" % client_user)

    client_cluster = module.params['client_cluster']
    if client_cluster:
        if client_cluster not in config['clusters']:
            module.fail_json(msg="Cluster %s not found in client config" %
                             client_cluster)
        if client_cluster != config['contexts'][client_context]['cluster']:
            client_opts.append("--cluster=%s" % client_cluster)

    node_def = dict(
            id = module.params['name'],
            kind = 'Node',
            apiVersion = 'v1beta1',
            resources = dict(
                capacity = dict()
            )
    )

    for key, value in module.params.iteritems():
        if key in ['cpu', 'memory']:
            node_def['resources']['capacity'][key] = value
        elif key == 'name':
            node_def['id'] = value
        elif key != 'client_config':
            if value:
                node_def[key] = value

    if not node_def['resources']['capacity']['cpu']:
        node_def['resources']['capacity']['cpu'] = multiprocessing.cpu_count()

    if not node_def['resources']['capacity']['memory']:
        with open('/proc/meminfo', 'r') as mem:
            for line in mem:
                entries = line.split()
                if str(entries.pop(0)) == 'MemTotal:':
                    mem_total_kb = int(entries.pop(0))
                    mem_capacity = int(mem_total_kb * 1024 * .75)
                    node_def['resources']['capacity']['memory'] = mem_capacity
                    break

    try:
        output = check_output(["/usr/bin/osc", "get", "nodes"] +  client_opts,
                stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        module.fail_json(msg="Failed to get node list", command=e.cmd,
                returncode=e.returncode, output=e.output)

    if re.search(module.params['name'], output, re.MULTILINE):
        module.exit_json(changed=False, node_def=node_def)
    elif module.check_mode:
        module.exit_json(changed=True, node_def=node_def)

    config_def = dict(
        metadata = dict(
            name = "add-node-%s" % module.params['name']
        ),
        kind = 'Config',
        apiVersion = 'v1beta1',
        items = [node_def]
    )

    p = Popen(["/usr/bin/osc"] + client_opts + ["create", "node"] + ["-f", "-"],
            stdin=subprocess.PIPE, stdout=subprocess.PIPE,
            stderr=subprocess.PIPE, close_fds=True)
    (out, err) = p.communicate(module.jsonify(config_def))
    ret = p.returncode

    if ret != 0:
        if re.search("minion \"%s\" already exists" % module.params['name'],
                err):
            module.exit_json(changed=False,
                    msg="node definition already exists", config_def=config_def)
        else:
            module.fail_json(msg="Node creation failed.", ret=ret, out=out,
                    err=err, config_def=config_def)

    module.exit_json(changed=True, out=out, err=err, ret=ret,
           node_def=config_def)

# import module snippets
from ansible.module_utils.basic import *
main()