elphel-scp.bbclass 4.74 KB
Newer Older
1 2
# Adding support for scp files to the target (similar to install) ---

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
3 4
REMOTE_USER ??= "root"
REMOTE_IP ??= "192.168.0.9"
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
5
IDENTITY_FILE ??= "~/.ssh/id_rsa"
6

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
7 8
inherit elphel-ssh

9 10 11 12 13 14 15 16 17 18
#do_target_scp () {
#    #Without next echo - no trace of the scp in the log!
#    SSH_COMMAND='tar -C / -xzpf /image.tar.gz; rm -f /image.tar.gz; sync'
#    tar -czvf ${WORKDIR}/image.tar.gz -C ${WORKDIR}/image .
#    echo scp -i ${IDENTITY_FILE} -p ${WORKDIR}/image.tar.gz ${REMOTE_USER}@${REMOTE_IP}:/
#    scp -i ${IDENTITY_FILE} -p ${WORKDIR}/image.tar.gz ${REMOTE_USER}@${REMOTE_IP}:/
#    echo ssh -i ${IDENTITY_FILE} ${REMOTE_USER}@${REMOTE_IP} ${SSH_COMMAND}
#    ssh -i ${IDENTITY_FILE} ${REMOTE_USER}@${REMOTE_IP} ${SSH_COMMAND}
#}

19 20 21 22 23
#python () {
#    #d.setVarFlag('do_target_scp', 'fakeroot', '1')
#    print("test")
#}

24 25
python do_target_scp () {
    import subprocess
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
26

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
27
    WORKDIR       = d.getVar('WORKDIR'      , True)
28
    IDENTITY_FILE = d.getVar('IDENTITY_FILE', True)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
29 30 31
    REMOTE_USER   = d.getVar('REMOTE_USER'  , True)
    REMOTE_IP     = d.getVar('REMOTE_IP'    , True)
    COPY_TO_NAND  = d.getVar('COPY_TO_NAND' , True)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
32

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
33 34
    # hardcodign for now
    MMC2    = "/dev/mmcblk0p2"
35 36
    MMC2MNT = "/mnt/mmc2"
    
37 38 39 40
    #cmd = "tar -czvf "+WORKDIR+"/image.tar.gz -C "+WORKDIR+"/image ."
    #print("cmd: "+cmd)
    #subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
41

42
    cmd = "ping "+REMOTE_IP+" -c 1"
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
43
    print("cmd: "+cmd)
44 45 46 47
    try:
        subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
    except subprocess.CalledProcessError:
        raise Exception("No route to target "+REMOTE_IP)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
48

49 50 51
    nandboot = command_over_ssh(d,"'if [ -d /tmp/rootfs.ro ]; then echo 1; else echo 0;fi'")
    
    if COPY_TO_NAND=='1':
52

53
        print("Copy to NAND")
54

55 56 57
        if nandboot=="1":
            #copy archive
            cmd = "scp -i "+IDENTITY_FILE+" -p "+WORKDIR+"/image.tar.gz "+REMOTE_USER+"@"+REMOTE_IP+":/"
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
58
            print("cmd: "+cmd)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
            subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
            #unpack archive to /
            command_over_ssh(d,"'tar -C / -xzpf /image.tar.gz; sync'")
            #unpack archive to /tmp/rootfs.ro
            command_over_ssh(d,"'tar -C /tmp/rootfs.ro/ -xzpf /image.tar.gz; rm -f /image.tar.gz; sync'")
        else:
            raise Exception("\033[1;37mPlease, reboot from NAND\033[0m")
    else:
        
        if nandboot=="1":
        
            print("Copy to MMC while booted from NAND")
        
            mmc2 = command_over_ssh(d,"'if [ -b "+MMC2+" ]; then echo 1; else echo 0;fi'")
            if mmc2=="1":
                # nobody likes empty output
                mmc2_mnt = command_over_ssh(d,"'TEST=`df -h | grep "+MMC2+"`;echo \"0\"$TEST'")
                
                if mmc2_mnt!="0":
                  tmp = mmc2_mnt.split()
                  mountpoint = tmp[5]
                  print("MMC rootfs partition is already mounted on "+str(mountpoint))
                else:
                  command_over_ssh(d,"'mkdir "+MMC2MNT+"; mount "+MMC2+" "+MMC2MNT+"'")
                  mountpoint = MMC2MNT
                  print("Created and mounted "+MMC2+" to "+mountpoint)
                                
                #copy archive
                print("Copy archive")
                cmd = "scp -i "+IDENTITY_FILE+" -p "+WORKDIR+"/image.tar.gz "+REMOTE_USER+"@"+REMOTE_IP+":/"
                subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
                
                #unpack archive to mountpoint
                print("Unpack archive then delete")
                command_over_ssh(d,"'tar -C "+mountpoint+" -xzpf /image.tar.gz; rm -f /image.tar.gz; sync'")
                
            else:
                raise Exception("\033[1;37mMMC rootfs partition "+MMC2+" not found.\033[0m")
            
        else:
        
            print("Copy to MMC while booted from MMC")
        
            #copy archive
            cmd = "scp -i "+IDENTITY_FILE+" -p "+WORKDIR+"/image.tar.gz "+REMOTE_USER+"@"+REMOTE_IP+":/"
            subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
            
            #unpack archive to /
            command_over_ssh(d,"'tar -C / -xzpf /image.tar.gz; rm -f /image.tar.gz; sync'")
108

109
}
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
110
    
111 112 113 114
addtask do_target_scp after do_install

do_target_scp[doc] = "scp installed files to the target. TARGET_USER and TARGET_IP should be defined (ssh-copy-id -i KEY.pub TARGET_USER@TARGET_IP should be issued once)"

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
115 116
#EXPORT_FUNCTIONS do_target_scp
#EXPORT_FUNCTIONS command_over_ssh
117 118 119 120 121 122 123 124

#REMOTE_USER=root
#REMOTE_IP=192.168.0.7
#DESTDIR=/home/eyesis/git/elphel393/poky/build/tmp/work/cortexa9-neon-poky-linux-gnueabi/web-393/1_0-4/image
#    echo "REMOTE_USER=${REMOTE_USER}"
#    echo "REMOTE_IP=${REMOTE_IP}"
#    echo "DESTDIR=${D}"
#scp -pr image/* root@192.168.0.9:/