Update Ubuntu using except script

I have a cluster and I want to update all its nodes automatically by using an except script. As a root user at every node I have to execute three commands

apt-get update
apt-get upgrade
apt-get dist-upgrade

For the moment the script I wrote is this,

set USER root
set MASTER_IP some_ip
set UPDATE_CMD "apt-get update"
set UPGRADE_CMD "apt-get upgrade"
set DIST_UPGRADE_CMD "apt-get dist-upgrade"
set SLAVES 2

set timeout -1
#set exp_internal from 0 -> 1 to debug this script
exp_internal 1
spawn ssh $USER@master
expect "*# "
send "$UPDATE_CMDr"
expect "*# "
send "$UPGRADE_CMDr"
expect {
         "Do*[Y/n] " { 
            send "Yr" 
         }
        "*# " {}
}
expect "*# "
send "$DIST_UPGRADE_CMDr"
expect {
        "Do*[Y/n] " { 
            send "Yr" 
        }
        "*# "
}

#update slave nodes
for {set slave 1} { $slave <= 2} {incr slave 1} {
    expect "*# "
    send_user "nConnecting to slave$slave node ...n"
    send "ssh $USER@slave$slaver"
    expect "*# "
    send "$UPDATE_CMDr"
    send_user "nRetrieving lists of avalaible updates ...n"
    expect "*# "
    send "$UPGRADE_CMDr"
    expect {
        "*[Y/n] " {
            send "Y"    
        }
        "*."
    }
    expect "*# "
    send "$DIST_UPGRADE_CMDr"
    expect {
        "*[Y/n] " {
            send "Y"    
        }
        "*."
    }
    expect "*# "
    send_user "Update of slave$slave is done!n"
    send "exitr"
}   
expect "*# "
send "exitr"
close

The upgrade and dist-upgrade commands have a different execution path depending whether the system is going to be updated or it is already updated. In the first case, I except something like

Do you want to continue? [Y/n] 

where I should

send "Yr"

In the second case, I except an information message and finally a prompt that for my cluster is “#”.

I tried debugging my program using exp_internal but either I got the message

match glob pattern "Do*[Y/n] "? no "*# "? yes

or the program terminates without ever executing

send "$DIST_UPGRADE_CMDr"

It ends with the information message printed from the command

send "$UPGRADE_CMDr"

Sometimes I saw the “Y” from command

send "Yr"

printed on the screen although I didn’t except it. Finally, sometimes I got on the screen the message

Do you want to continue? [Y/n] 

and when I typed “Y” and “enter” nothing happened; it is like the screen freezes. Moreover I would except that the “Y” + “enter” should be “typed” from script.

What I am doing wrong?


Source: ubuntu

Leave a Reply