Add ‘Template’ option to configs and secrets

This commit is contained in:
cuigh 2018-04-24 12:16:42 +08:00
parent 4266fcd296
commit 6e5a27b66d
11 changed files with 118 additions and 102 deletions

View File

@ -41,6 +41,12 @@ func ConfigCreate(info *model.ConfigCreateInfo) error {
spec.Name = info.Name
spec.Data = []byte(info.Data)
spec.Labels = info.Labels.ToMap()
if info.Template.Name != "" {
spec.Templating = &swarm.Driver{
Name: info.Template.Name,
Options: info.Template.Options,
}
}
_, err = cli.ConfigCreate(ctx, spec)
return
})

View File

@ -12,7 +12,7 @@ import (
)
const (
apiVersion = "1.32"
defaultAPIVersion = "1.32"
)
var mgr = &manager{}
@ -37,11 +37,15 @@ func (m *manager) Client() (ctx context.Context, cli *client.Client, err error)
defer m.locker.Unlock()
if m.client == nil {
apiVersion := misc.Options.DockerAPIVersion
if apiVersion == "" {
apiVersion = defaultAPIVersion
}
if misc.Options.DockerEndpoint == "" {
os.Setenv("DOCKER_API_VERSION", apiVersion)
m.client, err = client.NewEnvClient()
m.client, err = client.NewClientWithOpts(client.FromEnv)
} else {
m.client, err = client.NewClient(misc.Options.DockerEndpoint, apiVersion, nil, nil)
m.client, err = client.NewClientWithOpts(client.WithHost(misc.Options.DockerEndpoint), client.WithVersion(apiVersion))
}
if err != nil {
return

View File

@ -41,6 +41,12 @@ func SecretCreate(info *model.ConfigCreateInfo) error {
spec.Name = info.Name
spec.Data = []byte(info.Data)
spec.Labels = info.Labels.ToMap()
if info.Template.Name != "" {
spec.Templating = &swarm.Driver{
Name: info.Template.Name,
Options: info.Template.Options,
}
}
_, err = cli.SecretCreate(ctx, spec)
return
})

View File

@ -34,6 +34,7 @@ field.login-name: Login name
field.password: Password
field.id: ID
field.tag: Tags
field.label: Labels
field.size: Size
field.created-at: Created at
field.created-by: Created by
@ -57,6 +58,8 @@ field.title: Title
field.legend: Legend
field.desc: Description
field.dashboard: Dashboard
field.data: Data
field.template: Template
# menu
menu.home: Home

View File

@ -34,6 +34,7 @@ field.login-name: 登录名称
field.password: 密码
field.id: ID
field.tag: 标签
field.label: 标签
field.size: 大小
field.created-at: 创建时间
field.created-by: 创建人
@ -57,6 +58,8 @@ field.title: 标题
field.legend: 图例
field.desc: 描述
field.dashboard: 仪表盘
field.data: 数据
field.template: 模板
# menu
menu.home: 首页

View File

@ -7,14 +7,16 @@ import (
)
const (
keyDockerEndpoint = "swirl.docker_endpoint"
keyDBType = "swirl.db_type"
keyDBAddress = "swirl.db_address"
keyAuthTimeout = "swirl.auth_timeout"
envDockerEndpoint = "DOCKER_ENDPOINT"
envDBType = "DB_TYPE"
envDBAddress = "DB_ADDRESS"
envAuthTimeout = "AUTH_TIMEOUT"
keyDockerEndpoint = "swirl.docker_endpoint"
keyDockerAPIVersion = "swirl.docker_api_version"
keyDBType = "swirl.db_type"
keyDBAddress = "swirl.db_address"
keyAuthTimeout = "swirl.auth_timeout"
envDockerEndpoint = "DOCKER_ENDPOINT"
envDockerAPIVersion = "DOCKER_API_VERSION"
envDBType = "DB_TYPE"
envDBAddress = "DB_ADDRESS"
envAuthTimeout = "AUTH_TIMEOUT"
)
// TimeZones holds some commonly used time-zones.
@ -51,10 +53,11 @@ var TimeZones = []struct {
// Options holds custom options of swirl.
var Options = &struct {
DockerEndpoint string
DBType string
DBAddress string
AuthTimeout time.Duration
DockerEndpoint string
DockerAPIVersion string
DBType string
DBAddress string
AuthTimeout time.Duration
}{
DBType: "mongo",
DBAddress: "localhost:27017/swirl",
@ -64,6 +67,7 @@ var Options = &struct {
// BindOptions binds options to environment variables.
func BindOptions() {
config.BindEnv(keyDockerEndpoint, envDockerEndpoint)
config.BindEnv(keyDockerAPIVersion, envDockerAPIVersion)
config.BindEnv(keyDBType, envDBType)
config.BindEnv(keyDBAddress, envDBAddress)
config.BindEnv(keyAuthTimeout, envAuthTimeout)

View File

@ -553,10 +553,16 @@ func (pc *PlacementConstraint) ToConstraint() string {
return ""
}
type Driver struct {
Name string `json:"name,omitempty"`
Options map[string]string `json:"options,omitempty"`
}
type ConfigCreateInfo struct {
Name string `json:"name"`
Data string `json:"data"`
Labels Options `json:"labels"`
Name string `json:"name"`
Data string `json:"data"`
Labels Options `json:"labels"`
Template Driver `json:"template"`
}
type ConfigUpdateInfo struct {

View File

@ -50,16 +50,29 @@
<input name="id" value="{{ .Config.ID }}" type="hidden">
<input name="version" value="{{ .Config.Version.Index }}" data-type="integer" type="hidden">
<div class="field">
<label class="label">Data</label>
<label class="label">{{ i18n("field.data") }}</label>
<div class="control">
<textarea class="textarea" rows="12" readonly>{{ .Config.Spec.Data }}</textarea>
</div>
<p class="help is-danger">Editing is not supported now!</p>
<p class="help">Editing is not supported now!</p>
</div>
<div class="field">
<label class="label">Labels</label>
<label class="label">{{ i18n("field.label") }}</label>
{{ yield options(name="label", items=.Config.Spec.Labels) }}
</div>
<div class="field">
<label class="label">{{ i18n("field.template") }}</label>
<div class="control">
{{ if .Config.Spec.Templating }}
{{ yield radio(name="template.name", value="", label="None", disabled=true, checked=.Config.Spec.Templating.Name) }}
{{ yield radio(name="template.name", value="golang", label="Golang", disabled=true, checked=.Config.Spec.Templating.Name) }}
{{ else }}
{{ yield radio(name="template.name", value="", label="None", disabled=true, checked="") }}
{{ yield radio(name="template.name", value="golang", label="Golang", disabled=true) }}
{{ end }}
</div>
<p class="help">Template feature needs Docker API version 1.37+</p>
</div>
{{ yield form_submit(url="/config/") }}
</form>
</div>

View File

@ -19,52 +19,31 @@
<h2 class="title">Create config</h2>
<hr>
<form method="post" data-form="ajax-json" data-url="/config/">
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">{{ i18n("field.name") }}</label>
</div>
<div class="field-body">
<div class="field">
<p class="control is-expanded">
<input name="name" class="input" type="text" placeholder="Config file name" data-v-rule="native" required>
</p>
</div>
<div class="field">
<label class="label">{{ i18n("field.name") }}</label>
<div class="control">
<input name="name" class="input" type="text" placeholder="Config file name" data-v-rule="native" required>
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Data</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<textarea name="data" class="textarea" rows="12" placeholder="Config file content" data-v-rule="native" required></textarea>
</div>
</div>
<div class="field">
<label class="label">{{ i18n("field.data") }}</label>
<div class="control">
<textarea name="data" class="textarea" rows="12" placeholder="Config file content" data-v-rule="native" required></textarea>
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Labels</label>
</div>
<div class="field-body">
<div class="field">
{{ yield options(name="label") }}
</div>
</div>
<div class="field">
<label class="label">{{ i18n("field.label") }}</label>
{{ yield options(name="label") }}
</div>
<div class="field is-horizontal">
<div class="field-label">
</div>
<div class="field-body">
<div class="field">
<div class="control">
<button type="submit" class="button is-primary">{{ i18n("button.submit") }}</button>
<a href="/config/" class="button is-link">{{ i18n("button.cancel") }}</a>
</div>
</div>
<div class="field">
<label class="label">{{ i18n("field.template") }}</label>
<div class="control">
{{ yield radio(name="template.name", value="", label="None", checked="") }}
{{ yield radio(name="template.name", value="golang", label="Golang") }}
</div>
<p class="help">Template feature needs Docker API version 1.37+</p>
</div>
{{ yield form_submit(url="/config/") }}
</form>
</section>
{{ end }}

View File

@ -50,16 +50,29 @@
<input name="id" value="{{ .Secret.ID }}" type="hidden">
<input name="version" value="{{ .Secret.Version.Index }}" data-type="integer" type="hidden">
{*<div class="field">*}
{*<label class="label">Data</label>*}
{*<label class="label">{{ i18n("field.data") }}</label>*}
{*<div class="control">*}
{*<textarea class="textarea" rows="12" readonly>{{ .Secret.Spec.Data }}</textarea>*}
{*</div>*}
{*<p class="help is-danger">Editing is not supported now!</p>*}
{*<p class="help">Editing is not supported now!</p>*}
{*</div>*}
<div class="field">
<label class="label">Labels</label>
<label class="label">{{ i18n("field.label") }}</label>
{{ yield options(name="label", items=.Secret.Spec.Labels) }}
</div>
<div class="field">
<label class="label">{{ i18n("field.template") }}</label>
<div class="control">
{{ if .Secret.Spec.Templating }}
{{ yield radio(name="template.name", value="", label="None", disabled=true, checked=.Secret.Spec.Templating.Name) }}
{{ yield radio(name="template.name", value="golang", label="Golang", disabled=true, checked=.Secret.Spec.Templating.Name) }}
{{ else }}
{{ yield radio(name="template.name", value="", label="None", disabled=true, checked="") }}
{{ yield radio(name="template.name", value="golang", label="Golang", disabled=true) }}
{{ end }}
</div>
<p class="help">Template feature needs Docker API version 1.37+</p>
</div>
{{ yield form_submit(url="/secret/") }}
</form>
</div>

View File

@ -19,52 +19,31 @@
<h2 class="title">Create secret</h2>
<hr>
<form method="post" data-form="ajax-json" data-url="/secret/">
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">{{ i18n("field.name") }}</label>
</div>
<div class="field-body">
<div class="field">
<p class="control is-expanded">
<input name="name" class="input" type="text" placeholder="Secret file name" data-v-rule="native" required>
</p>
</div>
<div class="field">
<label class="label">{{ i18n("field.name") }}</label>
<div class="control">
<input name="name" class="input" type="text" placeholder="Secret file name" data-v-rule="native" required>
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Data</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<textarea name="data" class="textarea" rows="12" placeholder="Secret file content" data-v-rule="native" required></textarea>
</div>
</div>
<div class="field">
<label class="label">{{ i18n("field.data") }}</label>
<div class="control">
<textarea name="data" class="textarea" rows="12" placeholder="Secret file content" data-v-rule="native" required></textarea>
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Labels</label>
</div>
<div class="field-body">
<div class="field">
{{ yield options(name="label") }}
</div>
</div>
<div class="field">
<label class="label">{{ i18n("field.label") }}</label>
{{ yield options(name="label") }}
</div>
<div class="field is-horizontal">
<div class="field-label">
</div>
<div class="field-body">
<div class="field">
<div class="control">
<button type="submit" class="button is-primary">{{ i18n("button.submit") }}</button>
<a href="/secret/" class="button is-link">{{ i18n("button.cancel") }}</a>
</div>
</div>
<div class="field">
<label class="label">{{ i18n("field.template") }}</label>
<div class="control">
{{ yield radio(name="template.name", value="", label="None", checked="") }}
{{ yield radio(name="template.name", value="golang", label="Golang") }}
</div>
<p class="help">Template feature needs Docker API version 1.37+</p>
</div>
{{ yield form_submit(url="/secret/") }}
</form>
</section>
{{ end }}