Adapt compass4nfv scripts to new compass-deck 55/65555/4
authorHarry Huang <huangxiangyu5@huawei.com>
Tue, 4 Dec 2018 03:41:34 +0000 (11:41 +0800)
committerHarry Huang <huangxiangyu5@huawei.com>
Fri, 1 Feb 2019 09:24:50 +0000 (17:24 +0800)
JIRA: COMPASS-612

1. pass machine info through file
2. generate mac address in config_parse.py

Change-Id: Iaaf0f2b1f4ab97e8200c8c046746168d991570e3
Signed-off-by: Harry Huang <huangxiangyu5@huawei.com>
14 files changed:
build/build.yaml
deploy/client.py
deploy/compass_conf/templates/cobbler/ubuntu-16.04.3-server-x86_64/system.tmpl
deploy/compass_vm.sh
deploy/config_parse.py
deploy/deploy_host.sh
deploy/deploy_parameter.sh
deploy/host_baremetal.sh
deploy/host_virtual.sh
deploy/launch.sh
deploy/prepare.sh
manager.py.patch [deleted file]
util/check_valid.py
util/docker-compose/roles/machines/tasks/main.yml

index 6694c8e..75faa28 100644 (file)
@@ -24,12 +24,12 @@ packages:
   - name: compass-deck
     description: "RESTful API and DB Handlers for Compass"
     get_method: docker
-    url: compass4nfv/compass-deck:7.0.0
+    url: opnfv/compass-deck:latest
 
   - name: compass-tasks-osa
     description: "compass task container integrated with openstack-ansible"
     get_method: docker
-    url: compass4nfv/compass-tasks-osa:7.0.0
+    url: opnfv/compass-tasks-osa:latest
 
   - name: compass-tasks-k8s
     description: "compass task container integrated with kubespray"
index e612160..3a9b0de 100644 (file)
@@ -79,8 +79,8 @@ opts = [
     cfg.BoolOpt('poll_switches',
                 help='if the client polls switches',
                 default=True),
-    cfg.StrOpt('machines',
-               help='comma separated mac addresses of machines',
+    cfg.StrOpt('machine_file',
+               help='mac addresses and ipmi info of machines',
                default=''),
     cfg.StrOpt('subnets',
                help='comma seperated subnets',
@@ -327,12 +327,12 @@ class CompassClient(object):
                 'get all machines status: %s, resp: %s', status, resp)
             raise RuntimeError('failed to get machines')
 
-        machines_to_add = list(set([
-            machine for machine in CONF.machines.split(',')
-            if machine
-        ]))
+        with open(CONF.machine_file) as fd:
+            machines_to_add = [str(m["mac"]) for m in yaml.load(fd)]
 
+        resp = byteify(resp)
         machines_db = [str(m["mac"]) for m in resp]
+
         LOG.info(
             'machines in db: %s\n to add: %s',
             machines_db,
@@ -983,8 +983,6 @@ class CompassClient(object):
         ansible_log = "%s/work/deploy/docker/ansible/run/%s-%s/ansible.log" \
                       % (compass_dir, CONF.adapter_name, CONF.cluster_name)
         os.system("sudo touch %s" % ansible_log)
-        os.system("sudo chmod +x -R %s/work/deploy/docker/ansible/run/"
-                  % compass_dir)
         ansible_print = multiprocessing.Process(target=print_log,
                                                 args=(ansible_log,))
         ansible_print.start()
index cfcc883..366cbc1 100755 (executable)
     "proxy": "$getVar('http_proxy', '')",
     "modify_interface":
         #set networks = $networks
+        #set mac = $mac
         #set rekeyed = {}
         #set promic_nic = ""
-        #for $nic, $val in $networks.iteritems():
-            #set ip_key = '-'.join(('ipaddress', $nic))
-            #set netmask_key = '-'.join(('netmask', $nic))
-            #set mgmt_key = '-'.join(('management', $nic))
-            #set static_key = '-'.join(('static', $nic))
-            #set $rekeyed[$ip_key] = $val.ip
-            #set $rekeyed[$netmask_key] = $val.netmask
-            #set $rekeyed[$static_key] = True
+        #for $nic in $mac
+            #set mac_key = '-'.join(('macaddress', $nic))
+            #set $rekeyed[$mac_key] = $mac[$nic]
+            #if $nic in $networks
+                #set val = $networks[$nic]
+                #set ip_key = '-'.join(('ipaddress', $nic))
+                #set netmask_key = '-'.join(('netmask', $nic))
+                #set mgmt_key = '-'.join(('management', $nic))
+                #set static_key = '-'.join(('static', $nic))
+                #set $rekeyed[$ip_key] = $val.ip
+                #set $rekeyed[$netmask_key] = $val.netmask
+                #set $rekeyed[$static_key] = True
 
-            #set dns_key = '-'.join(('dnsname', $nic))
-            #if $val.is_mgmt
-                #set $rekeyed[$dns_key] = $dns
-            #else
-                #if '.' in $dns
-                    #set $dns_name, $dns_domain = $dns.split('.', 1)
-                    #set $dns_nic = '%s-%s.%s' % ($dns_name, $nic, $dns_domain)
+                #set dns_key = '-'.join(('dnsname', $nic))
+                #if $val.is_mgmt
+                    #set $rekeyed[$dns_key] = $dns
                 #else
-                    #set $dns_nic = '%s-%s' % ($dns, $nic)
+                    #if '.' in $dns
+                        #set $dns_name, $dns_domain = $dns.split('.', 1)
+                        #set $dns_nic = '%s-%s.%s' % ($dns_name, $nic, $dns_domain)
+                    #else
+                        #set $dns_nic = '%s-%s' % ($dns, $nic)
+                    #end if
+                    #set $rekeyed[$dns_key] = $dns_nic
                 #end if
-                #set $rekeyed[$dns_key] = $dns_nic
-            #end if
 
-            #if $val.is_promiscuous:
-                #set promic_nic = $nic
-            #end if
-            #if $val.is_mgmt:
-                #set mac_key = '-'.join(('macaddress', $nic))
-                #set $rekeyed[$mac_key] = $mac
+                #if $val.is_promiscuous
+                    #set promic_nic = $nic
+                #end if
             #end if
         #end for
         #set nic_info = json.dumps($rekeyed, encoding='utf-8')
@@ -71,5 +73,9 @@
         "ignore_proxy": "$no_proxy",
         "local_repo": "$getVar('local_repo', '')",
         "disk_num": "1"
-    }
+    },
+    "power_address": "$power_manage.ip",
+    "power_user": "$power_manage.username",
+    "power_pass": "$power_manage.password",
+    "power_type": "$getVar('power_type', 'ipmilan')"
 }
index f7bc378..b203889 100755 (executable)
@@ -47,9 +47,7 @@ function install_compass_core() {
 }
 
 function set_compass_machine() {
-    local config_file=$WORK_DIR/installer/docker-compose/group_vars/all
-    sed -i '/pxe_boot_macs/d' $config_file
-    echo "pxe_boot_macs: [${machines}]" >> $config_file
+    cp $WORK_DIR/script/machine $WORK_DIR/docker/
 
     ansible-playbook $WORK_DIR/installer/docker-compose/add_machine.yml
 }
index 9cdf1ac..88a31e9 100644 (file)
@@ -11,27 +11,44 @@ import os
 import netaddr
 import yaml
 import sys
+import random
 from Cheetah.Template import Template
 
 
-def init(file):
+def load_yaml(file):
     with open(file) as fd:
         return yaml.safe_load(fd)
 
 
-def export_env_dict(env_dict, ofile, direct=False):
-    if not os.path.exists(ofile):
-        raise IOError("output file: %s not exist" % ofile)
+def dump_yaml(data, file):
+    with open(file, "w") as fd:
+        yaml.safe_dump(data, fd, default_flow_style=False)
+
+
+def mac_generator():
+    def random_hex():
+        return random.choice("0123456789ABCDEF")
+    mac = "00:00"
+    for i in xrange(4):
+        mac += ":{0}{1}".format(random_hex(), random_hex())
+    return mac
+
+
+def export_env_dict(env_dict, output_path, direct=False):
+    if not os.path.exists(output_path):
+        raise IOError("output file: %s not exist" % output_path)
     if direct:
         for k, v in env_dict.items():
-            os.system("echo 'export %s=\"%s\"' >> %s" % (k, v, ofile))
+            os.system("echo 'export %s=\"%s\"' >> %s" %
+                      (k, v, output_path))
     else:
         for k, v in env_dict.items():
-            os.system("echo 'export %s=${%s:-%s}' >> %s" % (k, k, v, ofile))
+            os.system("echo 'export %s=${%s:-%s}' >> %s" %
+                      (k, k, v, output_path))
 
 
 def decorator(func):
-    def wrapter(s, seq):
+    def wrapter(s, seq=None):
         host_list = s.get('hosts', [])
         result = []
         for host in host_list:
@@ -41,8 +58,10 @@ def decorator(func):
             result.append(s)
         if len(result) == 0:
             return ""
-        else:
+        elif seq:
             return "\"" + seq.join(result) + "\""
+        else:
+            return result
     return wrapter
 
 
@@ -57,11 +76,15 @@ def hostroles(s, seq, host=None):
 
 
 @decorator
-def hostmacs(s, seq, host=None):
-    return host.get('mac', '')
+def hostmachines(s, seq, host=None):
+    return {'mac': host.get('interfaces', {}),
+            'power_type': host.get('power_type', ''),
+            'power_ip': host.get('power_ip', ''),
+            'power_user': host.get('power_user', ''),
+            'power_pass': host.get('power_pass', '')}
 
 
-def export_network_file(dha, network, ofile):
+def export_network_file(dha, network, output_path):
     install_network_env = {}
     host_network_env = {}
     ip_settings = network['ip_settings']
@@ -79,7 +102,7 @@ def export_network_file(dha, network, ofile):
     install_network_env.update({'INSTALL_NETMASK': mgmt_netmask})
     install_network_env.update({'INSTALL_IP_RANGE': dhcp_ip_range})
     install_network_env.update({'VIP': internal_vip})
-    export_env_dict(install_network_env, ofile)
+    export_env_dict(install_network_env, output_path)
 
     pxe_nic = os.environ['PXE_NIC']
     host_ip_range = mgmt_net['ip_ranges'][0]
@@ -94,10 +117,10 @@ def export_network_file(dha, network, ofile):
     host_network_env.update({'NETWORK_MAPPING': "install=" + pxe_nic})
     host_network_env.update({'HOST_NETWORKS': ';'.join(host_networks)})
     host_network_env.update({'SUBNETS': ','.join(host_subnets)})
-    export_env_dict(host_network_env, ofile, True)
+    export_env_dict(host_network_env, output_path, True)
 
 
-def export_dha_file(dha, dha_file, ofile):
+def export_dha_file(dha, output_path, machine_path):
     env = {}
     env.update(dha)
     if env.get('hosts', []):
@@ -121,19 +144,28 @@ def export_dha_file(dha, dha_file, ofile):
     env.update({'FLAVOR': dha.get('FLAVOR', "cluster")})
     env.update({'HOSTNAMES': hostnames(dha, ',')})
     env.update({'HOST_ROLES': hostroles(dha, ';')})
-    env.update({'DHA': dha_file})
 
-    value = hostmacs(dha, ',')
-    if len(value) > 0:
-        env.update({'HOST_MACS': value})
+    machine = []
+    if dha.get('TYPE') == "virtual":
+        virtual_mac = []
+        for host in dha.get('hosts'):
+            mac = mac_generator()
+            machine.append({"mac": {"eth0": mac}, "power_type": "libvirt"})
+            virtual_mac.append(mac)
+        env.update({'HOST_MACS': ",".join(virtual_mac)})
+    else:
+        value = hostmachines(dha)
+        for item in value:
+            machine.append(item)
+    dump_yaml(machine, machine_path)
 
     if dha.get('TYPE', "virtual") == "virtual":
         env.update({'VIRT_NUMBER': len(dha['hosts'])})
 
-    export_env_dict(env, ofile)
+    export_env_dict(env, output_path)
 
 
-def export_reset_file(dha, tmpl_dir, output_dir, ofile):
+def export_reset_file(dha, tmpl_dir, output_dir, output_path):
     tmpl_file_name = dha.get('POWER_TOOL', '')
     if not tmpl_file_name:
         return
@@ -151,28 +183,31 @@ def export_reset_file(dha, tmpl_dir, output_dir, ofile):
         f.write(tmpl.respond())
 
     power_manage_env = {'POWER_MANAGE': reset_file_name}
-    export_env_dict(power_manage_env, ofile, True)
+    export_env_dict(power_manage_env, output_path, True)
 
 if __name__ == "__main__":
-    if len(sys.argv) != 6:
-        print("parameter wrong%d %s" % (len(sys.argv), sys.argv))
+    if len(sys.argv) != 7:
+        print("parameter wrong %d %s" % (len(sys.argv), sys.argv))
         sys.exit(1)
 
-    _, dha_file, network_file, tmpl_dir, output_dir, output_file = sys.argv
+    _, dha_file, network_file, tmpl_dir, output_dir, output_file,\
+        machine_file = sys.argv
 
     if not os.path.exists(dha_file):
         print("%s is not exist" % dha_file)
         sys.exit(1)
 
-    ofile = os.path.join(output_dir, output_file)
-    os.system("touch %s" % ofile)
-    os.system("echo \#config file deployment parameter > %s" % ofile)
+    output_path = os.path.join(output_dir, output_file)
+    machine_path = os.path.join(output_dir, machine_file)
+    os.system("touch %s" % output_path)
+    os.system("echo \#config file deployment parameter > %s" % output_path)
+    os.system("touch %s" % machine_path)
 
-    dha_data = init(dha_file)
-    network_data = init(network_file)
+    dha_data = load_yaml(dha_file)
+    network_data = load_yaml(network_file)
 
-    export_dha_file(dha_data, dha_file, ofile)
-    export_network_file(dha_data, network_data, ofile)
-    export_reset_file(dha_data, tmpl_dir, output_dir, ofile)
+    export_dha_file(dha_data, output_path, machine_path)
+    export_network_file(dha_data, network_data, output_path)
+    export_reset_file(dha_data, tmpl_dir, output_dir, output_path)
 
     sys.exit(0)
index 94abf76..7a7b5dd 100755 (executable)
@@ -43,7 +43,7 @@ function deploy_host(){
     --console_credentials="${CONSOLE_CREDENTIALS}" --host_networks="${HOST_NETWORKS}" \
     --network_mapping="${NETWORK_MAPPING}" --package_config_json_file="${PACKAGE_CONFIG_FILENAME}" \
     --host_roles="${HOST_ROLES}" --default_roles="${DEFAULT_ROLES}" --switch_ips="${SWITCH_IPS}" \
-    --machines=${machines//\'} --switch_credential="${SWITCH_CREDENTIAL}" --deploy_type="${TYPE}" \
+    --machine_file=${machine_file} --switch_credential="${SWITCH_CREDENTIAL}" --deploy_type="${TYPE}" \
     --deployment_timeout="${DEPLOYMENT_TIMEOUT}" --${POLL_SWITCHES_FLAG} --dashboard_url="${DASHBOARD_URL}" \
     --cluster_vip="${VIP}" --network_cfg="$NETWORK" --neutron_cfg="$NEUTRON" \
     --enable_secgroup="${ENABLE_SECGROUP}" --enable_fwaas="${ENABLE_FWAAS}" --expansion="${EXPANSION}" \
index 78223fc..d45d417 100755 (executable)
@@ -100,7 +100,7 @@ function process_default_para()
            "$DHA" "$NETWORK" \
            "${COMPASS_DIR}/deploy/template" \
            "${WORK_DIR}/script" \
-           "deploy_config.sh"
+           "deploy_config.sh" machine
 
     echo ${WORK_DIR}/script/deploy_config.sh
 }
index 4c63f82..3c30356 100755 (executable)
@@ -15,14 +15,6 @@ function reboot_hosts() {
 }
 
 function get_host_macs() {
-    if [[ "$EXPANSION" == "false" ]]; then
-        machines=`echo $HOST_MACS | sed -e 's/,/'\',\''/g' -e 's/^/'\''/g' -e 's/$/'\''/g'`
-        echo $machines > $WORK_DIR/switch_machines
-    else
-        machines_old=`cat $WORK_DIR/switch_machines`
-        machines_add=`echo $HOST_MACS | sed -e 's/,/'\',\''/g' -e 's/^/'\''/g' -e 's/$/'\''/g'`
-        echo $machines_add $machines_old > $WORK_DIR/switch_machines
-        machines=`echo $machines_add $machines_old|sed 's/ /,/g'`
-    fi
+    local machines=`echo $HOST_MACS | sed -e 's/,/'\',\''/g' -e 's/^/'\''/g' -e 's/$/'\''/g'`
     echo $machines
 }
index d955b74..c3133cf 100755 (executable)
@@ -25,20 +25,21 @@ function tear_down_machines() {
 }
 
 function reboot_hosts() {
+    echo "reboot"
     # We do need it for aarch64
-    if [ "$COMPASS_ARCH" = "aarch64" ]; then
-        old_ifs=$IFS
-        IFS=,
-        for i in $HOSTNAMES; do
-            sudo virsh destroy $i
-            sleep 3
-            sudo virsh start $i
-            sleep 3
-        done
-        IFS=$old_ifs
-    else
-        log_warn "reboot_hosts do nothing"
-    fi
+#    if [ "$COMPASS_ARCH" = "aarch64" ]; then
+#        old_ifs=$IFS
+#        IFS=,
+#        for i in $HOSTNAMES; do
+#            sudo virsh destroy $i
+#            sleep 3
+#            sudo virsh start $i
+#            sleep 3
+#        done
+#        IFS=$old_ifs
+#    else
+#        log_warn "reboot_hosts do nothing"
+#    fi
 }
 
 function launch_host_vms() {
@@ -97,37 +98,7 @@ function recover_host_vms() {
 }
 
 function get_host_macs() {
-    local mac_generator=${COMPASS_DIR}/deploy/mac_generator.sh
-    local machines=
-
-    if [[ $REDEPLOY_HOST == "true" ]]; then
-        mac_array=`cat $WORK_DIR/switch_machines`
-        machines=`echo $mac_array|sed 's/ /,/g'`
-    else
-        if [[ -z $HOST_MACS ]]; then
-            if [[ "$EXPANSION" == "false" ]]; then
-                chmod +x $mac_generator
-                mac_array=`$mac_generator $VIRT_NUMBER`
-                echo $mac_array > $WORK_DIR/switch_machines
-                machines=`echo $mac_array|sed 's/ /,/g'`
-            else
-                machines_old=`cat $WORK_DIR/switch_machines`
-                chmod +x $mac_generator
-                machines_add=`$mac_generator $VIRT_NUMBER`
-                echo $machines_add $machines_old > $WORK_DIR/switch_machines
-                machines=`echo $machines_add $machines_old|sed 's/ /,/g'`
-            fi
-        else
-            if [[ "$EXPANSION" == "false" ]]; then
-                machines=`echo $HOST_MACS | sed -e 's/,/'\',\''/g' -e 's/^/'\''/g' -e 's/$/'\''/g'`
-            else
-                machines_old=`cat $WORK_DIR/switch_machines`
-                machines_add=`echo $HOST_MACS | sed -e 's/,/'\',\''/g' -e 's/^/'\''/g' -e 's/$/'\''/g'`
-                echo $machines_add $machines_old > $WORK_DIR/switch_machines
-                machines=`echo $machines_add $machines_old|sed 's/ /,/g'`
-            fi
-        fi
-    fi
+    local machines=`echo $HOST_MACS | sed -e 's/,/'\',\''/g' -e 's/^/'\''/g' -e 's/$/'\''/g'`
     echo $machines
 }
 
index 8a00900..b11127c 100755 (executable)
@@ -53,6 +53,7 @@ if [[ "$EXPANSION" == "false" ]]; then
         exit 1
     fi
 
+    export machine_file=$WORK_DIR/script/machine
     export machines
 
     CONTAINER_ALIVE=$(check_container_alive)
index 38d98e1..7447ae7 100755 (executable)
@@ -48,6 +48,13 @@ function extract_tar()
 function prepare_env() {
     sudo sed -i -e 's/^#user =.*/user = "root"/g' /etc/libvirt/qemu.conf
     sudo sed -i -e 's/^#group =.*/group = "root"/g' /etc/libvirt/qemu.conf
+    sudo sed -i 's/^.\?listen_tls.*/listen_tls = 0/g' /etc/libvirt/libvirtd.conf
+    sudo sed -i 's/^.\?listen_tcp.*/listen_tcp = 1/g' /etc/libvirt/libvirtd.conf
+    sudo sed -i 's/^.\?tcp_port.*/tcp_port = "16509"/g' /etc/libvirt/libvirtd.conf
+    sudo sed -i 's/^.\?listen_addr.*/listen_addr = "0.0.0.0"/g' /etc/libvirt/libvirtd.conf
+    sudo sed -i 's/^.\?auth_tcp.*/auth_tcp = "none"/g' /etc/libvirt/libvirtd.conf
+    sudo sed -i 's#\(exec /usr/sbin/libvirtd \$libvirtd_opts\)$#\1 -l#g' /etc/init/libvirt-bin.conf
+
     sudo service libvirt-bin restart
     if sudo service openvswitch-switch status|grep stop; then
         sudo service openvswitch-switch start
diff --git a/manager.py.patch b/manager.py.patch
deleted file mode 100644 (file)
index 198ff5b..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
---- manager.py 2018-11-07 03:51:22.764685289 -0800
-+++ manager.py.new     2018-11-07 03:58:21.014139558 -0800
-@@ -314,8 +314,7 @@
-         if self._events is None:
-             # NOTE(danms): We really should have a more specific error
-             # here, but this is what we use for our default error case
--            raise exception.NovaException('In shutdown, no new events '
--                                          'can be scheduled')
-+            self._events = {}
-
-         @utils.synchronized(self._lock_name(instance))
-         def _create_or_get_event():
index e6a72e7..9348d27 100644 (file)
@@ -98,25 +98,19 @@ def check_dha_file(dha):
     invalid = False
     if dha['TYPE'] == 'baremetal':
         for i in dha['hosts']:
-            if not is_valid_mac(i['mac']):
-                err_print('''invalid address:
-                hosts:
-                 - name: %s
-                   mac: %s''' % (i['name'], i['mac']))
-                invalid = True
             for j in i['interfaces']:
-                if not is_valid_mac(j.values()[0]):
+                if not is_valid_mac(i['interfaces'].get(j)):
                     err_print('''invalid address:
                     hosts:
                         - name: %s
                           interfaces:
-                            - %s: %s''' % (i['name'], j.keys()[0], j.values()[0]))  # noqa: E501
+                            - %s: %s''' % (i['name'], j, i['interfaces'].get(j)))  # noqa: E501
                     invalid = True
-            if not is_valid_ip(i['ipmiIp']):
+            if not is_valid_ip(i['power_ip']):
                 err_print('''invalid address:
                 hosts:
                  - name: %s
-                   ipmiIp: %s''' % (i['name'], i['ipmiIp']))
+                   power_ip: %s''' % (i['name'], i['power_ip']))
                 invalid = True
 
     if not invalid:
index 365a9d9..7b4a9f1 100755 (executable)
@@ -1,21 +1,15 @@
 ---
-- name: create switch file if test mode enabled
-  template: src=switch_machines_file.j2
-            dest="{{ docker_compose_dir }}/switch_machines_file"
-  tags:
-    - redploy
-
 - name: docker cp switch_machines_file
   shell: |
-    docker cp "{{ docker_compose_dir }}/switch_machines_file" \
-    compass-deck:/tmp/switch_machines_file
+    docker cp "{{ docker_compose_dir }}/machine" \
+    compass-deck:/tmp/machine
   tags:
     - redploy
 
 - name: inject switches and mac addresses
   shell: |
     docker exec compass-deck bash -c \
-    "/opt/compass/bin/manage_db.py set_switch_machines \
-    --switch_machines_file /tmp/switch_machines_file"
+    "/opt/compass/bin/manage_db.py set_machine \
+    --machine_file /tmp/machine"
   tags:
     - redploy