1*eb8dc403SDave Cobbley# Development tool - deploy/undeploy command plugin
2*eb8dc403SDave Cobbley#
3*eb8dc403SDave Cobbley# Copyright (C) 2014-2016 Intel Corporation
4*eb8dc403SDave Cobbley#
5*eb8dc403SDave Cobbley# This program is free software; you can redistribute it and/or modify
6*eb8dc403SDave Cobbley# it under the terms of the GNU General Public License version 2 as
7*eb8dc403SDave Cobbley# published by the Free Software Foundation.
8*eb8dc403SDave Cobbley#
9*eb8dc403SDave Cobbley# This program is distributed in the hope that it will be useful,
10*eb8dc403SDave Cobbley# but WITHOUT ANY WARRANTY; without even the implied warranty of
11*eb8dc403SDave Cobbley# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*eb8dc403SDave Cobbley# GNU General Public License for more details.
13*eb8dc403SDave Cobbley#
14*eb8dc403SDave Cobbley# You should have received a copy of the GNU General Public License along
15*eb8dc403SDave Cobbley# with this program; if not, write to the Free Software Foundation, Inc.,
16*eb8dc403SDave Cobbley# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17*eb8dc403SDave Cobbley"""Devtool plugin containing the deploy subcommands"""
18*eb8dc403SDave Cobbley
19*eb8dc403SDave Cobbleyimport logging
20*eb8dc403SDave Cobbleyimport os
21*eb8dc403SDave Cobbleyimport shutil
22*eb8dc403SDave Cobbleyimport subprocess
23*eb8dc403SDave Cobbleyimport tempfile
24*eb8dc403SDave Cobbley
25*eb8dc403SDave Cobbleyimport bb.utils
26*eb8dc403SDave Cobbleyimport argparse_oe
27*eb8dc403SDave Cobbleyimport oe.types
28*eb8dc403SDave Cobbley
29*eb8dc403SDave Cobbleyfrom devtool import exec_fakeroot, setup_tinfoil, check_workspace_recipe, DevtoolError
30*eb8dc403SDave Cobbley
31*eb8dc403SDave Cobbleylogger = logging.getLogger('devtool')
32*eb8dc403SDave Cobbley
33*eb8dc403SDave Cobbleydeploylist_path = '/.devtool'
34*eb8dc403SDave Cobbley
35*eb8dc403SDave Cobbleydef _prepare_remote_script(deploy, verbose=False, dryrun=False, undeployall=False, nopreserve=False, nocheckspace=False):
36*eb8dc403SDave Cobbley    """
37*eb8dc403SDave Cobbley    Prepare a shell script for running on the target to
38*eb8dc403SDave Cobbley    deploy/undeploy files. We have to be careful what we put in this
39*eb8dc403SDave Cobbley    script - only commands that are likely to be available on the
40*eb8dc403SDave Cobbley    target are suitable (the target might be constrained, e.g. using
41*eb8dc403SDave Cobbley    busybox rather than bash with coreutils).
42*eb8dc403SDave Cobbley    """
43*eb8dc403SDave Cobbley    lines = []
44*eb8dc403SDave Cobbley    lines.append('#!/bin/sh')
45*eb8dc403SDave Cobbley    lines.append('set -e')
46*eb8dc403SDave Cobbley    if undeployall:
47*eb8dc403SDave Cobbley        # Yes, I know this is crude - but it does work
48*eb8dc403SDave Cobbley        lines.append('for entry in %s/*.list; do' % deploylist_path)
49*eb8dc403SDave Cobbley        lines.append('[ ! -f $entry ] && exit')
50*eb8dc403SDave Cobbley        lines.append('set `basename $entry | sed "s/.list//"`')
51*eb8dc403SDave Cobbley    if dryrun:
52*eb8dc403SDave Cobbley        if not deploy:
53*eb8dc403SDave Cobbley            lines.append('echo "Previously deployed files for $1:"')
54*eb8dc403SDave Cobbley    lines.append('manifest="%s/$1.list"' % deploylist_path)
55*eb8dc403SDave Cobbley    lines.append('preservedir="%s/$1.preserve"' % deploylist_path)
56*eb8dc403SDave Cobbley    lines.append('if [ -f $manifest ] ; then')
57*eb8dc403SDave Cobbley    # Read manifest in reverse and delete files / remove empty dirs
58*eb8dc403SDave Cobbley    lines.append('    sed \'1!G;h;$!d\' $manifest | while read file')
59*eb8dc403SDave Cobbley    lines.append('    do')
60*eb8dc403SDave Cobbley    if dryrun:
61*eb8dc403SDave Cobbley        lines.append('        if [ ! -d $file ] ; then')
62*eb8dc403SDave Cobbley        lines.append('            echo $file')
63*eb8dc403SDave Cobbley        lines.append('        fi')
64*eb8dc403SDave Cobbley    else:
65*eb8dc403SDave Cobbley        lines.append('        if [ -d $file ] ; then')
66*eb8dc403SDave Cobbley        # Avoid deleting a preserved directory in case it has special perms
67*eb8dc403SDave Cobbley        lines.append('            if [ ! -d $preservedir/$file ] ; then')
68*eb8dc403SDave Cobbley        lines.append('                rmdir $file > /dev/null 2>&1 || true')
69*eb8dc403SDave Cobbley        lines.append('            fi')
70*eb8dc403SDave Cobbley        lines.append('        else')
71*eb8dc403SDave Cobbley        lines.append('            rm -f $file')
72*eb8dc403SDave Cobbley        lines.append('        fi')
73*eb8dc403SDave Cobbley    lines.append('    done')
74*eb8dc403SDave Cobbley    if not dryrun:
75*eb8dc403SDave Cobbley        lines.append('    rm $manifest')
76*eb8dc403SDave Cobbley    if not deploy and not dryrun:
77*eb8dc403SDave Cobbley        # May as well remove all traces
78*eb8dc403SDave Cobbley        lines.append('    rmdir `dirname $manifest` > /dev/null 2>&1 || true')
79*eb8dc403SDave Cobbley    lines.append('fi')
80*eb8dc403SDave Cobbley
81*eb8dc403SDave Cobbley    if deploy:
82*eb8dc403SDave Cobbley        if not nocheckspace:
83*eb8dc403SDave Cobbley            # Check for available space
84*eb8dc403SDave Cobbley            # FIXME This doesn't take into account files spread across multiple
85*eb8dc403SDave Cobbley            # partitions, but doing that is non-trivial
86*eb8dc403SDave Cobbley            # Find the part of the destination path that exists
87*eb8dc403SDave Cobbley            lines.append('checkpath="$2"')
88*eb8dc403SDave Cobbley            lines.append('while [ "$checkpath" != "/" ] && [ ! -e $checkpath ]')
89*eb8dc403SDave Cobbley            lines.append('do')
90*eb8dc403SDave Cobbley            lines.append('    checkpath=`dirname "$checkpath"`')
91*eb8dc403SDave Cobbley            lines.append('done')
92*eb8dc403SDave Cobbley            lines.append(r'freespace=$(df -P $checkpath | sed -nre "s/^(\S+\s+){3}([0-9]+).*/\2/p")')
93*eb8dc403SDave Cobbley            # First line of the file is the total space
94*eb8dc403SDave Cobbley            lines.append('total=`head -n1 $3`')
95*eb8dc403SDave Cobbley            lines.append('if [ $total -gt $freespace ] ; then')
96*eb8dc403SDave Cobbley            lines.append('    echo "ERROR: insufficient space on target (available ${freespace}, needed ${total})"')
97*eb8dc403SDave Cobbley            lines.append('    exit 1')
98*eb8dc403SDave Cobbley            lines.append('fi')
99*eb8dc403SDave Cobbley        if not nopreserve:
100*eb8dc403SDave Cobbley            # Preserve any files that exist. Note that this will add to the
101*eb8dc403SDave Cobbley            # preserved list with successive deployments if the list of files
102*eb8dc403SDave Cobbley            # deployed changes, but because we've deleted any previously
103*eb8dc403SDave Cobbley            # deployed files at this point it will never preserve anything
104*eb8dc403SDave Cobbley            # that was deployed, only files that existed prior to any deploying
105*eb8dc403SDave Cobbley            # (which makes the most sense)
106*eb8dc403SDave Cobbley            lines.append('cat $3 | sed "1d" | while read file fsize')
107*eb8dc403SDave Cobbley            lines.append('do')
108*eb8dc403SDave Cobbley            lines.append('    if [ -e $file ] ; then')
109*eb8dc403SDave Cobbley            lines.append('    dest="$preservedir/$file"')
110*eb8dc403SDave Cobbley            lines.append('    mkdir -p `dirname $dest`')
111*eb8dc403SDave Cobbley            lines.append('    mv $file $dest')
112*eb8dc403SDave Cobbley            lines.append('    fi')
113*eb8dc403SDave Cobbley            lines.append('done')
114*eb8dc403SDave Cobbley            lines.append('rm $3')
115*eb8dc403SDave Cobbley        lines.append('mkdir -p `dirname $manifest`')
116*eb8dc403SDave Cobbley        lines.append('mkdir -p $2')
117*eb8dc403SDave Cobbley        if verbose:
118*eb8dc403SDave Cobbley            lines.append('    tar xv -C $2 -f - | tee $manifest')
119*eb8dc403SDave Cobbley        else:
120*eb8dc403SDave Cobbley            lines.append('    tar xv -C $2 -f - > $manifest')
121*eb8dc403SDave Cobbley        lines.append('sed -i "s!^./!$2!" $manifest')
122*eb8dc403SDave Cobbley    elif not dryrun:
123*eb8dc403SDave Cobbley        # Put any preserved files back
124*eb8dc403SDave Cobbley        lines.append('if [ -d $preservedir ] ; then')
125*eb8dc403SDave Cobbley        lines.append('    cd $preservedir')
126*eb8dc403SDave Cobbley        # find from busybox might not have -exec, so we don't use that
127*eb8dc403SDave Cobbley        lines.append('    find . -type f | while read file')
128*eb8dc403SDave Cobbley        lines.append('    do')
129*eb8dc403SDave Cobbley        lines.append('        mv $file /$file')
130*eb8dc403SDave Cobbley        lines.append('    done')
131*eb8dc403SDave Cobbley        lines.append('    cd /')
132*eb8dc403SDave Cobbley        lines.append('    rm -rf $preservedir')
133*eb8dc403SDave Cobbley        lines.append('fi')
134*eb8dc403SDave Cobbley
135*eb8dc403SDave Cobbley    if undeployall:
136*eb8dc403SDave Cobbley        if not dryrun:
137*eb8dc403SDave Cobbley            lines.append('echo "NOTE: Successfully undeployed $1"')
138*eb8dc403SDave Cobbley        lines.append('done')
139*eb8dc403SDave Cobbley
140*eb8dc403SDave Cobbley    # Delete the script itself
141*eb8dc403SDave Cobbley    lines.append('rm $0')
142*eb8dc403SDave Cobbley    lines.append('')
143*eb8dc403SDave Cobbley
144*eb8dc403SDave Cobbley    return '\n'.join(lines)
145*eb8dc403SDave Cobbley
146*eb8dc403SDave Cobbley
147*eb8dc403SDave Cobbley
148*eb8dc403SDave Cobbleydef deploy(args, config, basepath, workspace):
149*eb8dc403SDave Cobbley    """Entry point for the devtool 'deploy' subcommand"""
150*eb8dc403SDave Cobbley    import math
151*eb8dc403SDave Cobbley    import oe.recipeutils
152*eb8dc403SDave Cobbley    import oe.package
153*eb8dc403SDave Cobbley
154*eb8dc403SDave Cobbley    check_workspace_recipe(workspace, args.recipename, checksrc=False)
155*eb8dc403SDave Cobbley
156*eb8dc403SDave Cobbley    try:
157*eb8dc403SDave Cobbley        host, destdir = args.target.split(':')
158*eb8dc403SDave Cobbley    except ValueError:
159*eb8dc403SDave Cobbley        destdir = '/'
160*eb8dc403SDave Cobbley    else:
161*eb8dc403SDave Cobbley        args.target = host
162*eb8dc403SDave Cobbley    if not destdir.endswith('/'):
163*eb8dc403SDave Cobbley        destdir += '/'
164*eb8dc403SDave Cobbley
165*eb8dc403SDave Cobbley    tinfoil = setup_tinfoil(basepath=basepath)
166*eb8dc403SDave Cobbley    try:
167*eb8dc403SDave Cobbley        try:
168*eb8dc403SDave Cobbley            rd = tinfoil.parse_recipe(args.recipename)
169*eb8dc403SDave Cobbley        except Exception as e:
170*eb8dc403SDave Cobbley            raise DevtoolError('Exception parsing recipe %s: %s' %
171*eb8dc403SDave Cobbley                            (args.recipename, e))
172*eb8dc403SDave Cobbley        recipe_outdir = rd.getVar('D')
173*eb8dc403SDave Cobbley        if not os.path.exists(recipe_outdir) or not os.listdir(recipe_outdir):
174*eb8dc403SDave Cobbley            raise DevtoolError('No files to deploy - have you built the %s '
175*eb8dc403SDave Cobbley                            'recipe? If so, the install step has not installed '
176*eb8dc403SDave Cobbley                            'any files.' % args.recipename)
177*eb8dc403SDave Cobbley
178*eb8dc403SDave Cobbley        if args.strip and not args.dry_run:
179*eb8dc403SDave Cobbley            # Fakeroot copy to new destination
180*eb8dc403SDave Cobbley            srcdir = recipe_outdir
181*eb8dc403SDave Cobbley            recipe_outdir = os.path.join(rd.getVar('WORKDIR'), 'deploy-target-stripped')
182*eb8dc403SDave Cobbley            if os.path.isdir(recipe_outdir):
183*eb8dc403SDave Cobbley                bb.utils.remove(recipe_outdir, True)
184*eb8dc403SDave Cobbley            exec_fakeroot(rd, "cp -af %s %s" % (os.path.join(srcdir, '.'), recipe_outdir), shell=True)
185*eb8dc403SDave Cobbley            os.environ['PATH'] = ':'.join([os.environ['PATH'], rd.getVar('PATH') or ''])
186*eb8dc403SDave Cobbley            oe.package.strip_execs(args.recipename, recipe_outdir, rd.getVar('STRIP'), rd.getVar('libdir'),
187*eb8dc403SDave Cobbley                        rd.getVar('base_libdir'))
188*eb8dc403SDave Cobbley
189*eb8dc403SDave Cobbley        filelist = []
190*eb8dc403SDave Cobbley        ftotalsize = 0
191*eb8dc403SDave Cobbley        for root, _, files in os.walk(recipe_outdir):
192*eb8dc403SDave Cobbley            for fn in files:
193*eb8dc403SDave Cobbley                # Get the size in kiB (since we'll be comparing it to the output of du -k)
194*eb8dc403SDave Cobbley                # MUST use lstat() here not stat() or getfilesize() since we don't want to
195*eb8dc403SDave Cobbley                # dereference symlinks
196*eb8dc403SDave Cobbley                fsize = int(math.ceil(float(os.lstat(os.path.join(root, fn)).st_size)/1024))
197*eb8dc403SDave Cobbley                ftotalsize += fsize
198*eb8dc403SDave Cobbley                # The path as it would appear on the target
199*eb8dc403SDave Cobbley                fpath = os.path.join(destdir, os.path.relpath(root, recipe_outdir), fn)
200*eb8dc403SDave Cobbley                filelist.append((fpath, fsize))
201*eb8dc403SDave Cobbley
202*eb8dc403SDave Cobbley        if args.dry_run:
203*eb8dc403SDave Cobbley            print('Files to be deployed for %s on target %s:' % (args.recipename, args.target))
204*eb8dc403SDave Cobbley            for item, _ in filelist:
205*eb8dc403SDave Cobbley                print('  %s' % item)
206*eb8dc403SDave Cobbley            return 0
207*eb8dc403SDave Cobbley
208*eb8dc403SDave Cobbley        extraoptions = ''
209*eb8dc403SDave Cobbley        if args.no_host_check:
210*eb8dc403SDave Cobbley            extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
211*eb8dc403SDave Cobbley        if not args.show_status:
212*eb8dc403SDave Cobbley            extraoptions += ' -q'
213*eb8dc403SDave Cobbley
214*eb8dc403SDave Cobbley        scp_port = ''
215*eb8dc403SDave Cobbley        ssh_port = ''
216*eb8dc403SDave Cobbley        if args.port:
217*eb8dc403SDave Cobbley            scp_port = "-P %s" % args.port
218*eb8dc403SDave Cobbley            ssh_port = "-p %s" % args.port
219*eb8dc403SDave Cobbley
220*eb8dc403SDave Cobbley        # In order to delete previously deployed files and have the manifest file on
221*eb8dc403SDave Cobbley        # the target, we write out a shell script and then copy it to the target
222*eb8dc403SDave Cobbley        # so we can then run it (piping tar output to it).
223*eb8dc403SDave Cobbley        # (We cannot use scp here, because it doesn't preserve symlinks.)
224*eb8dc403SDave Cobbley        tmpdir = tempfile.mkdtemp(prefix='devtool')
225*eb8dc403SDave Cobbley        try:
226*eb8dc403SDave Cobbley            tmpscript = '/tmp/devtool_deploy.sh'
227*eb8dc403SDave Cobbley            tmpfilelist = os.path.join(os.path.dirname(tmpscript), 'devtool_deploy.list')
228*eb8dc403SDave Cobbley            shellscript = _prepare_remote_script(deploy=True,
229*eb8dc403SDave Cobbley                                                verbose=args.show_status,
230*eb8dc403SDave Cobbley                                                nopreserve=args.no_preserve,
231*eb8dc403SDave Cobbley                                                nocheckspace=args.no_check_space)
232*eb8dc403SDave Cobbley            # Write out the script to a file
233*eb8dc403SDave Cobbley            with open(os.path.join(tmpdir, os.path.basename(tmpscript)), 'w') as f:
234*eb8dc403SDave Cobbley                f.write(shellscript)
235*eb8dc403SDave Cobbley            # Write out the file list
236*eb8dc403SDave Cobbley            with open(os.path.join(tmpdir, os.path.basename(tmpfilelist)), 'w') as f:
237*eb8dc403SDave Cobbley                f.write('%d\n' % ftotalsize)
238*eb8dc403SDave Cobbley                for fpath, fsize in filelist:
239*eb8dc403SDave Cobbley                    f.write('%s %d\n' % (fpath, fsize))
240*eb8dc403SDave Cobbley            # Copy them to the target
241*eb8dc403SDave Cobbley            ret = subprocess.call("scp %s %s %s/* %s:%s" % (scp_port, extraoptions, tmpdir, args.target, os.path.dirname(tmpscript)), shell=True)
242*eb8dc403SDave Cobbley            if ret != 0:
243*eb8dc403SDave Cobbley                raise DevtoolError('Failed to copy script to %s - rerun with -s to '
244*eb8dc403SDave Cobbley                                'get a complete error message' % args.target)
245*eb8dc403SDave Cobbley        finally:
246*eb8dc403SDave Cobbley            shutil.rmtree(tmpdir)
247*eb8dc403SDave Cobbley
248*eb8dc403SDave Cobbley        # Now run the script
249*eb8dc403SDave Cobbley        ret = exec_fakeroot(rd, 'tar cf - . | ssh  %s %s %s \'sh %s %s %s %s\'' % (ssh_port, extraoptions, args.target, tmpscript, args.recipename, destdir, tmpfilelist), cwd=recipe_outdir, shell=True)
250*eb8dc403SDave Cobbley        if ret != 0:
251*eb8dc403SDave Cobbley            raise DevtoolError('Deploy failed - rerun with -s to get a complete '
252*eb8dc403SDave Cobbley                            'error message')
253*eb8dc403SDave Cobbley
254*eb8dc403SDave Cobbley        logger.info('Successfully deployed %s' % recipe_outdir)
255*eb8dc403SDave Cobbley
256*eb8dc403SDave Cobbley        files_list = []
257*eb8dc403SDave Cobbley        for root, _, files in os.walk(recipe_outdir):
258*eb8dc403SDave Cobbley            for filename in files:
259*eb8dc403SDave Cobbley                filename = os.path.relpath(os.path.join(root, filename), recipe_outdir)
260*eb8dc403SDave Cobbley                files_list.append(os.path.join(destdir, filename))
261*eb8dc403SDave Cobbley    finally:
262*eb8dc403SDave Cobbley        tinfoil.shutdown()
263*eb8dc403SDave Cobbley
264*eb8dc403SDave Cobbley    return 0
265*eb8dc403SDave Cobbley
266*eb8dc403SDave Cobbleydef undeploy(args, config, basepath, workspace):
267*eb8dc403SDave Cobbley    """Entry point for the devtool 'undeploy' subcommand"""
268*eb8dc403SDave Cobbley    if args.all and args.recipename:
269*eb8dc403SDave Cobbley        raise argparse_oe.ArgumentUsageError('Cannot specify -a/--all with a recipe name', 'undeploy-target')
270*eb8dc403SDave Cobbley    elif not args.recipename and not args.all:
271*eb8dc403SDave Cobbley        raise argparse_oe.ArgumentUsageError('If you don\'t specify a recipe, you must specify -a/--all', 'undeploy-target')
272*eb8dc403SDave Cobbley
273*eb8dc403SDave Cobbley    extraoptions = ''
274*eb8dc403SDave Cobbley    if args.no_host_check:
275*eb8dc403SDave Cobbley        extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
276*eb8dc403SDave Cobbley    if not args.show_status:
277*eb8dc403SDave Cobbley        extraoptions += ' -q'
278*eb8dc403SDave Cobbley
279*eb8dc403SDave Cobbley    scp_port = ''
280*eb8dc403SDave Cobbley    ssh_port = ''
281*eb8dc403SDave Cobbley    if args.port:
282*eb8dc403SDave Cobbley        scp_port = "-P %s" % args.port
283*eb8dc403SDave Cobbley        ssh_port = "-p %s" % args.port
284*eb8dc403SDave Cobbley
285*eb8dc403SDave Cobbley    args.target = args.target.split(':')[0]
286*eb8dc403SDave Cobbley
287*eb8dc403SDave Cobbley    tmpdir = tempfile.mkdtemp(prefix='devtool')
288*eb8dc403SDave Cobbley    try:
289*eb8dc403SDave Cobbley        tmpscript = '/tmp/devtool_undeploy.sh'
290*eb8dc403SDave Cobbley        shellscript = _prepare_remote_script(deploy=False, dryrun=args.dry_run, undeployall=args.all)
291*eb8dc403SDave Cobbley        # Write out the script to a file
292*eb8dc403SDave Cobbley        with open(os.path.join(tmpdir, os.path.basename(tmpscript)), 'w') as f:
293*eb8dc403SDave Cobbley            f.write(shellscript)
294*eb8dc403SDave Cobbley        # Copy it to the target
295*eb8dc403SDave Cobbley        ret = subprocess.call("scp %s %s %s/* %s:%s" % (scp_port, extraoptions, tmpdir, args.target, os.path.dirname(tmpscript)), shell=True)
296*eb8dc403SDave Cobbley        if ret != 0:
297*eb8dc403SDave Cobbley            raise DevtoolError('Failed to copy script to %s - rerun with -s to '
298*eb8dc403SDave Cobbley                                'get a complete error message' % args.target)
299*eb8dc403SDave Cobbley    finally:
300*eb8dc403SDave Cobbley        shutil.rmtree(tmpdir)
301*eb8dc403SDave Cobbley
302*eb8dc403SDave Cobbley    # Now run the script
303*eb8dc403SDave Cobbley    ret = subprocess.call('ssh %s %s %s \'sh %s %s\'' % (ssh_port, extraoptions, args.target, tmpscript, args.recipename), shell=True)
304*eb8dc403SDave Cobbley    if ret != 0:
305*eb8dc403SDave Cobbley        raise DevtoolError('Undeploy failed - rerun with -s to get a complete '
306*eb8dc403SDave Cobbley                           'error message')
307*eb8dc403SDave Cobbley
308*eb8dc403SDave Cobbley    if not args.all and not args.dry_run:
309*eb8dc403SDave Cobbley        logger.info('Successfully undeployed %s' % args.recipename)
310*eb8dc403SDave Cobbley    return 0
311*eb8dc403SDave Cobbley
312*eb8dc403SDave Cobbley
313*eb8dc403SDave Cobbleydef register_commands(subparsers, context):
314*eb8dc403SDave Cobbley    """Register devtool subcommands from the deploy plugin"""
315*eb8dc403SDave Cobbley
316*eb8dc403SDave Cobbley    parser_deploy = subparsers.add_parser('deploy-target',
317*eb8dc403SDave Cobbley                                          help='Deploy recipe output files to live target machine',
318*eb8dc403SDave Cobbley                                          description='Deploys a recipe\'s build output (i.e. the output of the do_install task) to a live target machine over ssh. By default, any existing files will be preserved instead of being overwritten and will be restored if you run devtool undeploy-target. Note: this only deploys the recipe itself and not any runtime dependencies, so it is assumed that those have been installed on the target beforehand.',
319*eb8dc403SDave Cobbley                                          group='testbuild')
320*eb8dc403SDave Cobbley    parser_deploy.add_argument('recipename', help='Recipe to deploy')
321*eb8dc403SDave Cobbley    parser_deploy.add_argument('target', help='Live target machine running an ssh server: user@hostname[:destdir]')
322*eb8dc403SDave Cobbley    parser_deploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true')
323*eb8dc403SDave Cobbley    parser_deploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true')
324*eb8dc403SDave Cobbley    parser_deploy.add_argument('-n', '--dry-run', help='List files to be deployed only', action='store_true')
325*eb8dc403SDave Cobbley    parser_deploy.add_argument('-p', '--no-preserve', help='Do not preserve existing files', action='store_true')
326*eb8dc403SDave Cobbley    parser_deploy.add_argument('--no-check-space', help='Do not check for available space before deploying', action='store_true')
327*eb8dc403SDave Cobbley    parser_deploy.add_argument('-P', '--port', help='Specify port to use for connection to the target')
328*eb8dc403SDave Cobbley
329*eb8dc403SDave Cobbley    strip_opts = parser_deploy.add_mutually_exclusive_group(required=False)
330*eb8dc403SDave Cobbley    strip_opts.add_argument('-S', '--strip',
331*eb8dc403SDave Cobbley                               help='Strip executables prior to deploying (default: %(default)s). '
332*eb8dc403SDave Cobbley                                    'The default value of this option can be controlled by setting the strip option in the [Deploy] section to True or False.',
333*eb8dc403SDave Cobbley                               default=oe.types.boolean(context.config.get('Deploy', 'strip', default='0')),
334*eb8dc403SDave Cobbley                               action='store_true')
335*eb8dc403SDave Cobbley    strip_opts.add_argument('--no-strip', help='Do not strip executables prior to deploy', dest='strip', action='store_false')
336*eb8dc403SDave Cobbley
337*eb8dc403SDave Cobbley    parser_deploy.set_defaults(func=deploy)
338*eb8dc403SDave Cobbley
339*eb8dc403SDave Cobbley    parser_undeploy = subparsers.add_parser('undeploy-target',
340*eb8dc403SDave Cobbley                                            help='Undeploy recipe output files in live target machine',
341*eb8dc403SDave Cobbley                                            description='Un-deploys recipe output files previously deployed to a live target machine by devtool deploy-target.',
342*eb8dc403SDave Cobbley                                            group='testbuild')
343*eb8dc403SDave Cobbley    parser_undeploy.add_argument('recipename', help='Recipe to undeploy (if not using -a/--all)', nargs='?')
344*eb8dc403SDave Cobbley    parser_undeploy.add_argument('target', help='Live target machine running an ssh server: user@hostname')
345*eb8dc403SDave Cobbley    parser_undeploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true')
346*eb8dc403SDave Cobbley    parser_undeploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true')
347*eb8dc403SDave Cobbley    parser_undeploy.add_argument('-a', '--all', help='Undeploy all recipes deployed on the target', action='store_true')
348*eb8dc403SDave Cobbley    parser_undeploy.add_argument('-n', '--dry-run', help='List files to be undeployed only', action='store_true')
349*eb8dc403SDave Cobbley    parser_undeploy.add_argument('-P', '--port', help='Specify port to use for connection to the target')
350*eb8dc403SDave Cobbley    parser_undeploy.set_defaults(func=undeploy)
351