Removed redundant code

This commit is contained in:
Michele Marcucci 2024-04-12 11:31:46 +02:00
parent 24204f2ec3
commit c5ff3622ea
3 changed files with 18 additions and 108 deletions

View File

@ -30,23 +30,22 @@ const initEnvFile = async () => {
const runMigrations = async () => {
try {
console.log('Run migrations');
const resp = await knex.migrate.latest();
await createCkpoolConfigFile();
await createBitcoinConfigFile();
await runGenerateBitcoinPassword();
} catch (err) {
console.log(err);
}
};
const runGenerateBitcoinPassword = async () => {
try {
console.log('Checking bitcoin password existence');
await knex.migrate.latest();
const [settings] = await knex('settings')
.select(['node_rpc_password as nodeRpcPassword'])
.orderBy('created_at', 'desc')
.orderBy('id', 'desc')
.limit(1);
await utils.auth.manageBitcoinConf(settings);
await runGenerateBitcoinPassword(settings);
} catch (err) {
console.log(err);
}
};
const runGenerateBitcoinPassword = async (settings) => {
try {
console.log('Checking bitcoin password existence');
if (settings && settings.nodeRpcPassword)
return console.log('Bitcoin password found');
@ -56,95 +55,6 @@ const runGenerateBitcoinPassword = async () => {
}
};
const createCkpoolConfigFile = async () => {
const configFilePath = path.resolve(
__dirname,
'../backend/ckpool/ckpool.conf'
);
const configContent = `{
"btcd": [
{
"url": "127.0.0.1:8332",
"auth": "futurebit",
"pass": "",
"notify": true
}
],
"logdir": "/opt/apolloapi/backend/ckpool/logs"
}`;
try {
// Check if the file exists
await fs.access(configFilePath);
console.log('File ckpool.conf already exists.');
} catch (error) {
try {
// Create the file
await fs.writeFile(configFilePath, configContent, 'utf-8');
console.log('File ckpool.conf created.');
} catch (error) {
console.error(
`Error during the creation of the file ckpool.conf: ${error.message}`
);
}
} finally {
const [settings] = await knex('settings')
.select(['node_rpc_password as nodeRpcPassword'])
.orderBy('created_at', 'desc')
.orderBy('id', 'desc')
.limit(1);
if (settings && settings.nodeRpcPassword) {
exec(
`sudo sed -i 's#"pass":.*#"pass": "${settings.nodeRpcPassword}",#g' ${configFilePath}`
);
}
}
};
const createBitcoinConfigFile = async () => {
const configFilePath = path.resolve(
__dirname,
'../backend/node/bitcoin.conf'
);
const configContent = `server=1
rpcuser=futurebit
rpcpassword=
daemon=0
maxconnections=32
upnp=1
uacomment=FutureBit-Apollo-Node`;
try {
// Check if the file exists
await fs.access(configFilePath);
console.log('File bitcoin.conf already exists.');
} catch (error) {
try {
// Create the file
await fs.writeFile(configFilePath, configContent, 'utf-8');
console.log('File bitcoin.conf created.');
await utils.auth.changeNodeRpcPassword();
} catch (error) {
console.error(
`Error during the creation of the file bitcoin.conf: ${error.message}`
);
}
} finally {
const [settings] = await knex('settings')
.select(['node_rpc_password as nodeRpcPassword'])
.orderBy('created_at', 'desc')
.orderBy('id', 'desc')
.limit(1);
if (settings && settings.nodeRpcPassword) {
exec(
`sudo sed -i 's/rpcpassword.*/rpcpassword=${settings.nodeRpcPassword}/g' ${configFilePath}`
);
}
}
};
initEnvFile();
runMigrations().then(startServer);

View File

@ -11,7 +11,6 @@ module.exports = ({ define }) => {
})
utils.auth.changeSystemPassword(password)
await utils.auth.changeNodeRpcPassword()
} catch (err) {
console.log('ERROR', err);
}

View File

@ -55,11 +55,11 @@ module.exports.auth = {
await fsPromises.access(configCkpoolFilePath);
exec(
`sudo sed -i s/rpcpassword.*/rpcpassword=${password}/g ${configBitcoinFilePath}`
`sudo sed -i 's/rpcpassword.*/rpcpassword=${password}/g' ${configBitcoinFilePath}`
);
exec(
`sudo sed -i 's#"pass": ""#"pass": "${password}"#g' ${configCkpoolFilePath}`
`sudo sed -i 's#"pass":.*#"pass": "${password}",#g' ${configCkpoolFilePath}`
);
exec('sudo systemctl restart node');
@ -109,11 +109,11 @@ module.exports.auth = {
const defaultConf = `server=1\nrpcuser=futurebit\nrpcpassword=${settings.nodeRpcPassword}\ndaemon=0\nmaxconnections=32\nupnp=1\nuacomment=FutureBit-Apollo-Node`;
let conf = defaultConf;
this.manageCkpoolConf(settings);
if (settings.nodeEnableSoloMining) {
conf += `\n#SOLO_START\nzmqpubhashblock=tcp://127.0.0.1:28332\n#SOLO_END`;
this.manageCkpoolConf(settings);
exec(
`sudo cp ${configCkpoolServiceFilePath} /etc/systemd/system/ckpool.service`
);
@ -139,8 +139,9 @@ module.exports.auth = {
console.log('Writing Bitcoin conf file', conf);
exec(
`echo "${conf}" | sudo tee ${configBitcoinFilePath}`
await fsPromises.writeFile(
configBitcoinFilePath,
conf
);
exec('sudo systemctl restart node');