modules.opnfv: fuel adapter: Switch to MCP
[releng.git] / modules / opnfv / utils / Credentials.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Usage example (note: Fuel actually uses key-based auth, not user/pass):
11 #   from opnfv.utils.Credentials import Credentials as credentials
12 #   credentials("fuel", "10.20.0.2", "user", "password").fetch('./openrc')
13 #
14
15 import os
16
17 import opnfv.installer_adapters.InstallerHandler as ins_handler
18 import opnfv.utils.Connection as con
19 import opnfv.utils.OPNFVLogger as logger
20
21
22 class Credentials(object):
23
24     def __init__(self, installer, ip, user, password=None):
25         self.installer = installer
26         self.ip = ip
27         self.logger = logger.Logger("Credentials", level="DEBUG").getLogger()
28         self.connection = con.Connection()
29
30         if self.__check_installer_name(self.installer) != os.EX_OK:
31             self.logger.error("Installer %s not supported!" % self.installer)
32             return os.EX_CONFIG
33         else:
34             self.logger.debug("Installer %s supported." % self.installer)
35
36         if self.connection.verify_connectivity(self.ip) != os.EX_OK:
37             self.logger.error("Installer %s not reachable!" % self.ip)
38             return os.EX_UNAVAILABLE
39         else:
40             self.logger.debug("IP %s is reachable!" % self.ip)
41
42         self.logger.debug(
43             "Trying to stablish ssh connection to %s ..." % self.ip)
44         self.handler = ins_handler.InstallerHandler(installer,
45                                                     ip,
46                                                     user,
47                                                     password)
48
49     def __check_installer_name(self, installer):
50         if installer not in ("apex", "compass", "daisy", "fuel", "joid"):
51             return os.EX_CONFIG
52         else:
53             return os.EX_OK
54
55     def __check_path(self, path):
56         try:
57             with open(path, 'a'):
58                 os.utime(path, None)
59             return os.EX_OK
60         except IOError as e:
61             self.logger.error(e)
62             return os.EX_IOERR
63
64     def __fetch_creds_apex(self, target_path):
65         # TODO
66         pass
67
68     def __fetch_creds_compass(self, target_path):
69         # TODO
70         pass
71
72     def __fetch_creds_daisy(self, target_path):
73         # TODO
74         pass
75
76     def __fetch_creds_fuel(self, target_path):
77         # TODO
78         pass
79
80     def __fetch_creds_joid(self, target_path):
81         # TODO
82         pass
83
84     def fetch(self, target_path):
85         if self.__check_path(target_path) != os.EX_OK:
86             self.logger.error(
87                 "Target path %s does not exist!" % target_path)
88             return os.EX_IOERR
89         else:
90             self.logger.debug("Target path correct.")
91
92         self.logger.info("Fetching credentials from the deployment...")
93         if self.installer == "apex":
94             self.__fetch_creds_apex(target_path)
95         elif self.installer == "compass":
96             self.__fetch_creds_compass(target_path)
97         elif self.installer == "daisy":
98             self.__fetch_creds_daisy(target_path)
99         elif self.installer == "fuel":
100             self.__fetch_creds_fuel(target_path)
101         elif self.installer == "joid":
102             self.__fetch_creds_joid(target_path)