update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / examples / ping_pong_ns / rift / mano / examples / primitive_test.py
1 #!/usr/bin/env python3
2
3 ############################################################################
4 # Copyright 2017 RIFT.IO Inc #
5 # #
6 # Licensed under the Apache License, Version 2.0 (the "License"); #
7 # you may not use this file except in compliance with the License. #
8 # You may obtain a copy of the License at #
9 # #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # #
12 # Unless required by applicable law or agreed to in writing, software #
13 # distributed under the License is distributed on an "AS IS" BASIS, #
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 # See the License for the specific language governing permissions and #
16 # limitations under the License. #
17 ############################################################################
18
19
20 import argparse
21 import os
22 import sys
23 import random
24 import paramiko
25 import yaml
26 from glob import glob
27
28
29 def copy_file_ssh_sftp(server, remote_dir, remote_file, local_file):
30 """Copy file to VM."""
31 sshclient = paramiko.SSHClient()
32 sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
33 sshclient.load_system_host_keys(filename="/dev/null")
34 sshclient.connect(server, username="fedora", password="fedora")
35 sftpclient = sshclient.open_sftp()
36 sftpclient.put(local_file, remote_dir + '/' + remote_file)
37 sshclient.close()
38
39
40 def get_full_path(file_name, production_launchpad=True):
41 """Return the full path for the init cfg file."""
42 mpath = os.path.join(
43 os.getenv('RIFT_INSTALL'), 'var', 'rift')
44 if not production_launchpad:
45 launchpad_folder = glob('{}/*mgmt-vm-lp-2'.format(mpath))[0]
46 else:
47 launchpad_folder = ''
48 mpath = os.path.join(
49 os.getenv('RIFT_INSTALL'), 'var', 'rift', launchpad_folder,
50 'launchpad', 'packages', 'vnfd', 'default')
51 vnfd_folder = random.choice(
52 [x for x in os.listdir(mpath) if os.path.isdir(
53 os.path.join(mpath, x))])
54 full_path = glob(
55 '{}/{}/cloud_init/{}'.format(mpath, vnfd_folder, file_name))[0]
56 file_name = os.path.basename(os.path.normpath(full_path))
57 return full_path, file_name
58
59
60 def exists_remote(host, path):
61 """Test if a file exists at path on a host accessible with SSH."""
62 ssh = paramiko.SSHClient()
63 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
64 try:
65 ssh.connect(host, username="fedora", password="fedora")
66 sftp = ssh.open_sftp()
67 try:
68 sftp.stat(path)
69 except Exception:
70 raise Exception('Transfered file not found on the remote host')
71 ssh.close()
72 except paramiko.SSHException:
73 print("Connection Error")
74
75
76 def primitive_test(yaml_cfg):
77 """Transfer a cloud init file from the vnfd descriptor package.
78
79 Verify that the file is transfered.
80 """
81 for index, vnfr in yaml_cfg['vnfr_data_map'].items():
82 vnfd_ip = vnfr['mgmt_interface']['ip_address']
83 file_name = '*_cloud_init.cfg'
84 local_file, file_name = get_full_path(file_name)
85 copy_file_ssh_sftp(vnfd_ip, '/tmp/', file_name, local_file)
86 remote_file_path = os.path.join(
87 '/'
88 'tmp',
89 file_name)
90 exists_remote(vnfd_ip, remote_file_path)
91
92
93 def main(argv=sys.argv[1:]):
94 """Main."""
95 try:
96 parser = argparse.ArgumentParser()
97 parser.add_argument("yaml_cfg_file", type=argparse.FileType('r'))
98 parser.add_argument(
99 "-q", "--quiet", dest="verbose",
100 action="store_false")
101 args = parser.parse_args()
102
103 except Exception as e:
104 print("Exception in {}: {}".format(__file__, e))
105 sys.exit(1)
106
107 try:
108 yaml_str = args.yaml_cfg_file.read()
109 yaml_cfg = yaml.load(yaml_str)
110
111 primitive_test(yaml_cfg)
112
113 except Exception as e:
114 print("Exception: {}".format(e))
115 raise e
116
117 if __name__ == "__main__":
118 main()