diff options
Diffstat (limited to 'scripts/lib/devtool')
-rw-r--r-- | scripts/lib/devtool/deploy.py | 59 |
1 files changed, 39 insertions, 20 deletions
diff --git a/scripts/lib/devtool/deploy.py b/scripts/lib/devtool/deploy.py index d2a314b236..d54f6ba2e1 100644 --- a/scripts/lib/devtool/deploy.py +++ b/scripts/lib/devtool/deploy.py | |||
@@ -21,13 +21,14 @@ import subprocess | |||
21 | import logging | 21 | import logging |
22 | import tempfile | 22 | import tempfile |
23 | import shutil | 23 | import shutil |
24 | import argparse_oe | ||
24 | from devtool import exec_fakeroot, setup_tinfoil, check_workspace_recipe, DevtoolError | 25 | from devtool import exec_fakeroot, setup_tinfoil, check_workspace_recipe, DevtoolError |
25 | 26 | ||
26 | logger = logging.getLogger('devtool') | 27 | logger = logging.getLogger('devtool') |
27 | 28 | ||
28 | deploylist_path = '/.devtool' | 29 | deploylist_path = '/.devtool' |
29 | 30 | ||
30 | def _prepare_remote_script(deploy, verbose=False): | 31 | def _prepare_remote_script(deploy, verbose=False, dryrun=False, undeployall=False): |
31 | """ | 32 | """ |
32 | Prepare a shell script for running on the target to | 33 | Prepare a shell script for running on the target to |
33 | deploy/undeploy files. We have to be careful what we put in this | 34 | deploy/undeploy files. We have to be careful what we put in this |
@@ -38,19 +39,33 @@ def _prepare_remote_script(deploy, verbose=False): | |||
38 | lines = [] | 39 | lines = [] |
39 | lines.append('#!/bin/sh') | 40 | lines.append('#!/bin/sh') |
40 | lines.append('set -e') | 41 | lines.append('set -e') |
42 | if undeployall: | ||
43 | # Yes, I know this is crude - but it does work | ||
44 | lines.append('for entry in %s/*.list; do' % deploylist_path) | ||
45 | lines.append('[ ! -f $entry ] && exit') | ||
46 | lines.append('set `basename $entry | sed "s/.list//"`') | ||
47 | if dryrun: | ||
48 | if not deploy: | ||
49 | lines.append('echo "Previously deployed files for $1:"') | ||
41 | lines.append('manifest="%s/$1.list"' % deploylist_path) | 50 | lines.append('manifest="%s/$1.list"' % deploylist_path) |
42 | lines.append('if [ -f $manifest ] ; then') | 51 | lines.append('if [ -f $manifest ] ; then') |
43 | # Read manifest in reverse and delete files / remove empty dirs | 52 | # Read manifest in reverse and delete files / remove empty dirs |
44 | lines.append(' sed \'1!G;h;$!d\' $manifest | while read file') | 53 | lines.append(' sed \'1!G;h;$!d\' $manifest | while read file') |
45 | lines.append(' do') | 54 | lines.append(' do') |
46 | lines.append(' if [ -d $file ] ; then') | 55 | if dryrun: |
47 | lines.append(' rmdir $file > /dev/null 2>&1 || true') | 56 | lines.append(' if [ ! -d $file ] ; then') |
48 | lines.append(' else') | 57 | lines.append(' echo $file') |
49 | lines.append(' rm $file') | 58 | lines.append(' fi') |
50 | lines.append(' fi') | 59 | else: |
60 | lines.append(' if [ -d $file ] ; then') | ||
61 | lines.append(' rmdir $file > /dev/null 2>&1 || true') | ||
62 | lines.append(' else') | ||
63 | lines.append(' rm $file') | ||
64 | lines.append(' fi') | ||
51 | lines.append(' done') | 65 | lines.append(' done') |
52 | lines.append(' rm $manifest') | 66 | if not dryrun: |
53 | if not deploy: | 67 | lines.append(' rm $manifest') |
68 | if not deploy and not dryrun: | ||
54 | # May as well remove all traces | 69 | # May as well remove all traces |
55 | lines.append(' rmdir `dirname $manifest` > /dev/null 2>&1 || true') | 70 | lines.append(' rmdir `dirname $manifest` > /dev/null 2>&1 || true') |
56 | lines.append('fi') | 71 | lines.append('fi') |
@@ -63,6 +78,12 @@ def _prepare_remote_script(deploy, verbose=False): | |||
63 | else: | 78 | else: |
64 | lines.append(' tar xv -C $2 -f - > $manifest') | 79 | lines.append(' tar xv -C $2 -f - > $manifest') |
65 | lines.append('sed -i "s!^./!$2!" $manifest') | 80 | lines.append('sed -i "s!^./!$2!" $manifest') |
81 | |||
82 | if undeployall: | ||
83 | if not dryrun: | ||
84 | lines.append('echo "NOTE: Successfully undeployed $1"') | ||
85 | lines.append('done') | ||
86 | |||
66 | # Delete the script itself | 87 | # Delete the script itself |
67 | lines.append('rm $0') | 88 | lines.append('rm $0') |
68 | lines.append('') | 89 | lines.append('') |
@@ -149,6 +170,11 @@ def deploy(args, config, basepath, workspace): | |||
149 | 170 | ||
150 | def undeploy(args, config, basepath, workspace): | 171 | def undeploy(args, config, basepath, workspace): |
151 | """Entry point for the devtool 'undeploy' subcommand""" | 172 | """Entry point for the devtool 'undeploy' subcommand""" |
173 | if args.all and args.recipename: | ||
174 | raise argparse_oe.ArgumentUsageError('Cannot specify -a/--all with a recipe name', 'undeploy-target') | ||
175 | elif not args.recipename and not args.all: | ||
176 | raise argparse_oe.ArgumentUsageError('If you don\'t specify a recipe, you must specify -a/--all', 'undeploy-target') | ||
177 | |||
152 | extraoptions = '' | 178 | extraoptions = '' |
153 | if args.no_host_check: | 179 | if args.no_host_check: |
154 | extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' | 180 | extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' |
@@ -157,19 +183,10 @@ def undeploy(args, config, basepath, workspace): | |||
157 | 183 | ||
158 | args.target = args.target.split(':')[0] | 184 | args.target = args.target.split(':')[0] |
159 | 185 | ||
160 | if args.dry_run: | ||
161 | listfile = os.path.join(deploylist_path, '%s.list' % args.recipename) | ||
162 | print('Previously deployed files to be un-deployed for %s on target %s:' % (args.recipename, args.target)) | ||
163 | ret = subprocess.call('ssh %s %s \'[ -f %s ] && cat %s || true\'' % (extraoptions, args.target, listfile, listfile), shell=True) | ||
164 | if ret != 0: | ||
165 | raise DevtoolError('Undeploy failed - rerun with -s to get a complete ' | ||
166 | 'error message') | ||
167 | return 0 | ||
168 | |||
169 | tmpdir = tempfile.mkdtemp(prefix='devtool') | 186 | tmpdir = tempfile.mkdtemp(prefix='devtool') |
170 | try: | 187 | try: |
171 | tmpscript = '/tmp/devtool_undeploy.sh' | 188 | tmpscript = '/tmp/devtool_undeploy.sh' |
172 | shellscript = _prepare_remote_script(deploy=False) | 189 | shellscript = _prepare_remote_script(deploy=False, dryrun=args.dry_run, undeployall=args.all) |
173 | # Write out the script to a file | 190 | # Write out the script to a file |
174 | with open(os.path.join(tmpdir, os.path.basename(tmpscript)), 'w') as f: | 191 | with open(os.path.join(tmpdir, os.path.basename(tmpscript)), 'w') as f: |
175 | f.write(shellscript) | 192 | f.write(shellscript) |
@@ -187,7 +204,8 @@ def undeploy(args, config, basepath, workspace): | |||
187 | raise DevtoolError('Undeploy failed - rerun with -s to get a complete ' | 204 | raise DevtoolError('Undeploy failed - rerun with -s to get a complete ' |
188 | 'error message') | 205 | 'error message') |
189 | 206 | ||
190 | logger.info('Successfully undeployed %s' % args.recipename) | 207 | if not args.all and not args.dry_run: |
208 | logger.info('Successfully undeployed %s' % args.recipename) | ||
191 | return 0 | 209 | return 0 |
192 | 210 | ||
193 | 211 | ||
@@ -208,9 +226,10 @@ def register_commands(subparsers, context): | |||
208 | help='Undeploy recipe output files in live target machine', | 226 | help='Undeploy recipe output files in live target machine', |
209 | description='Un-deploys recipe output files previously deployed to a live target machine by devtool deploy-target.', | 227 | description='Un-deploys recipe output files previously deployed to a live target machine by devtool deploy-target.', |
210 | group='testbuild') | 228 | group='testbuild') |
211 | parser_undeploy.add_argument('recipename', help='Recipe to undeploy') | 229 | parser_undeploy.add_argument('recipename', help='Recipe to undeploy (if not using -a/--all)', nargs='?') |
212 | parser_undeploy.add_argument('target', help='Live target machine running an ssh server: user@hostname') | 230 | parser_undeploy.add_argument('target', help='Live target machine running an ssh server: user@hostname') |
213 | parser_undeploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true') | 231 | parser_undeploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true') |
214 | parser_undeploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true') | 232 | parser_undeploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true') |
233 | parser_undeploy.add_argument('-a', '--all', help='Undeploy all recipes deployed on the target', action='store_true') | ||
215 | parser_undeploy.add_argument('-n', '--dry-run', help='List files to be undeployed only', action='store_true') | 234 | parser_undeploy.add_argument('-n', '--dry-run', help='List files to be undeployed only', action='store_true') |
216 | parser_undeploy.set_defaults(func=undeploy) | 235 | parser_undeploy.set_defaults(func=undeploy) |