Skip to content

Commit

Permalink
添加了往 hyper.sh 容器平台的部署和升级脚本。现在 Discussion.Migrations 将同样以一个可执行程序的方式在容器…
Browse files Browse the repository at this point in the history
…中与 Discussion.Web 一起发行,可参考 upgrade-from-existing.sh 中的调用方法。
  • Loading branch information
Jijie Chen committed Sep 23, 2018
1 parent 1c3e09f commit 9018351
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ branches:

script:
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then ./build.sh --target=ci ; fi
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then tag=`date +%Y%m%d%H%M` ; export imagetag="jijiechen/dotnetclub:$tag" ; fi
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then tag=`date -u +%Y%m%d%H%M` ; export imagetag="jijiechen/dotnetclub:$tag" ; fi
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then ./build-linux.sh --target=ci --imagetag="$imagetag" ; fi
- if [ "$TRAVIS_OS_NAME" = "linux" ] && [ "$DOCKER_USERNAME" != "" ]; then docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD && docker push $imagetag ; fi

Expand Down
4 changes: 3 additions & 1 deletion DockerFile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# How to run this image?
# 1. (optional) At (WorkDir), Create your appsettings.json or appsettings.<env>.json
# 2. (optional) At (WorkDir), Create your database file
# 3. execute docker run -d -v (WorkDir):/club-data -p 5000:5000 jijiechen/dotnetclub
# 3. Execute docker run -d -v (WorkDir):/club-data -p 5000:5000 jijiechen/dotnetclub



Expand All @@ -12,10 +12,12 @@ VOLUME /club-data

COPY . /club-app


RUN useradd --uid 5000 --create-home --home /home/clubadm clubadm
RUN chown -R clubadm /home/clubadm
RUN chown -R clubadm /club-data


USER clubadm
WORKDIR /club-data
ENTRYPOINT ["dotnet", "/club-app/Discussion.Web.dll", "--webroot", "/club-app/wwwroot"]
Expand Down
2 changes: 2 additions & 0 deletions OpenAspNetOrg.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ ProjectSection(SolutionItems) = preProject
Readme.md = Readme.md
.gitignore = .gitignore
DockerFile = DockerFile
deploy-on-hyper.sh = deploy-on-hyper.sh
upgrade-from-existing.sh = upgrade-from-existing.sh
EndProjectSection
EndProject
Global
Expand Down
5 changes: 5 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ Task("package")
}
CopyFile("./DockerFile", "./src/Discussion.Web/publish/DockerFile");
CopyFile("./upgrade-from-existing.sh", "./src/Discussion.Web/publish/upgrade-from-existing.sh ");
CopyFile("src/Discussion.Migrations/bin/Release/netcoreapp2.1/Discussion.Migrations.deps.json", "./src/Discussion.Web/publish/Discussion.Migrations.deps.json");
CopyFile("src/Discussion.Migrations/bin/Release/netcoreapp2.1/Discussion.Migrations.runtimeconfig.json", "./src/Discussion.Web/publish/Discussion.Migrations.runtimeconfig.json");
Execute($"docker build ./src/Discussion.Web/publish -t {imagetag} -f ./src/Discussion.Web/publish/DockerFile");
}
});
Expand Down
51 changes: 51 additions & 0 deletions deploy-on-hyper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

set -e

oldVer=$1
newVer=$2

RED=`tput setaf 1`
GREEN=`tput setaf 2`
NC=`tput sgr0` # no color


if [ "$newVer" == "" ] || [ "$oldVer" == "" ]; then
echo "${RED}请指定要从哪个版本升级到哪个版本!${NC}"
echo "${RED}比如,从第 001 升级到 003,请指定 deploy-on-hyper.sh 001 003${NC}"
exit 1
fi


new_image="jijiechen/dotnetclub:$newVer"

if [ "$oldVer" == "0" ]; then
# 首次安装时,创建 volume
hyper volume create --size=10 --name clubdata

hyper run -d --name volumes -v clubdata:/club-data --size S3 --restart always hyperhq/nfs-server

hyper exec volumes mkdir /club-data/$newVer
hyper exec volumes chmod -R 777 /club-data/$newVer
hyper exec -i volumes tee /club-data/$newVer/appsettings.Production.json < ./src/Discussion.Web/appsettings.Production.json
else
# 从旧版本升级到新版本
hyper run --rm --name upgrade --volumes-from volumes --entrypoint "/club-app/upgrade-from-existing.sh" $new_image $oldVer $newVer
fi


# 运行新版本的程序
hyper run -d -p 80:5000 --name "club$newVer" \
--volumes-from volumes --size S4 --restart always \
-e "ASPNETCORE_contentRoot=/club-data/$newVer" --workdir /club-data/$newVer $new_image

# 分配新的 fip
fip=`hyper fip allocate --yes 1`
hyper fip attach $fip "club$newVer"


# 输出部署结果
echo ""
echo "${GREEN}A new version ($new_image) is deployed at IP:$fip${NC}"

# todo: your dns???
4 changes: 4 additions & 0 deletions src/Discussion.Migrations/Discussion.Migrations.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentMigrator" Version="3.1.3" />
<PackageReference Include="FluentMigrator.Runner" Version="3.1.3" />
<PackageReference Include="FluentMigrator.Runner.SQLite" Version="3.1.3" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0" />
</ItemGroup>
</Project>
81 changes: 81 additions & 0 deletions src/Discussion.Migrations/Supporting/SqliteMigrator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.IO;
using FluentMigrator.Runner;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Discussion.Migrations.Supporting
{
public static class SqliteMigrator
{
static void Main(string[] args)
{
var connectionString = args != null && args.Length > 0 ? args[0] : null;
if (string.IsNullOrWhiteSpace(connectionString))
{
PrintError("Please specify a connection string for the Sqlite db.");
Environment.Exit(1);
return;
}

Console.WriteLine($"Starting migrating...");

try
{
Migrate(connectionString, null);
}
catch (Exception e)
{
PrintError(e.ToString());
Environment.Exit(4);
}

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Migrating completed successfully");
Console.ResetColor();
}

static void PrintError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(message);
Console.ResetColor();
}


public static void Migrate(string connectionString, Action<ILoggingBuilder> configureLogging)
{
var services = CreateServices(connectionString, configureLogging);

using (var scope = services.CreateScope())
{
UpdateDatabase(scope.ServiceProvider);
}

(services as IDisposable)?.Dispose();
}

private static IServiceProvider CreateServices(string connectionString, Action<ILoggingBuilder> configureLogging)
{
return new ServiceCollection()
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddSQLite()
.WithGlobalConnectionString(connectionString)
.ScanIn(typeof(CreateArticleTable).Assembly).For.Migrations())
.AddLogging(logging =>
{
logging.AddFluentMigratorConsole();
configureLogging?.Invoke(logging);
})
.BuildServiceProvider(false);
}


private static void UpdateDatabase(IServiceProvider serviceProvider)
{
var runner = serviceProvider.GetRequiredService<IMigrationRunner>();
runner.MigrateUp();
}
}
}
3 changes: 2 additions & 1 deletion src/Discussion.Web/ApplicationSupport/DataInfrastructure.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using Discussion.Migrations.Supporting;
using Discussion.Web.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -53,7 +54,7 @@ static void InitializeDatabase(string connectionString, ILogger<Startup> logger,
{
logger.LogCritical("正在创建新的数据库结构...");

DatabaseMigrator.Migrate(connectionString, loggingConfiguration);
SqliteMigrator.Migrate(connectionString, logging => Configurer.ConfigureFileLogging(logging, true, loggingConfiguration));

logger.LogCritical("数据库结构创建完成");
}
Expand Down
47 changes: 0 additions & 47 deletions src/Discussion.Web/ApplicationSupport/DatabaseMigrator.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Discussion.Web/Discussion.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentMigrator.Runner" Version="3.1.3" />
<PackageReference Include="Markdig" Version="0.15.2" />
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.0" />
Expand Down
26 changes: 26 additions & 0 deletions upgrade-from-existing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

# this script is executed in an transitional upgrading image

set -e

oldVer=$1
newVer=$2


# todo: Make existing site readonly



if [ ! -d /club-data/$newVer/ ]; then
mkdir /club-data/$newVer/
chmod -R 777 /club-data/$newVer/
fi

cp /club-data/$oldVer/appsettings.Production.json /club-data/$newVer/
cp /club-data/$oldVer/dotnetclub.db /club-data/$newVer/


dotnet /club-app/Discussion.Migrations.dll "Data Source=/club-data/$newVer/dotnetclub.db"


0 comments on commit 9018351

Please sign in to comment.