【debug】构建网络切片平台的一键启动与资源初步显示与资源初步控制

流程:两虚拟机启动,运行控制界面,控制界面输入参数然后下发,生成切片,显示切片信息,业务的启动,业务的显示,资源占用的显示,占用资源的调控

在目前基础上需要完成的工作:

  1. 点击下发后核心网及基站的依次启动,业务相关程序(服务器 代理)的启动
  2. 读取docker容器的cpu 存储信息显示到界面上
  3. 设置docker容器的某种资源,

python代码与shell脚本联合编程,跨机器虚拟机控制,mysql数据库读取并引用到shell脚本中执行命令

1.在一台LINUX机器上让另一台Linux机器执行一些操作

需求:

  1. 在linux机器A上进行操作,让linux机器B上运行一些命令或执行一些程序

思路:

  1. 在A与B之间构建一个通信,A发送消息告知B需要执行什么(如:启动基站 查询mysql然后根据查询数据进行操作)
  2. 在A上体现ssh登录到B,直接进行操作

1.1 使用ssh进行操作

ssh简介
https://blog.csdn.net/weixin_44248258/article/details/126317631

  • 关掉ssh认证
    https://blog.csdn.net/qq_21480607/article/details/91845794

  • 开启远程连接/密码错误的原因
    link

  • 设置远程登陆免密码,这样可以在shell脚本中登录远程端进行操作
    https://blog.csdn.net/yueyeguzhuo/article/details/130047635
    ssh
    shell+ssh
    expect自动输入密码

  • sudo后不用输入密码的方法
    https://blog.csdn.net/wsa1635/article/details/119184284

 sudo apt  install expect
#退出ssh
exit
ssh root@$ip "cd /home/lab/5gc-slicing-v1/Slice1/gnb-slice ; ls ; docker-compose -f ueransim-slice1.yaml up -d"

我们的服务器A与主机B在统一网段同一局域网上,在主机B上开启ssh服务,在服务器A上就可以通过ssh远程登录到主机B上,然后就可以通过命令行执行一切操作,等同于在主机B上的终端进行操作。

2.mysql + python+shell编程,以python代码为主体,可以读取mysql数据库数据并引用到shell中执行命令

构建一个python代码,完成mysql的读取及shell命令的执行

  • 外部让容器执行命令

  • python 读取/输出内容到文件
    https://blog.csdn.net/zxfhahaha/article/details/81288660

  • 在给容器内部安装python3时会需要按一下 回车 确认yes安装:如何解决?/shell交互时需要按回车
    https://blog.csdn.net/lufubo/article/details/7627393
    https://blog.csdn.net/qq_18863573/article/details/52883786 (安装后加一个 -y)

3.如何读取docker container状态,并存储到txt/mysql中,供前端展示

咨询外援

思考

  1. 是否可以将切片一键启动过程进行拆分,变成 核心网启动+基站启动+业务启动 三个阶段,这样既方便展示,也方便实现,同时可以增加状态检测并显示到界面上,当核心网启动完成后检测核心网状态判断核心网是否正常,正常才可以进行基站启动,并在基站启动后也进行状态检测及界面的显示,否者一个切片启动的整个过程都是黑盒,出现问题解决的办法只有重启整个切片。

python

from ast import Str
from fileinput import filename
import os
import time
#import pymysql

slice_num='1'  #default slice_num
ssh_ip='192.168.16.66'

mysql1_host='192.168.16.6'
mysql1_port=3306
mysql1_user='root'
mysql1_password='123456'
mysql1_datebase='xxxx'
#*******************************************************************mysql function**********************************

class MYSQLClass(object):
    def __init__(self, host, port, user, password, database):
        # 建立连接
        self.con = pymysql.connect(host=host,  #或者写成 localhost
            port=port,
            user=user,
            password=password,
            database=database,)
        # 创建一个游标
        self.cur =self.con.cursor()
 
    def __del__(self):
        # 析构方法
        self.cur.close()
        self.con.close()
 
    def find_one(self,sql):
        """
        :return:查询一条数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchone()
 
    def find_all(self,sql):
        """
        :return:查询所有数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchall()
 
    def insert(self,sql):
        """
        :param sql:
        :return: 向数据库中插入数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def delet(self,sql):
        """
        :param sql:
        :return: 删除数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def updata(self,sql):
        """
        :param sql:
        :return: 更新数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()


#*******************************************************************deploye&undeploye function****************************
def initial_config_cn():
    os.system("sudo sysctl net.ipv4.conf.all.forwarding=1")
    os.system("sudo iptables -P FORWARD ACCEPT")

def initial_config_gnb():
    ip='192.168.16.66'
    route1_slice1='ssh root@'+ip+' " ip route add  192.168.10.0/24 via 192.168.16.6 dev ens41"'
    route2_slice1='ssh root@'+ip+' " ip route add  192.168.12.0/24 via 192.168.16.6 dev ens41"'
    os.system(route1_slice1)
    os.system(route2_slice1)

    route1_slice2='ssh root@'+ip+' " ip route add  192.168.20.0/24 via 192.168.26.6 dev ens39"'
    route2_slice2='ssh root@'+ip+' " ip route add  192.168.22.0/24 via 192.168.26.6 dev ens39"'
    os.system(route1_slice2)
    os.system(route2_slice2)

    route1_slice3='ssh root@'+ip+' " ip route add  192.168.30.0/24 via 192.168.36.6 dev ens40"'
    route2_slice3='ssh root@'+ip+' " ip route add  192.168.32.0/24 via 192.168.36.6 dev ens40"'
    os.system(route1_slice3)
    os.system(route2_slice3)


def deploye_cn(slice_num):
    print('deploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml up -d '
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'deploye complete!')

def undeploye_cn(slice_num):
    print('undeploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml down'
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'undeploye complete!')


def deploye_ueransim(slice_num):
    print('deploying ueransim_slice',slice_num)
    ip='192.168.16.66'
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml up -d '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'deploye complete!')

def undeploye_ueransim(slice_num):
    print('undeploying ueransim_slice',slice_num)
    ip='192.168.16.66'
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml down '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'undeploye complete!')

def ueransim_nrbinder(slice_num):
    print('change nr-binder of ueransim',slice_num)
    ip='192.168.16.66'
    change_nrbinder='ssh root@'+ip+' " docker exec  ueransim-slice'+str(slice_num)+" /bin/bash -c 'chmod 777 nr-binder && apt-get update && apt-get install -y python3'"+'"'
    print(change_nrbinder)
    os.system(change_nrbinder)
    print("set nr-binder complete!")

def ueransim_copyfile(slice_num,filename):
    print('copy file to ueransim',slice_num)
    ip='192.168.16.66'
  #  filename='file_name'
    copy_file='ssh root@'+ip+' "'+'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'+' && docker cp '+filename+' ueransim-slice'+str(slice_num)+':/ueransim/bin "'
    print(copy_file)
    os.system(copy_file)
    print("copy file complete!")

if __name__ == '__main__':

    print('configuring initial config')
    initial_config_cn()
    initial_config_gnb()
    print('initial config complete!')


 #   print("setting mysql ")
  #  s = MYSQLClass(mysql1_host, mysql1_port, mysql1_user, mysql1_password, mysql1_datebase)   #link mysql
  #  sql='select slice_num from user '   #uesr is form name  ,slice_num is row name
 #   slice_info=s.find_one(sql)
  #  print(slice_info)
 #   print("mysql setting complete")
    
    deploye_cn(slice_num)
    time.sleep(3)
    deploye_ueransim(slice_num)
  #  ueransim_nrbinder(slice_num)
    ueransim_copyfile(slice_num,'test.py')
    with open("/home/lab/oai-cn5g-fed/5gc-slicing-v1/code_CN/test.txt","w") as f:
        f.write("1") 


  • 停止切片
from ast import Str
from fileinput import filename
import os
import time
#import pymysql

slice_num='3'  #default slice_num
ssh_ip='192.168.16.66'

mysql1_host='192.168.16.6'
mysql1_port=3306
mysql1_user='root'
mysql1_password='123456'
mysql1_datebase='xxxx'
#*******************************************************************mysql function**********************************

class MYSQLClass(object):
    def __init__(self, host, port, user, password, database):
        # 建立连接
        self.con = pymysql.connect(host=host,  #或者写成 localhost
            port=port,
            user=user,
            password=password,
            database=database,)
        # 创建一个游标
        self.cur =self.con.cursor()
 
    def __del__(self):
        # 析构方法
        self.cur.close()
        self.con.close()
 
    def find_one(self,sql):
        """
        :return:查询一条数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchone()
 
    def find_all(self,sql):
        """
        :return:查询所有数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchall()
 
    def insert(self,sql):
        """
        :param sql:
        :return: 向数据库中插入数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def delet(self,sql):
        """
        :param sql:
        :return: 删除数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def updata(self,sql):
        """
        :param sql:
        :return: 更新数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()


#*******************************************************************deploye&undeploye function****************************
def initial_config_cn():
    os.system("sudo sysctl net.ipv4.conf.all.forwarding=1")
    os.system("sudo iptables -P FORWARD ACCEPT")

def initial_config_gnb():
    ip='192.168.16.66'
    route1_slice1='ssh root@'+ip+' " ip route add  192.168.10.0/24 via 192.168.16.6 dev ens41"'
    route2_slice1='ssh root@'+ip+' " ip route add  192.168.12.0/24 via 192.168.16.6 dev ens41"'
    os.system(route1_slice1)
    os.system(route2_slice1)

    route1_slice2='ssh root@'+ip+' " ip route add  192.168.20.0/24 via 192.168.26.6 dev ens39"'
    route2_slice2='ssh root@'+ip+' " ip route add  192.168.22.0/24 via 192.168.26.6 dev ens39"'
    os.system(route1_slice2)
    os.system(route2_slice2)

    route1_slice3='ssh root@'+ip+' " ip route add  192.168.30.0/24 via 192.168.36.6 dev ens40"'
    route2_slice3='ssh root@'+ip+' " ip route add  192.168.32.0/24 via 192.168.36.6 dev ens40"'
    os.system(route1_slice3)
    os.system(route2_slice3)


def deploye_cn(slice_num):
    print('deploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml up -d '
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'deploye complete!')

def undeploye_cn(slice_num):
    print('undeploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml down'
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'undeploye complete!')


def deploye_ueransim(slice_num):
    print('deploying ueransim_slice',slice_num)
    ip='192.168.16.66'
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml up -d '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'deploye complete!')

def undeploye_ueransim(slice_num):
    print('undeploying ueransim_slice',slice_num)
    ip='192.168.16.66'
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml down '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'undeploye complete!')

def ueransim_nrbinder(slice_num):
    print('change nr-binder of ueransim',slice_num)
    ip='192.168.16.66'
    change_nrbinder='ssh root@'+ip+' " docker exec  ueransim-slice'+str(slice_num)+" /bin/bash -c 'chmod 777 nr-binder && apt-get update && apt-get install -y python3'"+'"'
    print(change_nrbinder)
    os.system(change_nrbinder)
    print("set nr-binder complete!")

def ueransim_copyfile(slice_num,filename):
    print('copy file to ueransim',slice_num)
    ip='192.168.16.66'
  #  filename='file_name'
    copy_file='ssh root@'+ip+' "'+'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'+' && docker cp '+filename+' ueransim-slice'+str(slice_num)+':/ueransim/bin "'
    print(copy_file)
    os.system(copy_file)
    print("copy file complete!")

if __name__ == '__main__':

    

 #   print("setting mysql ")
  #  s = MYSQLClass(mysql1_host, mysql1_port, mysql1_user, mysql1_password, mysql1_datebase)   #link mysql
  #  sql='select slice_num from user '   #uesr is form name  ,slice_num is row name
 #   slice_info=s.find_one(sql)
  #  print(slice_info)
 #   print("mysql setting complete")
    
    

    undeploye_ueransim(slice_num)
    time.sleep(3)
    undeploye_cn(slice_num)
  
    with open("/home/lab/oai-cn5g-fed/5gc-slicing-v1/code_CN/test.txt","w") as f:
        f.write("2") 


面向3虚拟机的切片一件启停代码

from ast import Str
from fileinput import filename
import os
import time
#import pymysql

slice_num='3'  #default slice_num
ssh_ipnow='192.168.16.66'
ssh_ip1='192.168.16.66'
ssh_ip2='192.168.26.66'
ssh_ip3='192.168.36.66'

mysql1_host='192.168.16.6'
mysql1_port=3306
mysql1_user='root'
mysql1_password='123456'
mysql1_datebase='xxxx'
#*******************************************************************mysql function**********************************

class MYSQLClass(object):
    def __init__(self, host, port, user, password, database):
        # 建立连接
        self.con = pymysql.connect(host=host,  #或者写成 localhost
            port=port,
            user=user,
            password=password,
            database=database,)
        # 创建一个游标
        self.cur =self.con.cursor()
 
    def __del__(self):
        # 析构方法
        self.cur.close()
        self.con.close()
 
    def find_one(self,sql):
        """
        :return:查询一条数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchone()
 
    def find_all(self,sql):
        """
        :return:查询所有数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchall()
 
    def insert(self,sql):
        """
        :param sql:
        :return: 向数据库中插入数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def delet(self,sql):
        """
        :param sql:
        :return: 删除数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def updata(self,sql):
        """
        :param sql:
        :return: 更新数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()


#*******************************************************************deploye&undeploye function****************************
def initial_config_cn():
    os.system("sudo sysctl net.ipv4.conf.all.forwarding=1")
    os.system("sudo iptables -P FORWARD ACCEPT")

def ssh_select(slice_num):
    global ssh_ipnow,ssh_ip1,ssh_ip2,ssh_ip3
    if slice_num=='1' :
        ssh_ipnow=ssh_ip1
    elif slice_num=='2' :
        ssh_ipnow=ssh_ip2
    elif slice_num=='3' :
        ssh_ipnow=ssh_ip3

def initial_config_gnb():
    global ssh_ipnow
    ip=ssh_ipnow
    route1_slice1='ssh root@'+ip+' " ip route add  192.168.10.0/24 via 192.168.16.6 dev ens41"'
    route2_slice1='ssh root@'+ip+' " ip route add  192.168.12.0/24 via 192.168.16.6 dev ens41"'
    os.system(route1_slice1)
    os.system(route2_slice1)

    route1_slice2='ssh root@'+ip+' " ip route add  192.168.20.0/24 via 192.168.26.6 dev ens39"'
    route2_slice2='ssh root@'+ip+' " ip route add  192.168.22.0/24 via 192.168.26.6 dev ens39"'
    os.system(route1_slice2)
    os.system(route2_slice2)

    route1_slice3='ssh root@'+ip+' " ip route add  192.168.30.0/24 via 192.168.36.6 dev ens40"'
    route2_slice3='ssh root@'+ip+' " ip route add  192.168.32.0/24 via 192.168.36.6 dev ens40"'
    os.system(route1_slice3)
    os.system(route2_slice3)


def deploye_cn(slice_num):
    print('deploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml up -d '
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'deploye complete!')

def undeploye_cn(slice_num):
    print('undeploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml down'
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'undeploye complete!')


def deploye_ueransim(slice_num):
    global ssh_ipnow
    print('deploying ueransim_slice',slice_num)
    ip=ssh_ipnow
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml up -d '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'deploye complete!')

def undeploye_ueransim(slice_num):
    global ssh_ipnow
    print('undeploying ueransim_slice',slice_num)
    ip=ssh_ipnow
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml down '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'undeploye complete!')

def ueransim_nrbinder(slice_num):
    global ssh_ipnow
    print('change nr-binder of ueransim',slice_num)
    ip=ssh_ipnow
    change_nrbinder='ssh root@'+ip+' " docker exec  ueransim-slice'+str(slice_num)+" /bin/bash -c 'chmod 777 nr-binder && apt-get update && apt-get install -y python3'"+'"'
    print(change_nrbinder)
    os.system(change_nrbinder)
    print("set nr-binder complete!")

def ueransim_copyfile(slice_num,filename):
    global ssh_ipnow
    print('copy file to ueransim',slice_num)
    ip=ssh_ipnow
  #  filename='file_name'
    copy_file='ssh root@'+ip+' "'+'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'+' && docker cp '+filename+' ueransim-slice'+str(slice_num)+':/ueransim/bin "'
    print(copy_file)
    os.system(copy_file)
    print("copy file complete!")

if __name__ == '__main__':
    ssh_select(slice_num)
    print('ssh ip selected '+ssh_ipnow)
    print('configuring initial config')
    initial_config_cn()
    initial_config_gnb()
    print('initial config complete!')


 #   print("setting mysql ")
  #  s = MYSQLClass(mysql1_host, mysql1_port, mysql1_user, mysql1_password, mysql1_datebase)   #link mysql
  #  sql='select slice_num from user '   #uesr is form name  ,slice_num is row name
 #   slice_info=s.find_one(sql)
  #  print(slice_info)
 #   print("mysql setting complete")
    
    deploye_cn(slice_num)
    time.sleep(3)
    deploye_ueransim(slice_num)
  #  ueransim_nrbinder(slice_num)
    ueransim_copyfile(slice_num,'test.py')
    with open("/home/lab/oai-cn5g-fed/5gc-slicing-v1/code_CN/test.txt","w") as f:
        f.write("1") 

  • 一建停止
from ast import Str
from fileinput import filename
import os
import time
#import pymysql

slice_num='3'  #default slice_num
ssh_ipnow='192.168.16.66'
ssh_ip1='192.168.16.66'
ssh_ip2='192.168.26.66'
ssh_ip3='192.168.36.66'

mysql1_host='192.168.16.6'
mysql1_port=3306
mysql1_user='root'
mysql1_password='123456'
mysql1_datebase='xxxx'
#*******************************************************************mysql function**********************************

class MYSQLClass(object):
    def __init__(self, host, port, user, password, database):
        # 建立连接
        self.con = pymysql.connect(host=host,  #或者写成 localhost
            port=port,
            user=user,
            password=password,
            database=database,)
        # 创建一个游标
        self.cur =self.con.cursor()
 
    def __del__(self):
        # 析构方法
        self.cur.close()
        self.con.close()
 
    def find_one(self,sql):
        """
        :return:查询一条数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchone()
 
    def find_all(self,sql):
        """
        :return:查询所有数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchall()
 
    def insert(self,sql):
        """
        :param sql:
        :return: 向数据库中插入数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def delet(self,sql):
        """
        :param sql:
        :return: 删除数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def updata(self,sql):
        """
        :param sql:
        :return: 更新数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()


#*******************************************************************deploye&undeploye function****************************
def initial_config_cn():
    os.system("sudo sysctl net.ipv4.conf.all.forwarding=1")
    os.system("sudo iptables -P FORWARD ACCEPT")

def ssh_select(slice_num):
    global ssh_ipnow,ssh_ip1,ssh_ip2,ssh_ip3
    if slice_num=='1' :
        ssh_ipnow=ssh_ip1
    elif slice_num=='2' :
        ssh_ipnow=ssh_ip2
    elif slice_num=='3' :
        ssh_ipnow=ssh_ip3

def initial_config_gnb():
    global ssh_ipnow
    ip=ssh_ipnow
    route1_slice1='ssh root@'+ip+' " ip route add  192.168.10.0/24 via 192.168.16.6 dev ens41"'
    route2_slice1='ssh root@'+ip+' " ip route add  192.168.12.0/24 via 192.168.16.6 dev ens41"'
    os.system(route1_slice1)
    os.system(route2_slice1)

    route1_slice2='ssh root@'+ip+' " ip route add  192.168.20.0/24 via 192.168.26.6 dev ens39"'
    route2_slice2='ssh root@'+ip+' " ip route add  192.168.22.0/24 via 192.168.26.6 dev ens39"'
    os.system(route1_slice2)
    os.system(route2_slice2)

    route1_slice3='ssh root@'+ip+' " ip route add  192.168.30.0/24 via 192.168.36.6 dev ens40"'
    route2_slice3='ssh root@'+ip+' " ip route add  192.168.32.0/24 via 192.168.36.6 dev ens40"'
    os.system(route1_slice3)
    os.system(route2_slice3)


def deploye_cn(slice_num):
    print('deploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml up -d '
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'deploye complete!')

def undeploye_cn(slice_num):
    print('undeploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml down'
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'undeploye complete!')


def deploye_ueransim(slice_num):
    global ssh_ipnow
    print('deploying ueransim_slice',slice_num)
    ip=ssh_ipnow
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml up -d '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'deploye complete!')

def undeploye_ueransim(slice_num):
    global ssh_ipnow
    print('undeploying ueransim_slice',slice_num)
    ip=ssh_ipnow
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml down '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'undeploye complete!')

def ueransim_nrbinder(slice_num):
    global ssh_ipnow
    print('change nr-binder of ueransim',slice_num)
    ip=ssh_ipnow
    change_nrbinder='ssh root@'+ip+' " docker exec  ueransim-slice'+str(slice_num)+" /bin/bash -c 'chmod 777 nr-binder && apt-get update && apt-get install -y python3'"+'"'
    print(change_nrbinder)
    os.system(change_nrbinder)
    print("set nr-binder complete!")

def ueransim_copyfile(slice_num,filename):
    global ssh_ipnow
    print('copy file to ueransim',slice_num)
    ip=ssh_ipnow
  #  filename='file_name'
    copy_file='ssh root@'+ip+' "'+'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'+' && docker cp '+filename+' ueransim-slice'+str(slice_num)+':/ueransim/bin "'
    print(copy_file)
    os.system(copy_file)
    print("copy file complete!")

if __name__ == '__main__':
    ssh_select(slice_num)
    print('ssh ip selected '+ssh_ipnow)
    undeploye_ueransim(slice_num)
    time.sleep(3)
    undeploye_cn(slice_num)
      
    with open("/home/lab/oai-cn5g-fed/5gc-slicing-v1/code_CN/test.txt","w") as f:
        f.write("2")

业务服务器开启


from ast import Str
from fileinput import filename
import os
import time
#import pymysql

slice_num='3'  #default slice_num
ssh_ipnow='192.168.16.66'
ssh_ip1='192.168.16.66'
ssh_ip2='192.168.26.66'
ssh_ip3='192.168.36.66'

mysql1_host='192.168.16.6'
mysql1_port=3306
mysql1_user='root'
mysql1_password='123456'
mysql1_datebase='xxxx'

#service & proxy path
service1='/home/lab/oai-cn5g-fed/5gc-slicing-v1/Slice1/tcp_video_server.py'
service2=''
service3=''
service=[]
service.append(service1)
service.append(service2)
service.append(service3)
proxy1='ueransim-5gc-proxy.py'
proxy2=''
proxy3=''
proxy=[]
proxy.append(proxy1)
proxy.append(proxy2)
proxy.append(proxy3)

#*******************************************************************mysql function**********************************

class MYSQLClass(object):
    def __init__(self, host, port, user, password, database):
        # 建立连接
        self.con = pymysql.connect(host=host,  #或者写成 localhost
            port=port,
            user=user,
            password=password,
            database=database,)
        # 创建一个游标
        self.cur =self.con.cursor()
 
    def __del__(self):
        # 析构方法
        self.cur.close()
        self.con.close()
 
    def find_one(self,sql):
        """
        :return:查询一条数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchone()
 
    def find_all(self,sql):
        """
        :return:查询所有数据
        """
        self.res=self.cur.execute(sql)
        return self.cur.fetchall()
 
    def insert(self,sql):
        """
        :param sql:
        :return: 向数据库中插入数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def delet(self,sql):
        """
        :param sql:
        :return: 删除数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()
 
    def updata(self,sql):
        """
        :param sql:
        :return: 更新数据库中数据
        """
        self.res=self.cur.execute(sql)
        self.con.commit()


#*******************************************************************deploye&undeploye function****************************
def initial_config_cn():
    os.system("sudo sysctl net.ipv4.conf.all.forwarding=1")
    os.system("sudo iptables -P FORWARD ACCEPT")

def ssh_select(slice_num):
    global ssh_ipnow,ssh_ip1,ssh_ip2,ssh_ip3
    if slice_num=='1' :
        ssh_ipnow=ssh_ip1
    elif slice_num=='2' :
        ssh_ipnow=ssh_ip2
    elif slice_num=='3' :
        ssh_ipnow=ssh_ip3

def initial_config_gnb():
    global ssh_ipnow
    ip=ssh_ipnow
    route1_slice1='ssh root@'+ip+' " ip route add  192.168.10.0/24 via 192.168.16.6 dev ens41"'
    route2_slice1='ssh root@'+ip+' " ip route add  192.168.12.0/24 via 192.168.16.6 dev ens41"'
    os.system(route1_slice1)
    os.system(route2_slice1)

    route1_slice2='ssh root@'+ip+' " ip route add  192.168.20.0/24 via 192.168.26.6 dev ens39"'
    route2_slice2='ssh root@'+ip+' " ip route add  192.168.22.0/24 via 192.168.26.6 dev ens39"'
    os.system(route1_slice2)
    os.system(route2_slice2)

    route1_slice3='ssh root@'+ip+' " ip route add  192.168.30.0/24 via 192.168.36.6 dev ens40"'
    route2_slice3='ssh root@'+ip+' " ip route add  192.168.32.0/24 via 192.168.36.6 dev ens40"'
    os.system(route1_slice3)
    os.system(route2_slice3)


def deploye_cn(slice_num):
    print('deploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml up -d '
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'deploye complete!')

def undeploye_cn(slice_num):
    print('undeploying CN_slice',slice_num)
    filepath= 'cd /home/lab/oai-cn5g-fed/5gc-slicing-v1'+'/Slice'+str(slice_num)
    start_command='docker-compose -f docker-compose-basic-vpp-nrf-slice'+str(slice_num)+'.yaml down'
    print(filepath)
    print(start_command)
    command=filepath+'&&'+start_command
    os.system(command)
    print('CN ',slice_num,'undeploye complete!')


def deploye_ueransim(slice_num):
    global ssh_ipnow
    print('deploying ueransim_slice',slice_num)
    ip=ssh_ipnow
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml up -d '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'deploye complete!')

def undeploye_ueransim(slice_num):
    global ssh_ipnow
    print('undeploying ueransim_slice',slice_num)
    ip=ssh_ipnow
    filepath= 'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'
    start_command='docker-compose -f ueransim-slice'+str(slice_num)+'.yaml down '
    all_start_command='ssh root@'+ip+' " '+filepath+' ; ls ;'+start_command+'"'
    print(all_start_command)
    os.system(all_start_command)
    print('ueransim',slice_num,'undeploye complete!')

def ueransim_nrbinder(slice_num):
    global ssh_ipnow
    print('change nr-binder of ueransim',slice_num)
    ip=ssh_ipnow
    change_nrbinder='ssh root@'+ip+' " docker exec  ueransim-slice'+str(slice_num)+" /bin/bash -c 'chmod 777 nr-binder && apt-get update && apt-get install -y python3'"+'"'
    print(change_nrbinder)
    os.system(change_nrbinder)
    print("set nr-binder complete!")

def ueransim_copyfile(slice_num,filename):
    global ssh_ipnow
    print('copy file to ueransim',slice_num)
    ip=ssh_ipnow
  #  filename='file_name'
    copy_file='ssh root@'+ip+' "'+'cd /home/lab/5gc-slicing-v1'+'/Slice'+str(slice_num)+'/gnb-slice'+' && docker cp '+filename+' ueransim-slice'+str(slice_num)+':/ueransim/bin "'
    print(copy_file)
    os.system(copy_file)
    print("copy file complete!")


def service_cn(i) :
    print('starting service'+i)
    start_service='python3 '+service[i-1]
    print(start_service)
    os.system(start_service)
    print("service"+i+'started')

def proxy_gnb(slice_num,i):
    global ssh_ipnow
    ip=ssh_ipnow
    print("starting proxy"+i)
    start_proxy= 'ssh root@'+ip+' "'+  'docker exec  ueransim-slice'+str(slice_num)+" /bin/bash -c './nr-binder uesimtun0 python3 ueransim-5gc-proxy.py' "  + ' "'
    print(start_proxy)
    os.system(start_proxy)
    print("proxy"+i+"started")



if __name__ == '__main__':
    ssh_select(slice_num)
    print('ssh ip selected '+ssh_ipnow)
 #   print('configuring initial config')
  #  initial_config_cn()
  #  initial_config_gnb()
  #  print('initial config complete!')

    service_cn(1)
    proxy_gnb(1,1)

 #   print("setting mysql ")
  #  s = MYSQLClass(mysql1_host, mysql1_port, mysql1_user, mysql1_password, mysql1_datebase)   #link mysql
  #  sql='select slice_num from user '   #uesr is form name  ,slice_num is row name
 #   slice_info=s.find_one(sql)
  #  print(slice_info)
 #   print("mysql setting complete")
    
  #  deploye_cn(slice_num)
  #  time.sleep(3)
 #   deploye_ueransim(slice_num)
  #  ueransim_nrbinder(slice_num)
  #  ueransim_copyfile(slice_num,'test.py')
    with open("/home/lab/oai-cn5g-fed/5gc-slicing-v1/code_CN/test.txt","w") as f:
        f.write("3") 

优化

新增切片自定义生成yaml文件


yaml 变量

将资源控制写入yaml文件,在启动过程中直接控制资源

https://blog.csdn.net/fish2009122/article/details/112004464

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月早十

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值