Table of Contents

Temporary Subdomain for Sites

Requirement

When a user wants to have this own domain for his learning site, he can create a site in Learnexa but cannot use this till he brings up this own domain. We should be able to give a temporary sub domain to this site so that user can use the site right away.

Note: Temporary subdomains are only required/applicable to sites which have their own custom domain.

Implementation

We can generate a temporary subdomain for sites which are created with “use your own domain” option.

The temporary sub domain looks - “site-<company id from database>”. This is saved in the database for reference. Also, one more column is added in the data base which says if this site has activated it's own domain or not. Super admin will set this column after bringing up the own domain of the site. All the emails sent to users will have temporary sub domain before this column is set. After this is set the emails will refer to the own domain of the site.

Flow

For example, if a site is created with domain like http://mylearning-site.com and the database id of this company is 123.

The following is the behavior.

1) A temporary subdomain is created as soon as the site is created. In this case the site can be used as http://site-123.learnexa.com immediately after creation.

2) All the user registration mails and url references point to http://site-123.learnexa.com

3) After some time, Learnexa sys admin configures this domain (http://mylearning-site.com) following this procedure https://wiki.exphosted.com/doku.php/byodomain. After bringing this up, super admin of learnexa Updates the ip for this site from the site management page. An email is sent to the user with the assigned public ip which he can use to bring this domain up.

4) From now on all the emails and url references contain the URL - http://mylearning-site.com to refer this site. How ever users can still use both temporary subdomain and the own domain to access this site. When user types “http://site-123.learnexa.com” in the URL it is automatically redirected to “http://mylearning-site.com”.

Code Changes

1) Support temporary sub domain creation and a super admin option for sites to set public ip.

2) find_current_account method in application_controller.rb should be modified to find the site based on the temporary subdomain for the sites which are created using “use your own domain” option.

3) params_for_url_path, learning_site_url and uses_subdomain? methods in models/company.rb should be modified to return own domain url if it is activated otherwise use temporary subdomain url.

Database Changes

Add two columns to table - companies

Column_name type Description
generated_subdomain String the subdomain we generate for a site (ex- site-123 where 123 is id). Only populated for sites with use own domain option
public_ip String This is the ip assigned to this public site. If this value is set, Own domain URL is used in the emails and other links.

Notification Changes

Custom Domain Notification

IP Generation

Public ip can be dynamically generated using the create NAT rule end point of opsource cloud API.

We can create a NAT rule by passing a specific private ip to this API end point. In response we will receive the public ip to which this private ip is mapped. One important point to remember is the private ip should be unique and should not have a NAT rule created already.

Flow

1) User creates a company with “Use my own domain” option selected.

2) Learnexa super admin approves the company. As soon as super admin clicks on the approve button, request is sent to opsource cloud and upon successful response the newly generated public ip is emailed to the customer. Database tables are updated accordingly. Ip creation happens immediately after site creation if it does not require super admin's approval.

(Notification - https://wiki.exphosted.com/doku.php/documents#custom_host_name_notification_2_of_2).

3) Preparing front end server: [To BE DONE/DECIDED] We should try to automate the ip configuration on the front end web server. (https://wiki.exphosted.com/doku.php/byodomain#steps_to_be_taken_for_custom_domain_names).

a) If we can automate this in the background after above step (2), then the new ip is usable immediately.

b) If front end server configuration needs to be done manually then, a copy of notification 2 is sent to the super admin so that network team prepares the front end server with the new public ip.

Implementation

Accessing opsource API

Prerequisite: Need to create a sub adminstrator in the opsource cloud. This user is used by the learnexa app to authenticate and send RAPI requests to opsource. We should restrict this user's previleges as needed.

Reference - opsource-cloud-rest-api-v09-032113.pdf

Creating a public ip

Sample request -

curl -X POST -d '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><NatRule xmlns="http://oec.api.opsource.net/schemas/network"><name>10.166.152.201</name><sourceIp>10.166.152.201</sourceIp></NatRule>' --header 'Content-Type: text/xml'  https://[subadmin_username]:[password]@api.opsourcecloud.net/oec/0.9/[organization_id]/network/[network_id]/natrule

Sample response -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:NatRule xmlns="http://oec.api.opsource.net/schemas/server" xmlns:ns14="http://oec.api.opsource.net/schemas/manualimport" xmlns:ns9="http://oec.api.opsource.net/schemas/admin" xmlns:ns5="http://oec.api.opsource.net/schemas/vip" xmlns:ns12="http://oec.api.opsource.net/schemas/support" xmlns:ns6="http://oec.api.opsource.net/schemas/whitelabel" xmlns:ns13="http://oec.api.opsource.net/schemas/storage" xmlns:ns7="http://oec.api.opsource.net/schemas/datacenter" xmlns:ns10="http://oec.api.opsource.net/schemas/multigeo" xmlns:ns8="http://oec.api.opsource.net/schemas/general" xmlns:ns11="http://oec.api.opsource.net/schemas/reset" xmlns:ns2="http://oec.api.opsource.net/schemas/directory" xmlns:ns4="http://oec.api.opsource.net/schemas/network" xmlns:ns3="http://oec.api.opsource.net/schemas/organization">
    <ns4:id>[natrule_id]</ns4:id>
    <ns4:name>10.166.152.201</ns4:name>
    <ns4:natIp>207.20.54.2</ns4:natIp>
    <ns4:sourceIp>10.166.152.201</ns4:sourceIp>
</ns4:NatRule>

natIp is the public ip which needs to be sent in the email notification.

To be done

Write a ruby gem/plugin that acts as op-source API client. This will be a basic template with only one call implemented for now. It will be easy to extend this in future if needed.

Code Changes

1. Table/config file to track already used ips and keep updating them after every new ip creation.

2. Logic to generate the new private ip.

3. Implement opsource API client and call to create NAT rule api.

4. Integrate the above steps and notifications on super admin approving site flow.

Table Changes

Add one more column to companies table natrule_id.

Table - companies

Column_name type Description
natrule_id String Nat rule id on opsource. Need this if we want to delete this mapping later.
Deleting Public ip

A public ip can be deleted if the company is deleted permanently. Destroy NAT rule API call needs to be made for this. Entry in assigned_ips table is deleted on successful response.

Sample Request -

curl https://[subadmin_username]:[password]@api.opsourcecloud.net/oec/0.9/[organization_id]/network/[network_id]/natrule/[natrule_id]?delete

Sample Response -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:NatRule xmlns="http://oec.api.opsource.net/schemas/server" xmlns:ns14="http://oec.api.opsource.net/schemas/manualimport" xmlns:ns9="http://oec.api.opsource.net/schemas/admin" xmlns:ns5="http://oec.api.opsource.net/schemas/vip" xmlns:ns12="http://oec.api.opsource.net/schemas/support" xmlns:ns6="http://oec.api.opsource.net/schemas/whitelabel" xmlns:ns13="http://oec.api.opsource.net/schemas/storage" xmlns:ns7="http://oec.api.opsource.net/schemas/datacenter" xmlns:ns10="http://oec.api.opsource.net/schemas/multigeo" xmlns:ns8="http://oec.api.opsource.net/schemas/general" xmlns:ns11="http://oec.api.opsource.net/schemas/reset" xmlns:ns2="http://oec.api.opsource.net/schemas/directory" xmlns:ns4="http://oec.api.opsource.net/schemas/network" xmlns:ns3="http://oec.api.opsource.net/schemas/organization">
<ns6:operation>Delete Nat Rule</ns6:operation>
<ns6:result>SUCCESS</ns6:result>
<ns6:resultDetail>NAT Rule deleted</ns6:resultDetail>
<ns6:resultCode>REASON_0</ns6:resultCode>
</ns6:Status>