-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump-rundeck-resources.py
121 lines (82 loc) · 3.23 KB
/
dump-rundeck-resources.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Vultr Rundeck Resource Dump
Author: Kevin Fowlks
Date: 08/22/2016
This program reads servers from vultr and converts output to a rundeck compatible resources.xml file format.
rundeck resource xml format http://rundeck.org/1.5.2/manpages/man5/resource-v13.html
Requires
Under CYGWIN run to install pip
python -m ensurepip
# This code relies on the following lib: https://github.com/spry-group/python-vultr
pip install vultr
Usage:
The program can be run as shown below:
$ python dump-rundeck-resources.py -k [Vultr-API-Key]
or
export VULTR_KEY=APIKEY
$ python dump-rundeck-resources.py
The xml is generated to stdio
python dump-rundeck-resources.py > /var/rundeck/projects/rundeck-production/etc/resources.xml
"""
import sys
import argparse
import json
from json import dumps
from os import environ
from vultr import Vultr, VultrError
from xml.dom.minidom import Document
def main():
pass
def exportNode(dictofdicts):
node = xmldoc.createElement("node")
# Define our required attributes
node.setAttribute("name", str(dictofdicts['label']))
node.setAttribute("type", str("Node"))
node.setAttribute("hostname", str(dictofdicts['main_ip']))
node.setAttribute("username", "deploy")
node.setAttribute("description", "vultr instance {0} os: {1}, ".format(str(dictofdicts['label']), str(dictofdicts['os'])) )
node.setAttribute("tags", "{0},{1},{2},{3},{4}".format(str(dictofdicts['os']), str(dictofdicts['location']), str(dictofdicts['label']), str(dictofdicts['SUBID']), str(dictofdicts['tag'])) )
# This will exclude the below keys from being added to the xml attributes
exclude_set = ['name','type','hostname','username', 'description','SUBID','tag','label']
# Add remaining k,v pairs as custom attributes to the xml document.
for k,v in dictofdicts.items():
if k not in exclude_set:
node.setAttribute( k, str(v))
return node
parser = argparse.ArgumentParser(description='Vultr Rundeck Resource')
parser.add_argument('-k', '--api-key', metavar='N',dest='api_key', help='Vultr API-Key')
parser.add_argument('-v', '--verbose', help='verbosity',dest='enable_trace', action='store_true')
try:
args = parser.parse_args()
except:
parser.print_help()
sys.exit(0)
API_KEY = environ.get('VULTR_KEY')
#print 'API-KEY: ', args.api_key
if API_KEY is None:
if args.api_key is None:
print "A mandatory option api-key is missing\n"
parser.print_help()
exit(-1)
else:
api_key = args.api_key
else:
api_key = API_KEY
vultr = Vultr(api_key)
# Write out xml rundeck format http://rundeck.org/1.5.2/manpages/man5/resource-v13.html
xmldoc = Document()
try:
# curl -H 'API-Key: APIKEY' "https://api.vultr.com/v1/server/list"
server_list_json = vultr.server_list()
project = xmldoc.createElement("project")
xmldoc.appendChild(project)
for k in server_list_json:
node = exportNode(server_list_json[k])
project.appendChild(node)
node_attribute_values = {}
sys.stdout.write(xmldoc.toprettyxml(indent=" "))
except VultrError as ex:
logging.error('VultrError: %s', ex)
if __name__ == '__main__':
main()