You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ioannis Charalampidis edited this page Oct 25, 2014
·
3 revisions
In this final tutorial we are going to explore some more advanced features that CernVM WebAPI provides. We are going to explore some UI/UX optimizations and then the interface with the running VM.
For this tutorial we are going to start with the resulting index.html from the previous one. You can find it as simple-completed.html in the doc/tutorial folder in the repository.
Step 13 - Displaying progress information
You may have noticed that there is a considerable delay between clicking Start and until the moment the VM is actually started. That's because there are various task taking place in the background. Therefore it's usually a good idea to show this progress to the end-users.
To do so, you will need to listen for the started, completed, progress and failed events.
started is fired when a lenghty task is about to begin.
completed is fired when the lengthy task is completed successfuly
progress is fired during the task, informing the user of it's actions
failed is fired when something went wrong and the request cannot be completed.
Such events are fired both by the plugin and the session objects. Therefore we are going to start with some generic handlers of such events:
// Helper function to update progress valuefunctionupdate_progress_value(value){varpercent=Number(value*100).toFixed()+'%';$("#progress-frame .progress-bar").css('width',percent);$("#progress-frame .progress-bar").text(percent);}// Callback handler to show the progress barfunctionprogress_started(message){$("#progress-frame").show();$("#lbl-progress").text(message);update_progress_value(0.0);}// Callback handler to hide the progress bar when completedfunctionprogress_completed(message){$("#progress-frame").hide();$("#lbl-progress").text(message);update_progress_value(1.0);}// Callback handler to update the progress barfunctionprogress_updated(message,value){$("#lbl-progress").text(message);update_progress_value(value);}// Callback handler in case of errorfunctionprogress_error(message){$("#progress-frame").hide();$("#lbl-error").text(message);$('#alert-frame').fadeIn();}
And now we can register these event handlers both on the plugin and session instances:
...
// Initialize CernVM WebAPI & Callback when readyCVM.startCVMWebAPI(function(api){$("#lbl-status").text("CernVM WebAPI Ready");// Bind progress listeners on the plugin instanceapi.addEventListener('started',progress_started);api.addEventListener('completed',progress_completed);api.addEventListener('progress',progress_updated);api.addEventListener('failed',progress_error);// Request a session through our VMC endpointapi.requestSession('http://test.local:5000/vmcp',function(session){$("#lbl-status").text("Session open");window.s=session;// Obtained from the AppendixvarSTATE_NAMES=['Not yet created','','Powered off','Saved','Paused','Running'];// Bind progress listeners on the session instancesession.addEventListener('started',progress_started);session.addEventListener('completed',progress_completed);session.addEventListener('progress',progress_updated);session.addEventListener('failed',progress_error);
...
Step 14 - Listening for API port state
The CernVM WebAPI is not only reponsible for starting-up and controlling a Virtual Machine instance, but it's also the broker that enables interaction between the browser and the applications inside the guest.
Such interactions are performed through an HTTP channel called API Endpoint. By default that's the default webserver listening on port 80 inside the Virtual Machine, but this can be configured by the VMCP response.
The extension is constantly monitoring the status of the API Endpoint and when it changes state it fires the apiStateChanged event.
Let's make a simple listener of such event that just show the status to the user. We are starting with some additional HTML elements:
<strong>Session state:</strong><spanid="lbl-status">Not started</span></p><!-- An additional label to show the API Status --><p><strong>API state:</strong><ahref="#" id="lbl-api-status">Not available</a></p><p><buttonid="btn-start" class="btn btn-success">Start</button>
We then handle the apiStateChanged event from the session:
// Listen for API state changed eventssession.addEventListener('apiStateChanged',function(newState,apiURL){if(newState){$("#lbl-api-status").text(apiURL);$("#lbl-api-status").attr('href',apiURL);}else{$("#lbl-api-status").text("Not available");$("#lbl-api-status").attr('href',"#");}});
Refresh your browser and wait a couple of minutes until your VM is fully booted. The moment the webserver is started the 'API state' will point to a URL through which you can access the webserver inside the VM.
Step 15 - Overriding VMCP parameters
Some times it might be desirable to configure some of the Virtual Machine parameters specified by the VMCP, after the user has already created the session. By default such modificataions are prohibited since they violate the security restrictions enforced by the VMCP approach. However its possible to define in your VMCP response a list of parameters that the user is allowed to modify.
In our example, open the setup.py created in the third chapter of this tutorial and add a parameter canOverride the MACHINE_CONFIG like so: