141ecbd9354da22f3cfb33383cc1f5b16f48e75b
[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:
11 #   from opnfv.utils.Credentials import Credentials as credentials
12 #   credentials("fuel", "10.20.0.2", "root", "r00tme").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         creds_file = '/root/openrc'
78         try:
79             self.handler.get_file_from_controller(creds_file, target_path)
80         except Exception as e:
81             self.logger.error(
82                 "Cannot get %s from controller. %e" % (creds_file, e))
83         pass
84
85     def __fetch_creds_joid(self, target_path):
86         # TODO
87         pass
88
89     def fetch(self, target_path):
90         if self.__check_path(target_path) != os.EX_OK:
91             self.logger.error(
92                 "Target path %s does not exist!" % target_path)
93             return os.EX_IOERR
94         else:
95             self.logger.debug("Target path correct.")
96
97         self.logger.info("Fetching credentials from the deployment...")
98         if self.installer == "apex":
99             self.__fetch_creds_apex(target_path)
100         elif self.installer == "compass":
101             self.__fetch_creds_compass(target_path)
102         elif self.installer == "daisy":
103             self.__fetch_creds_daisy(target_path)
104         elif self.installer == "fuel":
105             self.__fetch_creds_fuel(target_path)
106         elif self.installer == "joid":
107             self.__fetch_creds_joid(target_path)