diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 39a367d..5edd0bf 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,4 +1,4 @@ -# Version 3.0.11 +# Version 3.0.12 ## New Features: * Manage Containers: Has a new option called "Commit Image" that allows you to update a Docker image with the current running image. @@ -10,9 +10,10 @@ * Webconsole: Removed depcreciated BodyParser constructor * Webconsole: Image list shows size and created time. * ElasticSearch: Support for SSL/TLS +* SystemD unit file generation for containers to start on Boot. ## Removed or Deprecated: -* None +* Auto-starting of containers at boot has been replaced with SystemD Unit files. ## Bug Fixes: * Docker Swarm fix for adding nodes to existing cluster diff --git a/agent/agent.js b/agent/agent.js index 76ca7e6..d6bdf57 100644 --- a/agent/agent.js +++ b/agent/agent.js @@ -355,6 +355,7 @@ app.post('/run', (req, res) => { node }; + const check_token = req.body.token; if (check_token !== token) { @@ -368,16 +369,93 @@ app.post('/run', (req, res) => { output.output = stderr; } else { output.output = stdout; + + if (config.autostart_containers) { + if (req.body.command.indexOf('docker container run') > -1) { + systemd(req.body.command); + } else if (req.body.command.indexOf('docker container rm') > -1) { + systemd_remove(req.body.command); + } + } + res.json(output); } - res.json(output); }, err => { if (err) { console.error('error:', err); } - // Console.log('output', output); + Console.log('output', output); }); }); +function systemd(data) { + const systemd = ["[Unit]", "Description=Container", "After=podman.service", "[Service]", "Type=oneshot", "RemainAfterExit=yes", "Environment=\"NAME=", "Environment=\"ARGUMENTS=", "ExecStartPre=/bin/sh -c \"/usr/bin/podman rm -f ${NAME}; exit 0;\"", "ExecStartPre=/bin/sh -c \"/usr/bin/podman build -t ${NAME} /docker/${NAME}; exit 0;\"", "ExecStart=/bin/sh -c \"podman run -d --name ${NAME} ${ARGUMENTS} localhost/${NAME}; exit 0;\"", "ExecStart=/bin/sh -c \"systemctl restart firewalld.service; exit 0;\"", "ExecStart=/bin/sh -c \"podman network reload -a; exit 0;\"", "ExecStop=/usr/bin/podman rm -f ${NAME}\"", "[Install]", "WantedBy=multi-user.target"]; + var container_name = data.split(' '); + var name = container_name[container_name.length - 1]; + + for (const unit_file of systemd) { + if (unit_file.indexOf('Unit') > -1) { + console.log(unit_file); + fs.writeFile('/etc/systemd/system/picluster-' + name + '.service', unit_file + '\n', err => { + if (err) { + console.log(err); + } + }); + } else if (unit_file.indexOf('NAME=') > -1) { + let analyze_unit_file = 'Environment=\"NAME=' + name + '"'; + fs.appendFile('/etc/systemd/system/picluster-' + name + '.service', analyze_unit_file + '\n', err => { + if (err) { + console.log(err); + } + }); + } else if (unit_file.indexOf('ARGUMENTS') > -1) { + + final_arguments = data.split(';'); + final_line = final_arguments[1].split(name); + end_line = 'Environment=\"ARGUMENTS="' + final_line[1] + '"'; + fs.appendFile('/etc/systemd/system/picluster-' + name + '.service', end_line + '\n', err => { + if (err) { + console.log(err); + } + }); + } else { + fs.appendFile('/etc/systemd/system/picluster-' + name + '.service', unit_file + '\n', err => { + if (err) { + console.log(err); + } + }); + console.log(unit_file); + } + } + exec('systemctl enable picluster-' + name + '.service', (error, stdout, stderr) => { + if (error) { + console.log(error); + } + if (stdout) { + console.log(stdout); + } + }); + +} + +function systemd_remove(data) { + var container_name = data.split(' '); + var name = container_name[container_name.length - 1]; + + exec('systemctl disable picluster-' + name + '.service', (error, stdout, stderr) => { + if (error) { + console.log(error); + } else { + console.log('\nRemoving picluster-' + name + '.service'); + fs.unlink('/etc/systemd/system/picluster-' + name + '.service', error => { + if (error) { + console.log(error); + } + }); + } + }); + +} + if (config.ssl && config.ssl_cert && config.ssl_key) { console.log('SSL Agent API enabled'); const ssl_options = { @@ -441,21 +519,6 @@ bootstrapNode(); function additional_services() { monitoring(); - if (config.autostart_containers) { - console.log('Starting all the containers.....'); - - superagent - .get(`${scheme}${server}:${server_port}/start`) - .query({ - token: token, - container: '*' - }) - .end((err, res) => { - if (err) { - console.log(error); - } - }); - } if (config.vip_ip && config.vip) { vip = config.vip_ip; diff --git a/server/server.js b/server/server.js index ca387c7..b00bf05 100644 --- a/server/server.js +++ b/server/server.js @@ -18,7 +18,7 @@ const { const { exec } = require('child_process'); -const version = "3.0.11" +const version = "3.0.12" const bootstrap = { status: 1