Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/agent_start_daemon'
Browse files Browse the repository at this point in the history
  • Loading branch information
fearenales committed Jun 17, 2015
2 parents daa254d + 4528a04 commit 5fdfbba
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 70 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## v0.14.1 - (2015-06-17)

* Bug
* [Agent] Fixing agent start progress in daemon mode

## v0.14.0 - (2015-06-16)

* Bug
Expand Down
82 changes: 45 additions & 37 deletions bin/azk
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export LIBNSS_RESOLVER_VERSION=${LIBNSS_RESOLVER_VERSION}
export RSYNC_MIN_VERSION=${RSYNC_MIN_VERSION}

# Agent opts
export AZK_AGENT_LOG_FILE=$AZK_DATA_PATH/logs/agent.log
export AZK_AGENT_LOG_OUT_FILE=$AZK_DATA_PATH/logs/agent.log.out
export AZK_AGENT_LOG_ERR_FILE=$AZK_DATA_PATH/logs/agent.log.err
export AZK_AGENT_CONF_FILE=$AZK_DATA_PATH/run/agent.json
export AZK_AGENT_PING_FILE=$AZK_DATA_PATH/run/ping

# No use DOCKER_HOST
unset DOCKER_HOST
Expand Down Expand Up @@ -86,12 +89,50 @@ azk_load_nvm() {
fi
}

npm() {
exec $AZK_NODE_PATH/npm --cache=${AZK_DATA_PATH}/nvm/cache ${@}
}

azk_node_run() {
exec $AZK_NODE_PATH/node --harmony $AZK_ROOT_PATH/bin/azk.js "${@}"
}

npm() {
exec $AZK_NODE_PATH/npm --cache=${AZK_DATA_PATH}/nvm/cache ${@}
azk_agent_daemon() {
# Clean log
echo "" > ${AZK_AGENT_LOG_OUT_FILE}
echo "" > ${AZK_AGENT_LOG_ERR_FILE}

# Run agent
set -m
tail -n0 -f ${AZK_AGENT_LOG_OUT_FILE} &
tail -n0 -f ${AZK_AGENT_LOG_ERR_FILE} 1>&2 &

quiet() {
"${@}" >/dev/null 2>&1
}

wait_started() {
while quiet kill -0 "${AGENT_PID}" && [ ! -f "${AZK_AGENT_PING_FILE}" ]; do
sleep 1;
done
}

trap "graceExit" INT TERM
graceExit() {
quiet kill -TERM ${AGENT_PID} || true
while quiet kill -0 "${AGENT_PID}"; do
sleep 1;
done
quiet kill %1 %2 || true
exit 1;
}

rm -f "${AZK_AGENT_PING_FILE}"
azk_node_run "${@}" >> "${AZK_AGENT_LOG_OUT_FILE}" 2>> "${AZK_AGENT_LOG_ERR_FILE}" &
AGENT_PID="$!"
wait_started &
wait $!
quiet kill %1 %2 || true
}

azk_main() {
Expand All @@ -105,40 +146,7 @@ azk_main() {
agent-daemon)
# Removing agent-daemon
shift;

# Reset log
echo "" > $AZK_AGENT_LOG_FILE

# Run agent
trap gracefulExit INT
azk_node_run "${@}" > $AZK_AGENT_LOG_FILE 2>&1 &
AGENT_PID="$!"
tail -n0 -f $AZK_AGENT_LOG_FILE 2>/dev/null &
TAIL_PID="$!"

kill_and_wait() {
kill -s TERM ${1} 2>/dev/null
while kill -0 ${1} 2>/dev/null; do
sleep 1;
done
}

gracefulExit() {
set +e
kill_and_wait ${AGENT_PID}
disown ${TAIL_PID}
kill_and_wait ${TAIL_PID}
echo "exit";
exit 1;
}

# Wait for start
until tail -1 $AZK_AGENT_LOG_FILE | grep -q 'Agent has been successfully started.'; do
kill -0 ${AGENT_PID} 2>/dev/null || gracefulExit;
sleep 1;
done

kill -9 $TAIL_PID
azk_agent_daemon "${@}"
;;
*)
azk_load_nvm
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azk",
"version": "0.14.0",
"version": "0.14.1",
"description": "Orchestrate development environments with agility and automation",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion shared/locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ module.exports = {
github_azk_version_error : 'Failed to access Github to get azk latest version number',
check_version_no_internet: 'Checking version: there is no internet connection to check azk version.',
check_version_error : 'Checking version: %(error_message)s',
clean_containers : "Clearing %(count)d lost containers",
clean_containers : "Cleaning %(count)d lost containers",
migrations: {
alert : "azk updated, checking update procedures...",
changing_domain: "Changing domain upgrading, (issue: #255)",
Expand Down
2 changes: 1 addition & 1 deletion shared/locales/usage-en-US.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$ azk [options] [*command]

Usage:
azk agent (start|status|stop) [--no-daemon --child --no-reload-vm -q -h -l <level>] [-v]...
azk agent (start|status|stop) [--no-daemon --child --no-reload-vm -q -h -l <level> --configure-file=<file>] [-v]...
azk config (track-toggle|track-status) [-q -h -l <level>] [-v]...
azk doctor [--logo -q -h -l <level>] [-v]...
azk docker [-q -h -l <level>] [-v]... [-- <docker-args>...]
Expand Down
4 changes: 2 additions & 2 deletions src/agent/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export class Configure extends UIProxy {
{ 'agent:dns:port': ports.dns, 'agent:balancer:port': ports.balancer },
yield this._checkDockerSocket(socket),
yield this._checkAndConfigureNetwork(ports, false),
yield this._cleanContainers(),
yield this._checkPorts(ports.dns, dns_key, 'dns', 'AZK_DNS_PORT'),
yield this._checkPorts(ports.balancer, balancer_key, 'balancer', 'AZK_BALANCER_PORT'),
yield this._loadDnsServers(),
yield this._cleanContainers()
yield this._loadDnsServers()
);
})
.catch(function (err) {
Expand Down
8 changes: 5 additions & 3 deletions src/agent/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _, config, log, lazy_require, set_config } from 'azk';
import { defer, promiseResolve } from 'azk/utils/promises';
import { _, config, log, lazy_require, set_config, fsAsync } from 'azk';
import { defer, async, promiseResolve } from 'azk/utils/promises';
import { publish } from 'azk/utils/postal';
import { Pid } from 'azk/utils/pid';
import { AgentStopError } from 'azk/utils/errors';
Expand Down Expand Up @@ -128,8 +128,10 @@ var Agent = {
this.processStateHandler();

// Start server and subsistems
return lazy.Server.start(this.stop.bind(this)).then(() => {
return async(this, function* () {
yield lazy.Server.start(this.stop.bind(this));
if (!this.stopping) {
yield fsAsync.writeFile(config("paths:agent_ping"), "");
this.change_status("started");
publish("agent.agent.started.event", {});
log.info("[azk] agent start with pid: " + process.pid);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function cli(args, cwd, ui = UI) {
.route('help', (p, args) => p.help || p['--help'] || _.isEmpty(args))
.route('version', (p) => p.version || p['--version'])
// Commands
.route('agent', (p, args) => p.agent && /(start|status|stop)/.test(args))
.route('agent', (p, args) => p.agent && /(start|status|stop|configure)/.test(args))
.route('vm', (p, args) => p.vm && /(ssh|start|status|installed|stop|remove)/.test(args))
.route('config', (p, args) => p.config && /(track-toggle|track-status)/.test(args))
.route('docker')
Expand Down
14 changes: 11 additions & 3 deletions src/cli/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,17 @@ var UI = {
},

// User interactions methods
execSh(...args) {
var result = (err) => { return (err) ? err.code : 0; };
return nfcall(lazy.execShLib, ...args).spread(result, result);
execSh(command, options = {}, callback = null) {
if (_.isFunction(options)) {
[callback, options] = [options, {}];
}

if (callback) {
return lazy.execShLib(command, options, callback);
} else {
var result = (err) => { return (err) ? err.code : 0; };
return nfcall(lazy.execShLib, command, options).spread(result, result);
}
},

prompt(questions) {
Expand Down
60 changes: 41 additions & 19 deletions src/cmds/agent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CliTrackerController } from 'azk/cli/cli_tracker_controller.js';
import { Helpers } from 'azk/cli/helpers';
import { _, config, lazy_require, log } from 'azk';
import { asyncUnsubscribe } from 'azk/utils/promises';
import { _, path, config, lazy_require, log, fsAsync } from 'azk';
import { defer, async, asyncUnsubscribe } from 'azk/utils/promises';
import { subscribe } from 'azk/utils/postal';

var lazy = lazy_require({
Expand All @@ -17,12 +17,7 @@ class Agent extends CliTrackerController {
}

index(opts) {
return this
.callAgent(opts)
.then((result) => {
process.stdin.pause();
return result;
});
return this.callAgent(opts);
}

callAgent(opts) {
Expand All @@ -40,11 +35,20 @@ class Agent extends CliTrackerController {
// And no running
var status = yield lazy.Client.status(opts.action, false);
if (!status.agent) {
// Check and load configures
var configure_file = this.normalized_params.options['configure-file'];
if (!_.isEmpty(configure_file)) {
configure_file = path.resolve(this.cwd, configure_file);
params.configs = require(configure_file);
} else {
this.ui.warning('status.agent.wait');
params.configs = yield Helpers.configure(this.ui);
}

// Run in daemon mode
if (!opts['no-daemon']) {
var args = _.clone(this.args);
var cmd = `azk agent-daemon --no-daemon "${args.join('" "')}"`;
return this._runDaemon(cmd);
return this._runDaemon(args, params.configs);
}

// Save pid and connect signals
Expand All @@ -63,10 +67,6 @@ class Agent extends CliTrackerController {
});
status.pid.update(process.pid);

// Check and load configures
this.ui.warning('status.agent.wait');
params.configs = yield Helpers.configure(this.ui);

// Remove and adding vm (to refresh vm configs)
if (config('agent:requires_vm') && !opts['no-reload-vm']) {
var cmd_vm = new lazy.VMController({ ui: this.ui });
Expand All @@ -93,11 +93,33 @@ class Agent extends CliTrackerController {
});
}

_runDaemon(cmd) {
this._captureSignal(() => {});
return this.ui.execSh(cmd, {
detached: false,
stdio: [ 'ignore', process.stdout, process.stderr ]
_runDaemon(args, configs) {
return async(this, function* () {
var file = config("paths:agent_config");
log.debug("[agent] save config file", file);
yield fsAsync.writeFile(file, JSON.stringify(configs));

args = args.concat(["--configure", file]);
var cmd = `azk agent-daemon --no-daemon "${args.join('" "')}"`;
return this._runDaemonCommand(cmd);
});

}

_runDaemonCommand(cmd) {
return defer((resolve) => {
var opts = {
detached: false,
stdio: [ 'ignore', process.stdout, process.stderr ]
};

var child = this.ui.execSh(cmd, opts, (err) => {
resolve(err ? err.code : 0);
});

this._captureSignal((signal) => {
child.kill(signal);
});
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ var options = mergeConfig({
azk_meta : path.join(data_path, azk_dir, "shared", "Azkfile.js"),
pems : path.join(paths.vm , '.docker'),
agent_pid : path.join(paths.run, 'agent.pid'),
agent_socket : path.join(paths.run, 'agent.socket'),
agent_ping : envs("AZK_AGENT_PING_FILE"),
agent_config : envs("AZK_AGENT_CONF_FILE"),
memcached_pid : path.join(paths.run, 'memcachedjs.pid'),
hipache_pid : path.join(paths.run, 'hipache.pid'),
agent_socket : path.join(paths.run, 'agent.socket'),
balancer_file : path.join(paths.run, 'hipache.json'),
memcached_socket : path.join(paths.run, 'memcachedjs.socket'),
api_socket : path.join(paths.run, 'api.socket'),
Expand Down

0 comments on commit 5fdfbba

Please sign in to comment.