Introduction
Currently Cloudstack accepts only URL's to register a template/volume. If the user has a downloaded template/volume available, user has to host it either on dropbox etc. or run web server so as to get downloadable url. It is desirable to be able to upload a template/volume directly through standard web browser.
This feature enables the users to directly upload the template/volume to Cloudstack and eliminates the dependency on an external http server. This will be a complementary functionality and users can continue to register template/volume with URL as well.
Use cases
- User wants to upload a template from the browser and launch a vm with it on Cloudstack
- User wants to upload a volume from the browser and attach it to a vm on Cloudstack
Functional Requirements
- Support upload of template through https POST request with pre-generated URL
- Support upload of volume through https POST request with pre-generated URL
- Template/volume upload failure is irrecoverable, it needs to be reinitiated by requesting a new upload url
- Cleanup of partially uploaded templates/volumes in case of failure
- Provide upload status (percentage, bytes etc) using the list templates/volumes api call
- Global configuration parameter to limit the max upload size of template/volume
- Enforcement of account limits for templates/volumes
- This feature is hypervisor agnostic and should work for all hypervisors
Out of Scope
No unencrypted or http version of POST will be available
- Upload progress from SSVM Apache web-server when POST request is active (http://creativejs.com/tutorials/advanced-uploading-techniques-part-1/)
Split upload and join in Apache so as to not hit browser timeouts
Ability to cancel in-progress uploads by admin
- Multiple files for the same template/volume will not be supported for now
Limitations
Upload url should be active only for one call - Its partially handled now (see below for details). The limitation needs to be addressed.
Design
Process Flow
The browser-based uploading process will have the following steps:

- Management Server generates a unique pre-shared key (PSK) and shares it with the SSVM agent.
- New API request "getUploadParams" encoded the usual Cloudstack API way using api keys. Request parameters include format, hypervisor, md5 sum etc.
- Management Server response to (b) returns post url to upload the file, params to send to the url while uploading the file and a SHA1 signature generated using PSK and params json.
- User should upload the file over https POST request to the url obtained in (c). The signature and params returned in (c) should be passed as is.
- The Apache web server on the SSVM matches the url, decrypts and passes on to the SSVM java agent as plain HTTP. The SSVM agent checks the signature against the params using the key PSK and SHA1. Once the params are verified, it writes to the NFS storage to the location encoded in the URL.
- Once the upload completes, the MD5 checksum is compared (if available from step c).
Pre-shared key management
This section shall cover when and how the pre-shared key is generated by management server, how is it stored, how is it passed to SSVM
- On start of management server, if the key doesn't exist, it auto-generates one using SHA1 and stores it in db
- Key is saved in encrypted form (db encryption is enabled) in the configuration table as hidden configuration with name upload.secret.key
- During SSVM start-up, the key is passed to it by management server
- SSVM stores this key in a file at /etc/cloudstack/agent/ms-psk
- If the key sharing fails SSVM agent would shut itself down
Sequence Diagram

Management server and SSVM agent interaction
When the management server receives getUploadParams api call, it selects SSVM and secondary store in the zone requested by the user and returns a POST url to that SSVM. There can be multiple SSVMs in the same zone. Management servers decides which SSVM to handle this based on the load. SSVM agent will keep track of the uploads and returns the status to the management server. If the template/volume is selected to be cross-zone, it will uploaded to one zone and then copied to other zones from it.
management server owns the lifecycle of the volume/template. It polls the SSVM agent(using DownloadProgressCommand of the agent) to get the status and updates it. Failure at any point during the form upload to ssvm apache would return error to the user and UPLOAD_ERROR when the management server pings for download status.
Polling starts after the successful return of the getUploadParams. The polling starts after an initial delay of 100ms and in intervals of n ms. The management server polls x times before it changes the state to ERROR. (add configurations to handle this polling)
If the management server itself goes down during the upload, if the timeout hasnt crossed, it will poll for the status once its up. Otherwise the state of the template will be transitioned to ERROR. In a load balanced situation where the management server handling the ssvm upload goes down, the status wont be updated unless the SSVM is connected to a new management server.
SSVM agent and SSVM apache interaction
TBD
status update
detecting failures
The below failures can happen while the agent is receiving the template. When management server asks for the status of the template/volume, if any of these below errors happen, the agent returns ERROR state and management server updates the state accordingly.
- pre validation errors (file upload hasnt started yet)
- SSL handshake failed
- params validation with PSK failed
- max file size from the header crossed the limit
- post validation error (once the file is successfully uploaded)
- file uploaded is not of the type specified (for ex: if the template is said to be of type tar, the actual downloaded file is not if this type)
- max upload limit crossed
- http errors can be caught through appropriate event handlers: exception,timeout,closed (http://hc.apache.org/httpcomponents-core-ga/tutorial/html/nio.html)
- user stopped/canceled the post request before completion
- uploading the template failed due to a network issue
- browser request timed out
- browser crashed
- management server crashed and hence the upload request is cancelled by the browser.
- SSVM apache down/restarted
- application errors will be caught through appropriate exceptions from the other services
- secondary storage is not accessible
- there isn't enough space in secondary storage and hence agent cant write to it.
- timeout reached and the management server will not poll anymore
- interruptions
- SSVM is down/restarted
- SSVM agent is down/restarted
successful completion
If the template install is successful within the polling timeout, when the management server asks for the status of it, agent returns a success state and it is updated by the management server accordingly.
State machine
Template/Volume go through below state machine state transitions, when it goes through the browser based upload.

States:
NOT_UPLOADED: getUploadParams API was successful and the template is registered. But, upload hasnt started yet
UPLOAD_IN_PROGRESS: User has initiated the POST request SSVM Apache and the validation/download is in progress
UPLOADED: SSVM Agent successfully Downloaded the template to secondary storage
UPLOAD_ERROR: Any Failure in between user initiating a post request and template being downloaded
ABANDONED: the post request isnt called in the stipulated time (check sequence diagram point 4)
State Transitions:
- Transition1: This state transition happens, when the user initiates the post request
- Transition2: Post request is active SSVM agent is downloading validation or downloading the template but, not done yet
- Transition3: on successful completion of template installation to secondary storage
- Transition4: This can happen in multiple ways outlined in the failures section above.
All these errors will result in the same state for the template/volume as there isnt any way to recover from them or the handling isnt different for these. Additional error information will be provided in the form of a message for respective errors.
- Transition5: The post request hasn't happened and the timeout has reached
Cleanup
Of the error categories defined in the failures above,
- pre validation errors will not result in any download and hence no cleanup is required.
- post validation errors, template/volume will be cleaned
- http errors shall be caught in the appropriate events and the partially downloaded file will be cleaned.
- application errors and interruptions: a new thread UploadToStorageGarbageCollector will be run periodically(based on a global configuration) on the management server. It will look at any template/volume which are not deleted and in UPLOAD_ERROR/ABANDONED state and send a DeleteCommand to the Agent for the same. It will also update the status of the template/volume to be deleted.
recovery mechanisms
There isnt any recovery or retry mechanism as this is a POST request. Once errored, template/volume will remain in the error state and admin will be able to troubleshoot it based on the appropriate log messages in management server log, agent log, apache access/error log files. This will be cleaned by the garbage collector eventually. The user has to reinitiate the upload by calling getUploadParams and getting a new POST url.
one time POST url and parallel uploads
The POST url returned by the management server to upload the file is expected to be used only once. If there is a download in-progress/error/success state for template/volume in the URL and the SSVM agent gets a new request for the same URL, the agent rejects this request saying there is another download in progress (409 conflict).
The limitation here is that if the template/volume cleanup happened and the template is cleaned, agent would start accepting the url again. This will be fixed later.
API changes
A new API getUploadParams which takes all the params as the registertemplate/uploadvolume except for URL and which returns all the required params plus a post URL to be used to uploadVolume/registerTemplate
- Request
- type: volume or template : required
- params while uploading a volume
- format: the format for the volume. Possible values include QCOW2, OVA, and VHD : required
- name: the name of the volume : required
- zoneid: the ID of the zone the volume is to be hosted on : required
- account: an optional accountName. Must be used with domainId.
- checksum: the MD5 checksum value of this volume
- domainid: an optional domainId. If the account parameter is used, domainId must also be used.
- imagestoreuuid: Image store uuid
- projectid: Upload volume for the project
- params while uploading a tempalte
- displaytext: the display text of the template. This is usually used for display purposes: required
- format: the format for the template. Possible values include QCOW2, RAW, and VHD : required
- hypervisor: the target hypervisor for the template : required
- name: the name of the template : required
- ostypeid: the ID of the OS Type that best represents the OS of this template : required
- zoneid: the ID of the zone the template is to be hosted on : required
- account: an optional accountName. Must be used with domainId.
- bits: 32 or 64 bits support. 64 by default
- checksum: the MD5 checksum value of this template
- details: Template details in key/value pairs.
- domainid: an optional domainId. If the account parameter is used, domainId must also be used.
- isdynamicallyscalable: true if template contains XS/VMWare tools inorder to support dynamic scaling of VM cpu/memory
- isextractable: true if the template or its derivatives are extractable; default is false
- isfeatured: true if this template is a featured template, false otherwise
- ispublic: true if the template is available to all accounts; default is true
- isrouting: true if the template type is routing i.e., if template is used to deploy router
- passwordenabled: true if the template supports the password reset feature; default is false
- projectid: Register template for the project
- requireshvm: true if this template requires HVM
- sshkeyenabled: true if the template supports the sshkey upload feature; default is false
- templatetag: the tag for this template.
- Response
- postURL: post url to upload the file to. Ex: "https://ssvmpublicip/upload/signature" (signature in the postURL is SHA1 key generated using PSK initially shared between SSVM agent and Management Server and the params in the response.)
- params: params to be sent in the post request Ex: params:{"store":{"com.cloud.agent.api.to.NfsTO":"nfs://10.147.28.7/export/home/rajani/lxc/secondary"}, "hvm":false,"description":"lxc1","maxDownloadSizeInBytes":53687091200,"id":207,"resourceType":"TEMPLATE","installPath":"template/tmpl/2/207", "format":"TAR","accountId":2,"name":"207-2-09c78adc-065f-3dc8-9621-9e7a036d7dc9"}
POST url to post the data on to SSVM. This post url is returned from the getUploadParams api call Ex: https://ssvmpublicip/upload/key
- Request: (this is a POST request)
- params: json string of params returned from previous getUploadParams api call file: file to upload
- file: the location of the template/volume to be posted a multipart/form-data
- Response:
- '200 OK' on successful upload
- '401 Authorization error' incase the signature key validation or any other validation failed
- '500 internal server' incase the file upload fails
- 409 'conflict' incase there is an upload in-progress/success/error
At any point, the api user should be able to call the listtemplates/listvolumes api to get the installation status of the template.
If the template failed to upload(401 or 500 above), the user should request for a new upload url and upload it again. He shouldn't be reusing the same upload URL again.
Example
management server api:
request:
http://managementip:8080/client?command=getUploadParams&type=template&response=json&sessionkey=OXgYX%2BINFOOcxQgI2yzEzuA3xCg%3D&name=lxc1&displayText=lxc1&zoneid=-1&format=TAR&isextractable=false&passwordEnabled=false&isdynamicallyscalable=false&osTypeId=1b510c30-3352-11e4-aaca-a5c7f57670d0&hypervisor=LXC&requireshvm=false&_=1410847402478
response:
{
"postURL":"https://ssvmpublicip/upload/uuid",
payload:{"store":{"com.cloud.agent.api.to.NfsTO":"nfs://10.147.28.7/export/home/rajani/lxc/secondary"}, "hvm":false,"description":"lxc1","maxDownloadSizeInBytes":53687091200,"id":207,"resourceType":"TEMPLATE","installPath":"template/tmpl/2/207", "format":"TAR","accountId":2,"name":"207-2-09c78adc-065f-3dc8-9621-9e7a036d7dc9"},
signature:de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
}
file upload:
request:
curl -X POST "https://ssvmpublicip/upload/uuid" -F "template=@templatelocation.tar" -F "signature=de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9" -F "payload={'store':{'com.cloud.agent.api.to.NfsTO':'nfs://10.147.28.7/export/home/rajani/lxc/secondary'}, 'hvm':false,'description':'lxc1','maxDownloadSizeInBytes':53687091200,'id':207,'resourceType':'TEMPLATE','installPath':'template/tmpl/2/207', 'format':'TAR','accountId':2,'name':'207-2-09c78adc-065f-3dc8-9621-9e7a036d7dc9'}" -v
response:
200 OK
DB Changes
No db changes.
Security
- The NFS Store url, path and some other information are exposed to the end user in the response to getUploadParams. This is done so that agent gets all the information required to download the template with the POST request.
- proxy between apache and agent is internal to the SSVM. Hence, no security threat here.
- SSVM PSK location can only be accessed by admin. Hence, no security threat.
- To increase the number of parallel uploads, multiple SSVMs needs to be created. This will be handled automatically by the management server using the SSVM scale up mechanism (system.vm.auto.reserve.capacity, secstorage.capacity.standby and secstorage.session.max configuration variables).
- TBD: number of parallel uploads a single SSVM can handle
UI Changes
A new UI to upload volume/template which should be able to accept "browse and upload" file.
UI flow:
- Browser UI requests the upload form by doing the getUploadParams api call.
- Management Server returns a upload url, payload json string which should be passed on to the upload url while doing the upload
- UI shows an upload dialog with a way to browse and upload file.
- User chooses a file and posts on the url.
- The post request completes once the template is downloaded/errored.
- The user can close the dialog(Post request should still be active) and use list page to check the status of it.
Open Issues
- SSL CA certificates - what kind of certificates will be used?
Bug Reference & Branch
CLOUDSTACK-....
template-upload branch off master