-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Evan D Shaw
committed
Jul 7, 2022
1 parent
c17e3ca
commit 2a6947d
Showing
16 changed files
with
407 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/ec2/ | ||
/sites/ | ||
node_modules | ||
/sites.json/ | ||
/sites.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
|
||
export const validateHost = (host) => { | ||
if (/^(http|https):\/\//.test(host)) { | ||
console.log('Scheme (http:// or https://) for host must be omitted.'); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
export const validateConfig = (config) => { | ||
if ( | ||
!config || | ||
config === true || | ||
Array.isArray(config) || | ||
config.length !== undefined || | ||
typeof config === "number" | ||
) { | ||
console.log("Configuration file is not valid."); | ||
process.exit(1); | ||
} | ||
|
||
if (!config.ec2) { | ||
console.log("'ec2' is required."); | ||
process.exit(1); | ||
} | ||
|
||
if (!config.ec2.host) { | ||
console.log("'ec2.host' is required."); | ||
process.exit(1); | ||
} | ||
|
||
validateHost(config.ec2.host); | ||
}; | ||
|
||
export const getConfigPath = () => { | ||
const args = process.argv.slice(2); | ||
const rawconfigf = args[0] ? args[0] : null; | ||
return rawconfigf; | ||
} | ||
|
||
export const getConfig = () => { | ||
const args = process.argv.slice(2); | ||
const rawconfigf = args[0] ? args[0] : null; | ||
|
||
if (rawconfigf === null) { | ||
console.log('A path to a valid JSON file is required as the first argument.'); | ||
process.exit(1); | ||
} | ||
|
||
if (!existsSync(rawconfigf)) { | ||
console.log(`${rawconfigf} doesnt exist`); | ||
process.exit(1); | ||
} | ||
|
||
const rawconfig = JSON.parse(readFileSync(rawconfigf, 'utf8')); | ||
validateConfig(rawconfig); | ||
|
||
return rawconfig; | ||
} | ||
|
||
export const updateConfigFile = (config) => { | ||
writeFileSync(getConfigPath(), JSON.stringify(config, null, 2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { exec } from 'child_process'; | ||
import _ from 'lodash'; | ||
import { updateConfigFile } from './config.mjs'; | ||
import { SSH, shell, genPwd } from "./utils.mjs"; | ||
|
||
/** | ||
* Initialize kusanagi (Nginx, MySQL, etc) | ||
*/ | ||
export const ec2Init = (config, callback) => { | ||
const client = new SSH(config); | ||
|
||
client.sshCentos("sudo dnf update -y"); | ||
|
||
exec(`ssh -i ${config.ec2.centos.pem} -t centos@${config.ec2.host} "sudo reboot"`, () => { | ||
console.log('Rebooted. Waiting for instance to come back online...') | ||
shell('sleep 60'); | ||
|
||
const kusanagiPwd = genPwd(); | ||
const dbpwd = genPwd(); | ||
|
||
const outjson = _.cloneDeep(config); | ||
|
||
client.sshCentos( | ||
`sudo su - -c 'kusanagi init --tz Asia/Tokyo --lang ja --keyboard en --passwd \"${kusanagiPwd}\" --nophrase --dbrootpass \"${dbpwd}\" --nginx121 --php74 --mariadb10.5'` | ||
); | ||
|
||
outjson.ec2.kusanagi.userpwd = kusanagiPwd; | ||
outjson.ec2.mysqlRootPass = dbpwd; | ||
updateConfigFile(outjson); | ||
|
||
client.sshCentos( | ||
'sudo mv /root/kusanagi.pem /home/centos/ && sudo chown centos:centos /home/centos/kusanagi.pem' | ||
); | ||
|
||
// download kusanagi SSH private key | ||
client.downloadCentos("/home/centos/kusanagi.pem", "./"); | ||
shell(`mv -f ./kusanagi.pem ${config.ec2.kusanagi.pem}`); | ||
shell(`chmod 400 ${config.ec2.kusanagi.pem}`); | ||
|
||
callback(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env node | ||
import { getConfig } from './config.mjs'; | ||
import { ec2Init } from './ec2-init.mjs'; | ||
|
||
const config = getConfig(); | ||
ec2Init(config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SSH } from "./utils.mjs"; | ||
|
||
/** | ||
* Configures Nginx to properly route subdirs to their corresponding WordPress install | ||
*/ | ||
export const nginxConfigure = (config) => { | ||
const client = new SSH(config); | ||
|
||
const commands = []; | ||
commands.push("cd /etc/opt/kusanagi/nginx/conf.d;"); | ||
config.subsites.forEach((subsite) => { | ||
commands.push( | ||
`echo \\"upstream ${subsite.name} { server 127.0.0.1; }\\" > ${subsite.name}.upstream.conf;` | ||
); | ||
|
||
client.sshCentos(`sudo sed -i '/root\\\s\\/home\\/kusanagi\\/${config.rootsite.name}\\/DocumentRoot/a "location ^~ /${subsite.name}/ { \\nproxy_pass http://${subsite.name}/ \\n}"' /etc/opt/kusanagi/nginx/conf.d/${config.rootsite.name}.conf`); | ||
}); | ||
commands.push("kusanagi nginx --reload;"); | ||
|
||
client.sshCentos(`sudo su - -c '${commands.join(" ")}'`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env node | ||
import { getConfig } from './config.mjs'; | ||
import { nginxConfigure } from './nginx-configure.mjs'; | ||
|
||
const config = getConfig(); | ||
nginxConfigure(config); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import _ from "lodash"; | ||
import { generate } from "generate-password"; | ||
import { updateConfigFile } from "./config.mjs"; | ||
import { SSH, genPwd } from "./utils.mjs"; | ||
|
||
export const provision = (config) => { | ||
const utils = new SSH(config); | ||
|
||
const dbpass = genPwd(); | ||
|
||
const outjson = _.cloneDeep(config); | ||
|
||
utils.sshKusanagi( | ||
`kusanagi provision --wp --wplang ja --fqdn ${config.ec2.host} --no-email --dbname ${config.rootsite.dbname} --dbuser ${config.rootsite.dbuser} --dbpass '${dbpass}' '${config.rootsite.name}'` | ||
); | ||
|
||
outjson.rootsite.dbpass = dbpass; | ||
outjson.rootsite.fullname = config.rootsite.name; | ||
outjson.rootsite.url = config.ec2.host; | ||
updateConfigFile(outjson); | ||
|
||
config.subsites.forEach((subsite, i) => { | ||
const fullname = `${config.rootsite.name}-${subsite.name}`; | ||
const subdbpass = genPwd(); | ||
utils.sshKusanagi( | ||
`kusanagi provision --wp --wplang ja --fqdn ${subsite.name} --no-email --dbname ${subsite.dbname} --dbuser ${subsite.dbuser} --dbpass '${subdbpass}' '${fullname}'` | ||
); | ||
|
||
outjson.subsites[i].dbpass = subdbpass; | ||
outjson.subsites[i].fullname = fullname; | ||
outjson.subsites[i].url = `${config.ec2.host}/${subsite.name}`; | ||
updateConfigFile(outjson); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env node | ||
import { getConfig } from './config.mjs'; | ||
import { provision } from './provision.mjs'; | ||
|
||
const config = getConfig(); | ||
provision(config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,11 @@ | ||
#!/usr/bin/env node | ||
import { existsSync, readFileSync } from 'fs'; | ||
import { validateConfig } from './validate.mjs'; | ||
|
||
const args = process.argv.slice(2); | ||
const rawconfigf = args[0] ? args[0] : null; | ||
|
||
if (rawconfigf === null) { | ||
console.log('A path to a valid JSON file is required as the first argument.'); | ||
process.exit(1); | ||
} | ||
|
||
if (!existsSync(rawconfigf)) { | ||
console.log(`${rawconfigf} doesnt exist`); | ||
process.exit(1); | ||
} | ||
|
||
const rawconfig = JSON.parse(readFileSync(rawconfigf, 'utf8')); | ||
console.log(rawconfig); | ||
|
||
validateConfig({ host: 'mysite.com'}); | ||
import { getConfig } from './config.mjs'; | ||
import { ec2Init } from './ec2-init.mjs'; | ||
import { provision } from './provision.mjs'; | ||
import { wpInstall } from './wp-install.mjs'; | ||
|
||
const config = getConfig(); | ||
ec2Init(config, () => { | ||
provision(config); | ||
wpInstall(config); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { generate } from "generate-password"; | ||
import { execSync } from "child_process"; | ||
|
||
export const shell = (command) => execSync(command, { | ||
shell: '/bin/bash', | ||
stdio: 'inherit', | ||
}); | ||
|
||
export const genPwd = () => { | ||
return generate({ | ||
length: 20, | ||
symbols: "%_#", | ||
numbers: true, | ||
strict: true, | ||
}); | ||
} | ||
|
||
export class SSH { | ||
finalc = ""; | ||
|
||
constructor(finalc) { | ||
this.finalc = finalc; | ||
} | ||
|
||
sshCentos(command) { | ||
shell(`chmod 400 ${this.finalc.ec2.centos.pem}`); | ||
shell( | ||
`ssh -i ${this.finalc.ec2.centos.pem} -t centos@${this.finalc.ec2.host} "${command}"` | ||
); | ||
} | ||
|
||
sshKusanagi(command) { | ||
shell(`chmod 400 ${this.finalc.ec2.kusanagi.pem}`); | ||
shell( | ||
`ssh -i ${this.finalc.ec2.kusanagi.pem} -t kusanagi@${this.finalc.ec2.host} "${command}"` | ||
); | ||
} | ||
|
||
uploadKusanagi(localpath, remotepath) { | ||
shell(`chmod 400 ${this.finalc.ec2.kusanagi.pem}`); | ||
shell( | ||
`scp -i ${this.finalc.ec2.kusanagi.pem} ${localpath} kusanagi@${this.finalc.ec2.host}:${remotepath}` | ||
); | ||
} | ||
|
||
downloadCentos(remotepath, localpath) { | ||
shell(`chmod 400 ${this.finalc.ec2.centos.pem}`); | ||
shell( | ||
`scp -i ${this.finalc.ec2.centos.pem} centos@${this.finalc.ec2.host}:${remotepath} ${localpath}` | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.