Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ You may refer to the follow examples:

Custom domains allows users to use their own domains.

For domain configuration please [Refer to Scaleway Documentation](https://www.scaleway.com/en/docs/compute/functions/how-to/add-a-custom-domain-name-to-a-function/)
For domain configuration please refer to [custom domains on Function](https://www.scaleway.com/en/docs/compute/functions/how-to/add-a-custom-domain-name-to-a-function/) or
[custom domains on Container](https://www.scaleway.com/en/docs/compute/containers/how-to/add-a-custom-domain-to-a-container/)

Integration with serverless framework example :

Expand Down
34 changes: 33 additions & 1 deletion deploy/lib/createContainers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const BbPromise = require('bluebird');
const singleSource = require('../../shared/singleSource');
const secrets = require('../../shared/secrets');
const domainUtils = require('../../shared/domains');

module.exports = {
createContainers() {
Expand All @@ -18,12 +19,33 @@ module.exports = {
`Container ${res.name} removed from config file, deleting it...`
);
this.waitForContainerStatus(containerIdToDelete, "deleted").then(
this.serverless.cli.log(`Container ${res.name} deleted`)
this.serverless.cli.log(`Container ${res.name} deleted`),
);
});
});
},


applyDomainsContainer(containerId, customDomains) {
this.listDomainsContainer(containerId).then((domains) => {
const existingDomains = domainUtils.formatDomainsStructure(domains);
const domainsToCreate = domainUtils.getDomainsToCreate(customDomains, existingDomains);
const domainsIdToDelete = domainUtils.getDomainsToDelete(customDomains, existingDomains);

domainsToCreate.forEach((newDomain) => {
const createDomainParams = { container_id: containerId, hostname: newDomain };

this.createDomainAndLog(createDomainParams);
});

domainsIdToDelete.forEach((domainId) => {
this.deleteDomain(domainId).then((res) => {
this.serverless.cli.log(`Deleting domain ${res.hostname}`);
});
});
});
},

createOrUpdateContainers(foundContainers) {
const { containers } = this.provider.serverless.service.custom;

Expand Down Expand Up @@ -68,6 +90,12 @@ module.exports = {
port: container.port,
};

// checking if there is custom_domains set on container creation.
if (container.custom_domains && container.custom_domains.length > 0) {
this.serverless.cli.log("WARNING: custom_domains are available on container update only. "+
"Redeploy your container to apply custom domains. Doc : https://www.scaleway.com/en/docs/compute/containers/how-to/add-a-custom-domain-to-a-container/")
}

this.serverless.cli.log(`Creating container ${container.name}...`);

return this.createContainer(params)
Expand All @@ -93,6 +121,10 @@ module.exports = {
};

this.serverless.cli.log(`Updating container ${container.name}...`);

// assign domains
this.applyDomainsContainer(foundContainer.id, container.custom_domains);

return this.updateContainer(foundContainer.id, params)
.then(response => Object.assign(response, { directory: container.directory }));
},
Expand Down
73 changes: 9 additions & 64 deletions deploy/lib/createFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const BbPromise = require('bluebird');
const secrets = require('../../shared/secrets');
const singleSource = require('../../shared/singleSource');
const domainUtils = require('../../shared/domains');
const { RUNTIME_STATUS_AVAILABLE, RUNTIME_STATUS_EOL, RUNTIME_STATUS_EOS } = require('../../shared/runtimes');

module.exports = {
Expand Down Expand Up @@ -53,77 +54,21 @@ module.exports = {
});
},

applyDomains(funcId, customDomains) {
applyDomainsFunc(funcId, customDomains) {
// we make a diff to know which domains to add or delete
const domainsToCreate = [];
const domainsIdToDelete = [];
const existingDomains = [];

this.listDomains(funcId).then((domains) => {
domains.forEach((domain) => {
existingDomains.push({ hostname: domain.hostname, id: domain.id });
});

if (
customDomains !== undefined &&
customDomains !== null &&
customDomains.length > 0
) {
customDomains.forEach((customDomain) => {
domainsIdToDelete.push(customDomain.id);

let domainFound = false;

existingDomains.forEach((existingDom) => {
if (existingDom.hostname === customDomain) {
domainFound = true;
return false;
}
});
if (!domainFound) {
domainsToCreate.push(customDomain);
}
});
}

existingDomains.forEach((existingDomain) => {
if (
(customDomains === undefined || customDomains === null) &&
existingDomain.id !== undefined
) {
domainsIdToDelete.push(existingDomain.id);
} else if (!customDomains.includes(existingDomain.hostname)) {
domainsIdToDelete.push(existingDomain.id);
}
});
this.listDomainsFunction(funcId).then((domains) => {
const existingDomains = domainUtils.formatDomainsStructure(domains);
const domainsToCreate = domainUtils.getDomainsToCreate(customDomains, existingDomains);
const domainsIdToDelete = domainUtils.getDomainsToDelete(customDomains, existingDomains);

domainsToCreate.forEach((newDomain) => {
const createDomainParams = { function_id: funcId, hostname: newDomain };

this.createDomain(createDomainParams)
.then((res) => {
this.serverless.cli.log(`Creating domain ${res.hostname}`);
})
.then(
() => {},
(reason) => {
this.serverless.cli.log(
`Error on domain : ${newDomain}, reason : ${reason.message}`
);

if (reason.message.includes("could not validate")) {
this.serverless.cli.log(
"Ensure CNAME configuration is ok, it can take some time for a record to propagate"
);
}
}
);
this.createDomainAndLog(createDomainParams);
});

domainsIdToDelete.forEach((domainId) => {
if (domainId === undefined) {
return;
}
this.deleteDomain(domainId).then((res) => {
this.serverless.cli.log(`Deleting domain ${res.hostname}`);
});
Expand Down Expand Up @@ -226,7 +171,7 @@ Runtime lifecycle doc : https://www.scaleway.com/en/docs/compute/functions/refer
params.runtime = this.validateRuntime(
func,
availableRuntimes,
this.serverless.cli
this.serverless.cli,
);

// checking if there is custom_domains set on function creation.
Expand Down Expand Up @@ -270,7 +215,7 @@ Runtime lifecycle doc : https://www.scaleway.com/en/docs/compute/functions/refer
this.serverless.cli.log(`Updating function ${func.name}...`);

// assign domains
this.applyDomains(foundFunc.id, func.custom_domains);
this.applyDomainsFunc(foundFunc.id, func.custom_domains);

return this.updateFunction(foundFunc.id, params).then((response) =>
Object.assign(response, { handler: func.handler })
Expand Down
33 changes: 25 additions & 8 deletions deploy/lib/deployContainers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@ const BbPromise = require('bluebird');

module.exports = {
deployContainers() {
this.serverless.cli.log('Deploying Containers...');
this.serverless.cli.log("Deploying Containers...");
return BbPromise.bind(this)
.then(this.deployEachContainer)
.then(() => this.serverless.cli.log('Waiting for container deployments, this may take multiple minutes...'))
.then(() =>
this.serverless.cli.log(
"Waiting for container deployments, this may take multiple minutes..."
)
)
.then(this.printContainerEndpointsAfterDeployment);
},

deployEachContainer() {
const promises = this.containers.map(
container => this.deployContainer(container.id),
const promises = this.containers.map((container) =>
this.deployContainer(container.id)
);
return Promise.all(promises);
},

printContainerEndpointsAfterDeployment() {
return this.waitContainersAreDeployed(this.namespace.id)
.then(containers => containers.forEach(
container => this.serverless.cli.log(`Container ${container.name} has been deployed to: https://${container.domain_name}`),
));
return this.waitContainersAreDeployed(this.namespace.id).then(
(containers) => {
containers.forEach((container) => {
this.serverless.cli.log(
`Container ${container.name} has been deployed to: https://${container.domain_name}`
);

this.serverless.cli.log("Waiting for domains deployment...");

this.waitDomainsAreDeployedContainer(container.id).then((domains) => {
domains.forEach((domain) => {
this.serverless.cli.log(`Domain ready : ${domain.hostname}`);
});
});
});
}
);
},
};
2 changes: 1 addition & 1 deletion deploy/lib/deployFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = {
'Waiting for domains deployment...',
);

this.waitDomainsAreDeployed(func.id)
this.waitDomainsAreDeployedFunction(func.id)
.then((domains) => {
domains.forEach((domain) => {
this.serverless.cli.log(`Domain ready : ${domain.hostname}`);
Expand Down
Loading