From 11cfaa7b57ab50c5ec8acbd8f9a42440de0d5a8e Mon Sep 17 00:00:00 2001 From: Tyler Gauntlett Date: Thu, 21 Sep 2023 11:31:23 -0400 Subject: [PATCH] feat(remote-sync): Enable remote syncing based on an API response (#1506) * feat(remote-sync): Enable remote syncing based on an API response * feat(remote-sync): Added option for passing SSH key --------- Co-authored-by: Tyler Gauntlett --- build/ApiRsyncPlugin.js | 22 ++++++++++++++++++++++ build/webpack.config.js | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 build/ApiRsyncPlugin.js diff --git a/build/ApiRsyncPlugin.js b/build/ApiRsyncPlugin.js new file mode 100644 index 000000000..e2ab72e0c --- /dev/null +++ b/build/ApiRsyncPlugin.js @@ -0,0 +1,22 @@ +const { execSync } = require('child_process'); + +function ApiRsyncPlugin(source, destination) { + this.source = source; + this.destination = destination; +} + +/* eslint-disable no-console */ +ApiRsyncPlugin.prototype.apply = function rsync(compiler) { + compiler.plugin('done', () => { + console.log(''); + console.log(`🔄 🔄 🔄 Rsync starting for ${this.source} 🔄 🔄 🔄`); + execSync( + `rsync -avz -e "ssh -p 8022 -o ConnectTimeout=3 ${process.env.DEVPOD_RSYNC_OPTIONS}" ${this.source} ${this.destination}`, + { + stdio: [0, 1, 2], + }, + ); + }); +}; + +module.exports = ApiRsyncPlugin; diff --git a/build/webpack.config.js b/build/webpack.config.js index 75d32380a..bf7523809 100644 --- a/build/webpack.config.js +++ b/build/webpack.config.js @@ -3,20 +3,25 @@ const isDev = NODE_ENV === 'dev'; const isProd = NODE_ENV === 'production'; const fs = require('fs'); +const get = require('lodash/get'); const path = require('path'); const locales = require('@box/languages'); const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); +const { execSync } = require('child_process'); const commonConfig = require('./webpack.common.config'); const RsyncPlugin = require('./RsyncPlugin'); +const ApiRsyncPlugin = require('./ApiRsyncPlugin'); const version = isProd ? require('../package.json').version : 'dev'; let rsyncLocation = ''; +let rsyncApiLocation = null; if (fs.existsSync('build/rsync.json')) { /* eslint-disable */ const rsyncConf = require('./rsync.json'); rsyncLocation = rsyncConf.location; + rsyncApiLocation = rsyncConf.apiLocation; /* eslint-enable */ } @@ -78,6 +83,20 @@ function updateConfig(conf, language, index) { if (rsyncLocation) { config.plugins.push(new RsyncPlugin('dist/.', rsyncLocation)); } + + if (rsyncApiLocation) { + const serverResponse = execSync( + `curl -sk -H "Content-Type: application/json" --connect-timeout 1 ${rsyncApiLocation.url}`, + ) + .toString() + .replace('\n', ''); + + const json = JSON.parse(serverResponse); + + const destination = `${rsyncApiLocation.user}@${get(json, rsyncApiLocation.ip)}:${rsyncApiLocation.path}`; + + config.plugins.push(new ApiRsyncPlugin('dist/.', destination)); + } } if (isProd) {