1# Development tool - deploy/undeploy command plugin
2#
3# Copyright (C) 2014-2015 Intel Corporation
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 2 as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17"""Devtool plugin containing the deploy subcommands"""
18
19import os
20import subprocess
21import logging
22from devtool import exec_fakeroot, setup_tinfoil, DevtoolError
23
24logger = logging.getLogger('devtool')
25
26def deploy(args, config, basepath, workspace):
27    """Entry point for the devtool 'deploy' subcommand"""
28    import re
29    import oe.recipeutils
30
31    if not args.recipename in workspace:
32        raise DevtoolError("no recipe named %s in your workspace" %
33                           args.recipename)
34    try:
35        host, destdir = args.target.split(':')
36    except ValueError:
37        destdir = '/'
38    else:
39        args.target = host
40
41    deploy_dir = os.path.join(basepath, 'target_deploy', args.target)
42    deploy_file = os.path.join(deploy_dir, args.recipename + '.list')
43
44    tinfoil = setup_tinfoil()
45    try:
46        rd = oe.recipeutils.parse_recipe_simple(tinfoil.cooker, args.recipename, tinfoil.config_data)
47    except Exception as e:
48        raise DevtoolError('Exception parsing recipe %s: %s' %
49                           (args.recipename, e))
50    recipe_outdir = rd.getVar('D', True)
51    if not os.path.exists(recipe_outdir) or not os.listdir(recipe_outdir):
52        raise DevtoolError('No files to deploy - have you built the %s '
53                           'recipe? If so, the install step has not installed '
54                           'any files.' % args.recipename)
55
56    if args.dry_run:
57        print('Files to be deployed for %s on target %s:' % (args.recipename, args.target))
58        for root, _, files in os.walk(recipe_outdir):
59            for fn in files:
60                print('  %s' % os.path.join(destdir, os.path.relpath(root, recipe_outdir), fn))
61        return 0
62
63    if os.path.exists(deploy_file):
64        if undeploy(args, config, basepath, workspace):
65            # Error already shown
66            return 1
67
68    extraoptions = ''
69    if args.no_host_check:
70        extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
71    if args.show_status:
72        tarextractopts = 'xv'
73    else:
74        tarextractopts = 'x'
75        extraoptions += ' -q'
76    # We cannot use scp here, because it doesn't preserve symlinks
77    ret = exec_fakeroot(rd, 'tar cf - . | ssh %s %s \'tar %s -C %s -f -\'' % (extraoptions, args.target, tarextractopts, destdir), cwd=recipe_outdir, shell=True)
78    if ret != 0:
79        raise DevtoolError('Deploy failed - rerun with -s to get a complete '
80                           'error message')
81
82    logger.info('Successfully deployed %s' % recipe_outdir)
83
84    if not os.path.exists(deploy_dir):
85        os.makedirs(deploy_dir)
86
87    files_list = []
88    for root, _, files in os.walk(recipe_outdir):
89        for filename in files:
90            filename = os.path.relpath(os.path.join(root, filename), recipe_outdir)
91            files_list.append(os.path.join(destdir, filename))
92
93    with open(deploy_file, 'w') as fobj:
94        fobj.write('\n'.join(files_list))
95
96    return 0
97
98def undeploy(args, config, basepath, workspace):
99    """Entry point for the devtool 'undeploy' subcommand"""
100    deploy_file = os.path.join(basepath, 'target_deploy', args.target, args.recipename + '.list')
101    if not os.path.exists(deploy_file):
102        raise DevtoolError('%s has not been deployed' % args.recipename)
103
104    if args.dry_run:
105        print('Previously deployed files to be un-deployed for %s on target %s:' % (args.recipename, args.target))
106        with open(deploy_file, 'r') as f:
107            for line in f:
108                print('  %s' % line.rstrip())
109        return 0
110
111    extraoptions = ''
112    if args.no_host_check:
113        extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
114    if not args.show_status:
115        extraoptions += ' -q'
116
117    ret = subprocess.call("scp %s %s %s:/tmp" % (extraoptions, deploy_file, args.target), shell=True)
118    if ret != 0:
119        raise DevtoolError('Failed to copy file list to %s - rerun with -s to '
120                           'get a complete error message' % args.target)
121
122    ret = subprocess.call("ssh %s %s 'xargs -n1 rm -f </tmp/%s'" % (extraoptions, args.target, os.path.basename(deploy_file)), shell=True)
123    if ret == 0:
124        logger.info('Successfully undeployed %s' % args.recipename)
125        os.remove(deploy_file)
126    else:
127        raise DevtoolError('Undeploy failed - rerun with -s to get a complete '
128                           'error message')
129
130    return ret
131
132
133def register_commands(subparsers, context):
134    """Register devtool subcommands from the deploy plugin"""
135    parser_deploy = subparsers.add_parser('deploy-target', help='Deploy recipe output files to live target machine')
136    parser_deploy.add_argument('recipename', help='Recipe to deploy')
137    parser_deploy.add_argument('target', help='Live target machine running an ssh server: user@hostname[:destdir]')
138    parser_deploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true')
139    parser_deploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true')
140    parser_deploy.add_argument('-n', '--dry-run', help='List files to be deployed only', action='store_true')
141    parser_deploy.set_defaults(func=deploy)
142
143    parser_undeploy = subparsers.add_parser('undeploy-target', help='Undeploy recipe output files in live target machine')
144    parser_undeploy.add_argument('recipename', help='Recipe to undeploy')
145    parser_undeploy.add_argument('target', help='Live target machine running an ssh server: user@hostname')
146    parser_undeploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true')
147    parser_undeploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true')
148    parser_undeploy.add_argument('-n', '--dry-run', help='List files to be undeployed only', action='store_true')
149    parser_undeploy.set_defaults(func=undeploy)
150