自动化环境部署

一些总结

在回归测试阶段,需要大量安装部署测试环境。针对传统的手工反复安装部署,需要自动化部署环境。这里是一些方法。

文件复制和网络映射

公司为了便于程序管理,将一些程序和文件远程存储在了某台服务器上,所以,每次在Windows环境下,启动电脑都要手动配置用户名和密码,然后,才可以完成程序的下载和部署。
实际上我们使用命令NET USE可以自动连接网络驱动器。

.bat文件创建

1.首先使用记事本创建

2.接着在记事本中另存为所有文件。在这里插入图片描述
3.填上以.bat结尾的名称。在这里插入图片描述

net use [盘符|*] [网络路径] [密码] [用户名]

网络映射批处理:net.bat

rem net use * /d /y
call net use W: \\10.176.36.40\data /user:auto auto
call net use X: \\10.176.33.66\Tangram /user:auto auto
pause
exit

针对c#中,net use W: \\10.176.36.40映射不到的情况,需要再加进一层W: \\10.176.36.40\data到这一层才能访问。

string clean = @"net use * /d /y";
            string temp_path = @"Y: \\10.176.36.49\vantage";
            //string UserName = "Vantage";
            //string PassWord = "1qasw@";
            string docline = "net use " + temp_path + " /user:Vantage 1qasw@";
            RunCmd(clean, false);
            Console.WriteLine("__success__");
            RunCmd(docline, false);
            Console.WriteLine("success------");
            string input = @"C:\Users\fuwh2\Desktop\7z1805-x64.exe";
            //C:\Users\fuwh2\Desktop\FileZilla_3.43.0_win64-setup.exe
            //C:\LenovoVantageTest\vantage-uwp-automation\LenovoVantageTest\bin\Debug\LenovoVantageTest.dll
            string output = @"Y:\smba\dev-sign";

这里的思路是:
1.首先,把网络映射到本地盘,不映射,在本地无法找到对应盘符,也就无法复制。而且,不用每次部署,一劳永逸。
2.开始copy file,这里,也不是直接按照,不同盘之间的文件copy,而是需要Xcopy,写.bat实现。

映射盘copy到本地:makeDatalink.bat

@echo off
call Xcopy "W:\\Tools\NUnit.org" C:\Users\wenhaofu\Desktop\NUnit.org  /s  /e  /y 
call Xcopy "W:\\Tools\WinAppDriver" C:\Users\wenhaofu\Desktop\WinAppDriver  /s  /e  /y

这里有两个问题:
1.它复制是将父类文件夹下面的子类文件夹,都复制到桌面。
2.它不能直接复制到指定的C:\Program Files (x86),因为有管理员权限。
我采用的是先复制到桌面,桌面再复制到C:\Program Files (x86),而复制到桌面,需要先创建文件夹。那么就有以下几个函数:

配置环境变量

环境变量批处理:enviroment_variables_change.bat

:: 设置环境变量
 
:: 关闭终端回显
@echo off
 
set ENV_PATH=%PATH%
@echo ====current environment:
@echo %ENV_PATH%
 
:: 添加环境变量,即在原来的环境变量后加上英文状态下的分号和路径
set MY_PATH=C:\Program Files (x86)\NUnit.org\nunit-console
set ENV_PATH=%PATH%;%MY_PATH%
@echo ====new environment:
@echo %ENV_PATH%

set MY_PATH=C:\Program Files (x86)\Windows Application Driver
set ENV_PATH=%PATH%;%MY_PATH%
@echo ====new environment:
@echo %ENV_PATH%

pause

代码

#include <iostream>
#include <conio.h>
#include <process.h>
#include <windows.h>
#include <vector>
#include <Windows.h>
#include <WtsApi32.h>
#include <unordered_map>
#include <stdlib.h>
#include <io.h>
#include <stdio.h>
#include <string>
#include <atlstr.h>
#include <direct.h>
#include <CString>
#include "atlconv.h"
//在桌面新建文件夹
void new_file(string s) {
    string folderPath = s;
    string command;
    command = "mkdir -p " + folderPath;
    system(command.c_str());
}
//不同盘之间,复制文件夹
/*
*我采用的是微软提供的 SHFileOperation 
*/
bool Copy_File(CString soures,CString des) {
    CString strSource = soures;
    strSource += '\0';//注意必须是'\0'而不是"\0"!~!!
    CString strDes = des;
    strDes += '\0';
    SHFILEOPSTRUCT fop;
    fop.wFunc = FO_COPY;//选择执行类型,FO_COPY,FO_DELETE,FO_RENAME,FO_MOVE四种
    fop.pFrom = strSource;//源文件夹的路径,以'\0'即空为结尾
    fop.pTo = strDes;//拷入文件的文件夹路径,以'\0'即空为结尾
    if (SHFileOperation(&fop) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
//复制到C:\Program Files (x86),并配置环境变量
void copy_to_c() {
    string s = "C:\\Users\\wenhaofu\\Desktop\\NUnit.org";
    string s1 = "C:\\Users\\wenhaofu\\Desktop\\WinAppDriver";
    new_file(s);
    new_file(s1);
    system("C:\\Users\\wenhaofu\\Desktop\\auto_bat\\makeDatalink.bat");
    CString temp = "C:\\Users\\wenhaofu\\Desktop\\NUnit.org";
    CString dis = "C:\\Program Files (x86)";
    CString temp1 = "C:\\Users\\wenhaofu\\Desktop\\WinAppDriver";
    CString dis1 = "C:\\Program Files (x86)";
    Copy_File(temp,dis);
    Copy_File(temp1, dis1);
    system("C:\\Users\\wenhaofu\\Desktop\\auto_bat\\enviroment_variables_change.bat");
}
//把文件复制到目标文件夹下
  public static void CopyToFile(string input,string output)
        {
            //源文件路径
            string sourceName = input;

            //目标路径:项目下的NewTest文件夹,(如果没有就创建该文件夹)
            //string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewTest");
            string folderPath = output;
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            //文件不用新的文件名,就用原文件文件名
            string fileName = Path.GetFileName(sourceName);
            可以选择给文件换个新名字
            //string fileName = string.Format("{0}.{1}", "newFileText", "txt");

            //目标整体路径
            string targetPath = Path.Combine(folderPath, fileName);

            //Copy到新文件下
            FileInfo file = new FileInfo(sourceName);
            if (file.Exists)
            {
                //true 覆盖已存在的同名文件,false不覆盖
                file.CopyTo(targetPath, true);
            }
        }

遇到的问题

在写.bat过程中,我发现两句net use不能连续执行,查找网络上的,大都是坑。最后,加上call就可以了。

call命令用来从一个批处理脚本中调用另一个批处理脚本。

静默安装

静默安装:setup.bat

@echo off
title 自动安装UltraVNC_1_3_1_X64_Setup
color 5f
echo          正在安装UltraVNC_1_3_1_X64_Setup 
echo.
echo.
echo           请不要关闭此窗口,耐心等待!!!
start /wait UltraVNC_1_3_1_X64_Setup.exe /verysilent sp-
echo.
echo.
echo                       已成功安装UltraVNC_1_3_1_X64_Setup

静默安装

关闭UAC,防火墙,睡眠

关闭UAC: auto_close_UAC.bat

@echo off
cd\&color 0a&cls&::By key9928
echo 禁用UAC&title 禁用UAC&title 注意事项
echo 注意:在个别win10上禁用UAC后无法使用edge浏览器
echo.&pause&title 禁用UAC
%windir%\system32\cmd.exe /k %windir%\System32\reg.exe add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
echo.echo 请重启电脑生效
echo.&pause

代码

//Power&Sleep
void power_management()
{
    //关闭显示器设置为永不
    std::string cmd_monitor1("powercfg /change monitor-timeout-ac 0");
    std::string cmd_monitor2("powercfg /change monitor-timeout-dc 0");
    //关闭硬盘设置为永不
    std::string cmd_disk1("powercfg /change disk-timeout-ac 0");
    std::string cmd_disk2("powercfg /change disk-timeout-dc 0");
    //睡眠设置为永不
    std::string cmd_standby1("powercfg /change standby-timeout-ac 0");
    std::string cmd_standby2("powercfg /change standby-timeout-dc 0");
    //休眠设置为永不
    std::string cmd_hibernate1("powercfg /change hibernate-timeout-ac 0");
    std::string cmd_hibernate2("powercfg /change hibernate-timeout-dc 0");
    system(cmd_monitor1.c_str());
    system(cmd_monitor2.c_str());
    system(cmd_disk1.c_str());
    system(cmd_disk2.c_str());
    system(cmd_standby1.c_str());
    system(cmd_standby2.c_str());
    system(cmd_hibernate1.c_str());
    system(cmd_hibernate2.c_str());
}
//sign-on never
void sign_on() {
    std::string cmd_sign("powercfg /SETDCVALUEINDEX SCHEME_CURRENT SUB_NONE CONSOLELOCK 0");
    system(cmd_sign.c_str());
}
//关闭UAC
void unused_uac() {
    //std::string cmd_sign("C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f");
    //system(cmd_sign.c_str());
    system("C:\\Users\\wenhaofu\\Desktop\\auto_bat\\auto_close_UAC.bat");
}
//关闭防火墙
void unused_fire() {
    std::string cmd_sign1("netsh advfirewall set Domainprofile state off");
    system(cmd_sign1.c_str());
    std::string cmd_sign2("netsh advfirewall set Privateprofile state off");
    system(cmd_sign2.c_str());
    std::string cmd_sign3("netsh advfirewall set Publicprofile state off");
    system(cmd_sign3.c_str());
}

开启远程桌面

remote_desktop.bat

@echo off
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
pause
void open_remote_desktop() {
    system("C:\\Users\\wenhaofu\\Desktop\\auto_bat\\remote_desktop.bat");
}

打开开发者模式

start_developer_mode.bat

@echo off
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock" /v AllowDevelopmentWithoutDevLicense /t REG_DWORD /d 1 /f
pause
void open_remote_desktop() {
    system("C:\\Users\\wenhaofu\\Desktop\\auto_bat\\start_developer_mode.bat");
}

安装证书

Certificate.bat

@echo off

rem 注意:需要管理员权限,否则运行失败!!
rem 作用:将mongoose的根证书ca.crt导入当前计算机

rem 导入时windows会弹出警告框( 缺点),提示用户是否信任该根证书,需要管理员权限
rem certmgr.exe /c /add C:\ca.crt /s root win10 ok,win 7没有 certmgr.exe

rem 导入证书到可信任证书颁发者,C:\ca.crt 证书路径需根据实际情况更改
certutil -addstore root C:\Users\wenhaofu\Desktop\auto_bat\LenovoDevOps.cer
certutil -addstore root C:\Users\wenhaofu\Desktop\auto_bat\LenovoSHA2ROOTCA.cer
certutil -addstore root C:\Users\wenhaofu\Desktop\auto_bat\LenovoSHA2SUBCA1.cer
certutil -addstore root C:\Users\wenhaofu\Desktop\auto_bat\lenovoSHA2SUBCA2.cer

pause
exit
void install_certificate() {
    system("C:\\Users\\wenhaofu\\Desktop\\auto_bat\\Certificate.bat");
    //system("CertMgr /add LenovoDevOps.cer /s /r localMachine root /all");
}

重启服务

server.bat

@echo off
::关闭回显
sc query |find /i "uvnc_service" >nul2 >nul
::通过sc query 查询服务是否存在
if  %errorlevel%==0 (goto exist) else goto notexist
::通过errorlevel 的返回值判断上次命令是否成功  errorlevel为0则为查询到结果,否则没有。
::如果errorlevel 的值为0 ,即查询到结果,也就是服务正在运行,则执行exist里面的命令。
::否则执行notexist里面的命令

:exist
echo ######### 服务存在,将重启服务 ###########
sc stop uvnc_service >nul2 >nul
::停止uvnc_service服务
if 0 == %ERRORLEVEL% (echo 停止服务成功 >>日志.log) else echo 停止服务失败 >>日志.log
::通过errorlevel判断命令是否执行成功
sc start uvnc_service >nul2 >nul
::启动uvnc_service服务
if 0 == %ERRORLEVEL% (echo 启动服务成功 >>日志.log) else echo 启动服务失败 >>日志.log
::通过errorlevel判断命令是否执行成功
echo %date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,8% 服务重新启动成功 >> 日志.log
pause
goto :eof

:notexist
echo ######### 服务不存在,将启动服务 ##########
sc start uvnc_service >nul2 >nul
if 0 == %ERRORLEVEL% (echo  启动任务成功 >>日志.log) else echo 启动服务失败 >>日志.log
::通过errorlevel判断命令是否执行成功
echo %date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,8% 服务启动成功 >> 日志.log
pause
goto :eof
void restart_server() {
    system("C:\\Users\\wenhaofu\\Desktop\\auto_bat\\server.bat");
}

launch 本地应用

void launch_UltraVNC_settings() {
    system("start C:\\ProgramData\\Microsoft\\Windows\\\"Start Menu\"\\Programs\\UltraVNC\\\"UltraVNC Server Settings.lnk\"\"");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值