3 ############################################################################
4 # Copyright 2017 RIFT.IO Inc #
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 #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
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 ############################################################################
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
)
40 def get_full_path(file_name
, production_launchpad
=True):
41 """Return the full path for the init cfg file."""
43 os
.getenv('RIFT_INSTALL'), 'var', 'rift')
44 if not production_launchpad
:
45 launchpad_folder
= glob('{}/*mgmt-vm-lp-2'.format(mpath
))[0]
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
))])
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
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())
65 ssh
.connect(host
, username
="fedora", password
="fedora")
66 sftp
= ssh
.open_sftp()
70 raise Exception('Transfered file not found on the remote host')
72 except paramiko
.SSHException
:
73 print("Connection Error")
76 def primitive_test(yaml_cfg
):
77 """Transfer a cloud init file from the vnfd descriptor package.
79 Verify that the file is transfered.
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(
90 exists_remote(vnfd_ip
, remote_file_path
)
93 def main(argv
=sys
.argv
[1:]):
96 parser
= argparse
.ArgumentParser()
97 parser
.add_argument("yaml_cfg_file", type=argparse
.FileType('r'))
99 "-q", "--quiet", dest
="verbose",
100 action
="store_false")
101 args
= parser
.parse_args()
103 except Exception as e
:
104 print("Exception in {}: {}".format(__file__
, e
))
108 yaml_str
= args
.yaml_cfg_file
.read()
109 yaml_cfg
= yaml
.load(yaml_str
)
111 primitive_test(yaml_cfg
)
113 except Exception as e
:
114 print("Exception: {}".format(e
))
117 if __name__
== "__main__":