forked from mihxil/ansible-nexus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexus
180 lines (149 loc) · 5.81 KB
/
nexus
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
#!/usr/bin/env python
DOCUMENTATION='''
---
module: nexus
short_description: Download and installs an artifact from nexus. See https://github.com/mihxil/ansible-nexus
author: Michiel Meeuwissen, Marc Bosserhoff
options:
nexus:
required: true
description:
- Base url of the nexus
repository:
required: false
description:
- Optional the used repository (Defaults to 'public')
destdir:
required: false
description:
- The destination dir of the downloaded artifacts (Defaults to '/tmp/downloaded_artifacts')
artifactId:
required:: true
description:
- The artifact to download. The format is separated by ':' an will be connected like so:
groupId:artifactId:version
extension:
required: false
description:
- The artifact extension (Defaults to 'war')
force:
required: false
description:
- Forces the download of the artifacts if they are present in the target destination
http_user:
required: false
description:
- If the nexus need a basic authentication, the user name can be provided here
http_pass:
required: false
description:
- If the nexus need a basic authentication, the password can be provided here
'''
EXAMPLES='''
- name: get statistics jar
nexus: nexus=http://nexus.vpro.nl artifactId=nl.vpro.stats:stats-backend:0.3-SNAPSHOT extension=jar
'''
import urllib2
import base64
from datetime import datetime
from wsgiref.handlers import format_date_time
from ansible.module_utils.basic import *
def loadArtifact(url, dest, http_user, http_pass, force):
result = dict(url = url, http_user = http_user, force = force)
try:
headers = {}
# Support if modified header if not using 'force' flag to always download artifacts
if os.path.isfile(dest) and not force:
headers['IF-Modified-Since'] = format_date_time(time.mktime(
datetime.fromtimestamp(os.path.getmtime(dest)
).timetuple()))
if http_user and http_pass:
headers['Authorization'] = "Basic %s" % base64.encodestring('%s:%s' % (http_user, http_pass)).replace('\n', '')
request = urllib2.Request(url, None, headers)
response = urllib2.urlopen(request)
if response.code == 200:
handle = open(dest,'wb')
handle.write(response.read())
handle.close()
# Everything went ok, set result set accordingly
result['failed'] = False
result['code'] = response.code
result['msg'] = "OK"
result['changed'] = True
return result
except Exception as e:
# In case of error, let ansible stop the playbook
result['failed'] = True
result['changed'] = False
result['msg'] = "Unknown error"
if hasattr(e, "code"):
result['code'] = e.code
if hasattr(e, "reason"):
result['msg'] = e.reason;
else:
result['msg'] = "The server couldn\'t fulfill the request.";
# In case of 304 the resource is still there in place and not updated
if e.code == 304:
result['failed'] = False;
return result;
raise e
def main():
module = AnsibleModule(
argument_spec = dict(
nexus = dict(required = True),
repository = dict(required = False, default = "public"),
destdir = dict(required = False, default = "/tmp/downloaded_artifacts"),
filename = dict(required = False, default = None),
artifactId = dict(required = True),
extension = dict(required = False, default = "war"),
force = dict(required = False, default = True, choices = BOOLEANS),
http_user = dict(required = False),
http_pass = dict(required = False)
),
supports_check_mode = False
)
nexus = module.params['nexus']
repository = module.params['repository']
destdir = module.params['destdir']
artifactId = module.params['artifactId']
filename = module.params['filename']
extension = module.params['extension']
force = module.boolean(module.params['force'])
http_user = module.params['http_user']
http_pass = module.params['http_pass']
# Prepare strings and urls before the nexus call
split = artifactId.split(":")
(groupId, artifactId, version) = split[0:3]
classifier = split[3] if len(split) >= 4 else ""
urlAppendClassifier = "&c=" + classifier if classifier else ""
postfix = "-" + classifier if classifier else ""
# Create generic filename if filename is not set
if filename == None:
filename = artifactId + "-" + version + postfix + "." + extension
if repository == "":
repository = "snapshots" if "SNAPSHOT" in version else "releases"
url = nexus + "/service/local/artifact/maven/redirect?r=" + repository + "&g=" + groupId + "&a=" + artifactId + "&v=" + version + "&e=" + extension + urlAppendClassifier
if not os.path.exists(destdir):
os.mkdir(destdir)
dest = destdir + "/" + filename
# Try to load artifact from nexus
result = loadArtifact(url, dest, http_user, http_pass, force)
if result['failed']:
module.fail_json(
artifactId = artifactId,
nexus = nexus,
url = url,
filename = filename,
dest = dest,
repository = repository,
changed = result['changed'],
msg = result['msg'],
result = result
)
module.exit_json(
dest = dest,
filename = filename,
artifactId = artifactId,
changed = result['changed']
)
main()