add option to ignore poll switches by env variable USE_POLL_SWITCHES

Change-Id: I9c7f4fef481314a8616ddb89b0da7fec0a6797c9
This commit is contained in:
xiaodongwang 2014-04-07 17:52:33 -07:00
parent 8f4b0a4e7a
commit 8065a459ff
4 changed files with 81 additions and 48 deletions

View File

@ -37,6 +37,12 @@ flags.add('switch_credential',
flags.add('switch_max_retries', type='int', flags.add('switch_max_retries', type='int',
help='max retries of poll switch', help='max retries of poll switch',
default=-1) default=-1)
flags.add('switch_retry_interval', type='int',
help='interval to repoll switch',
default=10)
flags.add_bool('poll_switches',
help='if the client polls switches',
default=True)
flags.add('machines', flags.add('machines',
help='comma separated mac addresses of machines', help='comma separated mac addresses of machines',
default='') default='')
@ -161,51 +167,53 @@ def _poll_switches(client):
else: else:
logging.info('switch %s is already added', switch_ip) logging.info('switch %s is already added', switch_ip)
for switch_ip, switch in all_switches.items(): remain_retries = flags.OPTIONS.switch_max_retries
switch_id = switch['id'] while True:
# if the switch is not in under_monitoring, wait for the time.sleep(flags.OPTIONS.switch_retry_interval)
# poll switch task update the switch information and change for switch_ip, switch in all_switches.items():
# the switch state. switch_id = switch['id']
remain_retries = flags.OPTIONS.switch_max_retries # if the switch is not in under_monitoring, wait for the
while True: # poll switch task update the switch information and change
if remain_retries != 0: # the switch state.
logging.info( logging.info(
'waiting for the switch %s into under_monitoring', 'waiting for the switch %s into under_monitoring',
switch_ip) switch_ip)
status, resp = client.get_switch(switch_id) status, resp = client.get_switch(switch_id)
logging.info('get switch %s status: %s, resp: %s', logging.info('get switch %s status: %s, resp: %s',
switch_ip, status, resp) switch_ip, status, resp)
if status >= 400: if status >= 400:
msg = 'failed to get switch %s' % switch_ip msg = 'failed to get switch %s' % switch_ip
raise Exception(msg)
switch = resp['switch']
all_switches[switch_ip] = switch
if switch['state'] == 'notsupported':
msg = 'switch %s is not supported', switch_ip
raise Exception(msg)
elif switch['state'] in ['initialized', 'repolling']:
logging.info('switch %s is not updated', switch_ip)
else:
if switch['state'] == 'under_monitoring':
logging.info('switch %s is ready', switch_ip)
try:
return _get_machines(client)
except Exception as error:
logging.exception(error)
status, resp = client.update_switch(
switch_id, switch_ip, **switch_credential)
if status >= 400:
msg = 'failed to update switch %s' % switch_ip
raise Exception(msg)
time.sleep(10)
remain_retries -= 1
else:
msg = 'max retries reached for switch %s' % switch_ip
raise Exception(msg) raise Exception(msg)
switch = resp['switch']
all_switches[switch_ip] = switch
if switch['state'] == 'notsupported':
msg = 'switch %s is not supported', switch_ip
raise Exception(msg)
elif switch['state'] in ['initialized', 'repolling']:
logging.info('switch %s is not updated', switch_ip)
elif switch['state'] == 'under_monitoring':
logging.info('switch %s is ready', switch_ip)
try:
return _get_machines(client)
except Exception:
logging.error('failed to get all machines')
if remain_retries > 0:
for switch_ip, switch in all_switches.items():
status, resp = client.update_switch(
switch_id, switch_ip, **switch_credential)
if status >= 400:
msg = 'failed to update switch %s' % switch_ip
raise Exception(msg)
remain_retries -= 1
else:
msg = 'max retries reached'
raise Exception(msg)
def _get_adapter(client): def _get_adapter(client):
"""get adapter.""" """get adapter."""
@ -566,7 +574,11 @@ def main():
flags.init() flags.init()
logsetting.init() logsetting.init()
client = _get_client() client = _get_client()
machines = _poll_switches(client) if flags.OPTIONS.poll_switches:
machines = _poll_switches(client)
else:
machines = _get_machines(client)
adapter_id = _get_adapter(client) adapter_id = _get_adapter(client)
cluster_hosts = _add_cluster(client, adapter_id, machines) cluster_hosts = _add_cluster(client, adapter_id, machines)
_set_cluster_security(client, cluster_hosts) _set_cluster_security(client, cluster_hosts)

View File

@ -36,9 +36,8 @@ fi
# configure chef client and knife # configure chef client and knife
rpm -q chef rpm -q chef
if [[ "$?" != "0" ]]; then if [[ "$?" != "0" ]]; then
download http://www.opscode.com/chef/install.sh chef_install.sh download http://opscode-omnibus-packages.s3.amazonaws.com/el/${IMAGE_VERSION_MAJOR}/${IMAGE_ARCH}/chef-11.8.0-1.el6.${IMAGE_ARCH}.rpm
sudo chmod 755 /tmp/chef_install.sh rpm -ivh /tmp/chef-11.8.0-1.el6.${IMAGE_ARCH}.rpm
sudo /tmp/chef_install.sh
if [[ "$?" != "0" ]]; then if [[ "$?" != "0" ]]; then
echo "chef install failed" echo "chef install failed"
exit 1 exit 1

View File

@ -7,6 +7,7 @@ export SWITCH_IPS=${SWITCH_IPS:-'10.145.81.219'}
export SWITCH_VERSION=${SWITCH_VERSION:-'2c'} export SWITCH_VERSION=${SWITCH_VERSION:-'2c'}
export SWITCH_COMMUNITY=${SWITCH_COMMUNITY:-'public'} export SWITCH_COMMUNITY=${SWITCH_COMMUNITY:-'public'}
export SWITCH_CREDENTIAL=${SWITCH_CREDENTIAL:-"version=${SWITCH_VERSION},community=${SWITCH_COMMUNITY}"} export SWITCH_CREDENTIAL=${SWITCH_CREDENTIAL:-"version=${SWITCH_VERSION},community=${SWITCH_COMMUNITY}"}
export USE_POLL_SWITCHES=${USE_POLL_SWITCHES:-true}
export HOST_ROLES=${HOST_ROLES:-''} export HOST_ROLES=${HOST_ROLES:-''}

View File

@ -143,7 +143,28 @@ if [[ "$?" != "0" ]]; then
exit 1 exit 1
fi fi
${CLIENT_SCRIPT} --logfile= --loglevel=info --logdir= --networking="${NETWORKING}" --partitions="${PARTITION}" --credentials="${SECURITY}" --host_roles="${host_roles_list}" --dashboard_role="${DASHBOARD_ROLE}" --switch_ips="${SWITCH_IPS}" --machines="${machines}" --switch_credential="${SWITCH_CREDENTIAL}" --deployment_timeout="${DEPLOYMENT_TIMEOUT}" if [[ "$USE_POLL_SWITCHES" == "0" || "$USE_POLL_SWITCHES" == "false" ]]; then
POLL_SWITCHES_FLAG="nopoll_switches"
TMP_SWITCH_MACHINE_FILE=$(mktemp)
> ${TMP_SWITCH_MACHINE_FILE}
for switch_ip in ${SWITCH_IPS//,/ }; do
echo "switch,${switch_ip},huawei,${SWITCH_VERSION},${SWITCH_COMMUNITY},under_monitoring" >> ${TMP_SWITCH_MACHINE_FILE}
switch_port=1
for mac in ${machines//,/ }; do
echo "machine,${switch_ip},${switch_port},1,${mac}" >> ${TMP_SWITCH_MACHINE_FILE}
let switch_port+=1
done
break
done
echo "generated switch machine file: $TMP_SWITCH_MACHINE_FILE"
cat $TMP_SWITCH_MACHINE_FILE
echo "======================================================="
/opt/compass/bin/manage_db.py set_switch_machines --switch_machines_file ${TMP_SWITCH_MACHINE_FILE}
else
POLL_SWITCHES_FLAG="poll_switches"
fi
${CLIENT_SCRIPT} --logfile= --loglevel=info --logdir= --networking="${NETWORKING}" --partitions="${PARTITION}" --credentials="${SECURITY}" --host_roles="${host_roles_list}" --dashboard_role="${DASHBOARD_ROLE}" --switch_ips="${SWITCH_IPS}" --machines="${machines}" --switch_credential="${SWITCH_CREDENTIAL}" --deployment_timeout="${DEPLOYMENT_TIMEOUT}" --${POLL_SWITCHES_FLAG}
rc=$? rc=$?
# Tear down machines after the test # Tear down machines after the test
if [[ $rc != 0 ]]; then if [[ $rc != 0 ]]; then