~openstack-charmers-next/charms/trusty/percona-cluster/trunk

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2016-07-06 15:42:34 UTC
  • Revision ID: james.page@ubuntu.com-20160706154234-imubbp2anajb019g
Resync charmhelpers for licensing change

The charm-helpers project have re-licensed to Apache 2.0
inline with the agreed licensing approach to intefaces,
layers and charms generally.

Resync helpers to bring charmhelpers inline with charm
codebase.

Change-Id: I980164c72b7df94e73d0c5dc474043b9a3993fe7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2014-2015 Canonical Limited.
2
2
#
3
 
# This file is part of charm-helpers.
4
 
#
5
 
# charm-helpers is free software: you can redistribute it and/or modify
6
 
# it under the terms of the GNU Lesser General Public License version 3 as
7
 
# published by the Free Software Foundation.
8
 
#
9
 
# charm-helpers 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 Lesser General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU Lesser General Public License
15
 
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#  http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
16
14
 
17
15
# Common python helper functions used for OpenStack charms.
18
16
from collections import OrderedDict
222
220
}
223
221
 
224
222
GIT_DEFAULT_BRANCHES = {
225
 
    'icehouse': 'icehouse-eol',
226
223
    'kilo': 'stable/kilo',
227
224
    'liberty': 'stable/liberty',
228
225
    'mitaka': 'stable/mitaka',
725
722
requirements_dir = None
726
723
 
727
724
 
728
 
def git_default_repos(projects):
 
725
def git_default_repos(projects_yaml):
729
726
    """
730
727
    Returns default repos if a default openstack-origin-git value is specified.
731
728
    """
732
729
    service = service_name()
 
730
    core_project = service
733
731
 
734
732
    for default, branch in GIT_DEFAULT_BRANCHES.iteritems():
735
 
        if projects == default:
 
733
        if projects_yaml == default:
736
734
 
737
735
            # add the requirements repo first
738
736
            repo = {
742
740
            }
743
741
            repos = [repo]
744
742
 
745
 
            # neutron and nova charms require some additional repos
746
 
            if service == 'neutron':
747
 
                for svc in ['neutron-fwaas', 'neutron-lbaas', 'neutron-vpnaas']:
 
743
            # NOTE(coreycb): This is a temp work-around until the requirements
 
744
            # repo moves from stable/kilo branch to kilo-eol tag. The core
 
745
            # repos have already done this.
 
746
            if default == 'kilo':
 
747
                branch = 'kilo-eol'
 
748
 
 
749
            # neutron-* and nova-* charms require some additional repos
 
750
            if service in ['neutron-api', 'neutron-gateway',
 
751
                           'neutron-openvswitch']:
 
752
                core_project = 'neutron'
 
753
                for project in ['neutron-fwaas', 'neutron-lbaas',
 
754
                                'neutron-vpnaas', 'nova']:
748
755
                    repo = {
749
 
                        'name': svc,
750
 
                        'repository': GIT_DEFAULT_REPOS[svc],
 
756
                        'name': project,
 
757
                        'repository': GIT_DEFAULT_REPOS[project],
751
758
                        'branch': branch,
752
759
                    }
753
760
                    repos.append(repo)
754
 
            elif service == 'nova':
 
761
 
 
762
            elif service in ['nova-cloud-controller', 'nova-compute']:
 
763
                core_project = 'nova'
755
764
                repo = {
756
765
                    'name': 'neutron',
757
766
                    'repository': GIT_DEFAULT_REPOS['neutron'],
758
767
                    'branch': branch,
759
768
                }
760
769
                repos.append(repo)
 
770
            elif service == 'openstack-dashboard':
 
771
                core_project = 'horizon'
761
772
 
762
 
            # finally add the current service's repo
 
773
            # finally add the current service's core project repo
763
774
            repo = {
764
 
                'name': service,
765
 
                'repository': GIT_DEFAULT_REPOS[service],
 
775
                'name': core_project,
 
776
                'repository': GIT_DEFAULT_REPOS[core_project],
766
777
                'branch': branch,
767
778
            }
768
779
            repos.append(repo)
769
780
 
770
781
            return yaml.dump(dict(repositories=repos))
771
782
 
772
 
    return projects
 
783
    return projects_yaml
773
784
 
774
785
 
775
786
def _git_yaml_load(projects_yaml):
829
840
        pip_install(p, upgrade=True, proxy=http_proxy,
830
841
                    venv=os.path.join(parent_dir, 'venv'))
831
842
 
 
843
    constraints = None
832
844
    for p in projects['repositories']:
833
845
        repo = p['repository']
834
846
        branch = p['branch']
840
852
                                                     parent_dir, http_proxy,
841
853
                                                     update_requirements=False)
842
854
            requirements_dir = repo_dir
 
855
            constraints = os.path.join(repo_dir, "upper-constraints.txt")
 
856
            # upper-constraints didn't exist until after icehouse
 
857
            if not os.path.isfile(constraints):
 
858
                constraints = None
843
859
        else:
844
860
            repo_dir = _git_clone_and_install_single(repo, branch, depth,
845
861
                                                     parent_dir, http_proxy,
846
 
                                                     update_requirements=True)
 
862
                                                     update_requirements=True,
 
863
                                                     constraints=constraints)
847
864
 
848
865
    os.environ = old_environ
849
866
 
875
892
 
876
893
 
877
894
def _git_clone_and_install_single(repo, branch, depth, parent_dir, http_proxy,
878
 
                                  update_requirements):
 
895
                                  update_requirements, constraints=None):
879
896
    """
880
897
    Clone and install a single git repository.
881
898
    """
898
915
 
899
916
    juju_log('Installing git repo from dir: {}'.format(repo_dir))
900
917
    if http_proxy:
901
 
        pip_install(repo_dir, proxy=http_proxy, venv=venv)
 
918
        pip_install(repo_dir, proxy=http_proxy, venv=venv,
 
919
                    constraints=constraints)
902
920
    else:
903
 
        pip_install(repo_dir, venv=venv)
 
921
        pip_install(repo_dir, venv=venv, constraints=constraints)
904
922
 
905
923
    return repo_dir
906
924
 
980
998
    script generation, which is used by the OpenStack packages.
981
999
    """
982
1000
    for f in os.listdir(templates_dir):
 
1001
        # Create the init script and systemd unit file from the template
983
1002
        if f.endswith(".init.in"):
984
1003
            init_in_file = f
985
1004
            init_file = f[:-8]
1005
1024
                os.remove(init_dest)
1006
1025
            if os.path.exists(service_dest):
1007
1026
                os.remove(service_dest)
1008
 
            shutil.move(init_source, init_dest)
1009
 
            shutil.move(service_source, service_dest)
 
1027
            shutil.copyfile(init_source, init_dest)
 
1028
            shutil.copyfile(service_source, service_dest)
1010
1029
            os.chmod(init_dest, 0o755)
1011
1030
 
 
1031
    for f in os.listdir(templates_dir):
 
1032
        # If there's a service.in file, use it instead of the generated one
 
1033
        if f.endswith(".service.in"):
 
1034
            service_in_file = f
 
1035
            service_file = f[:-3]
 
1036
 
 
1037
            service_in_source = os.path.join(templates_dir, service_in_file)
 
1038
            service_source = os.path.join(templates_dir, service_file)
 
1039
            service_dest = os.path.join('/lib/systemd/system', service_file)
 
1040
 
 
1041
            shutil.copyfile(service_in_source, service_source)
 
1042
 
 
1043
            if os.path.exists(service_dest):
 
1044
                os.remove(service_dest)
 
1045
            shutil.copyfile(service_source, service_dest)
 
1046
 
 
1047
    for f in os.listdir(templates_dir):
 
1048
        # Generate the systemd unit if there's no existing .service.in
 
1049
        if f.endswith(".init.in"):
 
1050
            init_in_file = f
 
1051
            init_file = f[:-8]
 
1052
            service_in_file = "{}.service.in".format(init_file)
 
1053
            service_file = "{}.service".format(init_file)
 
1054
 
 
1055
            init_in_source = os.path.join(templates_dir, init_in_file)
 
1056
            service_in_source = os.path.join(templates_dir, service_in_file)
 
1057
            service_source = os.path.join(templates_dir, service_file)
 
1058
            service_dest = os.path.join('/lib/systemd/system', service_file)
 
1059
 
 
1060
            if not os.path.exists(service_in_source):
 
1061
                cmd = ['pkgos-gen-systemd-unit', init_in_source]
 
1062
                subprocess.check_call(cmd)
 
1063
 
 
1064
                if os.path.exists(service_dest):
 
1065
                    os.remove(service_dest)
 
1066
                shutil.copyfile(service_source, service_dest)
 
1067
 
1012
1068
 
1013
1069
def os_workload_status(configs, required_interfaces, charm_func=None):
1014
1070
    """