From 0ccb2434dc7bbafa2d5e60ec5777dd03b00bc258 Mon Sep 17 00:00:00 2001 From: baigk Date: Sun, 27 Dec 2015 21:23:33 +0800 Subject: support redeploy JIRA: COMPASS-216 Change-Id: Ide78ac82c1615259eeb7093c7a4485fbe493c6d9 Signed-off-by: baigk --- deploy/client.py | 56 +++++++++++++++++++++++++++++++----- deploy/compass_vm.sh | 18 ++++++++++-- deploy/deploy_host.sh | 8 +++++- deploy/host_baremetal.sh | 8 ++---- deploy/host_virtual.sh | 18 +++++++----- deploy/launch.sh | 75 +++++++++++++++++++++--------------------------- deploy/prepare.sh | 13 ++++++++- deploy/restful.py | 5 ++++ 8 files changed, 133 insertions(+), 68 deletions(-) (limited to 'deploy') diff --git a/deploy/client.py b/deploy/client.py index 1952b3ab..b9cfd7fe 100644 --- a/deploy/client.py +++ b/deploy/client.py @@ -228,6 +228,9 @@ opts = [ cfg.StrOpt('deploy_type', help='deploy type', default='virtual'), + cfg.StrOpt('deploy_flag', + help='deploy flag', + default='deploy'), ] CONF.register_cli_opts(opts) @@ -279,9 +282,9 @@ class CompassClient(object): def get_machines(self): status, resp = self.client.list_machines() - LOG.info( - 'get all machines status: %s, resp: %s', status, resp) if not self.is_ok(status): + LOG.error( + 'get all machines status: %s, resp: %s', status, resp) raise RuntimeError('failed to get machines') machines_to_add = list(set([ @@ -289,12 +292,21 @@ class CompassClient(object): if machine ])) - LOG.info('machines to add: %s', machines_to_add) machines_db = [str(m["mac"]) for m in resp] - LOG.info('machines in db: %s', machines_db) - assert(set(machines_db) == set(machines_to_add)) + LOG.info('machines in db: %s\n to add: %s', machines_db, machines_to_add) + if not set(machines_to_add).issubset(set(machines_db)): + raise RuntimeError('unidentify machine to add') + + return [m["id"] for m in resp if str(m["mac"]) in machines_to_add] + + def list_clusters(self): + status, resp = self.client.list_clusters(name=CONF.cluster_name) + if not self.is_ok(status) or not resp: + raise RuntimeError('failed to list cluster') + + cluster = resp[0] - return [m["id"] for m in resp] + return cluster['id'] def get_adapter(self): """get adapter.""" @@ -806,6 +818,18 @@ class CompassClient(object): if not self.is_ok(status): raise RuntimeError("deploy cluster failed") + def redeploy_clusters(self, cluster_id): + status, response = self.client.redeploy_cluster( + cluster_id + ) + + if not self.is_ok(status): + LOG.info( + 'deploy cluster %s status %s: %s', + cluster_id, status, response + ) + raise RuntimeError("redeploy cluster failed") + def get_installing_progress(self, cluster_id): """get intalling progress.""" action_timeout = time.time() + 60 * float(CONF.action_timeout) @@ -862,7 +886,8 @@ class CompassClient(object): raise Exception(msg) -def main(): + +def deploy(): client = CompassClient() machines = client.get_machines() @@ -885,6 +910,23 @@ def main(): client.get_installing_progress(cluster_id) client.check_dashboard_links(cluster_id) +def redeploy(): + client = CompassClient() + + cluster_id = client.list_clusters() + + client.redeploy_clusters(cluster_id) + + client.get_installing_progress(cluster_id) + client.check_dashboard_links(cluster_id) + +def main(): + if CONF.deploy_flag == "redeploy": + redeploy() + else: + deploy() + + if __name__ == "__main__": CONF(args=sys.argv[1:]) main() diff --git a/deploy/compass_vm.sh b/deploy/compass_vm.sh index 71d9ad6c..5b94b603 100644 --- a/deploy/compass_vm.sh +++ b/deploy/compass_vm.sh @@ -14,14 +14,26 @@ function tear_down_compass() { } function install_compass_core() { + install_compass "compass_nodocker.yml" +} + +function set_compass_machine() { + local config_file=$WORK_DIR/installer/compass-install/install/group_vars/all + + sed -i -e '/test: true/d' -e '/pxe_boot_macs/d' $config_file + echo "test: true" >> $config_file + echo "pxe_boot_macs: [${machines}]" >> $config_file + + install_compass "compass_machine.yml" +} + +function install_compass() { local inventory_file=$compass_vm_dir/inventory.file - log_info "install_compass_core enter" sed -i "s/mgmt_next_ip:.*/mgmt_next_ip: ${COMPASS_SERVER}/g" $WORK_DIR/installer/compass-install/install/group_vars/all echo "compass_nodocker ansible_ssh_host=$MGMT_IP ansible_ssh_port=22" > $inventory_file - PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60s' python /usr/local/bin/ansible-playbook -e pipeline=true --private-key=$rsa_file --user=root --connection=ssh --inventory-file=$inventory_file $WORK_DIR/installer/compass-install/install/compass_nodocker.yml + PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60s' python /usr/local/bin/ansible-playbook -e pipeline=true --private-key=$rsa_file --user=root --connection=ssh --inventory-file=$inventory_file $WORK_DIR/installer/compass-install/install/$1 exit_status=$? rm $inventory_file - log_info "install_compass_core exit" if [[ $exit_status != 0 ]];then /bin/false fi diff --git a/deploy/deploy_host.sh b/deploy/deploy_host.sh index e7dedb89..be392d71 100644 --- a/deploy/deploy_host.sh +++ b/deploy/deploy_host.sh @@ -5,7 +5,13 @@ function deploy_host(){ reboot_hosts - python ${COMPASS_DIR}/deploy/client.py --compass_server="${HTTP_SERVER_URL}" \ + if [[ "$REDEPLOY_HOST" == true ]]; then + deploy_flag="redeploy" + else + deploy_flag="deploy" + fi + + python ${COMPASS_DIR}/deploy/client.py --deploy_flag=$deploy_flag --compass_server="${HTTP_SERVER_URL}" \ --compass_user_email="${COMPASS_USER_EMAIL}" --compass_user_password="${COMPASS_USER_PASSWORD}" \ --cluster_name="${CLUSTER_NAME}" --language="${LANGUAGE}" --timezone="${TIMEZONE}" \ --hostnames="${HOSTNAMES}" --partitions="${PARTITIONS}" --subnets="${SUBNETS}" \ diff --git a/deploy/host_baremetal.sh b/deploy/host_baremetal.sh index 9e25c98d..0210cee8 100644 --- a/deploy/host_baremetal.sh +++ b/deploy/host_baremetal.sh @@ -6,10 +6,6 @@ function reboot_hosts() { } function get_host_macs() { - local config_file=$WORK_DIR/installer/compass-install/install/group_vars/all - echo "test: true" >> $config_file - machine=`echo $HOST_MACS | sed -e 's/,/'\',\''/g' -e 's/^/'\''/g' -e 's/$/'\''/g'` - echo "pxe_boot_macs: [$machine]" >> $config_file - - echo $machine + machines=`echo $HOST_MACS | sed -e 's/,/'\',\''/g' -e 's/^/'\''/g' -e 's/$/'\''/g'` + echo $machines } diff --git a/deploy/host_virtual.sh b/deploy/host_virtual.sh index 4cb33613..c7e5f84e 100644 --- a/deploy/host_virtual.sh +++ b/deploy/host_virtual.sh @@ -1,10 +1,13 @@ host_vm_dir=$WORK_DIR/vm function tear_down_machines() { + old_ifs=$IFS + IFS=, for i in $HOSTNAMES; do sudo virsh destroy $i sudo virsh undefine $i rm -rf $host_vm_dir/$i done + IFS=$old_ifs } function reboot_hosts() { @@ -14,7 +17,6 @@ function reboot_hosts() { function launch_host_vms() { old_ifs=$IFS IFS=, - tear_down_machines #function_bod mac_array=($machines) log_info "bringing up pxe boot vms" @@ -44,16 +46,18 @@ function launch_host_vms() { } function get_host_macs() { - local config_file=$WORK_DIR/installer/compass-install/install/group_vars/all local mac_generator=${COMPASS_DIR}/deploy/mac_generator.sh local machines= - chmod +x $mac_generator - mac_array=`$mac_generator $VIRT_NUMBER` - machines=`echo $mac_array|sed 's/ /,/g'` + if [[ $REDEPLOY_HOST == "true" ]]; then + mac_array=`cat $WORK_DIR/switch_machines` + else + chmod +x $mac_generator + mac_array=`$mac_generator $VIRT_NUMBER` + echo $mac_array > $WORK_DIR/switch_machines + fi - echo "test: true" >> $config_file - echo "pxe_boot_macs: [${machines}]" >> $config_file + machines=`echo $mac_array|sed 's/ /,/g'` echo $machines } diff --git a/deploy/launch.sh b/deploy/launch.sh index 273e27a9..7160612f 100755 --- a/deploy/launch.sh +++ b/deploy/launch.sh @@ -18,67 +18,56 @@ source ${COMPASS_DIR}/deploy/compass_vm.sh source ${COMPASS_DIR}/deploy/deploy_host.sh ######################### main process -old_ifs=$IFS -IFS=, -tear_down_machines -IFS=$old_ifs +print_logo -if [[ "$DEPLOY_STEP" == "compass_only" || "$DEPLOY_STEP" == "all" ]] -then -if ! prepare_env;then - echo "prepare_env failed" - exit 1 +if [[ ! -z $VIRT_NUMBER ]];then + tear_down_machines fi log_info "########## get host mac begin #############" machines=`get_host_macs` -if [[ -z $machines ]];then +if [[ -z $machines ]]; then log_error "get_host_macs failed" exit 1 fi -log_info "deploy host macs: $machines" export machines -echo "export machines=\""$machines"\"" > $WORK_DIR/switch_machines -log_info "########## set up network begin #############" -if ! create_nets;then - log_error "create_nets failed" - exit 1 -fi -if ! launch_compass;then - log_error "launch_compass failed" - exit 1 -fi +if [[ "$DEPLOY_COMPASS" == "true" ]]; then + if ! prepare_env;then + echo "prepare_env failed" + exit 1 + fi -else + log_info "########## set up network begin #############" + if ! create_nets;then + log_error "create_nets failed" + exit 1 + fi -# test code -if [[ -f $WORK_DIR/switch_machines ]]; then - echo "using last generated machines" - source $WORK_DIR/switch_machines -else - export machines="'00:00:3d:a4:ee:4c','00:00:63:35:3c:2b','00:00:f2:f2:b7:a5','00:00:2f:d3:88:28','00:00:46:67:11:e7'" + if ! launch_compass;then + log_error "launch_compass failed" + exit 1 + fi fi +if [[ -z "$REDEPLOY_HOST" || "$REDEPLOY_HOST" == "false" ]]; then + if ! set_compass_machine; then + log_error "set_compass_machine fail" + fi fi -if [[ "$DEPLOY_STEP" == "host_only" || "$DEPLOY_STEP" == "all" ]]; then +if [[ "$DEPLOY_HOST" == "true" || $REDEPLOY_HOST == "true" ]]; then + if [[ ! -z $VIRT_NUMBER ]];then + if ! launch_host_vms;then + log_error "launch_host_vms failed" + exit 1 + fi + fi -if [[ ! -z $VIRT_NUMBER ]];then - if ! launch_host_vms;then - log_error "launch_host_vms failed" - exit 1 + if ! deploy_host;then + exit 1 fi fi -if ! deploy_host;then - #tear_down_machines - #tear_down_compass - exit 1 -else - #tear_down_machines - #tear_down_compass - exit 0 -fi -fi +figlet -ctf slant Installation Complete! diff --git a/deploy/prepare.sh b/deploy/prepare.sh index e8b2103e..f1aacd98 100755 --- a/deploy/prepare.sh +++ b/deploy/prepare.sh @@ -1,4 +1,16 @@ +function print_logo() +{ + if ! apt --installed list 2>/dev/null | grep "figlet" + then + sudo apt-get update -y + sudo apt-get install -y --force-yes figlet + fi + + figlet -ctf slant Compass Installer + set +x; sleep 2; set -x +} + function download_iso() { iso_name=`basename $ISO_URL` @@ -15,7 +27,6 @@ function download_iso() curl --connect-timeout 10 -o $WORK_DIR/cache/$iso_name $ISO_URL } - function prepare_env() { export PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages sudo apt-get update -y diff --git a/deploy/restful.py b/deploy/restful.py index 4d86da82..44e461c4 100644 --- a/deploy/restful.py +++ b/deploy/restful.py @@ -647,6 +647,11 @@ class Client(object): data['deploy'] = deploy return self._post('/clusters/%s/action' % cluster_id, data=data) + def redeploy_cluster(self, cluster_id): + data = {} + data['redeploy'] = {} + return self._post('/clusters/%s/action' % cluster_id, data=data) + def get_cluster_state(self, cluster_id): return self._get('/clusters/%s/state' % cluster_id) -- cgit 1.2.3-korg