elphel-scp.bbclass 4.92 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 19
#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}
#}

python do_target_scp () {
20

21
    import subprocess
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
22

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
23
    WORKDIR       = d.getVar('WORKDIR'      , True)
24
    IDENTITY_FILE = d.getVar('IDENTITY_FILE', True)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
25 26 27
    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
28

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
29 30
    # hardcodign for now
    MMC2    = "/dev/mmcblk0p2"
31
    MMC2MNT = "/mnt/mmc2"
32

33
    cmd = "ping "+REMOTE_IP+" -c 1"
34

35 36 37 38 39 40 41 42 43 44 45 46
    #run_shell_command(cmd)
    ret = subprocess.run("which ls",stderr=subprocess.STDOUT,shell=True)
    print(ret)
    ret = subprocess.run("which scp",stderr=subprocess.STDOUT,shell=True)
    print(ret)
    ret = subprocess.run("which cd",stderr=subprocess.STDOUT,shell=True)
    print(ret)
    ret = subprocess.run("which ping",stderr=subprocess.STDOUT,shell=True)
    print(ret)
    ret = subprocess.run("ls "+WORKDIR,stderr=subprocess.STDOUT,shell=True)
    print(ret)

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
47
    print("cmd: "+cmd)
48
    try:
49
        ret = subprocess.run(cmd,stderr=subprocess.STDOUT,shell=True)
50 51 52
    except subprocess.CalledProcessError as e:
        print("MOST EPIC FAIL 1: "+str(e.output))
        print("MOST EPIC FAIL 2: "+str(e.cmd))
53
        raise Exception("No route to target "+REMOTE_IP)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
54

55
    nandboot = command_over_ssh(d,"'if [ -d /tmp/rootfs.ro ]; then echo 1; else echo 0;fi'")
56

57
    if COPY_TO_NAND=='1':
58

59
        print("Copy to NAND")
60

61 62 63
        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
64
            print("cmd: "+cmd)
65
            subprocess.run(cmd,stderr=subprocess.STDOUT,shell=True)
66 67 68 69 70 71 72
            #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:
73

74
        if nandboot=="1":
75

76
            print("Copy to MMC while booted from NAND")
77

78 79 80 81
            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'")
82

83 84 85 86 87 88 89 90
                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)
91

92 93 94
                #copy archive
                print("Copy archive")
                cmd = "scp -i "+IDENTITY_FILE+" -p "+WORKDIR+"/image.tar.gz "+REMOTE_USER+"@"+REMOTE_IP+":/"
95 96
                subprocess.run(cmd,stderr=subprocess.STDOUT,shell=True)

97 98
                #unpack archive to mountpoint
                print("Unpack archive then delete")
99
                command_over_ssh(d,"'EXTRACT_UNSAFE_SYMLINKS=1 tar -C "+mountpoint+" -xzpf /image.tar.gz; rm -f /image.tar.gz; sync'")
100

101 102
            else:
                raise Exception("\033[1;37mMMC rootfs partition "+MMC2+" not found.\033[0m")
103

104
        else:
105

106
            print("Copy to MMC while booted from MMC")
107

108 109
            #copy archive
            cmd = "scp -i "+IDENTITY_FILE+" -p "+WORKDIR+"/image.tar.gz "+REMOTE_USER+"@"+REMOTE_IP+":/"
110 111
            subprocess.run(cmd,stderr=subprocess.STDOUT,shell=True)

112
            #unpack archive to /
113
            command_over_ssh(d,"'EXTRACT_UNSAFE_SYMLINKS=1 tar -C / -xzpf /image.tar.gz; rm -f /image.tar.gz; sync'")
114

115
}
116

117 118 119 120
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)"

121
EXPORT_FUNCTIONS do_target_scp
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
122
#EXPORT_FUNCTIONS command_over_ssh
123 124 125 126 127 128 129

#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}"
130
#scp -pr image/* root@192.168.0.9:/