Skip to content

Commit

Permalink
doc: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
knight42 committed Jan 29, 2020
1 parent 96b572d commit 67e283b
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 25 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ README

- [Requirements](#requirements)
- [Quickstart](#quickstart)
- [CLI](#cli)
- [Handbook](#handbook)

Sync local repositories with remote.

Expand Down Expand Up @@ -50,6 +50,7 @@ Start yukid:
# systemctl start yukid
```

## CLI
## Handbook

[Yuki-cli](https://github.com/ustclug/Yuki-cli)
* [yukid](./cmd/yukid/README.md): Yuki daemon
* [yukictl](./cmd/yukictl/README.md): Yuki cli
81 changes: 81 additions & 0 deletions cmd/yukictl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# yukictl

### Table of Content

+ [Introduction](#introduction)
+ [Handbook](#handbook)
- [自动补全](#自动补全)
- [获取同步状态](#获取同步状态)
- [手动开始同步任务](#手动开始同步任务)
- [更新仓库同步配置](#更新仓库同步配置)
- [获取同步日志](#获取同步日志)

### Introduction

yuki 的命令行客户端。

### Handbook

#### 自动补全

```bash
# Zsh:
$ yukictl completion zsh

# Bash:
$ yukictl completion bash
```

#### 获取同步状态

```bash
$ yukictl meta ls [repo]
```

#### 手动开始同步任务

```bash
$ yukictl sync <repo>
```

开启同步任务的 debug 模式,并查看同步日志
```bash
$ yukictl sync --debug <repo>
```

#### 更新仓库同步配置

新增或修改完仓库的 YAML 配置后,需要执行下面的命令来更新配置。
```bash
$ yukictl reload <repo>
```
注意:在新增配置前需要先创建仓库相应的 `storageDir`

如果不带任何参数的话,则该命令会更新所有仓库的同步配置,并且删除配置里没有但数据库里有的仓库配置。
```bash
$ yukictl reload
```

若需要删除仓库,则可以删除相应的配置文件然后执行 `yukictl repo rm <repo>` 或直接 `yukictl reload` 来从数据库里删除配置。

#### 获取同步日志

当前的同步日志:
```bash
$ yukictl ct logs <container ID or repo name>
```

跟踪并从倒数第 5 行开始:
```bash
$ yukictl ct logs --tail 5 -f <container ID or repo name>
```

列出以往的同步日志:
```bash
$ yukictl repo logs --stats <repo name>
```

查看以往倒数第二次同步日志的倒数 10 行:
```bash
$ yukictl repo logs -n 1 --tail 10 <repo name>
```
96 changes: 96 additions & 0 deletions cmd/yukid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# yukid

### Table of Content
* [Introduction](#introduction)
* [Server Configuration](#server-configuration)
* [Repo Configuration](#repo-configuration)

### Introduction

yukid 是 yuki 的服务端,负责定期同步仓库,并且提供 RESTful API 用于管理。

### Server Configuration

yukid 的配置,路径 `/etc/yuki/daemon.toml`

```toml
## 设置 debug 为 true 后会打开 echo web 框架的 debug 模式
## 以及在日志里输出程序里打印日志的位置
#debug = true

## 设置 MongoDB 地址
## 完整格式为
## [mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
#db_url = "127.0.0.1:27017"

## 设置 db 名字
#db_name = "mirror"

## 数据所在位置的文件系统
## 可选的值为 "zfs" | "xfs" | "default"
## 影响获取仓库大小的方式,如果是 "default" 的话仓库大小恒为 `-1`
fs = "default"

## 每个仓库的同步配置存放的文件夹
## 每个配置的后缀名必须是 `.yaml`
## 配置的格式参考下方 Repo Configuration
repo_config_dir = "/path/to/config-dir"

## 设置 Docker Daemon 地址
## unix local socket: unix:///var/run/docker.sock
## tcp: tcp://127.0.0.1:2375
#docker_endpoint = "unix:///var/run/docker.sock"

## 设置同步程序的运行时的 uid 跟 gid,会影响仓库文件的 uid 跟 gid
## 格式为 uid:gid
#owner = "1000:1000"

## 设置日志所在文件夹
#log_dir = "/var/log/yuki/"

## 设置 log level
## 可选的值为 "debug" | "info" | "warn" | "error"
#log_level = "info"

## 设置监听地址
#listen_addr = "127.0.0.1:9999"

## 设置同步仓库的时候默认绑定的 IP
#bind_ip = "1.2.3.4"

## 设置创建的 container 的名字前缀
#name_prefix = "syncing-"

## 设置同步完后执行的命令
#post_sync = ["/path/to/the/program"]

## 设置更新用到的 docker images 的频率
## 格式为 crontab
#images_upgrade_interval = "@every 1h"
```

### Repo Configuration

yukid 启动的时候只会从数据库里读取仓库的同步配置,不会读取 `repo_config_dir` 下的配置,所以如果有新增配置的话需要执行 `yukictl reload` 来把配置写到数据库中。

存放在 `repo_config_dir` 下的每个仓库的同步配置,文件名必须以 `.yaml` 结尾。

示例如下。不同的 image 需要的 envs 可参考 [这里](https://github.com/ustclug/ustcmirror-images#table-of-content)
```yaml
name: bioc # required
image: ustcmirror/rsync:latest # required
interval: 2 2 31 4 * # required
storageDir: /srv/repo/bioc # required
logRotCycle: 1 # 保留多少次同步日志
bindIP: 1.2.3.4
retry: 2 # 同步失败后的重试次数
envs: # 传给同步程序的环境变量
RSYNC_HOST: rsync.exmaple.com
RSYNC_PATH: /
RSYNC_RSH: ssh -i /home/mirror/.ssh/id_rsa
RSYNC_USER: bioc-rsync
volumes: # 同步的时候需要挂载的 volume
# 注意: 由于 MongoDB 的限制,key 不能包含 `.`
/etc/passwd: /etc/passwd:ro
/ssh: /home/mirror/.ssh:ro
```
2 changes: 2 additions & 0 deletions dist/daemon.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# "default"
fs = "default"

repo_config_dir = "/path/to/config-dir"

# unix local socket: unix:///var/run/docker.sock
# tcp: tcp://127.0.0.1:2375
#docker_endpoint = "unix:///var/run/docker.sock"
Expand Down
21 changes: 21 additions & 0 deletions pkg/utils/size.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import (
"fmt"
)

func PrettySize(size int64) string {
if size < 0 {
return "unknown"
}
const n = float64(1024)
a := float64(size)
units := []string{"B", "KiB", "MiB", "GiB"}
for _, u := range units {
if a < n {
return fmt.Sprintf("%.1f %s", a, u)
}
a /= n
}
return fmt.Sprintf("%.1f TiB", a)
}
18 changes: 1 addition & 17 deletions pkg/yukictl/cmd/meta/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,9 @@ type outputMeta struct {
NextRun *time.Time `json:"nextRun,omitempty"`
}

func prettySize(size int64) string {
if size < 0 {
return "unknown"
}
const n = float64(1024)
a := float64(size)
units := []string{"B", "KiB", "MiB", "GiB"}
for _, u := range units {
if a < n {
return fmt.Sprintf("%.1f %s", a, u)
}
a /= n
}
return fmt.Sprintf("%.1f TiB", a)
}

func (o *outputMeta) From(m api.Meta) *outputMeta {
o.Meta = m
o.Size = prettySize(m.Size)
o.Size = utils.PrettySize(m.Size)
if m.LastSuccess > 0 {
t := time.Unix(m.LastSuccess, 0)
o.LastSuccess = &t
Expand Down
13 changes: 8 additions & 5 deletions pkg/yukictl/cmd/repo/logs.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package repo

import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"time"

"github.com/labstack/echo/v4"
"github.com/spf13/cobra"

"github.com/ustclug/Yuki/pkg/api"
"github.com/ustclug/Yuki/pkg/tabwriter"
"github.com/ustclug/Yuki/pkg/utils"
"github.com/ustclug/Yuki/pkg/yukictl/factory"
)
Expand Down Expand Up @@ -54,10 +55,12 @@ func (o *logsOptions) Run(cmd *cobra.Command, f factory.Factory) error {
if resp.IsError() {
return fmt.Errorf("%s", errMsg.Message)
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
err = encoder.Encode(stats)
return err
printer := tabwriter.New(os.Stdout)
printer.SetHeader([]string{"name", "mtime", "size"})
for _, s := range stats {
printer.Append(s.Name, s.Mtime.Format(time.RFC3339), utils.PrettySize(s.Size))
}
return printer.Render()
}

if flags.Changed("nth") {
Expand Down

0 comments on commit 67e283b

Please sign in to comment.