blob: c90c6b1f763bd03804e959b27d2df768e9bc6a2b [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
Patrick Williamsf1e5d692016-03-30 15:21:19 -050022from devtool import exec_fakeroot, setup_tinfoil, check_workspace_recipe, DevtoolError
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023
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
Patrick Williamsf1e5d692016-03-30 15:21:19 -050031 check_workspace_recipe(workspace, args.recipename, checksrc=False)
32
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 try:
34 host, destdir = args.target.split(':')
35 except ValueError:
36 destdir = '/'
37 else:
38 args.target = host
39
40 deploy_dir = os.path.join(basepath, 'target_deploy', args.target)
41 deploy_file = os.path.join(deploy_dir, args.recipename + '.list')
42
Patrick Williamsf1e5d692016-03-30 15:21:19 -050043 tinfoil = setup_tinfoil(basepath=basepath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 try:
45 rd = oe.recipeutils.parse_recipe_simple(tinfoil.cooker, args.recipename, tinfoil.config_data)
46 except Exception as e:
47 raise DevtoolError('Exception parsing recipe %s: %s' %
48 (args.recipename, e))
49 recipe_outdir = rd.getVar('D', True)
50 if not os.path.exists(recipe_outdir) or not os.listdir(recipe_outdir):
51 raise DevtoolError('No files to deploy - have you built the %s '
52 'recipe? If so, the install step has not installed '
53 'any files.' % args.recipename)
54
55 if args.dry_run:
56 print('Files to be deployed for %s on target %s:' % (args.recipename, args.target))
57 for root, _, files in os.walk(recipe_outdir):
58 for fn in files:
59 print(' %s' % os.path.join(destdir, os.path.relpath(root, recipe_outdir), fn))
60 return 0
61
62 if os.path.exists(deploy_file):
63 if undeploy(args, config, basepath, workspace):
64 # Error already shown
65 return 1
66
67 extraoptions = ''
68 if args.no_host_check:
69 extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
70 if args.show_status:
71 tarextractopts = 'xv'
72 else:
73 tarextractopts = 'x'
74 extraoptions += ' -q'
75 # We cannot use scp here, because it doesn't preserve symlinks
76 ret = exec_fakeroot(rd, 'tar cf - . | ssh %s %s \'tar %s -C %s -f -\'' % (extraoptions, args.target, tarextractopts, destdir), cwd=recipe_outdir, shell=True)
77 if ret != 0:
78 raise DevtoolError('Deploy failed - rerun with -s to get a complete '
79 'error message')
80
81 logger.info('Successfully deployed %s' % recipe_outdir)
82
83 if not os.path.exists(deploy_dir):
84 os.makedirs(deploy_dir)
85
86 files_list = []
87 for root, _, files in os.walk(recipe_outdir):
88 for filename in files:
89 filename = os.path.relpath(os.path.join(root, filename), recipe_outdir)
90 files_list.append(os.path.join(destdir, filename))
91
92 with open(deploy_file, 'w') as fobj:
93 fobj.write('\n'.join(files_list))
94
95 return 0
96
97def undeploy(args, config, basepath, workspace):
98 """Entry point for the devtool 'undeploy' subcommand"""
99 deploy_file = os.path.join(basepath, 'target_deploy', args.target, args.recipename + '.list')
100 if not os.path.exists(deploy_file):
101 raise DevtoolError('%s has not been deployed' % args.recipename)
102
103 if args.dry_run:
104 print('Previously deployed files to be un-deployed for %s on target %s:' % (args.recipename, args.target))
105 with open(deploy_file, 'r') as f:
106 for line in f:
107 print(' %s' % line.rstrip())
108 return 0
109
110 extraoptions = ''
111 if args.no_host_check:
112 extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
113 if not args.show_status:
114 extraoptions += ' -q'
115
116 ret = subprocess.call("scp %s %s %s:/tmp" % (extraoptions, deploy_file, args.target), shell=True)
117 if ret != 0:
118 raise DevtoolError('Failed to copy file list to %s - rerun with -s to '
119 'get a complete error message' % args.target)
120
121 ret = subprocess.call("ssh %s %s 'xargs -n1 rm -f </tmp/%s'" % (extraoptions, args.target, os.path.basename(deploy_file)), shell=True)
122 if ret == 0:
123 logger.info('Successfully undeployed %s' % args.recipename)
124 os.remove(deploy_file)
125 else:
126 raise DevtoolError('Undeploy failed - rerun with -s to get a complete '
127 'error message')
128
129 return ret
130
131
132def register_commands(subparsers, context):
133 """Register devtool subcommands from the deploy plugin"""
134 parser_deploy = subparsers.add_parser('deploy-target', help='Deploy recipe output files to live target machine')
135 parser_deploy.add_argument('recipename', help='Recipe to deploy')
136 parser_deploy.add_argument('target', help='Live target machine running an ssh server: user@hostname[:destdir]')
137 parser_deploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true')
138 parser_deploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true')
139 parser_deploy.add_argument('-n', '--dry-run', help='List files to be deployed only', action='store_true')
140 parser_deploy.set_defaults(func=deploy)
141
142 parser_undeploy = subparsers.add_parser('undeploy-target', help='Undeploy recipe output files in live target machine')
143 parser_undeploy.add_argument('recipename', help='Recipe to undeploy')
144 parser_undeploy.add_argument('target', help='Live target machine running an ssh server: user@hostname')
145 parser_undeploy.add_argument('-c', '--no-host-check', help='Disable ssh host key checking', action='store_true')
146 parser_undeploy.add_argument('-s', '--show-status', help='Show progress/status output', action='store_true')
147 parser_undeploy.add_argument('-n', '--dry-run', help='List files to be undeployed only', action='store_true')
148 parser_undeploy.set_defaults(func=undeploy)