-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreamce_api.py
233 lines (183 loc) · 8.85 KB
/
creamce_api.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/python
#
# creamce_api.py - an CREAM CE plugin for Vcycle
#
# Andrew McNab, University of Manchester.
# Copyright (c) 2013-7. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# o Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
# o Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Contacts: [email protected] http://www.gridpp.ac.uk/vcycle/
#
import pprint
import os
import re
import sys
import stat
import time
import json
import shutil
import string
import urllib
import random
import base64
import StringIO
import tempfile
import calendar
import subprocess
import vcycle.vacutils
class CreamceError(Exception):
pass
class CreamceSpace(vcycle.BaseSpace):
def __init__(self, api, apiVersion, spaceName, parser, spaceSectionName, updatePipes):
# Initialize data structures from configuration files
# Generic initialization
vcycle.BaseSpace.__init__(self, api, apiVersion, spaceName, parser, spaceSectionName, updatePipes)
self.maxStartingSeconds = None # no limit
try:
self.url = parser.get(spaceSectionName, 'url')
except Exception as e:
raise CreamceError('url is required in CREAM CE [space ' + spaceName + '] (' + str(e) + ')')
def connect(self):
pass
def scanMachines(self):
"""Query Cream CE compute service for details of machines in this space"""
# For each job found in the space, this method is responsible for
# either (a) ignorning non-Vcycle VMs but updating self.totalProcessors
# or (b) creating a Machine object for the VM in self.spaces
fd,path = tempfile.mkstemp()
os.write(fd, '##CREAMJOBS##\n')
for jobidEncoded in os.listdir('/var/lib/vcycle/spaces/' + self.spaceName + '/jobids/'):
try:
os.write(fd, urllib.unquote(jobidEncoded) + '\n')
except Exception as e:
print str(e)
os.close(fd)
with subprocess.Popen('glite-ce-job-status --donot-verify-ac-sign --level 0 --input %s' % path, shell=True, stdout=subprocess.PIPE).stdout as p:
rawStatuses = p.read()
os.remove(path)
# Convert machines from None to an empty dictionary since we successfully connected
self.machines = {}
for oneStatus in self.parseGliteCeJobStatus(rawStatuses):
try:
with open('/var/lib/vcycle/spaces/' + self.spaceName + '/jobids/' + urllib.quote(oneStatus['JobID'], '')) as f:
machineName = f.read().strip()
except:
vcycle.vacutils.logLine('Failed to read jobid file for %s!' % oneStatus['JobID'])
continue
# Collect values saved in machine's directory
try:
machinetypeName = open('/var/lib/vcycle/machines/%s/machinetype_name' % machineName, 'r').readline().strip()
except:
vcycle.vacutils.logLine('Failed to read machinetype_name file for %s!' % machineName)
continue
# Map CREAM Status to Vcycle state
if oneStatus['Status'] in ('REGISTERED','PENDING','IDLE'):
state = vcycle.MachineState.starting
elif oneStatus['Status'] in ('RUNNING','REALLY-RUNNING'):
state = vcycle.MachineState.running
elif oneStatus['Status'] in ('DONE-FAILED','ABORTED'):
state = vcycle.MachineState.failed
elif oneStatus['Status'] in ('DONE-OK','CANCELLED'):
state = vcycle.MachineState.shutdown
else:
state = vcycle.MachineState.unknown
self.machines[machineName] = vcycle.shared.Machine(name = machineName,
spaceName = self.spaceName,
state = state,
ip = '0.0.0.0',
createdTime = None,
startedTime = None,
updatedTime = None,
uuidStr = oneStatus['JobID'],
machinetypeName = machinetypeName,
zone = None)
def parseGliteCeJobStatus(self, rawStatuses):
# State machine to go through rawStatuses from glite-ce-job-status
# output, populating jobs list with status information
jobs = []
job = None
for line in rawStatuses.split('\n'):
if line.startswith('****** JobID=['):
job = { 'JobID': line.split('[')[1].split(']')[0] }
elif line.strip().startswith('Status = ['):
job['Status'] = line.split('[')[1].split(']')[0]
elif line.strip().startswith('ExitCode = ['):
job['ExitCode'] = line.split('[')[1].split(']')[0]
elif job and line.strip() == '':
if 'JobID' in job and 'Status' in job:
# Add properly formed job items to the jobs list
jobs.append(job)
job = None
return jobs
def createMachine(self, machineName, machinetypeName, zone = None):
# Cream CE-specific job submission
vcycle.vacutils.createFile('/var/lib/vcycle/machines/' + machineName + '/jdl',
'''[
Type = "Job";
JobType = "Normal";
Executable = "user_data";
StdOutput = "stdout.log";
StdError = "stderr.log";
InputSandbox = {"/var/lib/vcycle/machines/''' + machineName + '''/user_data"};
OutputSandbox = {"stdout.log", "stderr.log"};
OutputSandboxBaseDestURI = "gsiftp://localhost";
]
''',
0600, '/var/lib/vcycle/tmp')
if self.url.startswith('https://'):
endpoint = self.url[8:]
else:
endpoint = self.url
try:
with subprocess.Popen('glite-ce-job-submit --autm-delegation --donot-verify-ac-sign --resource %s %s' %
(endpoint, '/var/lib/vcycle/machines/' + machineName + '/jdl'),
shell=True, stdout=subprocess.PIPE).stdout as p:
jobID = p.read().strip()
except Exception as e:
raise CreamceError('Failed to submit new job %s: %s' % (machineName, str(e)))
if not jobID:
raise CreamceError('Could not get Job ID from %s job submission response' % machineName)
vcycle.vacutils.createFile('/var/lib/vcycle/machines/' + machineName + '/jobid', jobID, 0600, '/var/lib/vcycle/tmp')
vcycle.vacutils.createFile('/var/lib/vcycle/spaces/' + self.spaceName + '/jobids/' + urllib.quote(jobID,''), machineName, 0600, '/var/lib/vcycle/tmp')
vcycle.vacutils.logLine('Created ' + machineName + ' (' + jobID + ') for ' + machinetypeName + ' within ' + self.spaceName)
self.machines[machineName] = vcycle.shared.Machine(name = machineName,
spaceName = self.spaceName,
state = vcycle.MachineState.starting,
ip = '0.0.0.0',
createdTime = int(time.time()),
startedTime = None,
updatedTime = int(time.time()),
uuidStr = jobID,
machinetypeName = machinetypeName)
def deleteOneMachine(self, machineName):
try:
jobID = open('/var/lib/vcycle/machines/' + machineName + '/jobid', 'r').read().strip()
# All we do is remove it from the list of jobids to ignore it from now on
os.remove('/var/lib/vcycle/spaces/' + self.spaceName + '/jobids/' + urllib.quote(jobID, ''))
except Exception as e:
vcycle.vacutils.logLine('Failed deleting %s (%s)' % (machineName, str(e)))