2014-12-19 11:41:56 +00:00
|
|
|
# Development tool - deploy/undeploy command plugin
|
|
|
|
#
|
2015-03-08 12:25:56 +00:00
|
|
|
# Copyright (C) 2014-2015 Intel Corporation
|
2014-12-19 11:41:56 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2015-05-11 13:17:01 +00:00
|
|
|
"""Devtool plugin containing the deploy subcommands"""
|
2014-12-19 11:41:56 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import logging
|
2015-09-22 16:21:24 +00:00
|
|
|
from devtool import exec_fakeroot, setup_tinfoil, check_workspace_recipe, DevtoolError
|
2014-12-19 11:41:56 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger('devtool')
|
|
|
|
|
|
|
|
def deploy(args, config, basepath, workspace):
|
2015-05-11 13:17:01 +00:00
|
|
|
"""Entry point for the devtool 'deploy' subcommand"""
|
2014-12-19 11:41:56 +00:00
|
|
|
import re
|
2015-05-14 10:47:35 +00:00
|
|
|
import oe.recipeutils
|
2014-12-19 11:41:56 +00:00
|
|
|
|
2015-09-22 16:21:24 +00:00
|
|
|
check_workspace_recipe(workspace, args.recipename, checksrc=False)
|
|
|
|
|
2014-12-19 11:41:56 +00:00
|
|
|
try:
|
|
|
|
host, destdir = args.target.split(':')
|
|
|
|
except ValueError:
|
|
|
|
destdir = '/'
|
|
|
|
else:
|
|
|
|
args.target = host
|
|
|
|
|
|
|
|
deploy_dir = os.path.join(basepath, 'target_deploy', args.target)
|
|
|
|
deploy_file = os.path.join(deploy_dir, args.recipename + '.list')
|
|
|
|
|
2015-09-23 10:05:23 +00:00
|
|
|
tinfoil = setup_tinfoil(basepath=basepath)
|
2015-05-14 10:47:35 +00:00
|
|
|
try:
|
|
|
|
rd = oe.recipeutils.parse_recipe_simple(tinfoil.cooker, args.recipename, tinfoil.config_data)
|
|
|
|
except Exception as e:
|
2015-05-27 14:59:09 +00:00
|
|
|
raise DevtoolError('Exception parsing recipe %s: %s' %
|
|
|
|
(args.recipename, e))
|
2015-05-14 10:47:35 +00:00
|
|
|
recipe_outdir = rd.getVar('D', True)
|
2015-03-08 12:51:36 +00:00
|
|
|
if not os.path.exists(recipe_outdir) or not os.listdir(recipe_outdir):
|
2015-05-27 14:59:09 +00:00
|
|
|
raise DevtoolError('No files to deploy - have you built the %s '
|
|
|
|
'recipe? If so, the install step has not installed '
|
|
|
|
'any files.' % args.recipename)
|
2015-03-08 12:51:36 +00:00
|
|
|
|
2015-03-10 14:42:36 +00:00
|
|
|
if args.dry_run:
|
|
|
|
print('Files to be deployed for %s on target %s:' % (args.recipename, args.target))
|
2015-05-11 13:17:03 +00:00
|
|
|
for root, _, files in os.walk(recipe_outdir):
|
2015-03-10 14:42:36 +00:00
|
|
|
for fn in files:
|
|
|
|
print(' %s' % os.path.join(destdir, os.path.relpath(root, recipe_outdir), fn))
|
|
|
|
return 0
|
|
|
|
|
2014-12-19 11:41:56 +00:00
|
|
|
if os.path.exists(deploy_file):
|
2015-03-08 12:34:01 +00:00
|
|
|
if undeploy(args, config, basepath, workspace):
|
|
|
|
# Error already shown
|
2015-05-27 14:59:09 +00:00
|
|
|
return 1
|
2014-12-19 11:41:56 +00:00
|
|
|
|
2015-03-08 12:25:56 +00:00
|
|
|
extraoptions = ''
|
|
|
|
if args.no_host_check:
|
|
|
|
extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
|
2015-06-16 16:16:51 +00:00
|
|
|
if args.show_status:
|
|
|
|
tarextractopts = 'xv'
|
|
|
|
else:
|
|
|
|
tarextractopts = 'x'
|
2015-03-08 13:03:33 +00:00
|
|
|
extraoptions += ' -q'
|
2015-06-16 16:16:51 +00:00
|
|
|
# We cannot use scp here, because it doesn't preserve symlinks
|
|
|
|
ret = exec_fakeroot(rd, 'tar cf - . | ssh %s %s \'tar %s -C %s -f -\'' % (extraoptions, args.target, tarextractopts, destdir), cwd=recipe_outdir, shell=True)
|
2014-12-19 11:41:56 +00:00
|
|
|
if ret != 0:
|
2015-05-27 14:59:09 +00:00
|
|
|
raise DevtoolError('Deploy failed - rerun with -s to get a complete '
|
|
|
|
'error message')
|
2014-12-19 11:41:56 +00:00
|
|
|
|
|
|
|
logger.info('Successfully deployed %s' % recipe_outdir)
|
|
|
|
|
|
|
|
if not os.path.exists(deploy_dir):
|
|
|
|
os.makedirs(deploy_dir)
|
|
|
|
|
|
|
|
files_list = []
|
|
|
|
for root, _, files in os.walk(recipe_outdir):
|
|
|
|
for filename in files:
|
|
|
|
filename = os.path.relpath(os.path.join(root, filename), recipe_outdir)
|
|
|
|
files_list.append(os.path.join(destdir, filename))
|
|
|
|
|
|
|
|
with open(deploy_file, 'w') as fobj:
|
|
|
|
fobj.write('\n'.join(files_list))
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def undeploy(args, config, basepath, workspace):
|
2015-05-11 13:17:01 +00:00
|
|
|
"""Entry point for the devtool 'undeploy' subcommand"""
|
2014-12-19 11:41:56 +00:00
|
|
|
deploy_file = os.path.join(basepath, 'target_deploy', args.target, args.recipename + '.list')
|
|
|
|
if not os.path.exists(deploy_file):
|
2015-05-27 14:59:09 +00:00
|
|
|
raise DevtoolError('%s has not been deployed' % args.recipename)
|
2014-12-19 11:41:56 +00:00
|
|
|
|
2015-03-10 14:42:36 +00:00
|
|
|
if args.dry_run:
|
|
|
|
print('Previously deployed files to be un-deployed for %s on target %s:' % (args.recipename, args.target))
|
|
|
|
with open(deploy_file, 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
print(' %s' % line.rstrip())
|
|
|
|
return 0
|
|
|
|
|
2015-03-08 12:25:56 +00:00
|
|
|
extraoptions = ''
|
|
|
|
if args.no_host_check:
|
|
|
|
extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
|
2015-03-08 13:03:33 +00:00
|
|
|
if not args.show_status:
|
|
|
|
extraoptions += ' -q'
|
2015-03-08 12:25:56 +00:00
|
|
|
|
2015-03-08 13:03:33 +00:00
|
|
|
ret = subprocess.call("scp %s %s %s:/tmp" % (extraoptions, deploy_file, args.target), shell=True)
|
2014-12-19 11:41:56 +00:00
|
|
|
if ret != 0:
|
2015-05-27 14:59:09 +00:00
|
|
|
raise DevtoolError('Failed to copy file list to %s - rerun with -s to '
|
|
|
|
'get a complete error message' % args.target)
|
2014-12-19 11:41:56 +00:00
|
|
|
|
2015-03-08 12:25:56 +00:00
|
|
|
ret = subprocess.call("ssh %s %s 'xargs -n1 rm -f </tmp/%s'" % (extraoptions, args.target, os.path.basename(deploy_file)), shell=True)
|
2014-12-19 11:41:56 +00:00
|
|
|
if ret == 0:
|
|
|
|
logger.info('Successfully undeployed %s' % args.recipename)
|
|
|
|
os.remove(deploy_file)
|
2015-03-08 13:03:33 +00:00
|
|
|
else:
|
2015-05-27 14:59:09 +00:00
|
|
|
raise DevtoolError('Undeploy failed - rerun with -s to get a complete '
|
|
|
|
'error message')
|
2014-12-19 11:41:56 +00:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def register_commands(subparsers, context):
|
2015-05-11 13:17:01 +00:00
|
|
|
"""Register devtool subcommands from the deploy plugin"""
|
devtool: categorise and order subcommands in help output
The listing of subcommands in the --help output for devtool was starting
to get difficult to follow, with commands appearing in no particular
order (due to some being in separate modules and the order of those
modules being parsed). Logically grouping the subcommands as well as
being able to exercise some control over the order of the subcommands
and groups would help, if we do so without losing the dynamic nature of
the list (i.e. that it comes from the plugins). Argparse provides no
built-in way to handle this and really, really makes it a pain to add,
but with some subclassing and hacking it's now possible, and can be
extended by any plugin as desired.
To put a subcommand into a group, all you need to do is specify a group=
parameter in the call to subparsers.add_parser(). you can also specify
an order= parameter to make the subcommand sort higher or lower in the
list (higher order numbers appear first, so use negative numbers to
force items to the end if that's what you want). To add a new group, use
subparsers.add_subparser_group(), supplying the name, description and
optionally an order number for the group itself (again, higher numbers
appear first).
(From OE-Core rev: e1b9d31e6ea3c254ecfe940fe795af44761e0e69)
Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2016-02-19 09:38:53 +00:00
|
|
|
parser_deploy = subparsers.add_parser('deploy-target',
|
|
|
|
help='Deploy recipe output files to live target machine',
|
|
|
|
group='testbuild')
|
2014-12-19 11:41:56 +00:00
|
|
|
parser_deploy.add_argument('recipename', help='Recipe to deploy')
|
|
|
|
parser_deploy.add_argument('target', help='Live target machine running an ssh server: user@hostname[:destdir]')
|
2015-03-08 12:25:56 +00:00
|
|
|
parser_deploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true')
|
2015-03-08 13:03:33 +00:00
|
|
|
parser_deploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true')
|
2015-03-10 14:42:36 +00:00
|
|
|
parser_deploy.add_argument('-n', '--dry-run', help='List files to be deployed only', action='store_true')
|
2014-12-19 11:41:56 +00:00
|
|
|
parser_deploy.set_defaults(func=deploy)
|
|
|
|
|
devtool: categorise and order subcommands in help output
The listing of subcommands in the --help output for devtool was starting
to get difficult to follow, with commands appearing in no particular
order (due to some being in separate modules and the order of those
modules being parsed). Logically grouping the subcommands as well as
being able to exercise some control over the order of the subcommands
and groups would help, if we do so without losing the dynamic nature of
the list (i.e. that it comes from the plugins). Argparse provides no
built-in way to handle this and really, really makes it a pain to add,
but with some subclassing and hacking it's now possible, and can be
extended by any plugin as desired.
To put a subcommand into a group, all you need to do is specify a group=
parameter in the call to subparsers.add_parser(). you can also specify
an order= parameter to make the subcommand sort higher or lower in the
list (higher order numbers appear first, so use negative numbers to
force items to the end if that's what you want). To add a new group, use
subparsers.add_subparser_group(), supplying the name, description and
optionally an order number for the group itself (again, higher numbers
appear first).
(From OE-Core rev: e1b9d31e6ea3c254ecfe940fe795af44761e0e69)
Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2016-02-19 09:38:53 +00:00
|
|
|
parser_undeploy = subparsers.add_parser('undeploy-target',
|
|
|
|
help='Undeploy recipe output files in live target machine',
|
|
|
|
group='testbuild')
|
2014-12-19 11:41:56 +00:00
|
|
|
parser_undeploy.add_argument('recipename', help='Recipe to undeploy')
|
|
|
|
parser_undeploy.add_argument('target', help='Live target machine running an ssh server: user@hostname')
|
2015-03-08 12:25:56 +00:00
|
|
|
parser_undeploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true')
|
2015-03-08 13:03:33 +00:00
|
|
|
parser_undeploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true')
|
2015-03-10 14:42:36 +00:00
|
|
|
parser_undeploy.add_argument('-n', '--dry-run', help='List files to be undeployed only', action='store_true')
|
2014-12-19 11:41:56 +00:00
|
|
|
parser_undeploy.set_defaults(func=undeploy)
|