Skip to content

Commit

Permalink
more asyncy
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeledy committed Dec 12, 2024
1 parent 558dc10 commit 8e5ec53
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 69 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## {{ UNRELEASED_VERSION }} - [{{ UNRELEASED_DATE }}]({{ UNRELEASED_LINK }})

* Fixed bugs present in various interactions with the Pantheon API.

## v1.9.1 - [December 11, 2024](https://github.com/lando/pantheon/releases/tag/v1.9.1)

* Fixed bug preventing `lando pull` and `lando push` from correctly surfacing available environments
Expand Down
5 changes: 3 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ module.exports = (app, lando) => {
// this allows these commands to all be overriden without causing a failure here
if (answers.auth && (answers.auth !== token || !email)) {
const api = new PantheonApiClient(answers.auth, app.log);
return api.auth().then(() => api.getUser().then(results => {
return api.auth().then(async () => {
const results = await api.getUser();
const cache = {token: answers.auth, email: results.email, date: _.toInteger(_.now() / 1000)};
// Reset this apps metacache
lando.cache.set(app.metaCache, _.merge({}, app.meta, cache), {persist: true});
// Set lando's store of pantheon machine tokens
lando.cache.set(app.pantheonTokenCache, utils.sortTokens(app.pantheonTokens, [cache]), {persist: true});
// Wipe out the apps tooling cache to reset with the new MT
lando.cache.remove(`${app.name}.tooling.cache`);
}))
})
// Throw some sort of error
// NOTE: this provides some error handling when we are completely non-interactive
.catch(err => {
Expand Down
134 changes: 69 additions & 65 deletions inits/pantheon.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@ const showTokenList = (data, tokens = []) => data === 'pantheon' && !_.isEmpty(t
// Helper to determine whether to show token password entry or not
const showTokenEntry = (data, answer, tokens = []) => data === 'pantheon' && (_.isEmpty(tokens) || answer === 'more');

// Helper to get sites for autocomplete
const getAutoCompleteSites = (answers, lando, input = null) => {
// Helper to get sites for autocomplete - simplify Promise chain
const getAutoCompleteSites = async (answers, lando, input = null) => {
if (!_.isEmpty(pantheonSites)) {
return lando.Promise.resolve(pantheonSites).filter(site => _.startsWith(site.name, input));
} else {
const api = new PantheonApiClient(answers['pantheon-auth'], lando.log);
return api.auth().then(async () => {
const sites = await api.getSites();
pantheonSites = sites.map(site => ({name: site.name, value: site.name}));
return pantheonSites;
});
return pantheonSites.filter(site => _.startsWith(site.name, input));
}

Check failure on line 45 in inits/pantheon.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Trailing spaces not allowed

Check failure on line 45 in inits/pantheon.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Trailing spaces not allowed
const api = new PantheonApiClient(answers['pantheon-auth'], lando.log);
await api.auth();
const sites = await api.getSites();
pantheonSites = sites.map(site => ({name: site.name, value: site.name}));
return pantheonSites;
};

/*
Expand Down Expand Up @@ -117,73 +116,78 @@ module.exports = {
build: () => ([
{name: 'wait-for-user', cmd: '/helpers/pantheon-wait-for-user.sh'},
{name: 'generate-key', cmd: `/helpers/generate-key.sh ${pantheonLandoKey} ${pantheonLandoKeyComment}`},
{name: 'post-key', func: (options, lando) => {
{name: 'post-key', func: async (options, lando) => {
const api = new PantheonApiClient(options['pantheon-auth'], lando.log);
const pubKey = path.join(lando.config.userConfRoot, 'keys', `${pantheonLandoKey}.pub`);
return api.auth().then(() => api.postKey(pubKey));
await api.auth();
return api.postKey(pubKey);
}},
{name: 'get-git-url', func: (options, lando) => {
{name: 'get-git-url', func: async (options, lando) => {
const api = new PantheonApiClient(options['pantheon-auth'], lando.log);
return api.auth().then(() => api.getSite(options['pantheon-site'], false)).then(site => {
options['pantheon-git-url'] = getGitUrl(site);
});
await api.auth();
const site = await api.getSite(options['pantheon-site'], false);
options['pantheon-git-url'] = getGitUrl(site);
}},
{name: 'reload-keys', cmd: '/helpers/load-keys.sh --silent', user: 'root'},
{name: 'clone-repo', cmd: options => `/helpers/get-remote-url.sh ${options['pantheon-git-url']}`, remove: 'true'},
]),
}],
build: (options, lando) => {
build: async (options, lando) => {
const api = new PantheonApiClient(options['pantheon-auth'], lando.log);
const pubKey = path.join(lando.config.userConfRoot, 'keys', `${pantheonLandoKey}.pub`);

return lando.Promise.try(() => {
// If we don't have the keys, then resend to be make sure they are there
if (!fs.existsSync(pubKey)) {
return lando.engine.run({
id: 'generate-key',
cmd: `/helpers/generate-key.sh ${pantheonLandoKey} ${pantheonLandoKeyComment}`,
compose: utils.generateComposeFiles(options, lando),
project: lando.config.landoFileConfig.project,
opts: {
mode: 'attach',
services: ['init'],
},
});
}
})
.then(() => api.auth())
// Post our keys again.
.then(() => api.postKey(pubKey))
.then(() => api.auth())
// Get our sites and user
.then(() => {
return Promise.all([api.getSite(options['pantheon-site']), api.getUser()]);
})
// Parse the dataz and set the things
.then(results => {
// Get our site and email
const site = results[0];
const user = results[1];

// Error if site doesn't exist
if (_.isEmpty(site)) throw Error(`${site} does not appear to be a Pantheon site!`);

// This is a good token, lets update our cache
const cache = {token: options['pantheon-auth'], email: user.email, date: _.toInteger(_.now() / 1000)};

// Update lando's store of pantheon machine tokens
const tokens = lando.cache.get(pantheonTokenCache) || [];
lando.cache.set(pantheonTokenCache, utils.sortTokens(tokens, [cache]), {persist: true});
// Update app metdata
const metaData = lando.cache.get(`${options.name}.meta.cache`);
lando.cache.set(`${options.name}.meta.cache`, _.merge({}, metaData, cache), {persist: true});

// Add some stuff to our landofile
return {config: {
framework: _.get(site, 'framework', 'drupal'),
site: _.get(site, 'name', options.name),
id: _.get(site, 'id', 'lando'),
}};
// If we don't have the keys, generate them
if (!fs.existsSync(pubKey)) {
await lando.engine.run({
id: 'generate-key',
cmd: `/helpers/generate-key.sh ${pantheonLandoKey} ${pantheonLandoKeyComment}`,
compose: utils.generateComposeFiles(options, lando),
project: lando.config.landoFileConfig.project,
opts: {
mode: 'attach',
services: ['init'],
},
});
}

// Authenticate and post keys
await api.auth();
await api.postKey(pubKey);
await api.auth();

// Get site and user info
const [site, user] = await Promise.all([
api.getSite(options['pantheon-site']),
api.getUser(),
]);

// Error if site doesn't exist
if (_.isEmpty(site)) {
throw new Error(`${site} does not appear to be a Pantheon site!`);
}

// This is a good token, lets update our cache
const cache = {
token: options['pantheon-auth'],
email: user.email,
date: _.toInteger(_.now() / 1000),
};

// Update lando's store of pantheon machine tokens
const tokens = lando.cache.get(pantheonTokenCache) || [];
lando.cache.set(pantheonTokenCache, utils.sortTokens(tokens, [cache]), {persist: true});

// Update app metadata
const metaData = lando.cache.get(`${options.name}.meta.cache`);
lando.cache.set(`${options.name}.meta.cache`, _.merge({}, metaData, cache), {persist: true});

// Return config to be added to the landofile
return {
config: {
framework: _.get(site, 'framework', 'drupal'),
site: _.get(site, 'name', options.name),
id: _.get(site, 'id', 'lando'),
},
};
},
};
4 changes: 2 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ module.exports = class PantheonApiClient {
*
* @return {Promise<Object>} User account data
*/
getUser() {
return pantheonRequest(this.request, this.log, 'get', ['users', _.get(this.session, 'user_id')]);
async getUser() {
return await pantheonRequest(this.request, this.log, 'get', ['users', _.get(this.session, 'user_id')]);
}

/**
Expand Down

0 comments on commit 8e5ec53

Please sign in to comment.