aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityrulestoportconnectiongetresource/inputfiles/wait_for_resources.py
blob: 5d960d659eebaea2d3cc084034f22fd637650c64 (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
import argparse
import json
import netifaces
import os
import sys
import time

TIME_INTERVAL = 10


def parse_json_file(json_path):
    with open(json_path, 'r') as json_file:
        data = json.load(json_file)
    return data


def check_network_interfaces():
    for interface in netifaces.interfaces():
        if(sys.platform != 'win32' or netifaces.ifaddresses(interface)[-1000][0]['addr'] != '00:00:00:00:00:00:00:e0'):
            while 2 not in netifaces.ifaddresses(interface).keys() and 23 not in netifaces.ifaddresses(interface).keys():
                print "Still waiting for interface:", interface
                time.sleep(TIME_INTERVAL)


def check_connectivity():
    if sys.platform.startswith('linux'):
        ping_str = "ping -c 1 "
    elif sys.platform == 'cygwin' or sys.platform == 'win32':
        ping_str = "ping -n 1 "

    while os.system(ping_str + component_ip) != 0:
        print "No connectivity to", component_ip, "waiting", TIME_INTERVAL, "seconds"
        time.sleep(TIME_INTERVAL)


def check_cinder_mounts():
    if sys.platform.startswith('linux'):
        meta_data = parse_json_file('/meta.js')
    elif sys.platform == 'cygwin' or sys.platform == 'win32':
        meta_data = parse_json_file('c:\\meta.js')

    cinder_count = 0

    for info in meta_data:
        if info.startswith('mount'):
            cinder_count += 1

    if sys.platform.startswith('linux'):
        cinder_attached = os.popen('ls /dev/disk/by-id/virtio* | wc -l').read()
    elif sys.platform == 'cygwin' or sys.platform == 'win32':
        cinder_attached = os.popen("wmic diskdrive get DeviceID | find /i \"PHYSICALDRIVE\" | find /V \"0\" /C").read()

    while (int(cinder_attached) < cinder_count) and (cinder_count != 0):
        print "Missing a cinder mount, waiting", TIME_INTERVAL, "seconds"
        time.sleep(TIME_INTERVAL)

        if sys.platform.startswith('linux'):
            cinder_attached = os.popen('ls /dev/disk/by-id/virtio* | wc -l').read()

        elif sys.platform == 'cygwin' or sys.platform == 'win32':
            cinder_attached = os.popen(
                "wmic diskdrive get DeviceID | find /i \"PHYSICALDRIVE\" | find /V \"0\" /C").read()

        if int(cinder_attached) == cinder_count:
            print "All cinder are attached and ready to be formatted and mounted"


def main():
    check_network_interfaces()
    check_cinder_mounts()

    if component_ip is not None:
        check_connectivity()

    print "All resources are ready"


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='This script is waiting for network and volume resources to come up')
    parser.add_argument('-m', '--component_ip', metavar='component_ip', type=str, help='The component ip', required=False)
    args = parser.parse_args()
    component_ip = args.component_ip
    globals().update(args.__dict__)
    main()