This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
CumulusRepository.java
326 lines (282 loc) · 10.5 KB
/
CumulusRepository.java
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright 1999-2008 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.nimbustools.messaging.gt4_0_elastic.v2008_05_05.image.defaults;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nimbus.authz.AuthzDBAdapter;
import org.nimbus.authz.AuthzDBException;
import org.nimbus.authz.ObjectWrapper;
import org.nimbus.authz.UserAlias;
import org.nimbustools.api._repr.vm._VMFile;
import org.nimbustools.api.brain.ModuleLocator;
import org.nimbustools.api.repr.Caller;
import org.nimbustools.api.repr.CannotTranslateException;
import org.nimbustools.api.repr.ReprFactory;
import org.nimbustools.api.repr.vm.ResourceAllocation;
import org.nimbustools.api.repr.vm.VMFile;
import org.nimbustools.messaging.gt4_0_elastic.v2008_05_05.image.FileListing;
import org.nimbustools.messaging.gt4_0_elastic.v2008_05_05.image.ListingException;
import org.nimbustools.messaging.gt4_0_elastic.v2008_05_05.image.Repository;
import org.nimbustools.messaging.gt4_0_elastic.v2008_05_05.rm.ContainerInterface;
import javax.sql.DataSource;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CumulusRepository implements Repository {
// -------------------------------------------------------------------------
// STATIC VARIABLES
// -------------------------------------------------------------------------
private static final Log logger =
LogFactory.getLog(CumulusRepository.class.getName());
// -------------------------------------------------------------------------
// INSTANCE VARIABLES
// -------------------------------------------------------------------------
protected final ContainerInterface container;
protected final ReprFactory repr;
protected AuthzDBAdapter authDB;
protected String cumulusHost = null;
protected String repoBucket = null;
protected String prefix = null;
protected int repo_id = -1;
protected String rootFileMountAs = null;
// -------------------------------------------------------------------------
// CONSTRUCTOR
// -------------------------------------------------------------------------
public CumulusRepository(ContainerInterface containerImpl,
ModuleLocator locator,
DataSource ds)
throws Exception
{
if (containerImpl == null)
{
throw new IllegalArgumentException("containerImpl may not be null");
}
this.authDB = new AuthzDBAdapter(ds);
if (containerImpl == null)
{
throw new IllegalArgumentException("containerImpl may not be null");
}
this.container = containerImpl;
if (locator == null)
{
throw new IllegalArgumentException("locator may not be null");
}
this.repr = locator.getReprFactory();
}
// -------------------------------------------------------------------------
// IoC INIT METHOD
// -------------------------------------------------------------------------
void validate()
throws Exception
{
if (this.cumulusHost == null)
{
throw new Exception("Invalid: Missing 'cumulus host' string");
}
if (this.repoBucket == null)
{
throw new Exception("Missing the 'repoBucket' setting");
}
if (this.prefix == null)
{
throw new Exception("Missing the 'prefix' setting");
}
if(this.rootFileMountAs == null)
{
throw new Exception("Missing the 'rootFileMountAs' setting");
}
this.repo_id = this.authDB.getFileID(this.repoBucket, -1, AuthzDBAdapter.ALIAS_TYPE_S3);
if(this.repo_id < 0)
{
throw new Exception("Could not find the repo bucket " + this.repoBucket);
}
}
public void setRepoBucket(String rb)
throws Exception
{
this.repoBucket= rb;
}
public String getRepoBucket()
{
return this.repoBucket;
}
public void setCumulusHost(String rb)
throws Exception
{
this.cumulusHost = rb;
}
public String getPrefix()
{
return this.cumulusHost;
}
public void setPrefix(String rb)
throws Exception
{
this.prefix = rb;
}
public String getCumulusHost()
{
return this.cumulusHost;
}
public boolean isListingEnabled()
{
return true;
}
public String getRootFileMountAs()
{
return this.rootFileMountAs;
}
public void setRootFileMountAs(String rootFileMountAs)
{
this.rootFileMountAs = rootFileMountAs;
}
// -------------------------------------------------------------------------
// implements Repository
// -------------------------------------------------------------------------
// XXXX not sure what to do here
public String getImageLocation(Caller caller)
throws CannotTranslateException
{
final String dn = caller.getIdentity();
String ownerID;
try
{
ownerID = this.authDB.getCanonicalUserIdFromDn(dn);
}
catch(AuthzDBException ex)
{
throw new CannotTranslateException(ex.toString(), ex);
}
if (ownerID == null)
{
throw new CannotTranslateException("No caller/ownerID?");
}
return "cumulus://" + this.cumulusHost + "/" + this.repoBucket + "/" + this.prefix + "/" + ownerID;
}
public VMFile[] constructFileRequest(String imageID,
ResourceAllocation ra,
Caller caller)
throws CannotTranslateException
{
final String dn = caller.getIdentity();
if (dn == null)
{
throw new CannotTranslateException("Cannot construct file " +
"request without owner hash/ID, the file(s) location is " +
"based on it");
}
// todo: look at RA and construct blankspace request
//return new VMFile[]{this.getRootFile(imageID, ownerID)};
VMFile [] vma = new VMFile[1];
// vma[0].
final _VMFile file = this.repr._newVMFile();
file.setRootFile(true);
file.setDiskPerms(VMFile.DISKPERMS_ReadWrite);
// must convert to scp url
// look up image id
try
{
String urlStr = getImageLocation(caller) + "/" + imageID;
file.setMountAs(this.getRootFileMountAs());
URI imageURI = new URI(urlStr);
file.setURI(imageURI);
vma[0] = file;
return vma;
}
catch(Exception ex)
{
throw new CannotTranslateException(ex);
}
}
// return a list of all the images owned by this user
public FileListing[] listFiles(Caller caller,
String[] nameScoped,
String[] ownerScoped)
throws CannotTranslateException, ListingException
{
int repo_id;
String keyName;
final String dn = caller.getIdentity();
if (dn == null)
{
throw new CannotTranslateException("Cannot construct file " +
"request without owner hash/ID, the file(s) location is " +
"based on it");
}
try
{
String ownerID = this.authDB.getCanonicalUserIdFromDn(dn);
final ArrayList files = new ArrayList();
//String canUser = this.authDB.getCanonicalUserIdFromS3(ownerID);
keyName = this.prefix + "/" + ownerID + '%';
final List<ObjectWrapper> objList = this.authDB.searchParentFilesByKey(this.repo_id, keyName);
for (ObjectWrapper ow : objList)
{
FileListing fl = new FileListing();
String name = ow.getName();
String [] parts = name.split("/", 3);
if(parts.length != 3)
{
// if a bad name jsut skip this file... they may have uploaded baddness
logger.error("The filename " + name + " is not in the proper format");
continue;
}
name = parts[2];
fl.setName(name);
fl.setSize(ow.getSize());
long tm = ow.getTime();
Date dt = new Date(tm);
Calendar cl = Calendar.getInstance();
cl.setTime(dt);
String tStr = new Integer(cl.get(Calendar.HOUR_OF_DAY)).toString() + ":" + new Integer(cl.get(Calendar.MINUTE)).toString();
fl.setTime(tStr);
String dStr = getMonthStr(cl.get(Calendar.MONTH)) + " " + new Integer(cl.get(Calendar.DAY_OF_MONTH)).toString();
fl.setDate(dStr);
fl.setReadWrite(true);
files.add(fl);
}
return (FileListing[]) files.toArray(new FileListing[files.size()]);
}
catch(AuthzDBException ex)
{
throw new ListingException("file listing failed when accessing authz db", ex);
}
}
private static String getMonthStr(int month) {
switch (month) {
case 1: return "Jan";
case 2: return "Feb";
case 3: return "Mar";
case 4: return "Apr";
case 5: return "May";
case 6: return "Jun";
case 7: return "Jul";
case 8: return "Aug";
case 9: return "Sep";
case 10: return "Oct";
case 11: return "Nov";
case 12: return "Dec";
default: return "???";
}
}
}