~hopem/charms/trusty/neutron-openvswitch/lp1500386

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/utils.py

  • Committer: james.page at ubuntu
  • Date: 2015-06-10 14:01:59 UTC
  • mfrom: (62.1.32 neutron-openvswitch)
  • Revision ID: james.page@ubuntu.com-20150610140159-83k3sr2pq07gs4a3
Adds support for:

* Use a Python virtualenv when deploying from source
* Default to shallow clone for git (depth=1)
* Pass http_proxy to pip installs if specified

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
    get_ipv6_addr
54
54
)
55
55
 
 
56
from charmhelpers.contrib.python.packages import (
 
57
    pip_create_virtualenv,
 
58
    pip_install,
 
59
)
 
60
 
56
61
from charmhelpers.core.host import lsb_release, mounts, umount
57
62
from charmhelpers.fetch import apt_install, apt_cache, install_remote
58
 
from charmhelpers.contrib.python.packages import pip_install
59
63
from charmhelpers.contrib.storage.linux.utils import is_block_device, zap_disk
60
64
from charmhelpers.contrib.storage.linux.loopback import ensure_loopback_device
61
65
 
497
501
requirements_dir = None
498
502
 
499
503
 
500
 
def git_clone_and_install(projects_yaml, core_project):
 
504
def _git_yaml_load(projects_yaml):
 
505
    """
 
506
    Load the specified yaml into a dictionary.
 
507
    """
 
508
    if not projects_yaml:
 
509
        return None
 
510
 
 
511
    return yaml.load(projects_yaml)
 
512
 
 
513
 
 
514
def git_clone_and_install(projects_yaml, core_project, depth=1):
501
515
    """
502
516
    Clone/install all specified OpenStack repositories.
503
517
 
510
524
             repository: 'git://git.openstack.org/openstack/requirements.git',
511
525
             branch: 'stable/icehouse'}
512
526
        directory: /mnt/openstack-git
513
 
        http_proxy: http://squid.internal:3128
514
 
        https_proxy: https://squid.internal:3128
 
527
        http_proxy: squid-proxy-url
 
528
        https_proxy: squid-proxy-url
515
529
 
516
530
        The directory, http_proxy, and https_proxy keys are optional.
517
531
    """
518
532
    global requirements_dir
519
533
    parent_dir = '/mnt/openstack-git'
520
 
 
521
 
    if not projects_yaml:
522
 
        return
523
 
 
524
 
    projects = yaml.load(projects_yaml)
 
534
    http_proxy = None
 
535
 
 
536
    projects = _git_yaml_load(projects_yaml)
525
537
    _git_validate_projects_yaml(projects, core_project)
526
538
 
527
539
    old_environ = dict(os.environ)
528
540
 
529
541
    if 'http_proxy' in projects.keys():
 
542
        http_proxy = projects['http_proxy']
530
543
        os.environ['http_proxy'] = projects['http_proxy']
531
544
    if 'https_proxy' in projects.keys():
532
545
        os.environ['https_proxy'] = projects['https_proxy']
534
547
    if 'directory' in projects.keys():
535
548
        parent_dir = projects['directory']
536
549
 
 
550
    pip_create_virtualenv(os.path.join(parent_dir, 'venv'))
 
551
 
537
552
    for p in projects['repositories']:
538
553
        repo = p['repository']
539
554
        branch = p['branch']
540
555
        if p['name'] == 'requirements':
541
 
            repo_dir = _git_clone_and_install_single(repo, branch, parent_dir,
 
556
            repo_dir = _git_clone_and_install_single(repo, branch, depth,
 
557
                                                     parent_dir, http_proxy,
542
558
                                                     update_requirements=False)
543
559
            requirements_dir = repo_dir
544
560
        else:
545
 
            repo_dir = _git_clone_and_install_single(repo, branch, parent_dir,
 
561
            repo_dir = _git_clone_and_install_single(repo, branch, depth,
 
562
                                                     parent_dir, http_proxy,
546
563
                                                     update_requirements=True)
547
564
 
548
565
    os.environ = old_environ
574
591
        error_out('openstack-origin-git key \'{}\' is missing'.format(key))
575
592
 
576
593
 
577
 
def _git_clone_and_install_single(repo, branch, parent_dir, update_requirements):
 
594
def _git_clone_and_install_single(repo, branch, depth, parent_dir, http_proxy,
 
595
                                  update_requirements):
578
596
    """
579
597
    Clone and install a single git repository.
580
598
    """
587
605
 
588
606
    if not os.path.exists(dest_dir):
589
607
        juju_log('Cloning git repo: {}, branch: {}'.format(repo, branch))
590
 
        repo_dir = install_remote(repo, dest=parent_dir, branch=branch)
 
608
        repo_dir = install_remote(repo, dest=parent_dir, branch=branch,
 
609
                                  depth=depth)
591
610
    else:
592
611
        repo_dir = dest_dir
593
612
 
598
617
        _git_update_requirements(repo_dir, requirements_dir)
599
618
 
600
619
    juju_log('Installing git repo from dir: {}'.format(repo_dir))
601
 
    pip_install(repo_dir)
 
620
    if http_proxy:
 
621
        pip_install(repo_dir, proxy=http_proxy,
 
622
                    venv=os.path.join(parent_dir, 'venv'))
 
623
    else:
 
624
        pip_install(repo_dir,
 
625
                    venv=os.path.join(parent_dir, 'venv'))
602
626
 
603
627
    return repo_dir
604
628
 
621
645
    os.chdir(orig_dir)
622
646
 
623
647
 
 
648
def git_pip_venv_dir(projects_yaml):
 
649
    """
 
650
    Return the pip virtualenv path.
 
651
    """
 
652
    parent_dir = '/mnt/openstack-git'
 
653
 
 
654
    projects = _git_yaml_load(projects_yaml)
 
655
 
 
656
    if 'directory' in projects.keys():
 
657
        parent_dir = projects['directory']
 
658
 
 
659
    return os.path.join(parent_dir, 'venv')
 
660
 
 
661
 
624
662
def git_src_dir(projects_yaml, project):
625
663
    """
626
664
    Return the directory where the specified project's source is located.
627
665
    """
628
666
    parent_dir = '/mnt/openstack-git'
629
667
 
630
 
    if not projects_yaml:
631
 
        return
632
 
 
633
 
    projects = yaml.load(projects_yaml)
 
668
    projects = _git_yaml_load(projects_yaml)
634
669
 
635
670
    if 'directory' in projects.keys():
636
671
        parent_dir = projects['directory']
640
675
            return os.path.join(parent_dir, os.path.basename(p['repository']))
641
676
 
642
677
    return None
 
678
 
 
679
 
 
680
def git_yaml_value(projects_yaml, key):
 
681
    """
 
682
    Return the value in projects_yaml for the specified key.
 
683
    """
 
684
    projects = _git_yaml_load(projects_yaml)
 
685
 
 
686
    if key in projects.keys():
 
687
        return projects[key]
 
688
 
 
689
    return None