[WIP]Add Odoo install / update commands

* Handle install or update ;
* Handle stop before or not.
This commit is contained in:
Fabien BOURGEOIS 2019-05-14 12:07:10 +02:00
parent e4a58bd800
commit 25215c116b
2 changed files with 117 additions and 57 deletions

View File

@ -13,7 +13,7 @@ function createLocalState(initState) {
const initialState = {
address: '', port: '', username: '', key: '', database: '',
databases: [], modules: '', serverlog: '', connected: false
databases: [], modules: '', serverlog: '', connected: false, stop: false
};
const [isConnected, setIsConnected] = createSignal(false);
@ -183,7 +183,7 @@ const OdooDockerSshAdminActions = () => {
const webStop = cmdFetch('webstop');
const webRestart = cmdFetch('webrestart');
const odooStart = cmdFetch('odoostart');
const odooStop = cmdFetch('odoostop');
const stop = cmdFetch('odoostop');
const odooRestart = cmdFetch('odoorestart');
const dbStart = cmdFetch('dbstart');
const dbStop = cmdFetch('dbstop');
@ -198,7 +198,7 @@ const OdooDockerSshAdminActions = () => {
</button>
</div>
<div class="col">
<button type="button" class="button clear" onClick={odooStop}>
<button type="button" class="button clear" onClick={stop}>
Odoo : arrêter
</button>
</div>
@ -254,6 +254,34 @@ const OdooDockerSshAdminModules = () => {
return names.length ? names.join(', ') : 'Aucune base sélectionnée';
};
const odooCmd = (action) => {
return () => {
if (!isConnected()) {
humane.log('Non connecté : impossible de lancer une commande Odoo.');
} else {
humane.log('Commande lancée, veuillez patienter.');
fetch('/odooaction', {
method: 'POST',
body: JSON.stringify({
stop: state.stop,
databases: state.databases.filter((db) => db.checked).map((db) => db.name),
modules: state.modules,
action: action
}),
headers: {'Content-Type': 'application/json'}
})
.then(response => {
if (response.status != 200) { humane.log('Erreur : voyez le retour serveur'); }
return response.json();
})
.then(body => {
humane.log('Commande terminée.');
setState('serverlog', state.serverlog + body);
})
}
};
};
return <div>
<h2>🗄 Modules</h2>
<form name="modules-form">
@ -266,11 +294,16 @@ const OdooDockerSshAdminModules = () => {
</div>
<div class="col-6">
<label for="stop"> Arrêt serveur préalable ?</label>
<input type="checkbox" name="stop" value={(state.stop)} />
<input type="checkbox" name="stop" checked={(state.stop)}
onChange={(e) => setState('stop', e.target.checked)} />
</div>
</div>
<button type="button" class="button primary outline">Installer</button>
<button type="button" class="button primary">Mettre à jour</button>
<button type="button" class="button primary outline" onClick={odooCmd('install')}>
Installer
</button>
<button type="button" class="button primary" onClick={odooCmd('update')}>
Mettre à jour
</button>
</form>
</div>
};

129
server.js
View File

@ -54,58 +54,84 @@ const disconnect = (req, res) => {
};
const serverCommand = (req, res) => {
if (!req.params.cmd) {
res.end(sendJSON(res, 403, 'Pas de commande à exécuter'));
} else {
let execCmd = 'cd ~/docker/hosts/*/ && ';
switch (req.params.cmd) {
case 'webstart':
execCmd += 'docker-compose start nginx';
break;
case 'webstop':
execCmd += 'docker-compose stop nginx';
break;
case 'webrestart':
execCmd += 'docker-compose restart nginx';
break;
case 'odoostart':
execCmd += 'docker-compose start golem';
break;
case 'odoostop':
execCmd += 'docker-compose stop golem';
break;
case 'odoorestart':
execCmd += 'docker-compose restart golem';
break;
case 'dbstart':
execCmd += 'docker-compose start pggolem';
break;
case 'dbstop':
execCmd += 'docker-compose stop pggolem';
break;
case 'dbrestart':
execCmd += 'docker-compose restart pggolem';
break;
default:
execCmd = '/bin/true';
}
if (sshConn) {
sshConn.exec(execCmd, (err, stream) => {
if (err) throw err;
let std = '';
stream.on('close', function(code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
res.end(sendJSON(res, 200, std));
}).on('data', function(data) {
console.log('STDOUT: ' + data);
std += data;
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
std += data;
});
});
}
let execCmd = 'cd ~/docker/hosts/*/ && ';
switch (req.params.cmd) {
case 'webstart':
execCmd += 'docker-compose start nginx';
break;
case 'webstop':
execCmd += 'docker-compose stop nginx';
break;
case 'webrestart':
execCmd += 'docker-compose restart nginx';
break;
case 'odoostart':
execCmd += 'docker-compose start golem';
break;
case 'odoostop':
execCmd += 'docker-compose stop golem';
break;
case 'odoorestart':
execCmd += 'docker-compose restart golem';
break;
case 'dbstart':
execCmd += 'docker-compose start pggolem';
break;
case 'dbstop':
execCmd += 'docker-compose stop pggolem';
break;
case 'dbrestart':
execCmd += 'docker-compose restart pggolem';
break;
default:
execCmd = '/bin/true';
}
if (sshConn) {
sshConn.exec(execCmd, (err, stream) => {
if (err) throw err;
let std = '';
stream.on('close', function(code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
res.end(sendJSON(res, 200, std));
}).on('data', function(data) {
console.log('STDOUT: ' + data);
std += data;
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
std += data;
});
});
}
};
const odooAction = (req, res) => {
console.log(req.body);
if (!req.body.databases.length) {
res.end(sendJSON(res, 500, 'Erreur : pas de base de données sélectionnée.\n'));
}
if (!req.body.modules.length) {
res.end(sendJSON(res, 500, 'Erreur : aucun module fourni.\n'));
}
let execCmd = 'cd ~/docker/hosts/*/ && ';
// Base command
let odooCmd = 'OCB/odoo-bin -c odoo.conf --no-xmlrpc --stop-after-init ';
// Install or update
odooCmd += (req.body.action == 'install') ? '-i ' : '-u ';
odooCmd += req.body.modules;
// TODO: multi databases
if (req.body.databases.length == 1) {
odooCmd += ' -d ' + req.body.databases[0];
}
if (req.body.stop) {
execCmd += 'docker-compose stop golem && '
execCmd += 'docker-compose run --rm golem bash -c \'bash genconf.sh && ' + odooCmd + '\' && ';
execCmd += 'docker-compose start golem'
} else {
execCmd += 'docker-compose exec golem bash -c \'' + odooCmd + '\'';
}
console.log(odooCmd);
console.log(execCmd);
res.end(sendJSON(res, 200, 'OK'));
};
polka({ server })
@ -113,6 +139,7 @@ polka({ server })
.post('/connect', connect)
.get('/disconnect', disconnect)
.get('/cmd/:cmd', serverCommand)
.post('/odooaction', odooAction)
.listen(PORT, err => {
if (err) throw err;
console.log(`> Running on localhost:${PORT}`);