~junaidali/charms/trusty/plumgrid-director/pg-restart

« back to all changes in this revision

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

  • Committer: bbaqar at plumgrid
  • Date: 2015-07-29 18:07:31 UTC
  • Revision ID: bbaqar@plumgrid.com-20150729180731-ioynar8x3u5pxytc
Addressed reviews by Charmers

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
 
75
79
    ('trusty', 'icehouse'),
76
80
    ('utopic', 'juno'),
77
81
    ('vivid', 'kilo'),
 
82
    ('wily', 'liberty'),
78
83
])
79
84
 
80
85
 
87
92
    ('2014.1', 'icehouse'),
88
93
    ('2014.2', 'juno'),
89
94
    ('2015.1', 'kilo'),
 
95
    ('2015.2', 'liberty'),
90
96
])
91
97
 
92
98
# The ugly duckling
109
115
    ('2.2.0', 'juno'),
110
116
    ('2.2.1', 'kilo'),
111
117
    ('2.2.2', 'kilo'),
 
118
    ('2.3.0', 'liberty'),
112
119
])
113
120
 
114
121
DEFAULT_LOOPBACK_SIZE = '5G'
317
324
            'kilo': 'trusty-updates/kilo',
318
325
            'kilo/updates': 'trusty-updates/kilo',
319
326
            'kilo/proposed': 'trusty-proposed/kilo',
 
327
            'liberty': 'trusty-updates/liberty',
 
328
            'liberty/updates': 'trusty-updates/liberty',
 
329
            'liberty/proposed': 'trusty-proposed/liberty',
320
330
        }
321
331
 
322
332
        try:
497
507
requirements_dir = None
498
508
 
499
509
 
500
 
def git_clone_and_install(projects_yaml, core_project):
 
510
def _git_yaml_load(projects_yaml):
 
511
    """
 
512
    Load the specified yaml into a dictionary.
 
513
    """
 
514
    if not projects_yaml:
 
515
        return None
 
516
 
 
517
    return yaml.load(projects_yaml)
 
518
 
 
519
 
 
520
def git_clone_and_install(projects_yaml, core_project, depth=1):
501
521
    """
502
522
    Clone/install all specified OpenStack repositories.
503
523
 
504
524
    The expected format of projects_yaml is:
 
525
 
505
526
        repositories:
506
527
          - {name: keystone,
507
528
             repository: 'git://git.openstack.org/openstack/keystone.git',
509
530
          - {name: requirements,
510
531
             repository: 'git://git.openstack.org/openstack/requirements.git',
511
532
             branch: 'stable/icehouse'}
 
533
 
512
534
        directory: /mnt/openstack-git
513
 
        http_proxy: http://squid.internal:3128
514
 
        https_proxy: https://squid.internal:3128
515
 
 
516
 
        The directory, http_proxy, and https_proxy keys are optional.
 
535
        http_proxy: squid-proxy-url
 
536
        https_proxy: squid-proxy-url
 
537
 
 
538
    The directory, http_proxy, and https_proxy keys are optional.
 
539
 
517
540
    """
518
541
    global requirements_dir
519
542
    parent_dir = '/mnt/openstack-git'
520
 
 
521
 
    if not projects_yaml:
522
 
        return
523
 
 
524
 
    projects = yaml.load(projects_yaml)
 
543
    http_proxy = None
 
544
 
 
545
    projects = _git_yaml_load(projects_yaml)
525
546
    _git_validate_projects_yaml(projects, core_project)
526
547
 
527
548
    old_environ = dict(os.environ)
528
549
 
529
550
    if 'http_proxy' in projects.keys():
 
551
        http_proxy = projects['http_proxy']
530
552
        os.environ['http_proxy'] = projects['http_proxy']
531
553
    if 'https_proxy' in projects.keys():
532
554
        os.environ['https_proxy'] = projects['https_proxy']
534
556
    if 'directory' in projects.keys():
535
557
        parent_dir = projects['directory']
536
558
 
 
559
    pip_create_virtualenv(os.path.join(parent_dir, 'venv'))
 
560
 
 
561
    # Upgrade setuptools and pip from default virtualenv versions. The default
 
562
    # versions in trusty break master OpenStack branch deployments.
 
563
    for p in ['pip', 'setuptools']:
 
564
        pip_install(p, upgrade=True, proxy=http_proxy,
 
565
                    venv=os.path.join(parent_dir, 'venv'))
 
566
 
537
567
    for p in projects['repositories']:
538
568
        repo = p['repository']
539
569
        branch = p['branch']
540
570
        if p['name'] == 'requirements':
541
 
            repo_dir = _git_clone_and_install_single(repo, branch, parent_dir,
 
571
            repo_dir = _git_clone_and_install_single(repo, branch, depth,
 
572
                                                     parent_dir, http_proxy,
542
573
                                                     update_requirements=False)
543
574
            requirements_dir = repo_dir
544
575
        else:
545
 
            repo_dir = _git_clone_and_install_single(repo, branch, parent_dir,
 
576
            repo_dir = _git_clone_and_install_single(repo, branch, depth,
 
577
                                                     parent_dir, http_proxy,
546
578
                                                     update_requirements=True)
547
579
 
548
580
    os.environ = old_environ
574
606
        error_out('openstack-origin-git key \'{}\' is missing'.format(key))
575
607
 
576
608
 
577
 
def _git_clone_and_install_single(repo, branch, parent_dir, update_requirements):
 
609
def _git_clone_and_install_single(repo, branch, depth, parent_dir, http_proxy,
 
610
                                  update_requirements):
578
611
    """
579
612
    Clone and install a single git repository.
580
613
    """
587
620
 
588
621
    if not os.path.exists(dest_dir):
589
622
        juju_log('Cloning git repo: {}, branch: {}'.format(repo, branch))
590
 
        repo_dir = install_remote(repo, dest=parent_dir, branch=branch)
 
623
        repo_dir = install_remote(repo, dest=parent_dir, branch=branch,
 
624
                                  depth=depth)
591
625
    else:
592
626
        repo_dir = dest_dir
593
627
 
 
628
    venv = os.path.join(parent_dir, 'venv')
 
629
 
594
630
    if update_requirements:
595
631
        if not requirements_dir:
596
632
            error_out('requirements repo must be cloned before '
597
633
                      'updating from global requirements.')
598
 
        _git_update_requirements(repo_dir, requirements_dir)
 
634
        _git_update_requirements(venv, repo_dir, requirements_dir)
599
635
 
600
636
    juju_log('Installing git repo from dir: {}'.format(repo_dir))
601
 
    pip_install(repo_dir)
 
637
    if http_proxy:
 
638
        pip_install(repo_dir, proxy=http_proxy, venv=venv)
 
639
    else:
 
640
        pip_install(repo_dir, venv=venv)
602
641
 
603
642
    return repo_dir
604
643
 
605
644
 
606
 
def _git_update_requirements(package_dir, reqs_dir):
 
645
def _git_update_requirements(venv, package_dir, reqs_dir):
607
646
    """
608
647
    Update from global requirements.
609
648
 
612
651
    """
613
652
    orig_dir = os.getcwd()
614
653
    os.chdir(reqs_dir)
615
 
    cmd = ['python', 'update.py', package_dir]
 
654
    python = os.path.join(venv, 'bin/python')
 
655
    cmd = [python, 'update.py', package_dir]
616
656
    try:
617
657
        subprocess.check_call(cmd)
618
658
    except subprocess.CalledProcessError:
619
659
        package = os.path.basename(package_dir)
620
 
        error_out("Error updating {} from global-requirements.txt".format(package))
 
660
        error_out("Error updating {} from "
 
661
                  "global-requirements.txt".format(package))
621
662
    os.chdir(orig_dir)
622
663
 
623
664
 
 
665
def git_pip_venv_dir(projects_yaml):
 
666
    """
 
667
    Return the pip virtualenv path.
 
668
    """
 
669
    parent_dir = '/mnt/openstack-git'
 
670
 
 
671
    projects = _git_yaml_load(projects_yaml)
 
672
 
 
673
    if 'directory' in projects.keys():
 
674
        parent_dir = projects['directory']
 
675
 
 
676
    return os.path.join(parent_dir, 'venv')
 
677
 
 
678
 
624
679
def git_src_dir(projects_yaml, project):
625
680
    """
626
681
    Return the directory where the specified project's source is located.
627
682
    """
628
683
    parent_dir = '/mnt/openstack-git'
629
684
 
630
 
    if not projects_yaml:
631
 
        return
632
 
 
633
 
    projects = yaml.load(projects_yaml)
 
685
    projects = _git_yaml_load(projects_yaml)
634
686
 
635
687
    if 'directory' in projects.keys():
636
688
        parent_dir = projects['directory']
640
692
            return os.path.join(parent_dir, os.path.basename(p['repository']))
641
693
 
642
694
    return None
 
695
 
 
696
 
 
697
def git_yaml_value(projects_yaml, key):
 
698
    """
 
699
    Return the value in projects_yaml for the specified key.
 
700
    """
 
701
    projects = _git_yaml_load(projects_yaml)
 
702
 
 
703
    if key in projects.keys():
 
704
        return projects[key]
 
705
 
 
706
    return None