This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
146 lines (121 loc) · 4.84 KB
/
index.js
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
const config = require('config');
const asyncPool = require('tiny-async-pool');
const SiteFactoryClient = require('./lib/site-factory-client');
const SiteFactoryClient2 = require('./lib/site-factory-client-2');
const DomainUtility = require('./lib/domain-utility');
const SiteFilter = require('./lib/site-filter');
const UserFilter = require('./lib/user-filter');
const factoryConn = config.get('factoryConnection');
// Client for v1 of the site factory API.
const sourceClient = new SiteFactoryClient({
username: factoryConn.username,
apikey: factoryConn.apikey,
factoryHost: factoryConn.factoryHost
});
// Client for v2 of the site factory API.
const sourceClient2 = new SiteFactoryClient2({
username: factoryConn.username,
apikey: factoryConn.apikey,
factoryHost: factoryConn.factoryHost
});
async function main() {
try {
const targetEnv = getTargetEnvironment();
if (targetEnv !== 'dev' && targetEnv !== 'test') {
console.error('You must specify a staging target, either "DEV" or "TEST".');
process.exit(1);
}
const srcSitelist = await sourceClient.sites.list(); // List of ACSF sites
const filtered = SiteFilter.filterSiteList(srcSitelist, config.stageList);
const siteIds = filtered.map((site) => site.id);
// Stage to requested lower tier.
let stageTask = await sourceClient2.stage.stage(targetEnv, siteIds, true, true, false);
// Wait for staging to complete.
await sourceClient.tasks.waitForCompletion(stageTask.task_id, showProgress);
// Get client for target server.
const targetServer = DomainUtility.SFToStagingTarget(factoryConn.factoryHost, targetEnv);
const destClient = new SiteFactoryClient({
username: factoryConn.username,
apikey: factoryConn.apikey,
factoryHost: targetServer
});
// Re-assign domains to the target.
// Translate site domains from the source to match the target.
const srcDomainList = await getDomainList(sourceClient, siteIds)
const destDomainList = DomainUtility.GenerateDestinationDomains(srcDomainList, targetEnv);
await doDomainAssignments(destClient, destDomainList);
// Clear the varnish and drupal caches
const clearCache = id => destClient.sites.clearCache(id);
await asyncPool(5, siteIds, clearCache);
// // Set tier-specific permissions..
// let users = await destClient.users.list();
// users = UserFilter(users, config.roles.promote, config.roles.remove);
// const assign = async user => destClient.users.setRoles(user.uid, config.roles.desired);
// await asyncPool(5, users, assign);
console.log(`Done staging to '${targetEnv}' environment.`);
} catch (err) {
console.error(err);
}
}
/**
* Callback function for updating progress of the staging operation.
*
* @param {Boolean} isComplete Pass true if the operation has completed, false otherwise.
*/
function showProgress(isComplete) {
process.stdout.write('.');
if (isComplete) {
process.stdout.write('\n');
}
}
/**
* Parse the command line to find out the name of the environment to stage.
*/
function getTargetEnvironment() {
const rawArgs = process.argv;
if (rawArgs.length > 2 && typeof (rawArgs[2]) === "string") {
return rawArgs[2].toLowerCase();
}
else {
return null;
}
}
/**
* Retrieve all the domain information objects for a list of site IDs.
*
* @param {SiteFactoryClient} sfClient Client connected to the staging origin server.
* @param {Int32Array} siteIDList An array of the sites.
*/
async function getDomainList(sfClient, siteIDList) {
const getDomains = async siteID => sfClient.domains.get(siteID);
return asyncPool(5, siteIDList, getDomains);
}
/**
* Perform the actual domain assignments.
*
* @param {SiteFactoryClient} sfClient Client connected to staging destination server.
* @param {Array} assignments array of domain structures.
*/
async function doDomainAssignments(sfClient, assignments) {
/**
* Note: Individual entries in the assignments array have the structure
* {
* siteID: 123,
* primary: 'site1-test-acsf.example.com',
* secondary: [
* "site1-cms-test.example.com"
* ]
* }
*/
const perSite = async siteInfo => {
// The primary domain _must_ be set before the secondaries.
await sfClient.domains.add(siteInfo.siteID, siteInfo.primary);
await Promise.all(
siteInfo.secondary.map(siteAlias => {
sfClient.domains.add(siteInfo.siteID, siteAlias);
})
);
};
return asyncPool(5, assignments, perSite);
}
main();