Skip to content

Commit

Permalink
[v1.0.0] feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
jarryxy committed May 6, 2023
0 parents commit cd24c6b
Show file tree
Hide file tree
Showing 36 changed files with 2,499 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Realse
Debug
x64
x86
.vs
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## keepalived-windows

`startup.bat` 启动服务

`stop.bat` 停止服务

`reload.bat` 重载服务 修改配置后请重载服务。

### 配置说明

`ha.conf`

```conf
global_defs {
vrrp_mcast_group4 224.100.100.100
}
vrrp_instance {
state MASTER
interface 192.168.64.128
interface_name Ethernet0
virtual_router_id 51
priority 121
advert_int 1
virtual_ipaddress {
192.168.64.200
192.168.64.201
}
}
```



- `global_defs` 全局配置
+ `vrrp_mcast_group4` 组播地址
- `vrrp_instance` 节点配置
+ `state` 当前节点初始化状态 可选值(MASTER | BACKUP)
+ `interface` 绑定网卡ip
+ `interface_name` 绑定网卡连接名 (ipconfig查看)
+ `virtual_router_id` 虚拟路由id,同一集群id值请设置一致 范围0到254
+ `priority` 权重,谁最大谁最终竞争胜利,state变为MASTER BACKUP的权重设置要小于MASTER 范围0到254
+ `advert_int` 心跳间隔 单位秒 范围0到254
+ `virtual_ipaddress` 虚拟ip,请保证在同一子网范围内,可配置多个
12 changes: 12 additions & 0 deletions keepalived-setup-action/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace keepalived_setup_action
{
public class Class1
{
}
}
36 changes: 36 additions & 0 deletions keepalived-setup-action/Installer1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 133 additions & 0 deletions keepalived-setup-action/Installer1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace keepalived_setup_action
{

[RunInstaller(true)]
public partial class Installer1 : Installer
{
string serviceName = "keepalived-service";
string exe = "keepalived-windows.exe";
static string logPath = @"C:\log\log.txt";
public Installer1()
{
InitializeComponent();
}

protected override void OnAfterInstall(IDictionary savedState)
{
string path = this.Context.Parameters["targetdir"];
string binPath = path + exe;
Logger("安装...");
Logger(path + exe);
Process scProcess = new Process();
scProcess.StartInfo.FileName = "sc.exe";
scProcess.StartInfo.Arguments = "create " + serviceName + " binPath= \"" + binPath + "\" start= auto DisplayName= \"" + serviceName + "\"";
scProcess.Start();
scProcess.WaitForExit();

if (scProcess.ExitCode != 0)
{
Logger("服务注册成功");
// Failed to create service
// Handle error
}
else
{
Logger("服务注册失败");
// Service created successfully
}
base.OnAfterInstall(savedState);
}

protected override void OnBeforeUninstall(IDictionary savedState)
{
Process scProcess = new Process();
scProcess.StartInfo.FileName = "sc.exe";
scProcess.StartInfo.Arguments = "delete \"" + serviceName + "\"";
scProcess.Start();
scProcess.WaitForExit();

if (scProcess.ExitCode != 0)
{
Logger("服务删除成功");
// Failed to create service
// Handle error
}
else
{
Logger("服务删除失败");
// Service created successfully
}
base.OnBeforeUninstall(savedState);
}

protected override void OnAfterUninstall(IDictionary savedState)
{
Logger("OnAfterUninstall...");

var savedStateValue = savedState.Contains("savedState") ? savedState["savedState"] : "未获取到安装的目录";
Logger($"OnAfterUninstall从OnAfterInstall获取 savedState,值为:{savedStateValue}");
string path = this.Context.Parameters["targetdir"];
Logger($"targetdir:{path}");
Logger($"开始删除目录:{path}");
if (Directory.Exists(path))
{
RemoveSubDirectory(new DirectoryInfo(path));
Logger($"删除目录:{path} 成功");
}
Logger("OnAfterUninstall 完成了...");
base.OnAfterUninstall(savedState);
}

protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
}

/// 卸载完成后删除多余的文件
private static void RemoveSubDirectory(DirectoryInfo directory)
{
Logger($"目录信息 directory:{directory}");
foreach (DirectoryInfo sub in directory.GetDirectories())
{
if (sub.GetFiles().Length > 0 || sub.GetDirectories().Length > 0)
RemoveSubDirectory(sub);
sub.Delete(true);
Logger($"要删除的目录信息 sub:{sub}");
}
Logger($"目录成功");
}



/// <summary>
/// 记录日志
/// </summary>
/// <param name="message"></param>
private static void Logger(string message)
{
string directory = Path.GetDirectoryName(logPath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (!File.Exists(logPath))
{
File.Create(logPath).Close();
}
Trace.Listeners.Clear();
Trace.AutoFlush = true;
Trace.Listeners.Add(new TextWriterTraceListener(logPath));
Trace.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}" + message);
}
}
}
36 changes: 36 additions & 0 deletions keepalived-setup-action/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("keepalived-setup-action")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("keepalived-setup-action")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("ad6b433b-3fa0-42bd-99b0-3fa2cdc5c63f")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file not shown.
Binary file not shown.
56 changes: 56 additions & 0 deletions keepalived-setup-action/keepalived-setup-action.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AD6B433B-3FA0-42BD-99B0-3FA2CDC5C63F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>keepalived_setup_action</RootNamespace>
<AssemblyName>keepalived-setup-action</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Installer1.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Installer1.Designer.cs">
<DependentUpon>Installer1.cs</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.5", FrameworkDisplayName = ".NET Framework 4.5")]
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7fc890b94e55473e13332b7c3ed9700d0091be55
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
D:\keepalived-windows\keepalived-setup-action\bin\Release\keepalived-setup-action.dll
D:\keepalived-windows\keepalived-setup-action\bin\Release\keepalived-setup-action.pdb
D:\keepalived-windows\keepalived-setup-action\obj\Release\keepalived-setup-action.csproj.AssemblyReference.cache
D:\keepalived-windows\keepalived-setup-action\obj\Release\keepalived-setup-action.csproj.CoreCompileInputs.cache
D:\keepalived-windows\keepalived-setup-action\obj\Release\keepalived-setup-action.dll
D:\keepalived-windows\keepalived-setup-action\obj\Release\keepalived-setup-action.pdb
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit cd24c6b

Please sign in to comment.