~smoser/curtin/trunk.use-smtar

« back to all changes in this revision

Viewing changes to curtin/util.py

  • Committer: Scott Moser
  • Date: 2015-10-02 19:59:11 UTC
  • mfrom: (269.1.11 trunk.dist-upgrade)
  • Revision ID: smoser@ubuntu.com-20151002195911-om9vrzt0uqcnak4b
add 'system-upgrade' command

The intent of this is to be runnable easily as 'curtin system-upgrade'
We can expose this from a config option and run it in curthooks also.

One such reason for this is shown in bug 1481992.
upgrading a system is often times unfortunately more complex than
'apt-get dist-upgrade'.

And the goal would be to make this run the appropriate system upgrade
command for the os in target (yum update for example).

Also enabled here is running system-upgrade in the target via
config.  Example:

  system_upgrade:
     enabled: True

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import argparse
19
19
import errno
 
20
import glob
20
21
import os
21
22
import shutil
22
23
import subprocess
443
444
        return False
444
445
 
445
446
 
 
447
def find_newer(src, files):
 
448
    mtime = os.stat(src).st_mtime
 
449
    return [f for f in files if
 
450
            os.path.exists(f) and os.stat(f).st_mtime > mtime]
 
451
 
 
452
 
446
453
def apt_update(target=None, env=None, force=False, comment=None,
447
454
               retries=None):
448
455
 
465
472
        comment = comment[:-1]
466
473
 
467
474
    marker = os.path.join(target, marker)
468
 
    if not force and os.path.exists(marker):
469
 
        return
 
475
    # if marker exists, check if there are files that would make it obsolete
 
476
    if os.path.exists(marker) and not force:
 
477
        listfiles = [os.path.join(target, "/etc/apt/sources.list")]
 
478
        listfiles += glob.glob(
 
479
            os.path.join(target, "/etc/apt/sources.list.d/*.list"))
 
480
        if len(find_newer(marker, listfiles)) == 0:
 
481
            return
470
482
 
 
483
    # we're not using 'run_apt_command' so we can use 'retries' to subp
471
484
    apt_update = ['apt-get', 'update', '--quiet']
472
 
    with RunInChroot(target) as inchroot:
 
485
    with RunInChroot(target, allow_daemons=True) as inchroot:
473
486
        inchroot(apt_update, env=env, retries=retries)
474
487
 
475
488
    with open(marker, "w") as fp:
476
489
        fp.write(comment + "\n")
477
490
 
478
491
 
479
 
def install_packages(pkglist, aptopts=None, target=None, env=None,
480
 
                     allow_daemons=False):
481
 
    apt_inst_cmd = ['apt-get', 'install', '--quiet', '--assume-yes',
482
 
                    '--option=Dpkg::options::=--force-unsafe-io']
 
492
def run_apt_command(mode, args=None, aptopts=None, env=None, target=None,
 
493
                    execute=True, allow_daemons=False):
 
494
    opts = ['--quiet', '--assume-yes',
 
495
            '--option=Dpkg::options::=--force-unsafe-io',
 
496
            '--option=Dpkg::Options::=--force-confold']
 
497
 
 
498
    if args is None:
 
499
        args = []
483
500
 
484
501
    if aptopts is None:
485
502
        aptopts = []
486
 
    apt_inst_cmd.extend(aptopts)
 
503
 
 
504
    if env is None:
 
505
        env = os.environ.copy()
 
506
        env['DEBIAN_FRONTEND'] = 'noninteractive'
487
507
 
488
508
    if which('eatmydata', target=target):
489
509
        emd = ['eatmydata']
490
510
    else:
491
511
        emd = []
492
512
 
 
513
    cmd = emd + ['apt-get'] + opts + aptopts + [mode] + args
 
514
    if not execute:
 
515
        return env, cmd
 
516
 
 
517
    apt_update(target, env=env, comment=' '.join(cmd))
 
518
    ric = RunInChroot(target, allow_daemons=allow_daemons)
 
519
    with ric as inchroot:
 
520
        return inchroot(cmd, env=env)
 
521
 
 
522
 
 
523
def system_upgrade(aptopts=None, target=None, env=None, allow_daemons=False):
 
524
    LOG.debug("Upgrading system in %s", target)
 
525
    for mode in ('dist-upgrade', 'autoremove'):
 
526
        ret = run_apt_command(
 
527
            mode, aptopts=aptopts, target=target,
 
528
            env=env, allow_daemons=allow_daemons)
 
529
    return ret
 
530
 
 
531
 
 
532
def install_packages(pkglist, aptopts=None, target=None, env=None,
 
533
                     allow_daemons=False):
493
534
    if isinstance(pkglist, str):
494
535
        pkglist = [pkglist]
495
 
 
496
 
    if env is None:
497
 
        env = os.environ.copy()
498
 
        env['DEBIAN_FRONTEND'] = 'noninteractive'
499
 
 
500
 
    apt_update(target, comment=' '.join(pkglist))
501
 
    ric = RunInChroot(target, allow_daemons=allow_daemons)
502
 
    with ric as inchroot:
503
 
        return inchroot(
504
 
            emd + apt_inst_cmd + list(pkglist), env=env)
 
536
    return run_apt_command(
 
537
        'install', args=pkglist,
 
538
        aptopts=aptopts, target=target, env=env, allow_daemons=allow_daemons)
505
539
 
506
540
 
507
541
def is_uefi_bootable():