summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/create_keypairs.py
blob: b037c3f59d90114cfc779465b969d758787785f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

import os
from novaclient.exceptions import NotFound

from snaps import file_utils
from snaps.config.keypair import KeypairConfig
from snaps.openstack.openstack_creator import OpenStackComputeObject
from snaps.openstack.utils import nova_utils

__author__ = 'spisarski'

logger = logging.getLogger('OpenStackKeypair')


class OpenStackKeypair(OpenStackComputeObject):
    """
    Class responsible for managing a keypair in OpenStack
    """

    def __init__(self, os_creds, keypair_settings):
        """
        Constructor - all parameters are required
        :param os_creds: The credentials to connect with OpenStack
        :param keypair_settings: a KeypairConfig object
        """
        super(self.__class__, self).__init__(os_creds)

        self.keypair_settings = keypair_settings
        self.__delete_keys_on_clean = True

        # Attributes instantiated on create()
        self.__keypair = None

    def initialize(self):
        """
        Loads the existing OpenStack Keypair
        :return: The Keypair domain object or None
        """
        super(self.__class__, self).initialize()

        try:
            self.__keypair = nova_utils.get_keypair_by_name(
                self._nova, self.keypair_settings.name)
            return self.__keypair
        except Exception as e:
            logger.warn('Cannot load existing keypair - %s', e)

    def create(self):
        """
        Responsible for creating the keypair object.
        :return: The Keypair domain object or None
        """
        self.initialize()

        if not self.__keypair:
            logger.info('Creating keypair %s...' % self.keypair_settings.name)

            if self.keypair_settings.public_filepath and os.path.isfile(
                    self.keypair_settings.public_filepath):
                logger.info("Uploading existing keypair")
                self.__keypair = nova_utils.upload_keypair_file(
                    self._nova, self.keypair_settings.name,
                    self.keypair_settings.public_filepath)

                if self.keypair_settings.delete_on_clean is not None:
                    delete_on_clean = self.keypair_settings.delete_on_clean
                    self.__delete_keys_on_clean = delete_on_clean
                else:
                    self.__delete_keys_on_clean = False
            else:
                logger.info("Creating new keypair")
                keys = nova_utils.create_keys(self.keypair_settings.key_size)
                self.__keypair = nova_utils.upload_keypair(
                    self._nova, self.keypair_settings.name,
                    nova_utils.public_key_openssh(keys))
                file_utils.save_keys_to_files(
                    keys, self.keypair_settings.public_filepath,
                    self.keypair_settings.private_filepath)

                if self.keypair_settings.delete_on_clean is not None:
                    delete_on_clean = self.keypair_settings.delete_on_clean
                    self.__delete_keys_on_clean = delete_on_clean
                else:
                    self.__delete_keys_on_clean = True
        elif self.__keypair and not os.path.isfile(
                self.keypair_settings.private_filepath):
            logger.warn("The public key already exist in OpenStack \
                        but the private key file is not found ..")

        return self.__keypair

    def clean(self):
        """
        Removes and deletes the keypair.
        """
        if self.__keypair:
            try:
                nova_utils.delete_keypair(self._nova, self.__keypair)
            except NotFound:
                pass
            self.__keypair = None

        if self.__delete_keys_on_clean:
            if (self.keypair_settings.public_filepath and
                    file_utils.file_exists(
                        self.keypair_settings.public_filepath)):
                expanded_path = os.path.expanduser(
                    self.keypair_settings.public_filepath)
                os.chmod(expanded_path, 0o755)
                os.remove(expanded_path)
                logger.info('Deleted public key file [%s]', expanded_path)
            if (self.keypair_settings.private_filepath and
                    file_utils.file_exists(
                        self.keypair_settings.private_filepath)):
                expanded_path = os.path.expanduser(
                    self.keypair_settings.private_filepath)
                os.chmod(expanded_path, 0o755)
                os.remove(expanded_path)
                logger.info('Deleted private key file [%s]', expanded_path)

        super(self.__class__, self).clean()

    def get_keypair(self):
        """
        Returns the OpenStack keypair object
        :return:
        """
        return self.__keypair


class KeypairSettings(KeypairConfig):
    """
    Class representing a keypair configuration
    """

    def __init__(self, **kwargs):
        from warnings import warn
        warn('Use snaps.config.keypair.KeypairConfig instead',
             DeprecationWarning)
        super(self.__class__, self).__init__(**kwargs)